Sizerboard.Views = function(sizerboard, inputs){ const self = this, views = {}; let started = false, default_overwrite = false; const construct = () => { sizerboard.print("info", "sizerboard_views_building"); sizerboard.print("ok", "sizerboard_views_built"); }; this.start = callback => { const end = status => typeof callback == "function" && callback(status); sizerboard.print("info", "sizerboard_views_starting"); if(started){ sizerboard.print("warn", "sizerboard_views_already_started"); end(false); return false; }; started = true; default_overwrite = sizerboard.settings(["views_overwrite", "overwrite"]); sizerboard.execute_array_items(["default_views_files", "views_files"], (key, callback) => { self.add(sizerboard.settings(key), true, callback); }, () => { sizerboard.print("ok", "sizerboard_views_started"); end(true); }); return true; }; const add_json = (data, overwrite, callback) => { let json; try{ json = JSON.parse(data); }catch(exception){}; if(json) add(json instanceof Array ? json : [json], overwrite, callback, 0); else callback(); }; const add = (inputs, overwrite, callback, i) => { if(!inputs || (i || (i = 0)) >= inputs.length){ typeof callback == "function" && callback(); return; }; const end = () => add(inputs, overwrite, callback, i + 1); if(!inputs[i]){ end(); return; }; switch(typeof inputs[i]){ case "string": if(/^(\[(.+|[\r\n]+)\]|\{(.+|[\r\n]+)\})$/.test(inputs[i].trim())) add_json(inputs[i], overwrite, end); else if(/^[^\s][^\r\n]+[^\s]$/.test(inputs[i])) sizerboard.load_file(inputs[i], response => add([response], overwrite, end, 0)); else{ let matches; while(matches = inputs[i].match(/\<\!\-{2} *\[{2}([^\[\]]+)\]{2} *\-{2}\>/)){ const key = matches[1].trim(); inputs[i] = inputs[i].substring(matches.index + matches[0].length); if(matches = inputs[i].match(new RegExp("\\<\\!\\-{2} *\\[{2} *" + key + " *\\]{2} *\\-{2}\\>"))){ (overwrite || views[key] === undefined) && (views[key] = inputs[i].substring(0, matches.index)); inputs[i] = inputs[i].substring(matches.index + matches[0].length); }; }; end(); }; break; case "object": if(inputs[i] instanceof Array) add(inputs[i], overwrite, end, 0); else{ for(const key in inputs[i]) typeof inputs[i] == "string" && (overwrite || views[key] === undefined) && (views[key] = inputs[i]); end(); }; break; default: end(); break; }; }; this.add = (inputs, overwrite, callback) => add( typeof inputs == "object" && inputs instanceof Array ? inputs : [inputs], typeof overwrite == "boolean" ? overwrite : default_overwrite, callback, 0 ); this.validate = key => ( key === undefined ? 1 << 0 : key === null ? 1 << 1 : typeof key != "string" ? 1 << 2 : !key ? 1 << 3 : !(key = key.trim()) ? 1 << 4 : views[key] === undefined ? 1 << 5 : 0); const set_variables = (view, variables, _default) => view.replace(/(object_name)/g, "{$1}").replace(/\{([^\{\}]+)\}|\{{2}([^\{\}]+)\}{2}/g, (all, key, i18n) => { if(key){ for(let i = 0, l = variables.length; i < l; i ++) if(variables[i][key] !== undefined) return variables[i][key]; }else{ for(let i = 0, l = variables.length; i < l; i ++) if(variables[i][i18n] !== undefined){ i18n = variables[i][i18n]; break; }; const text = sizerboard.i18n(i18n, variables, _default === undefined ? null : _default); return text !== null ? text : i18n; }; return all; }); const get_variables_array = variables => ( variables && typeof variables == "object" ? variables instanceof Array ? variables : [variables] : [] ).filter(subset => subset && typeof subset == "object"); this.get = (key, variables, _default) => { let error = self.validate(key) << 1, view = _default; !error && (view = set_variables(views[key], get_variables_array(variables).concat([{ object_name : sizerboard.object_name }]), _default)); return [view, error]; }; this.get_multiple_items = (key, items, variables, _default) => { let error = ( (self.validate(key) << 0) | (( items === undefined ? 1 << 0 : items === null ? 1 << 1 : typeof items != "object" ? 1 << 2 : !(items instanceof Array) ? 1 << 3 : items.some(item => !item || typeof item != "object") ? 1 << 4 : 0) << 7) | 0), results = null; !error && (results = items.map(item => set_variables(views[key], get_variables_array(variables).concat([item]), _default)).join("")); return [results, error]; }; construct(); };