nb.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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) && (selector.length > 4)){
  18. selector = selector.slice(0,selector.length - 4)
  19. dom = true;
  20. }
  21. if ((selector.indexOf('|closure') === selector.length - 8) && (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. if (closure){
  62. callback(item, $s, selector, $s[selector]);
  63. }
  64. else {
  65. callback(item, $s, selector, $s[selector][i]);
  66. }
  67. }
  68. else {
  69. var res = callback(item, $s, selector);
  70. if (typeof res !== "undefined"){
  71. result.push(res)
  72. }
  73. }
  74. }
  75. $s[selector] = result.length ? (result.length == 1 ? result[0] : result) : $s[selector];
  76. if (prop) return; //exit if selector passed, no iteration
  77. }
  78. }
  79. function recursiveObjectSet(item, value){
  80. if (typeof item === 'undefined'){
  81. return;
  82. }
  83. for (var key in value){
  84. if (typeof value[key] !== 'object'){
  85. item[key] = value[key];
  86. }
  87. else {
  88. recursiveObjectSet(item[key], value[key]);
  89. }
  90. }
  91. }
  92. function syncToDOM(prop){
  93. nBind(function (item, $s, selector, value, key, thisByClass){
  94. value = typeof value === 'undefined' ? $s[selector] : value;
  95. var keyExists = typeof key !== 'undefined';
  96. if (closure){
  97. item.nbData = value[0].apply(item, value.slice(1))
  98. return;
  99. }
  100. if ((!item.children.length && !Array.isArray(value) && typeof value === 'object') || thisByClass || dom){ //hash array on single leaf node -> set attrs on the tag
  101. recursiveObjectSet(item,value);
  102. if (!thisByClass && !dom){
  103. item.nbData = value;
  104. }
  105. return;
  106. }
  107. if (keyExists && "value" in item){ //if hash key-value pair. Usable for select > option
  108. item.value = key;
  109. }
  110. if (typeof value === "boolean" && item.type !== 'checkbox'){ //boolean means visibility, except checkbox
  111. if (value){
  112. item.style.display = "originalDisplay" in item ? item.originalDisplay : "";
  113. }
  114. else {
  115. item.originalDisplay = "originalDisplay" in item ? item.originalDisplay : item.style.display;
  116. item.style.display = "none";
  117. }
  118. return;
  119. }
  120. if (item.type === 'radio' && !keyExists){ //radiogroup set
  121. if (item.value === value){ //only item with right value to set
  122. item.checked = true;
  123. }
  124. return;
  125. }
  126. if (item.type === 'checkbox' && !keyExists){ //checkbox setting by boolean
  127. item.checked = !!value;
  128. return;
  129. }
  130. if (item.children.length && typeof value === "object"){ //recursive fill
  131. item.copy = item.copy || item.cloneNode(true); //original node
  132. item.nbData = value;
  133. var originalChildren = item.copy.children;
  134. var i = 0;
  135. var isArray = Array.isArray(value); //different logic for array and objects
  136. if (!isArray){ // if first key in array find as class name in one of subnodes
  137. var classFound = false;
  138. for (var key in value){
  139. if (item.getElementsByClassName(key).length){
  140. classFound = true;
  141. break;
  142. }
  143. }
  144. if (classFound){
  145. for (var key in value){
  146. var classSubnodes = item.getElementsByClassName(key);
  147. for (var i=0;i<classSubnodes.length;i++){
  148. 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
  149. }
  150. if (item.classList.contains(key)){
  151. 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
  152. }
  153. }
  154. return;
  155. }
  156. }
  157. item.innerHTML = ""; //remove sub nodes
  158. for (var key in value){ //otherwise iterate over array or object
  159. var newNode = originalChildren[i].cloneNode(true);
  160. item.appendChild(newNode);
  161. if (isArray){
  162. arguments.callee(newNode, $s, selector, value[key]);
  163. }
  164. else {
  165. arguments.callee(newNode, $s, selector, value[key], key);
  166. }
  167. i = (i +1) % originalChildren.length;
  168. }
  169. return;
  170. }
  171. if (!keyExists){ //default logic: set text or value to data value
  172. item[("value" in item) && item.tagName !== 'LI' ? "value" : "innerText"] = value;
  173. }
  174. else {
  175. item.innerText = value; // do not try to overwrite value on option nodes
  176. }
  177. },prop, "write");
  178. }
  179. function syncFromDOM(prop){
  180. nBind(function(item, $s, selector){
  181. if (closure){
  182. return item.nbData()
  183. }
  184. if (item.type === 'radio'){
  185. if (item.checked)
  186. return item.value;
  187. return;
  188. }
  189. if (item.type === 'checkbox'){
  190. return item.checked;
  191. }
  192. if (item.tagName === 'SELECT'){
  193. return item.value;
  194. }
  195. if ("nbData" in item){
  196. var value = item.nbData;
  197. var isArray = Array.isArray(value); //different logic for array and objects
  198. if (item.children.length && typeof value === "object"){ //recursive fill
  199. if (!isArray){ // if first key in array find as class name in one of subnodes
  200. var classFound = false;
  201. for (var key in value){
  202. if (item.getElementsByClassName(key).length){
  203. classFound = true;
  204. break;
  205. }
  206. }
  207. if (classFound){
  208. for (var key in value){
  209. var classSubnodes = item.getElementsByClassName(key);
  210. for (var i=0;i<classSubnodes.length;i++){
  211. 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
  212. }
  213. }
  214. return value;
  215. }
  216. }
  217. else{
  218. value.length = 0;
  219. for (var key=0;key<item.children.length;key++){ //otherwise iterate over array or object
  220. value[key] = arguments.callee(item.children[key], $s, selector);
  221. }
  222. return value;
  223. }
  224. //else {
  225. //for (var key in value){
  226. //value[key] = arguments.callee(item.children[key], $s, selector);
  227. //}
  228. //}
  229. }
  230. if (!isArray && typeof value === 'object' && item.children.length === 0){ //hash array on single leaf node -> set attrs on the tag
  231. for (var key in value){
  232. value[key] = item[key];
  233. }
  234. return value;
  235. }
  236. if (!isArray && typeof value === 'object'){ //hash array on single leaf node -> set attrs on the tag
  237. value = {};
  238. for (var i=0;i<item.children.length;i++){
  239. var childItem = item.children[i];
  240. value[childItem.value] = childItem.innerText;
  241. }
  242. return value;
  243. }
  244. }
  245. if ("value" in item){
  246. return item.value;
  247. }
  248. return item.innerText;
  249. },prop, "read");
  250. }
  251. syncToDOM();
  252. var scopeProxy = new Proxy($s,{
  253. get(target, prop){
  254. //if (!(prop in target) && (document.getElementById(prop) ||
  255. //document.querySelectorAll(prop).length ||
  256. //document.getElementsByName(prop).length ||
  257. //document.getElementsByClassName(prop).length)){
  258. target[prop] = null;
  259. //}
  260. syncFromDOM(prop);
  261. dom = null;
  262. closure = null;
  263. return target[prop];
  264. },
  265. set(target, prop, value){
  266. //syncFromDOM();
  267. target[prop] = value
  268. syncToDOM(prop);
  269. dom = null;
  270. closure = null;
  271. return true;
  272. },
  273. })
  274. return scopeProxy;
  275. }
  276. function nbGetData(el){
  277. while (!('nbData' in el)){
  278. if (el.parentElement){
  279. el = el.parentElement;
  280. }
  281. else {
  282. return null;
  283. }
  284. }
  285. return el.nbData;
  286. }