|
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 }, |
| 36 | + class: { value: classToggleBind, writable: true, configurable: false, enumerable: false }, |
| 37 | + attribute: { value: attributeBind, writable: true, configurable: false, enumerable: false }, |
36 | 38 |
|
37 | 39 | input: { value: valueBind, writable: true, configurable: false, enumerable: false }, |
38 | 40 | input_checkbox: { value: checkBind, writable: true, configurable: false, enumerable: false }, |
|
122 | 124 |
|
123 | 125 | var parent = element.parentElement; |
124 | 126 |
|
125 | | - while (parent !== rootElement) { |
| 127 | + while (parent && parent !== rootElement) { |
126 | 128 |
|
127 | 129 | if (parent.attributes.template || parent.attributes.link) { return true; } |
128 | 130 |
|
|
284 | 286 | if (!appendTemplate(keys[i])) { break; } |
285 | 287 | } |
286 | 288 |
|
287 | | - if (i !== 0) { |
| 289 | + setTimeout(function () { |
288 | 290 |
|
289 | | - setTimeout(function () { |
| 291 | + if (_bindingContext?.value?._isDataContext) { |
290 | 292 |
|
291 | | - if (_bindingContext.value && typeof _bindingContext.value.on === "function") { |
| 293 | + if (isFunctionNotAdded('addTemplate')) { |
292 | 294 |
|
293 | 295 | _bindingContext.value.on( |
294 | 296 | "-", |
|
303 | 305 | }, |
304 | 306 | element |
305 | 307 | ); |
| 308 | + } |
| 309 | + |
| 310 | + if (isFunctionNotAdded('removeTemplate')) { |
306 | 311 |
|
307 | 312 | _bindingContext.value.on( |
308 | 313 | "-", |
|
321 | 326 | element |
322 | 327 | ); |
323 | 328 | } |
324 | | - }); |
| 329 | + } |
| 330 | + }); |
325 | 331 |
|
326 | | - return; |
327 | | - } |
| 332 | + return; |
328 | 333 | } |
329 | 334 |
|
330 | 335 | return appendTemplate(); |
331 | 336 |
|
332 | 337 |
|
| 338 | + function isFunctionNotAdded(fnName) { |
| 339 | + |
| 340 | + return _bindingContext?.value?._events?.["-"] |
| 341 | + ?.find(function (fn) { return fn.name.includes(fnName); }) === undefined; |
| 342 | + } |
333 | 343 | function appendTemplate(key, target) { |
334 | 344 |
|
335 | 345 | var ret = false; |
|
394 | 404 |
|
395 | 405 | try { |
396 | 406 |
|
397 | | - var fnName = element.attributes.bind.value; |
| 407 | + var fnName = element.attributes.bind.value, |
| 408 | + isValueInverted = false, |
| 409 | + bindArgs = []; |
398 | 410 |
|
399 | | - if (!fnName) { |
| 411 | + if (fnName) { |
| 412 | + |
| 413 | + //!fnName(bindArg1,bindArg2,...) |
| 414 | + if (fnName.startsWith("!")) { isValueInverted = true; fnName = fnName.substring(1); } |
| 415 | + bindArgs = fnName.split(/[\(\)]/); |
| 416 | + fnName = bindArgs.shift().trim(); |
| 417 | + if (bindArgs[0]) { bindArgs = bindArgs[0].split(/,/); } |
| 418 | + bindArgs = bindArgs.map(function (arg) { return arg.trim(); }); |
| 419 | + } |
| 420 | + |
| 421 | + else { |
400 | 422 |
|
401 | 423 | fnName = element.tagName.toLowerCase(); |
402 | 424 |
|
|
423 | 445 |
|
424 | 446 | function _bind(fnBind) { |
425 | 447 |
|
426 | | - if (fnBind.call(element, { eventName: "bind", target: _bindingContext.source, propertyPath: [_bindingContext.property], oldValue: _bindingContext.value, newValue: _bindingContext.value })) { |
| 448 | + fnBind = (function (fn) { |
427 | 449 |
|
428 | | - if (_bindingContext.source && typeof _bindingContext.source.on === "function") { |
| 450 | + return function (event) { |
| 451 | + |
| 452 | + if (element.contextValue?.() === undefined) { |
| 453 | + |
| 454 | + element.setAttribute("unbinded", ""); |
| 455 | + event.named = "unbind"; |
| 456 | + } |
| 457 | + else { element.removeAttribute("unbinded"); } |
| 458 | + |
| 459 | + return fn.call(this, event); |
| 460 | + }; |
429 | 461 |
|
430 | | - element.removeAttribute("unbinded"); |
| 462 | + })(fnBind); |
| 463 | + |
| 464 | + if (fnBind.call(element, { |
| 465 | + eventName: "bind", |
| 466 | + isValueInverted, |
| 467 | + bindArgs, |
| 468 | + target: _bindingContext.source, |
| 469 | + propertyPath: [_bindingContext.property], |
| 470 | + oldValue: _bindingContext.value, |
| 471 | + newValue: _bindingContext.value |
| 472 | + })) { |
| 473 | + |
| 474 | + if (_bindingContext.source && typeof _bindingContext.source.on === "function") { |
431 | 475 |
|
432 | 476 | _bindingContext.source.on( |
433 | 477 | _bindingContext.property, |
434 | | - function (event) { return fnBind.call(element, event); }, |
| 478 | + fnBind.bind(element), |
435 | 479 | element |
436 | 480 | ); |
437 | 481 | } |
|
915 | 959 | return false; |
916 | 960 | } |
917 | 961 |
|
918 | | - this.contextValue() ? this.removeAttribute("hidden") : this.setAttribute("hidden", ""); |
| 962 | + if (event.eventName === "bind") { |
| 963 | + |
| 964 | + this.isValueInverted = event.isValueInverted; |
| 965 | + } |
| 966 | + |
| 967 | + updateAttributeBinding.call(this, |
| 968 | + this.isValueInverted ? this.contextValue() : !this.contextValue(), |
| 969 | + 'hidden', |
| 970 | + 'hidden' |
| 971 | + ); |
919 | 972 |
|
920 | 973 | // I am alive! |
921 | 974 | return this.contextValue() === undefined ? false : true; |
|
928 | 981 | return false; |
929 | 982 | } |
930 | 983 |
|
931 | | - this.contextValue() ? this.setAttribute("hidden", "") : this.removeAttribute("hidden"); |
| 984 | + if (event.eventName === "bind") { |
| 985 | + |
| 986 | + this.isValueInverted = event.isValueInverted; |
| 987 | + } |
| 988 | + |
| 989 | + updateAttributeBinding.call(this, |
| 990 | + this.isValueInverted ? !this.contextValue() : this.contextValue(), |
| 991 | + 'hidden', |
| 992 | + 'hidden' |
| 993 | + ); |
932 | 994 |
|
933 | 995 | // I am alive! |
934 | 996 | return this.contextValue() === undefined ? false : true; |
|
991 | 1053 | return false; |
992 | 1054 | } |
993 | 1055 |
|
| 1056 | + if (event.eventName === "bind") { |
| 1057 | + |
| 1058 | + this.isValueInverted = event.isValueInverted; |
| 1059 | + } |
| 1060 | + |
| 1061 | + updateAttributeBinding.call(this, |
| 1062 | + this.isValueInverted ? this.contextValue() : !this.contextValue(), |
| 1063 | + 'disabled', |
| 1064 | + 'disabled' |
| 1065 | + ); |
994 | 1066 | if (this.contextValue()) { this.removeAttribute('disabled'); } |
995 | 1067 | else { this.setAttribute('disabled', 'disabled'); } |
996 | 1068 |
|
|
1007 | 1079 | return false; |
1008 | 1080 | } |
1009 | 1081 |
|
1010 | | - if (this.contextValue()) { this.setAttribute('disabled', 'disabled'); } |
1011 | | - else { this.removeAttribute('disabled'); } |
| 1082 | + if (event.eventName === "bind") { |
| 1083 | + |
| 1084 | + this.isValueInverted = event.isValueInverted; |
| 1085 | + } |
| 1086 | + |
| 1087 | + updateAttributeBinding.call(this, |
| 1088 | + this.isValueInverted ? !this.contextValue() : this.contextValue(), |
| 1089 | + 'disabled', |
| 1090 | + 'disabled' |
| 1091 | + ); |
1012 | 1092 |
|
1013 | 1093 | // I am alive! |
1014 | | - return true; |
| 1094 | + return this.contextValue() === undefined ? false : true; |
1015 | 1095 | }; |
| 1096 | + function classToggleBind(event) { |
| 1097 | + |
| 1098 | + if (event.eventName === "unbind") { |
| 1099 | + |
| 1100 | + toggle(this, false); |
| 1101 | + |
| 1102 | + // I am dead! |
| 1103 | + return false; |
| 1104 | + } |
1016 | 1105 |
|
| 1106 | + if (event.eventName === "bind") { |
| 1107 | + |
| 1108 | + this.isValueInverted = event.isValueInverted; |
| 1109 | + |
| 1110 | + if (event.bindArgs.length > 1) { |
| 1111 | + |
| 1112 | + this.primaryClasses = parseClassNames(event.bindArgs[0]); |
| 1113 | + this.secondaryClasses = parseClassNames(event.bindArgs[1]); |
| 1114 | + } |
| 1115 | + else { |
| 1116 | + |
| 1117 | + this.primaryClasses = parseClassNames(event.bindArgs[0]); |
| 1118 | + this.secondaryClasses = []; |
| 1119 | + } |
| 1120 | + } |
| 1121 | + |
| 1122 | + toggle(this, this.isValueInverted ? !this.contextValue() : this.contextValue()); |
| 1123 | + |
| 1124 | + // I am alive! |
| 1125 | + return true; |
| 1126 | + |
| 1127 | + |
| 1128 | + function parseClassNames(classNames) { |
| 1129 | + |
| 1130 | + return classNames |
| 1131 | + .split(/\s/) |
| 1132 | + .filter(function (c) { return c; }); |
| 1133 | + } |
| 1134 | + function toggle(element, isPrimary) { |
| 1135 | + |
| 1136 | + if (isPrimary) { |
| 1137 | + element.primaryClasses.forEach(function (c) { element.classList.add(c); }); |
| 1138 | + element.secondaryClasses.forEach(function (c) { element.classList.remove(c); }); |
| 1139 | + } |
| 1140 | + else { |
| 1141 | + element.primaryClasses.forEach(function (c) { element.classList.remove(c); }); |
| 1142 | + element.secondaryClasses.forEach(function (c) { element.classList.add(c); }); |
| 1143 | + } |
| 1144 | + } |
| 1145 | + } |
| 1146 | + function attributeBind(event) { |
| 1147 | + |
| 1148 | + if (event.eventName === "unbind") { |
| 1149 | + |
| 1150 | + updateAttributeBinding.call(this, false, this.attributeBindingName, this.attributeBindingValue); |
| 1151 | + |
| 1152 | + // I am dead! |
| 1153 | + return false; |
| 1154 | + } |
| 1155 | + |
| 1156 | + if (event.eventName === "bind") { |
| 1157 | + |
| 1158 | + this.isValueInverted = event.isValueInverted; |
| 1159 | + this.attributeBindingName = event.bindArgs[0] || ''; |
| 1160 | + this.attributeBindingValue = event.bindArgs[1] || ''; |
| 1161 | + } |
| 1162 | + |
| 1163 | + updateAttributeBinding.call(this, |
| 1164 | + this.isValueInverted ? !this.contextValue() : this.contextValue(), |
| 1165 | + this.attributeBindingName, |
| 1166 | + this.attributeBindingValue |
| 1167 | + ); |
| 1168 | + |
| 1169 | + // I am alive! |
| 1170 | + return this.contextValue() === undefined ? false : true; |
| 1171 | + } |
| 1172 | + |
| 1173 | + |
| 1174 | + function updateAttributeBinding(isEnabled, attributeName, attributeValue = '') { |
| 1175 | + |
| 1176 | + if (attributeName) { |
| 1177 | + |
| 1178 | + if (isEnabled) { this.setAttribute(attributeName, attributeValue); } |
| 1179 | + |
| 1180 | + else { this.removeAttribute(attributeName); } |
| 1181 | + } |
| 1182 | + } |
1017 | 1183 |
|
1018 | 1184 | //#endregion |
1019 | 1185 | }); |
|
0 commit comments