index.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. function nbInit($s){
  2. function nBind(callback, prop){
  3. for (var selector in $s){
  4. selector = prop || selector;
  5. var items = document.querySelectorAll(selector);
  6. items = items.length ? items : [document.getElementById(selector)];
  7. items = items[0] ? items : document.getElementsByName(selector);
  8. for (var i=0,item=items[i];i<items.length;i++,item=items[i]){
  9. callback(item, $s, selector)
  10. }
  11. if (prop) return;
  12. }
  13. }
  14. function syncToDOM(prop){
  15. nBind(function (item, $s, selector, value, key){
  16. value = typeof value === 'undefined' ? $s[selector] : value;
  17. var keyExists = typeof key !== 'undefined';
  18. if (keyExists){
  19. item.value = key;
  20. }
  21. if (item.type === 'radio' && !keyExists){
  22. if (item.value === value){
  23. item.checked = true;
  24. }
  25. return;
  26. }
  27. if (item.type === 'checkbox' && !keyExists){
  28. item.checked = !!value;
  29. return;
  30. }
  31. if (item.children.length && typeof value === "object"){
  32. var originalChildren = Array.prototype.slice.call(item.children);
  33. var i = 0;
  34. var isArray = Array.isArray(value);
  35. item.innerHTML = "";
  36. for (var key in value){
  37. var newNode = originalChildren[i].cloneNode(true);
  38. item.appendChild(newNode);
  39. if (isArray){
  40. arguments.callee(newNode, $s, selector, value[key]);
  41. }
  42. else {
  43. arguments.callee(newNode, $s, selector, value[key], key);
  44. }
  45. i = (i +1) % originalChildren.length;
  46. }
  47. return;
  48. }
  49. if (!keyExists){
  50. item["value" in item ? "value" : "innerText"] = value;
  51. }
  52. else {
  53. item.innerText = value;
  54. }
  55. },prop);
  56. }
  57. function syncFromDOM(){
  58. nBind(function(item, $s, selector){
  59. if (item.type === 'radio'){
  60. if (item.checked)
  61. $s[selector] = item.value;
  62. return;
  63. }
  64. if (item.type === 'checkbox'){
  65. $s[selector] = item.checked;
  66. return;
  67. }
  68. $s[selector] = item["value" in item ? "value" : "innerText"];
  69. });
  70. }
  71. syncToDOM();
  72. var scopeProxy = new Proxy($s,{
  73. get(target, prop){
  74. if (!(prop in target) && (document.querySelectorAll(prop).length || document.getElementById(prop) || document.getElementsByName(prop).length)){
  75. target[prop] = null;
  76. }
  77. syncFromDOM();
  78. return target[prop];
  79. },
  80. set(target, prop, value){
  81. //syncFromDOM();
  82. target[prop] = value
  83. syncToDOM(prop);
  84. return true;
  85. },
  86. })
  87. return scopeProxy;
  88. }
  89. var $s;
  90. setTimeout(function(){
  91. $s = nbInit({
  92. text1: "txt",
  93. p1: "para",
  94. select: {"": "--",
  95. M: "Male",
  96. F: "Female",
  97. X: "Xenomorph"},
  98. tbody: [[1,2,3,4,5,6],
  99. [7,8,9,10,11,12],
  100. [13,14,15,16,17,18],
  101. [19,20,21,22,23,24]] });
  102. $s.textarea; $s.select; $s.dzen; $s.check1; //just for init
  103. },2000);
  104. document.onchange = function(){
  105. $s.state = "";
  106. $s.state = JSON.stringify($s, null, 4);
  107. }