ConfigHelper:
A tiny class which helps creating script config items, their default values and descriptions.
- it helps to prevent errors (because you don't have to make sure item-name and description-map entry match)
- it saves time (because you need to type less code and changes are done more quickly)
- it looks nicer (I think it's capable of hiding some irritative syntax and commands)
Usage:
- Put the code-snippet below somewhere in your script. I suggest putting it directly *intourlTmpthe OnInit() function, as that's the only place it's going to be used (it's a just local function/class/object then).
function ConfigHelper(data){ //v1.2
var t=this; t.d=data; t.c=data.config; t.cd=DOpus.Create.Map();
t.add=function(name, val, des){ t.l={n:name,ln:name.
toLowerCase()}; return t.val(val).des(des);}
t.des=function(des){ if (!des) return t; if (t.cd.empty)
t.d.config_desc=t.cd; t.cd(t.l.n)=des; return t;}
t.val=function(val){ var l=t.l; if (l.v!==l.x&&typeof l.v=="object")
l.v.push_back(val);else l.v=t.c[l.n]=val;return t;}
t.trn=function(){return t.des(t("script.config."+t.l.ln));}
}
- After that, create an instance of the ConfigHelper while passing OnScriptInitData:
var cfg = new ConfigHelper( OnScriptInitData );
-
Now you can start adding your config items in various manners. The following snippets all do the same, just formatting and the order of methods is different. Each snippet adds 4 types of config items (boolean, string, vector, multiline string).
Doesn't this look clean and easy to maintain? o)
-
There are 2 additional ways of chaining the methods demonstrated in the download.
Methods:
- add() - adds a config item (value and description can be specified as well if desired)
- val() - sets the default value, if it's a new empty vector, following calls to val() add items to it
- des() - sets the description
-
trn() - fetches and sets a language specific description (you need the translator to use this one)
- The methods val(), des() and trn() always operate on the last item added by add(), so don't mix them up. o)
Demo-Config:
Cya, tbone
Download:
-
Latest: v1.2 / 2015.05.27 - enhanced functionality vs. size ratio:
Xtra.Demo_ConfigHelper.js.txt (11 KB)