resonantjs 1.0.9 → 1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/examples/example-taskmanager.html +25 -18
- package/package.json +1 -1
- package/resonant.js +24 -10
- package/resonant.min.js +1 -1
|
@@ -3,6 +3,10 @@
|
|
|
3
3
|
<head>
|
|
4
4
|
<title>Resonant.js Task Manager Demo</title>
|
|
5
5
|
<script src="../resonant.js"></script>
|
|
6
|
+
<link
|
|
7
|
+
rel="stylesheet"
|
|
8
|
+
href="https://cdn.jsdelivr.net/npm/@picocss/pico@2.0.6/css/pico.min.css"
|
|
9
|
+
/>
|
|
6
10
|
</head>
|
|
7
11
|
<style>
|
|
8
12
|
.done {
|
|
@@ -10,26 +14,29 @@
|
|
|
10
14
|
}
|
|
11
15
|
</style>
|
|
12
16
|
<body>
|
|
13
|
-
<
|
|
17
|
+
<main class="container">
|
|
18
|
+
<h1>Resonant.js Task Manager Demo</h1>
|
|
14
19
|
|
|
15
|
-
<!-- Task Input -->
|
|
16
|
-
<div>
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
</div>
|
|
20
|
+
<!-- Task Input -->
|
|
21
|
+
<div>
|
|
22
|
+
<h2>Add New Task</h2>
|
|
23
|
+
<input type="text" placeholder="Task Name" res="taskName" />
|
|
24
|
+
<button onclick="addTask()">Add Task</button>
|
|
25
|
+
</div>
|
|
26
|
+
|
|
27
|
+
<!-- Task List -->
|
|
28
|
+
<div>
|
|
29
|
+
<h2>Task List</h2>
|
|
30
|
+
<ul res="tasks">
|
|
31
|
+
<li res-style="tasks.done ? 'done' : ''">
|
|
32
|
+
<input type="checkbox" res-prop="done" />
|
|
33
|
+
<span res-prop="name"></span>
|
|
34
|
+
<button res-onclick="remove">Remove</button>
|
|
35
|
+
</li>
|
|
36
|
+
</ul>
|
|
37
|
+
</div>
|
|
38
|
+
</main>
|
|
21
39
|
|
|
22
|
-
<!-- Task List -->
|
|
23
|
-
<div>
|
|
24
|
-
<h2>Task List</h2>
|
|
25
|
-
<ul res="tasks">
|
|
26
|
-
<li res-style="tasks.done ? 'done' : ''">
|
|
27
|
-
<input type="checkbox" res-prop="done" />
|
|
28
|
-
<span res-prop="name"></span>
|
|
29
|
-
<button res-onclick="remove">Remove</button>
|
|
30
|
-
</li>
|
|
31
|
-
</ul>
|
|
32
|
-
</div>
|
|
33
40
|
|
|
34
41
|
<script>
|
|
35
42
|
const resonantJs = new Resonant();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "resonantjs",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.1",
|
|
4
4
|
"description": "A lightweight JavaScript framework that enables reactive data-binding for building dynamic and responsive web applications. It simplifies creating interactive UIs by automatically updating the DOM when your data changes.",
|
|
5
5
|
"main": "resonant.js",
|
|
6
6
|
"repository": {
|
package/resonant.js
CHANGED
|
@@ -4,6 +4,9 @@ class ObservableArray extends Array {
|
|
|
4
4
|
return super(...args);
|
|
5
5
|
}
|
|
6
6
|
super(...args);
|
|
7
|
+
|
|
8
|
+
var isCreating = resonantInstance.data[variableName] === undefined;
|
|
9
|
+
|
|
7
10
|
this.variableName = variableName;
|
|
8
11
|
this.resonantInstance = resonantInstance;
|
|
9
12
|
this.isDeleting = false;
|
|
@@ -13,6 +16,10 @@ class ObservableArray extends Array {
|
|
|
13
16
|
this[index] = this._createProxy(item, index);
|
|
14
17
|
}
|
|
15
18
|
});
|
|
19
|
+
|
|
20
|
+
if(!isCreating) {
|
|
21
|
+
this.forceUpdate();
|
|
22
|
+
}
|
|
16
23
|
}
|
|
17
24
|
|
|
18
25
|
_createProxy(item, index) {
|
|
@@ -221,9 +228,11 @@ class Resonant {
|
|
|
221
228
|
|
|
222
229
|
if (this.pendingUpdates.get(variableName).length === 1) {
|
|
223
230
|
setTimeout(() => {
|
|
224
|
-
|
|
231
|
+
let updates = this.pendingUpdates.get(variableName);
|
|
225
232
|
this.updatePersistantData(variableName);
|
|
226
233
|
this.pendingUpdates.delete(variableName);
|
|
234
|
+
|
|
235
|
+
updates = updates.filter((v, i, a) => a.findIndex(t => (t.property === v.property && t.action === v.action)) === i);
|
|
227
236
|
updates.forEach(update => {
|
|
228
237
|
this._triggerCallbacks(variableName, update);
|
|
229
238
|
});
|
|
@@ -388,36 +397,41 @@ class Resonant {
|
|
|
388
397
|
const clonedEl = template.cloneNode(true);
|
|
389
398
|
clonedEl.setAttribute("res-index", index);
|
|
390
399
|
for (let key in instance) {
|
|
391
|
-
|
|
400
|
+
let overrideInstanceValue = null;
|
|
401
|
+
let subEl = clonedEl.querySelector(`[res-prop="${key}"]`);
|
|
402
|
+
if(!subEl) {
|
|
403
|
+
subEl = clonedEl.querySelector(`[res-prop=""]`);
|
|
404
|
+
overrideInstanceValue = instance;
|
|
405
|
+
}
|
|
392
406
|
if (subEl) {
|
|
393
407
|
if (!subEl.hasAttribute('data-resonant-bound')) {
|
|
394
408
|
if (subEl.tagName === 'INPUT' || subEl.tagName === 'TEXTAREA') {
|
|
395
409
|
if (subEl.type === 'checkbox') {
|
|
396
|
-
subEl.checked = instance[key];
|
|
410
|
+
subEl.checked = overrideInstanceValue ?? instance[key] ;
|
|
397
411
|
subEl.onchange = () => {
|
|
398
412
|
instance[key] = subEl.checked;
|
|
399
|
-
this._queueUpdate(variableName, 'modified', instance, key, instance[key]);
|
|
413
|
+
this._queueUpdate(variableName, 'modified', instance, key, overrideInstanceValue ?? instance[key]);
|
|
400
414
|
};
|
|
401
415
|
} else {
|
|
402
|
-
subEl.value = instance[key];
|
|
416
|
+
subEl.value = overrideInstanceValue ?? instance[key];
|
|
403
417
|
subEl.oninput = () => {
|
|
404
418
|
instance[key] = subEl.value;
|
|
405
|
-
this._queueUpdate(variableName, 'modified', instance, key, instance[key]);
|
|
419
|
+
this._queueUpdate(variableName, 'modified', instance, key, overrideInstanceValue ?? instance[key]);
|
|
406
420
|
};
|
|
407
421
|
}
|
|
408
422
|
} else {
|
|
409
|
-
subEl.innerHTML = instance[key];
|
|
423
|
+
subEl.innerHTML = overrideInstanceValue ?? instance[key];
|
|
410
424
|
}
|
|
411
425
|
subEl.setAttribute('data-resonant-bound', 'true');
|
|
412
426
|
} else {
|
|
413
427
|
if (subEl.tagName === 'INPUT' || subEl.tagName === 'TEXTAREA') {
|
|
414
428
|
if (subEl.type === 'checkbox') {
|
|
415
|
-
subEl.checked = instance[key];
|
|
429
|
+
subEl.checked = overrideInstanceValue ?? instance[key];
|
|
416
430
|
} else {
|
|
417
|
-
subEl.value = instance[key];
|
|
431
|
+
subEl.value = overrideInstanceValue ?? instance[key];
|
|
418
432
|
}
|
|
419
433
|
} else {
|
|
420
|
-
subEl.innerHTML = instance[key];
|
|
434
|
+
subEl.innerHTML = overrideInstanceValue ?? instance[key];
|
|
421
435
|
}
|
|
422
436
|
}
|
|
423
437
|
}
|
package/resonant.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
class ObservableArray extends Array{constructor(e,t,...a){if(void 0===t)return super(...a);super(...a)
|
|
1
|
+
class ObservableArray extends Array{constructor(e,t,...a){if(void 0===t)return super(...a);super(...a);var s=void 0===t.data[e];this.variableName=e,this.resonantInstance=t,this.isDeleting=!1,this.forEach(((e,t)=>{"object"==typeof e&&(this[t]=this._createProxy(e,t))})),s||this.forceUpdate()}_createProxy(e,t){return new Proxy(e,{set:(e,a,s)=>{if(e[a]!==s){const i=e[a];e[a]=s,this.resonantInstance._queueUpdate(this.variableName,"modified",e,a,i,t)}return!0}})}forceUpdate(){this.resonantInstance.arrayDataChangeDetection[this.variableName]=this.slice(),this.resonantInstance._queueUpdate(this.variableName,"modified",this.slice())}update(e){window[this.variableName]=e,this.resonantInstance._queueUpdate(this.variableName,"updated",e)}push(...e){e=e.map(((e,t)=>"object"==typeof e?this._createProxy(e,this.length+t):e));const t=super.push(...e);return this.resonantInstance.arrayDataChangeDetection[this.variableName]=this.slice(),e.forEach(((e,t)=>{this.resonantInstance._queueUpdate(this.variableName,"added",e,this.length-1-t)})),t}splice(e,t,...a){a=a.map(((t,a)=>"object"==typeof t?this._createProxy(t,e+a):t));const s=super.splice(e,t,...a);return this.resonantInstance.arrayDataChangeDetection[this.variableName]=this.slice(),t>0&&s.forEach(((t,a)=>{this.resonantInstance._queueUpdate(this.variableName,"removed",t,e+a)})),a.length>0&&a.forEach(((t,a)=>{this.resonantInstance._queueUpdate(this.variableName,"added",t,e+a)})),s}set(e,t){if(this[e]!==t){if(this.isDeleting)return!0;const a=this.resonantInstance.arrayDataChangeDetection[this.variableName];let s="modified";(e>=a.length||void 0===a[e])&&(s="added"),this.resonantInstance.arrayDataChangeDetection[this.variableName]=this.slice();const i=this[e];this[e]=t,this.resonantInstance._queueUpdate(this.variableName,s,this[e],e,i)}return!0}delete(e){const t=this[e];return this.isDeleting=!0,this.splice(e,1),this.resonantInstance.arrayDataChangeDetection[this.variableName]=this.slice(),this.resonantInstance._queueUpdate(this.variableName,"removed",null,e,t),this.isDeleting=!1,!0}filter(e,t=!0){if(void 0===this.resonantInstance||!1===t)return super.filter(e);const a=super.filter(e);return this.resonantInstance.arrayDataChangeDetection[this.variableName]=this.slice(),this.resonantInstance._queueUpdate(this.variableName,"filtered"),a}}class Resonant{constructor(){this.data={},this.callbacks={},this.pendingUpdates=new Map,this.arrayDataChangeDetection={}}add(e,t,a){t=this.persist(e,t,a),Array.isArray(t)?(this.data[e]=new ObservableArray(e,this,...t),this.arrayDataChangeDetection[e]=this.data[e].slice()):this.data[e]="object"==typeof t?this._createObject(e,t):t,this._defineProperty(e),this.updateElement(e)}persist(e,t,a){if(void 0===a||!a)return t;var s=localStorage.getItem("res_"+e);return null!=s?JSON.parse(localStorage.getItem("res_"+e)):(localStorage.setItem("res_"+e,JSON.stringify(t)),t)}updatePersistantData(e){localStorage.getItem("res_"+e)&&localStorage.setItem("res_"+e,JSON.stringify(this.data[e]))}addAll(e){Object.entries(e).forEach((([e,t])=>{this.add(e,t)}))}_createObject(e,t){return t[Symbol("isProxy")]=!0,new Proxy(t,{set:(t,a,s)=>{if(t[a]!==s){const i=t[a];t[a]=s,this._queueUpdate(e,"modified",t,a,i)}return!0}})}_defineProperty(e){Object.defineProperty(window,e,{get:()=>this.data[e],set:t=>{Array.isArray(t)?(this.data[e]=new ObservableArray(e,this,...t),this.arrayDataChangeDetection[e]=this.data[e].slice()):this.data[e]="object"==typeof t?this._createObject(e,t):t,this.updateElement(e),this.updateDisplayConditionalsFor(e),this.updateStylesFor(e),Array.isArray(t)||"object"==typeof t||this._queueUpdate(e,"modified",this.data[e])}})}_queueUpdate(e,t,a,s,i){this.pendingUpdates.has(e)||this.pendingUpdates.set(e,[]),this.pendingUpdates.get(e).push({action:t,item:a,property:s,oldValue:i}),1===this.pendingUpdates.get(e).length&&setTimeout((()=>{let t=this.pendingUpdates.get(e);this.updatePersistantData(e),this.pendingUpdates.delete(e),t=t.filter(((e,t,a)=>a.findIndex((t=>t.property===e.property&&t.action===e.action))===t)),t.forEach((t=>{this._triggerCallbacks(e,t)})),this.updateElement(e),this.updateDisplayConditionalsFor(e),this.updateStylesFor(e)}),0)}_triggerCallbacks(e,t){this.callbacks[e]&&this.callbacks[e].forEach((a=>{const s=t.item||t.oldValue;a(this.data[e],s,t.action)}))}updateElement(e){const t=document.querySelectorAll(`[res="${e}"]`),a=this.data[e];t.forEach((t=>{if(t.value=a,"INPUT"===t.tagName||"TEXTAREA"===t.tagName)t.hasAttribute("data-resonant-bound")||(t.oninput=()=>{this.data[e]=t.value,this._queueUpdate(e,"modified",this.data[e])},t.setAttribute("data-resonant-bound","true"));else if(Array.isArray(a))t.querySelectorAll(`[res="${e}"][res-rendered=true]`).forEach((e=>e.remove())),this._renderArray(e,t);else if("object"==typeof a){t.querySelectorAll("[res-prop]").forEach((t=>{const s=t.getAttribute("res-prop");s&&s in a&&(t.hasAttribute("data-resonant-bound")?"INPUT"===t.tagName||"TEXTAREA"===t.tagName?"checkbox"===t.type?t.checked=a[s]:t.value=a[s]:t.innerHTML=a[s]:("INPUT"===t.tagName||"TEXTAREA"===t.tagName?"checkbox"===t.type?(t.checked=a[s],t.onchange=()=>{this.data[e][s]=t.checked}):(t.value=a[s],t.oninput=()=>{this.data[e][s]=t.value}):t.innerHTML=a[s],t.setAttribute("data-resonant-bound","true")))}))}else t.innerHTML=a})),this.updateDisplayConditionalsFor(e),this.updateStylesFor(e)}updateDisplayConditionalsFor(variableName){const conditionalElements=document.querySelectorAll(`[res-display*="${variableName}"]`);conditionalElements.forEach((conditionalElement=>{const condition=conditionalElement.getAttribute("res-display");try{eval(condition)?conditionalElement.style.display="":conditionalElement.style.display="none"}catch(e){console.error(`Error evaluating condition for ${variableName}: ${condition}`,e)}}))}updateStylesFor(variableName){const styleElements=document.querySelectorAll(`[res-style*="${variableName}"]`);styleElements.forEach((styleElement=>{let styleCondition=styleElement.getAttribute("res-style");try{let parent=styleElement,index=null;for(;parent&&!index;)index=parent.getAttribute("res-index"),parent=parent.parentElement;let resStyles=styleElement.getAttribute("res-styles");if(resStyles){let e=resStyles.split(" ");e.forEach((e=>{styleElement.classList.remove(e)}))}if(null!==index){const e=this.data[variableName][index];styleCondition=styleCondition.replace(new RegExp(`\\b${variableName}\\b`,"g"),"item");const t=new Function("item",`return ${styleCondition}`)(e);if(t)styleElement.classList.add(t);else{var elementHasStyle=styleElement.classList.contains(t);elementHasStyle&&styleElement.classList.remove(t)}}else{const styleClass=eval(styleCondition);if(styleClass)styleElement.classList.add(styleClass),styleElement.setAttribute("res-styles",styleClass);else{var elementHasStyle=styleElement.classList.contains(styleClass);elementHasStyle&&styleElement.classList.remove(styleClass)}}}catch(e){console.error(`Error evaluating style for ${variableName}: ${styleCondition}`,e)}}))}_renderArray(e,t){let a=t.cloneNode(!0);t.innerHTML="",window[e+"_template"]?a=window[e+"_template"]:window[e+"_template"]=a,this.data[e].forEach(((s,i)=>{const n=a.cloneNode(!0);n.setAttribute("res-index",i);for(let t in s){let a=null,i=n.querySelector(`[res-prop="${t}"]`);i||(i=n.querySelector('[res-prop=""]'),a=s),i&&(i.hasAttribute("data-resonant-bound")?"INPUT"===i.tagName||"TEXTAREA"===i.tagName?"checkbox"===i.type?i.checked=a??s[t]:i.value=a??s[t]:i.innerHTML=a??s[t]:("INPUT"===i.tagName||"TEXTAREA"===i.tagName?"checkbox"===i.type?(i.checked=a??s[t],i.onchange=()=>{s[t]=i.checked,this._queueUpdate(e,"modified",s,t,a??s[t])}):(i.value=a??s[t],i.oninput=()=>{s[t]=i.value,this._queueUpdate(e,"modified",s,t,a??s[t])}):i.innerHTML=a??s[t],i.setAttribute("data-resonant-bound","true")))}n.querySelectorAll("[res-onclick], [res-onclick-remove]").forEach((t=>{const a=t.getAttribute("res-onclick"),i=t.getAttribute("res-onclick-remove");a&&(t.onclick=()=>{new Function("item",`return ${a}(item)`)(s)}),i&&(t.onclick=()=>{const t=this.data[e].findIndex((e=>e[i]===s[i]));-1!==t&&this.data[e].splice(t,1)})})),n.setAttribute("res-rendered",!0),t.appendChild(n)}))}addCallback(e,t){this.callbacks[e]||(this.callbacks[e]=[]),this.callbacks[e].push(t)}}
|