|
@@ -1,15 +1,29 @@
|
|
function nbInit($s){
|
|
function nbInit($s){
|
|
- function nBind(callback, prop){
|
|
|
|
|
|
+ function nBind(callback, prop, direction){
|
|
|
|
+ direction = direction || "write";
|
|
for (var selector in $s){
|
|
for (var selector in $s){
|
|
|
|
+ var result = [];
|
|
selector = prop || selector; //change selector to passed if it
|
|
selector = prop || selector; //change selector to passed if it
|
|
|
|
|
|
- var items = document.querySelectorAll(selector);
|
|
|
|
- items = items.length ? items : [document.getElementById(selector)];
|
|
|
|
- items = items[0] ? items : document.getElementsByName(selector);
|
|
|
|
|
|
+ var items = [document.getElementById(selector)];
|
|
|
|
+ items = items[0] ? items : document.querySelectorAll(selector);
|
|
|
|
+ items = items.length ? items : document.getElementsByName(selector);
|
|
|
|
+ items = items.length ? items : document.getElementsByClassName(selector);
|
|
for (var i=0,item=items[i];i<items.length;i++,item=items[i]){
|
|
for (var i=0,item=items[i];i<items.length;i++,item=items[i]){
|
|
- callback(item, $s, selector)
|
|
|
|
|
|
+ if (direction == "write" && item.children.length == 0 && Array.isArray($s[selector])){
|
|
|
|
+ callback(item, $s, selector, $s[selector][i]);
|
|
|
|
+ }
|
|
|
|
+ else {
|
|
|
|
+ var res = callback(item, $s, selector);
|
|
|
|
+ if (typeof res !== "undefined"){
|
|
|
|
+ result.push(res)
|
|
|
|
+ }
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+
|
|
|
|
+ $s[selector] = result.length ? (result.length == 1 ? result[0] : result) : $s[selector];
|
|
|
|
+
|
|
if (prop) return; //exit if selector passed, no iteration
|
|
if (prop) return; //exit if selector passed, no iteration
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -18,6 +32,12 @@ function nbInit($s){
|
|
nBind(function (item, $s, selector, value, key){
|
|
nBind(function (item, $s, selector, value, key){
|
|
value = typeof value === 'undefined' ? $s[selector] : value;
|
|
value = typeof value === 'undefined' ? $s[selector] : value;
|
|
var keyExists = typeof key !== 'undefined';
|
|
var keyExists = typeof key !== 'undefined';
|
|
|
|
+ if (!item.children.length && !Array.isArray(value) && typeof value === 'object'){ //hash array on single leaf node -> set attrs on the tag
|
|
|
|
+ for (var key in value){
|
|
|
|
+ item[key] = value[key];
|
|
|
|
+ }
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
if (keyExists && "value" in item){ //if hash key-value pair. Usable for select > option
|
|
if (keyExists && "value" in item){ //if hash key-value pair. Usable for select > option
|
|
item.value = key;
|
|
item.value = key;
|
|
}
|
|
}
|
|
@@ -84,32 +104,34 @@ function nbInit($s){
|
|
else {
|
|
else {
|
|
item.innerText = value; // do not try to overwrite value on option nodes
|
|
item.innerText = value; // do not try to overwrite value on option nodes
|
|
}
|
|
}
|
|
- },prop);
|
|
|
|
|
|
+ },prop, "write");
|
|
}
|
|
}
|
|
|
|
|
|
- function syncFromDOM(){
|
|
|
|
|
|
+ function syncFromDOM(prop){
|
|
nBind(function(item, $s, selector){
|
|
nBind(function(item, $s, selector){
|
|
if (item.type === 'radio'){
|
|
if (item.type === 'radio'){
|
|
if (item.checked)
|
|
if (item.checked)
|
|
- $s[selector] = item.value;
|
|
|
|
|
|
+ return item.value;
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
if (item.type === 'checkbox'){
|
|
if (item.type === 'checkbox'){
|
|
- $s[selector] = item.checked;
|
|
|
|
- return;
|
|
|
|
|
|
+ return item.checked;
|
|
}
|
|
}
|
|
- $s[selector] = item["value" in item ? "value" : "innerText"];
|
|
|
|
- });
|
|
|
|
|
|
+ return item["value" in item ? "value" : "innerText"];
|
|
|
|
+ },prop, "read");
|
|
}
|
|
}
|
|
|
|
|
|
syncToDOM();
|
|
syncToDOM();
|
|
|
|
|
|
var scopeProxy = new Proxy($s,{
|
|
var scopeProxy = new Proxy($s,{
|
|
get(target, prop){
|
|
get(target, prop){
|
|
- if (!(prop in target) && (document.querySelectorAll(prop).length || document.getElementById(prop) || document.getElementsByName(prop).length)){
|
|
|
|
|
|
+ if (!(prop in target) && (document.getElementById(prop) ||
|
|
|
|
+ document.querySelectorAll(prop).length ||
|
|
|
|
+ document.getElementsByName(prop).length ||
|
|
|
|
+ document.getElementsByClassName(prop).length)){
|
|
target[prop] = null;
|
|
target[prop] = null;
|
|
}
|
|
}
|
|
- syncFromDOM();
|
|
|
|
|
|
+ syncFromDOM(prop);
|
|
return target[prop];
|
|
return target[prop];
|
|
},
|
|
},
|
|
set(target, prop, value){
|
|
set(target, prop, value){
|
|
@@ -137,17 +159,66 @@ setTimeout(function(){
|
|
[7,8,9,10,11,12],
|
|
[7,8,9,10,11,12],
|
|
[13,14,15,16,17,18],
|
|
[13,14,15,16,17,18],
|
|
/*[19,20,21,22,23,24]*/],
|
|
/*[19,20,21,22,23,24]*/],
|
|
|
|
+ //numberTable: [[[{"": "--",
|
|
|
|
+ //M: "Male",
|
|
|
|
+ //F: "Female",
|
|
|
|
+ //X: "Xenomorph"}],
|
|
|
|
+ //[{"": "--",
|
|
|
|
+ //M: "Male",
|
|
|
|
+ //F: "Female",
|
|
|
|
+ //X: "Xenomorph"}],
|
|
|
|
+ //[{"": "--",
|
|
|
|
+ //M: "Male",
|
|
|
|
+ //F: "Female",
|
|
|
|
+ //X: "Xenomorph"}]],
|
|
|
|
+ //[[{"": "--",
|
|
|
|
+ //M: "Male",
|
|
|
|
+ //F: "Female",
|
|
|
|
+ //X: "Xenomorph"}],
|
|
|
|
+ //[{"": "--",
|
|
|
|
+ //M: "Male",
|
|
|
|
+ //F: "Female",
|
|
|
|
+ //X: "Xenomorph"}],
|
|
|
|
+ //[{"": "--",
|
|
|
|
+ //M: "Male",
|
|
|
|
+ //F: "Female",
|
|
|
|
+ //X: "Xenomorph"}]]],
|
|
hashTable: [{ name: "Ivan",
|
|
hashTable: [{ name: "Ivan",
|
|
surname: "Ivanovv",
|
|
surname: "Ivanovv",
|
|
- age: "57"},
|
|
|
|
|
|
+ age: "57",
|
|
|
|
+ note: {innerText: "Buhaet", name: 'ivanovvsTextArea'},
|
|
|
|
+ married: true},
|
|
{ name: "Petr",
|
|
{ name: "Petr",
|
|
surname: "Petroff",
|
|
surname: "Petroff",
|
|
- age: "17"},
|
|
|
|
|
|
+ age: "17",
|
|
|
|
+ note: "Tyolki v golove",
|
|
|
|
+ married: false,
|
|
|
|
+ },
|
|
{ name: "Mary",
|
|
{ name: "Mary",
|
|
surname: "Tester",
|
|
surname: "Tester",
|
|
- age: "27"} ]});
|
|
|
|
|
|
+ married: true,
|
|
|
|
+ note: "Ovulyashka",
|
|
|
|
+ age: "27"} ],
|
|
|
|
+ checks: [
|
|
|
|
+ {check: {name: "first", checked: true},
|
|
|
|
+ description: "first check"},
|
|
|
|
+ {check: {name: "second", checked: false},
|
|
|
|
+ description: "second check"},
|
|
|
|
+ {check: {name: "third", checked: true},
|
|
|
|
+ description: "third check"},
|
|
|
|
+ ],
|
|
|
|
+ radios: [
|
|
|
|
+ {radio: {name: "sexism", value: "M", checked: true},
|
|
|
|
+ description: "Male"},
|
|
|
|
+ {radio: {name: "sexism", value: "F"},
|
|
|
|
+ description: "Female"},
|
|
|
|
+ {radio: {name: "racism", value: "white", checked: true},
|
|
|
|
+ description: "White"},
|
|
|
|
+ {radio: {name: "racism", value: "black"},
|
|
|
|
+ description: "Black"}, ]
|
|
|
|
+ });
|
|
|
|
|
|
- $s.textarea; $s.select; $s.dzen; $s.check1; //just for init
|
|
|
|
|
|
+ $s.textarea; $s.select; $s.dzen; $s.check1; $s.note; $s.married; //just for init
|
|
|
|
|
|
document.onchange();
|
|
document.onchange();
|
|
},2000);
|
|
},2000);
|