From f09499b6419ad22152996c519cf906276c7ab6b5 Mon Sep 17 00:00:00 2001 From: KyMAN <0kyman0@gmail.com> Date: Sun, 31 Mar 2024 21:20:12 +0200 Subject: [PATCH] #wip(ecma): Base parcial done. --- Public/ecma/Sizerboard.ecma.js | 123 ++++++++++++++++++++++++++++++--- 1 file changed, 113 insertions(+), 10 deletions(-) diff --git a/Public/ecma/Sizerboard.ecma.js b/Public/ecma/Sizerboard.ecma.js index 847255d..e074f9c 100644 --- a/Public/ecma/Sizerboard.ecma.js +++ b/Public/ecma/Sizerboard.ecma.js @@ -1,11 +1,39 @@ Sizerboard = function(inputs){ const self = this, - default_settings = {}, - settings = {}; - let started = false; + default_settings = { + nulls : false, + default_value : null, + default_text : "", + autostart : true, + default_language : "english", + timeout : 2000 + }, + settings = {}, + sentences = {}; + let started = false, + language, default_language, + allow_settings_nulls, default_text, default_value, + ajax_timeout; - const construct = () => {}; + const construct = () => { + + basic_values(); + + self.settings("autostart") && self.start(); + + }; + + const basic_values = () => { + + default_value = self.settings("default_value", null, null, true); + default_text = self.settings("default_text"); + allow_settings_nulls = self.settings("nulls", null, false, false); + language = self.settings(["language", "default_language"]); + default_language = self.settings(["default_language", "language"]); + ajax_timeout = self.settings(["ajax_timeout", "timeout"]); + + }; this.start = callback => { @@ -16,15 +44,23 @@ Sizerboard = function(inputs){ return false; }; started = true; - - end(true); + + self.execute_array_items(["default_settings_files", "settings_files"], (key, callback) => { + self.settings_add(self.settings(key), true, callback); + }, () => { + self.execute_array_items(["default_i18n_files", "i18n_files"], (key, callback) => { + self.i18n_add(self.settings(key), true, callback); + }, () => { + end(true); + }); + }); return true; }; - this.nulls = nulls => typeof nulls == "boolean" ? nulls : self.settings("nulls", null, false, false); + this.nulls = nulls => typeof nulls == "boolean" ? nulls : allow_settings_nulls; - this.default_value = (_default, nulls) => _default !== undefined && (_default !== null || self.nulls(nulls)) ? _default : self.settings("default_value", null, null, true); + this.default_value = (_default, nulls) => _default !== undefined && (_default !== null || self.nulls(nulls)) ? _default : default_value; this.settings = (keys, inputs, _default, nulls) => { @@ -54,7 +90,7 @@ Sizerboard = function(inputs){ let ended = false; const ajax = new XMLHttpRequest(), end = message => !ended && (ended = true) && typeof callback == "function" && callback(ajax.responseText, ajax.status, ajax.readyState, message, message == "OK"); - timeout = self.settings(["ajax_timeout", "timeout"]), + timeout = ajax_timeout, date = Date.now(); ajax.send("get", url, true); @@ -102,7 +138,7 @@ Sizerboard = function(inputs){ let json; - if(/^(\{(.|[\r\n])*\}|\[(.|[\r\n])*)\])$/.test(inputs[i].trim()){ + if(/^(\{(.|[\r\n])*\}|\[(.|[\r\n])*)\])$/.test(inputs[i].trim())){ try{ json = JSON.parse(inputs[i]); }catch(exception){}; @@ -119,6 +155,23 @@ Sizerboard = function(inputs){ }; + this.execute_array_items = (items, action, callback, i) => { + (typeof items != "object" || !(items instanceof Array)) && (items = [items]); + + if(!items || i >= items.length){ + typeof callback == "function" && callback(); + return; + }; + + const end = () => execute_array_items(items, action, callback, i + 1); + + if(typeof action == "function") + action(items[i], end); + else + end(); + + }; + this.settings_add = (inputs, overwrite, callback) => { typeof overwrite != "boolean" && (overwrite = settings_overwrite); self.execute_array_dictionaries(inputs, data => { @@ -127,6 +180,56 @@ Sizerboard = function(inputs){ settings[key] = data[key]; }, callback); }; + + this.i18n_add = (inputs, overwrite, callback) => { + typeof overwrite != "boolean" && (overwrite = i18n_overwrite); + self.execute_array_dictionaries(inputs, data => { + for(const language in data){ + !sentences[language] && (sentences[language] = {}); + if(typeof data[language] == "object") + for(const key in data[language]) + (overwrite || sentences[language][key] === undefined) && + (sentences[language][key] = data[language][key]); + }; + }, callback); + }; + + this.string_variables = (string, variables, _default) => { + + const l = (variables = ( + typeof variables == "object" && variables instanceof Array ? variables : [variables] + ).filter(variables => typeof variables == "object")).length; + + return string.replace(/\{([^\{\}]+)\}/g, (all, key) => { + for(let i = 0; i < l; i ++) + if(variables[i][key] !== undefined) + return variables[i][key]; + return _default !== undefined ? _default : all; + }); + }; + + this.default_text = _default => _default !== undefined ? _default : default_text; + + const i18n = (keys, _default) => { + + const j = (keys = (typeof keys == "object" && keys instanceof Array ? keys : [keys]).filter(key => typeof key == "string").map(key => key.trim()).filter(key => key)); + + if(j){ + + const languages = [language, default_language].concat(Object.keys(sentences)), + l = languages.length; + + for(let i = 0; i < l; i ++) + if(languages[i] && typeof languages[i] == "object") + for(let j = 0; j < l; j ++) + if(languages[i][keys[j]] !== undefined) + return languages[i][keys[j]]; + return keys[0]; + }; + return self.default_text(_default); + }; + + this.i18n = (keys, variables, _default) => self.string_variables(i18n(keys, _default), variables, _default); construct();