nb.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*
  2. Copyright 2016-100500 Ivan Grynkin
  3. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
  4. to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
  5. and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  6. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  7. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  8. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  9. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  10. DEALINGS IN THE SOFTWARE.
  11. */
  12. function nbInit(a,b){
  13. var root=document, $s=a;
  14. var dom = null;
  15. var closure = null;
  16. function searchElement(root, selector){
  17. if (selector.indexOf('|dom') === selector.length - 4){
  18. selector = selector.slice(0,selector.length - 4)
  19. dom = true;
  20. }
  21. if (selector.indexOf('|closure') === selector.length - 8){
  22. selector = selector.slice(0,selector.length - 8)
  23. closure = true;
  24. }
  25. if (root === document){
  26. var items = [root.getElementById(selector)];
  27. }
  28. else {
  29. items = root.querySelectorAll("#" + selector);
  30. }
  31. items = items[0] ? items : root.querySelectorAll(selector);
  32. items = items.length ? items : root === document ? root.getElementsByName(selector) : root.querySelectorAll("[name='" + selector + "']");
  33. items = items.length ? items : root.getElementsByClassName(selector);
  34. return items;
  35. }
  36. if (a instanceof HTMLElement){
  37. root = a;
  38. $s = b;
  39. }
  40. else if (typeof a === "string"){
  41. root = searchElement(document, a)[0];
  42. $s = b;
  43. }
  44. if (b instanceof HTMLElement){
  45. root = b;
  46. }
  47. else if (typeof b === "string"){
  48. root = searchElement(document, a)[0];
  49. }
  50. if (typeof $s === "undefined"){
  51. $s = {}
  52. }
  53. function nBind(callback, prop, direction){
  54. direction = direction || "write";
  55. for (var selector in $s){
  56. var result = [];
  57. selector = prop || selector; //change selector to passed if it
  58. var items = searchElement(root, selector);
  59. for (var i=0,item=items[i];i<items.length;i++,item=items[i]){
  60. if (direction == "write" && Array.isArray($s[selector]) && (item.children.length == 0 || (items.length == $s[selector].length && items.length > 1))){
  61. callback(item, $s, selector, $s[selector][i]);
  62. }
  63. else {
  64. var res = callback(item, $s, selector);
  65. if (typeof res !== "undefined"){
  66. result.push(res)
  67. }
  68. }
  69. }
  70. $s[selector] = result.length ? (result.length == 1 ? result[0] : result) : $s[selector];
  71. if (prop) return; //exit if selector passed, no iteration
  72. }
  73. }
  74. function recursiveObjectSet(item, value){
  75. if (typeof item === 'undefined'){
  76. return;
  77. }
  78. for (var key in value){
  79. if (typeof value[key] !== 'object'){
  80. item[key] = value[key];
  81. }
  82. else {
  83. recursiveObjectSet(item[key], value[key]);
  84. }
  85. }
  86. }
  87. function syncToDOM(prop){
  88. nBind(function (item, $s, selector, value, key, thisByClass){
  89. value = typeof value === 'undefined' ? $s[selector] : value;
  90. var keyExists = typeof key !== 'undefined';
  91. if (closure){
  92. item.nbData = value[0].apply(item, value.slice(1))
  93. }
  94. if ((!item.children.length && !Array.isArray(value) && typeof value === 'object') || thisByClass || dom){ //hash array on single leaf node -> set attrs on the tag
  95. recursiveObjectSet(item,value);
  96. if (!thisByClass && !dom){
  97. item.nbData = value;
  98. }
  99. return;
  100. }
  101. if (keyExists && "value" in item){ //if hash key-value pair. Usable for select > option
  102. item.value = key;
  103. }
  104. if (typeof value === "boolean" && item.type !== 'checkbox'){ //boolean means visibility, except checkbox
  105. if (value){
  106. item.style.display = "originalDisplay" in item ? item.originalDisplay : "";
  107. }
  108. else {
  109. item.originalDisplay = "originalDisplay" in item ? item.originalDisplay : item.style.display;
  110. item.style.display = "none";
  111. }
  112. return;
  113. }
  114. if (item.type === 'radio' && !keyExists){ //radiogroup set
  115. if (item.value === value){ //only item with right value to set
  116. item.checked = true;
  117. }
  118. return;
  119. }
  120. if (item.type === 'checkbox' && !keyExists){ //checkbox setting by boolean
  121. item.checked = !!value;
  122. return;
  123. }
  124. if (item.children.length && typeof value === "object"){ //recursive fill
  125. item.copy = item.copy || item.cloneNode(true); //original node
  126. item.nbData = value;
  127. var originalChildren = item.copy.children;
  128. var i = 0;
  129. var isArray = Array.isArray(value); //different logic for array and objects
  130. if (!isArray){ // if first key in array find as class name in one of subnodes
  131. var classFound = false;
  132. for (var key in value){
  133. if (item.getElementsByClassName(key).length){
  134. classFound = true;
  135. break;
  136. }
  137. }
  138. if (classFound){
  139. for (var key in value){
  140. var classSubnodes = item.getElementsByClassName(key);
  141. for (var i=0;i<classSubnodes.length;i++){
  142. arguments.callee(classSubnodes[i], $s, selector, value[key]); // recursively fill subnode with that data. No reason to pass a key, because key are class selector, not value for option
  143. }
  144. if (item.classList.contains(key)){
  145. arguments.callee(item, $s, selector, value[key], undefined, true); // recursively fill subnode with that data. No reason to pass a key, because key are class selector, not value for option
  146. }
  147. }
  148. return;
  149. }
  150. }
  151. item.innerHTML = ""; //remove sub nodes
  152. for (var key in value){ //otherwise iterate over array or object
  153. var newNode = originalChildren[i].cloneNode(true);
  154. item.appendChild(newNode);
  155. if (isArray){
  156. arguments.callee(newNode, $s, selector, value[key]);
  157. }
  158. else {
  159. arguments.callee(newNode, $s, selector, value[key], key);
  160. }
  161. i = (i +1) % originalChildren.length;
  162. }
  163. return;
  164. }
  165. if (!keyExists){ //default logic: set text or value to data value
  166. item[("value" in item) && item.tagName !== 'LI' ? "value" : "innerText"] = value;
  167. }
  168. else {
  169. item.innerText = value; // do not try to overwrite value on option nodes
  170. }
  171. },prop, "write");
  172. }
  173. function syncFromDOM(prop){
  174. nBind(function(item, $s, selector){
  175. if (closure){
  176. return item.nbData()
  177. }
  178. if (item.type === 'radio'){
  179. if (item.checked)
  180. return item.value;
  181. return;
  182. }
  183. if (item.type === 'checkbox'){
  184. return item.checked;
  185. }
  186. if (item.tagName === 'SELECT'){
  187. return item.value;
  188. }
  189. if ("nbData" in item){
  190. var value = item.nbData;
  191. var isArray = Array.isArray(value); //different logic for array and objects
  192. if (item.children.length && typeof value === "object"){ //recursive fill
  193. if (!isArray){ // if first key in array find as class name in one of subnodes
  194. var classFound = false;
  195. for (var key in value){
  196. if (item.getElementsByClassName(key).length){
  197. classFound = true;
  198. break;
  199. }
  200. }
  201. if (classFound){
  202. for (var key in value){
  203. var classSubnodes = item.getElementsByClassName(key);
  204. for (var i=0;i<classSubnodes.length;i++){
  205. value[key] = arguments.callee(classSubnodes[i], $s, selector); // recursively fill subnode with that data. No reason to pass a key, because key are class selector, not value for option
  206. }
  207. }
  208. return value;
  209. }
  210. }
  211. else{
  212. value.length = 0;
  213. for (var key=0;key<item.children.length;key++){ //otherwise iterate over array or object
  214. value[key] = arguments.callee(item.children[key], $s, selector);
  215. }
  216. return value;
  217. }
  218. //else {
  219. //for (var key in value){
  220. //value[key] = arguments.callee(item.children[key], $s, selector);
  221. //}
  222. //}
  223. }
  224. if (!isArray && typeof value === 'object' && item.children.length === 0){ //hash array on single leaf node -> set attrs on the tag
  225. for (var key in value){
  226. value[key] = item[key];
  227. }
  228. return value;
  229. }
  230. if (!isArray && typeof value === 'object'){ //hash array on single leaf node -> set attrs on the tag
  231. value = {};
  232. for (var i=0;i<item.children.length;i++){
  233. var childItem = item.children[i];
  234. value[childItem.value] = childItem.innerText;
  235. }
  236. return value;
  237. }
  238. }
  239. if ("value" in item){
  240. return item.value;
  241. }
  242. return item.innerText;
  243. },prop, "read");
  244. }
  245. syncToDOM();
  246. var scopeProxy = new Proxy($s,{
  247. get(target, prop){
  248. //if (!(prop in target) && (document.getElementById(prop) ||
  249. //document.querySelectorAll(prop).length ||
  250. //document.getElementsByName(prop).length ||
  251. //document.getElementsByClassName(prop).length)){
  252. target[prop] = null;
  253. //}
  254. syncFromDOM(prop);
  255. dom = null;
  256. closure = null;
  257. return target[prop];
  258. },
  259. set(target, prop, value){
  260. //syncFromDOM();
  261. target[prop] = value
  262. syncToDOM(prop);
  263. dom = null;
  264. closure = null;
  265. return true;
  266. },
  267. })
  268. return scopeProxy;
  269. }
  270. function nbGetData(el){
  271. while (!('nbData' in el)){
  272. if (el.parentElement){
  273. el = el.parentElement;
  274. }
  275. else {
  276. return null;
  277. }
  278. }
  279. return el.nbData;
  280. }