|
1 | | - |
2 | | -/** Copyright (c) 2024, Manuel Lõhmus (MIT License). */ |
| 1 | +/** Copyright (c) Manuel Lõhmus (MIT License). */ |
3 | 2 |
|
4 | 3 | "use strict"; |
5 | 4 |
|
|
30 | 29 | getvalue: { value: getValueBind, writable: true, configurable: false, enumerable: false }, |
31 | 30 | setvalue: { value: setValueBind, writable: true, configurable: false, enumerable: false }, |
32 | 31 | check: { value: checkBind, writable: true, configurable: false, enumerable: false }, |
| 32 | + visible: { value: visibleBind, writable: true, configurable: false, enumerable: false }, |
33 | 33 | hidden: { value: hiddenBind, writable: true, configurable: false, enumerable: false }, |
34 | 34 | enabled: { value: enabledBind, writable: true, configurable: false, enumerable: false }, |
35 | 35 | disabled: { value: disabledBind, writable: true, configurable: false, enumerable: false }, |
|
104 | 104 |
|
105 | 105 | bindElement(rootElement, rebinding); |
106 | 106 |
|
107 | | - rootElement.querySelectorAll("[bind]:not(template>*):not([link]>*),[onbind]:not(template>*):not([link]>*),[onunbind]:not(template>*):not([link]>*),[template]:not(template>*):not([link]>*),[templates]:not(template>*):not([link]>*),[rebinding]:not(template>*):not([link]>*),[link]:not(template>*):not([link]>*)") |
| 107 | + rootElement.querySelectorAll(`[bind],[onbind],[template],[templates],[rebinding],[link]`) |
108 | 108 | .forEach(function (element) { |
109 | 109 |
|
| 110 | + if (isParentTemplateOrLink(element)) { return; } |
| 111 | + |
110 | 112 | if (!element.contextValue || rebinding) { |
111 | 113 |
|
112 | 114 | Promise.resolve(1).then((function (element, rebinding) { |
|
115 | 117 | })(element, rebinding)); |
116 | 118 | } |
117 | 119 | }); |
| 120 | + |
| 121 | + function isParentTemplateOrLink(element) { |
| 122 | + |
| 123 | + var parent = element.parentElement; |
| 124 | + |
| 125 | + while (parent !== rootElement) { |
| 126 | + |
| 127 | + if (parent.attributes.template || parent.attributes.link) { return true; } |
| 128 | + |
| 129 | + parent = parent.parentElement; |
| 130 | + } |
| 131 | + return false; |
| 132 | + } |
118 | 133 | } |
119 | 134 |
|
120 | 135 | function rebindAllElements(rootElement) { |
|
136 | 151 | !element?.attributes?.template && |
137 | 152 | !element?.attributes?.templates && |
138 | 153 | !element?.attributes?.onbind && |
139 | | - !element?.attributes?.onunbind && |
140 | 154 | !element?.attributes?.bind && |
141 | 155 | !element?.attributes?.rebinding && |
142 | 156 | !element?.attributes?.link |
|
155 | 169 |
|
156 | 170 | if (element.bindingContext?.isActive) { element.bindingContext.isActive(false); } // for rebinding |
157 | 171 |
|
158 | | - if (element.attributes.link && !element.linked) {// if true then linked |
| 172 | + if (element.attributes.link) { |
159 | 173 |
|
160 | 174 | _link(element.attributes.link.value); |
161 | 175 |
|
|
164 | 178 |
|
165 | 179 | var _bindingContext = bindingContext(element, rebinding); |
166 | 180 |
|
167 | | - if (!rebinding && element.attributes.template) { |
| 181 | + if (element.attributes.template) { |
168 | 182 |
|
169 | 183 | _template(element.attributes.template.value); |
170 | 184 |
|
171 | 185 | return; |
172 | 186 | } |
173 | 187 |
|
174 | | - if (!rebinding && element.attributes.templates) { |
| 188 | + if (element.attributes.templates) { |
175 | 189 |
|
176 | 190 | _template(element.attributes.templates.value, true); |
177 | 191 |
|
|
185 | 199 |
|
186 | 200 | function _link(path) { |
187 | 201 |
|
| 202 | + if (element.linked) { return; } |
| 203 | + |
188 | 204 | var { CreateLink } = globalScope?.modules?.['ws-user']; |
189 | 205 |
|
190 | 206 | if (CreateLink) { |
191 | 207 |
|
| 208 | + //element.setAttribute("linked", path); |
| 209 | + //element.removeAttribute("link"); |
| 210 | + element.linked = true; |
192 | 211 | element.wsLink = WsUser.CreateLink(path, element); |
193 | | - element.wsLink.bindAllElements = function (elem) { bindAllElements(elem, rebinding); }; |
194 | | - element.setAttribute("linked", path); |
195 | | - element.removeAttribute("link"); |
| 212 | + element.wsLink.bindAllElements = function (elem, rebind = false) { bindAllElements(elem, rebind); }; |
196 | 213 | } |
197 | 214 | } |
198 | 215 |
|
|
258 | 275 |
|
259 | 276 | if (isMultiple) { |
260 | 277 |
|
| 278 | + if (typeof _bindingContext.value !== 'object' || !_bindingContext.value) { return; } |
| 279 | + |
261 | 280 | let keys = Object.keys(_bindingContext.value);//.sort(); |
262 | 281 |
|
263 | 282 | for (var i = 0; i < keys.length; i++) { |
|
269 | 288 |
|
270 | 289 | setTimeout(function () { |
271 | 290 |
|
272 | | - if (_bindingContext.source && typeof _bindingContext.source.on === "function") { |
| 291 | + if (_bindingContext.value && typeof _bindingContext.value.on === "function") { |
273 | 292 |
|
274 | | - _bindingContext.source.on( |
| 293 | + _bindingContext.value.on( |
275 | 294 | "-", |
276 | 295 | function addTemplate(event) { |
277 | 296 |
|
278 | | - if (event.eventName === "new") { |
| 297 | + if (event.eventName === "new" && (!isMultiple || isMultiple && event.propertyPath.length === 1)) { |
279 | 298 |
|
280 | 299 | return appendTemplate(event.propertyPath.at(-1), event.target); |
281 | 300 | } |
|
285 | 304 | element |
286 | 305 | ); |
287 | 306 |
|
288 | | - _bindingContext.source.on( |
| 307 | + _bindingContext.value.on( |
289 | 308 | "-", |
290 | 309 | function removeTemplate(event) { |
291 | 310 |
|
292 | | - if (event.eventName === "delete") { |
| 311 | + if (event.eventName === "delete" && (!isMultiple || isMultiple && event.propertyPath.length === 1)) { |
293 | 312 |
|
294 | 313 | element.querySelectorAll(`:scope > [path="${event.propertyPath.at(-1)}"]`).forEach(function (e) { |
295 | 314 |
|
|
307 | 326 | return; |
308 | 327 | } |
309 | 328 | } |
310 | | - else { |
311 | 329 |
|
312 | | - return appendTemplate(); |
313 | | - } |
| 330 | + return appendTemplate(); |
314 | 331 |
|
315 | | - return; |
316 | 332 |
|
317 | 333 | function appendTemplate(key, target) { |
318 | 334 |
|
|
392 | 408 | } catch (err) { return _error("[ bind ] ", err); } |
393 | 409 | } |
394 | 410 |
|
395 | | - if (element.attributes.onbind && DataContextBinding.bind) { |
| 411 | + if (element.attributes.onbind) { |
396 | 412 |
|
397 | 413 | try { |
398 | 414 |
|
399 | | - _bind(DataContextBinding.bind("event", element.attributes.onbind.value)); |
| 415 | + element.setAttribute('onblur', element.attributes.onbind.value); |
| 416 | + _bind(element.onblur); |
| 417 | + element.removeAttribute('onblur'); |
400 | 418 |
|
401 | 419 | } catch (err) { return _error("[ onbind ] ", err); } |
402 | 420 | } |
|
405 | 423 |
|
406 | 424 | function _bind(fnBind) { |
407 | 425 |
|
408 | | - if (_bindingContext.source === undefined) { |
409 | | - |
410 | | - var event = { eventName: "unbind", target: undefined, propertyPath: [_bindingContext.property], oldValue: undefined, newValue: undefined }; |
411 | | - |
412 | | - element.setAttribute("unbinded", ""); |
413 | | - |
414 | | - fnBind.call(element, event); |
415 | | - |
416 | | - if (element.attributes.onunbind && DataContextBinding.bind) { |
417 | | - |
418 | | - try { |
419 | | - |
420 | | - (DataContextBinding.bind("event", element.attributes.onunbind.value)).call(element, event); |
421 | | - |
422 | | - } catch (err) { return _error("[ onunbind ] ", err); } |
423 | | - } |
424 | | - } |
425 | | - else if (fnBind.call(element, { eventName: "bind", target: _bindingContext.source, propertyPath: [_bindingContext.property], oldValue: _bindingContext.value, newValue: _bindingContext.value })) { |
| 426 | + if (fnBind.call(element, { eventName: "bind", target: _bindingContext.source, propertyPath: [_bindingContext.property], oldValue: _bindingContext.value, newValue: _bindingContext.value })) { |
426 | 427 |
|
427 | 428 | if (_bindingContext.source && typeof _bindingContext.source.on === "function") { |
428 | 429 |
|
|
800 | 801 |
|
801 | 802 | function innerHTMLBind(event) { |
802 | 803 |
|
803 | | - if (event.eventName === "unbind") { |
| 804 | + if (event?.eventName === "unbind") { |
804 | 805 |
|
805 | 806 | this.innerHTML = ''; |
806 | 807 | // I am dead! |
|
817 | 818 |
|
818 | 819 | if (event.eventName === "unbind") { |
819 | 820 |
|
820 | | - this.onchange = null; |
821 | | - this.onkeydown = null; |
| 821 | + this.removeEventListener("change", change); |
| 822 | + this.removeEventListener("keydown", keydown); |
822 | 823 |
|
823 | 824 | this.value = ''; |
824 | 825 |
|
|
831 | 832 | // Init |
832 | 833 | if (event.eventName === "bind") { |
833 | 834 |
|
834 | | - this.onchange = function () { this.contextValue(this.value); }; |
835 | | - this.onkeydown = function (ev) { if (ev.keyCode === 27) { this.value = his.contextValue(); } }; |
| 835 | + this.addEventListener("change", change); |
| 836 | + this.addEventListener("keydown", keydown); |
836 | 837 | } |
837 | 838 |
|
838 | 839 | // I am alive! |
839 | 840 | return true; |
| 841 | + |
| 842 | + function change(ev) { if (this.contextValue !== undefined) { this.contextValue(this.value); } } |
| 843 | + function keydown(ev) { if (ev.keyCode === 27) { this.value = this.contextValue(); } } |
840 | 844 | } |
841 | 845 | function getValueBind(event) { |
842 | 846 |
|
843 | 847 | if (event.eventName === "unbind") { |
844 | 848 |
|
845 | | - this.onchange = null; |
846 | | - this.onkeydown = null; |
847 | | - |
848 | 849 | this.value = ''; |
849 | 850 |
|
850 | 851 | // I am dead! |
|
860 | 861 |
|
861 | 862 | if (event.eventName === "unbind") { |
862 | 863 |
|
863 | | - this.onchange = null; |
864 | | - this.onkeydown = null; |
| 864 | + this.removeEventListener("change", change); |
| 865 | + this.removeEventListener("keydown", keydown); |
865 | 866 |
|
866 | 867 | this.value = ''; |
867 | 868 |
|
|
872 | 873 | // Init |
873 | 874 | if (event.eventName === "bind") { |
874 | 875 |
|
875 | | - this.onchange = function () { this.contextValue(this.value); }; |
876 | | - this.onkeydown = function (ev) { if (ev.keyCode === 27) { this.value = this.contextValue(); } }; |
| 876 | + this.addEventListener("change", change); |
| 877 | + this.addEventListener("keydown", keydown); |
877 | 878 | } |
878 | 879 |
|
879 | 880 | // I am alive! |
880 | 881 | return true; |
| 882 | + |
| 883 | + function change(ev) { if (this.contextValue !== undefined) { this.contextValue(this.value); } } |
| 884 | + function keydown(ev) { if (ev.keyCode === 27) { this.value = this.contextValue(); } } |
881 | 885 | } |
882 | 886 | function checkBind(event) { |
883 | 887 |
|
884 | 888 | if (event.eventName === "unbind") { |
885 | 889 |
|
886 | | - this.onchange = null; |
| 890 | + this.removeEventListener("change", change); |
887 | 891 |
|
888 | 892 | // I am dead! |
889 | 893 | return false; |
|
895 | 899 | // Init |
896 | 900 | if (event.eventName === "bind") { |
897 | 901 |
|
898 | | - this.onchange = function () { this.contextValue(this.checked); }; |
| 902 | + this.addEventListener("change", change); |
899 | 903 | } |
900 | 904 |
|
901 | 905 | // I am alive! |
902 | 906 | return this.contextValue() === undefined ? false : true; |
| 907 | + |
| 908 | + function change(ev) { if (this.contextValue() !== undefined) { this.contextValue(this.checked); } } |
| 909 | + } |
| 910 | + function visibleBind(event) { |
| 911 | + |
| 912 | + if (event.eventName === "unbind") { |
| 913 | + |
| 914 | + // I am dead! |
| 915 | + return false; |
| 916 | + } |
| 917 | + |
| 918 | + this.contextValue() ? this.removeAttribute("hidden") : this.setAttribute("hidden", ""); |
| 919 | + |
| 920 | + // I am alive! |
| 921 | + return this.contextValue() === undefined ? false : true; |
903 | 922 | } |
904 | 923 | function hiddenBind(event) { |
905 | 924 |
|
|
918 | 937 |
|
919 | 938 | if (event.eventName === "unbind") { |
920 | 939 |
|
| 940 | + this.removeEventListener("change", change); |
921 | 941 | // I am dead! |
922 | 942 | return false; |
923 | 943 | } |
|
936 | 956 | _this.value = str; |
937 | 957 | }, 50, this); |
938 | 958 |
|
939 | | - //TODO [ x ] I am dead! |
| 959 | + // I am dead! |
940 | 960 | return this.bindingContext?.isActive(); |
941 | 961 | }, |
942 | 962 | // Set DOM element. |
|
945 | 965 | this |
946 | 966 | ); |
947 | 967 |
|
948 | | - this.onchange = function () { |
| 968 | + this.addEventListener("change", change); |
| 969 | + } |
| 970 | + |
| 971 | + // I am alive! |
| 972 | + return true; |
| 973 | + |
| 974 | + function change(ev) { |
| 975 | + |
| 976 | + if (this.contextValue !== undefined) { |
949 | 977 | // Let's take the context of the data. |
950 | 978 | this.contextValue() |
951 | 979 | // We will overvalue the modified JSON string data. |
952 | 980 | .overwritingData(this.value); |
953 | | - }; |
| 981 | + } |
954 | 982 | } |
955 | | - |
956 | | - // I am alive! |
957 | | - return true; |
958 | 983 | } |
959 | 984 | function enabledBind(event) { |
960 | 985 |
|
|
0 commit comments