@adaas/are-html 0.0.2 → 0.0.4

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.
Files changed (200) hide show
  1. package/README.md +4 -4
  2. package/dist/browser/index.d.mts +88 -5
  3. package/dist/browser/index.mjs +542 -176
  4. package/dist/browser/index.mjs.map +1 -1
  5. package/dist/node/attributes/AreBinding.attribute.js +17 -4
  6. package/dist/node/attributes/AreBinding.attribute.js.map +1 -1
  7. package/dist/node/attributes/AreBinding.attribute.mjs +10 -3
  8. package/dist/node/attributes/AreBinding.attribute.mjs.map +1 -1
  9. package/dist/node/attributes/AreDirective.attribute.js +17 -4
  10. package/dist/node/attributes/AreDirective.attribute.js.map +1 -1
  11. package/dist/node/attributes/AreDirective.attribute.mjs +10 -3
  12. package/dist/node/attributes/AreDirective.attribute.mjs.map +1 -1
  13. package/dist/node/attributes/AreEvent.attribute.js +17 -4
  14. package/dist/node/attributes/AreEvent.attribute.js.map +1 -1
  15. package/dist/node/attributes/AreEvent.attribute.mjs +10 -3
  16. package/dist/node/attributes/AreEvent.attribute.mjs.map +1 -1
  17. package/dist/node/attributes/AreStatic.attribute.js +17 -4
  18. package/dist/node/attributes/AreStatic.attribute.js.map +1 -1
  19. package/dist/node/attributes/AreStatic.attribute.mjs +10 -3
  20. package/dist/node/attributes/AreStatic.attribute.mjs.map +1 -1
  21. package/dist/node/directives/AreDirectiveFor.directive.d.mts +8 -0
  22. package/dist/node/directives/AreDirectiveFor.directive.d.ts +8 -0
  23. package/dist/node/directives/AreDirectiveFor.directive.js +78 -33
  24. package/dist/node/directives/AreDirectiveFor.directive.js.map +1 -1
  25. package/dist/node/directives/AreDirectiveFor.directive.mjs +78 -33
  26. package/dist/node/directives/AreDirectiveFor.directive.mjs.map +1 -1
  27. package/dist/node/directives/AreDirectiveIf.directive.d.mts +18 -0
  28. package/dist/node/directives/AreDirectiveIf.directive.d.ts +18 -0
  29. package/dist/node/directives/AreDirectiveIf.directive.js +10 -3
  30. package/dist/node/directives/AreDirectiveIf.directive.js.map +1 -1
  31. package/dist/node/directives/AreDirectiveIf.directive.mjs +10 -3
  32. package/dist/node/directives/AreDirectiveIf.directive.mjs.map +1 -1
  33. package/dist/node/engine/AreHTML.compiler.d.mts +2 -2
  34. package/dist/node/engine/AreHTML.compiler.d.ts +2 -2
  35. package/dist/node/engine/AreHTML.compiler.js +57 -29
  36. package/dist/node/engine/AreHTML.compiler.js.map +1 -1
  37. package/dist/node/engine/AreHTML.compiler.mjs +58 -30
  38. package/dist/node/engine/AreHTML.compiler.mjs.map +1 -1
  39. package/dist/node/engine/AreHTML.constants.d.mts +53 -1
  40. package/dist/node/engine/AreHTML.constants.d.ts +53 -1
  41. package/dist/node/engine/AreHTML.constants.js +100 -0
  42. package/dist/node/engine/AreHTML.constants.js.map +1 -1
  43. package/dist/node/engine/AreHTML.constants.mjs +93 -0
  44. package/dist/node/engine/AreHTML.constants.mjs.map +1 -1
  45. package/dist/node/engine/AreHTML.context.d.mts +6 -2
  46. package/dist/node/engine/AreHTML.context.d.ts +6 -2
  47. package/dist/node/engine/AreHTML.context.js +42 -7
  48. package/dist/node/engine/AreHTML.context.js.map +1 -1
  49. package/dist/node/engine/AreHTML.context.mjs +35 -6
  50. package/dist/node/engine/AreHTML.context.mjs.map +1 -1
  51. package/dist/node/engine/AreHTML.engine.js +10 -7
  52. package/dist/node/engine/AreHTML.engine.js.map +1 -1
  53. package/dist/node/engine/AreHTML.engine.mjs +10 -7
  54. package/dist/node/engine/AreHTML.engine.mjs.map +1 -1
  55. package/dist/node/engine/AreHTML.interpreter.js +155 -43
  56. package/dist/node/engine/AreHTML.interpreter.js.map +1 -1
  57. package/dist/node/engine/AreHTML.interpreter.mjs +155 -43
  58. package/dist/node/engine/AreHTML.interpreter.mjs.map +1 -1
  59. package/dist/node/engine/AreHTML.lifecycle.js +17 -12
  60. package/dist/node/engine/AreHTML.lifecycle.js.map +1 -1
  61. package/dist/node/engine/AreHTML.lifecycle.mjs +9 -2
  62. package/dist/node/engine/AreHTML.lifecycle.mjs.map +1 -1
  63. package/dist/node/engine/AreHTML.tokenizer.js +14 -9
  64. package/dist/node/engine/AreHTML.tokenizer.js.map +1 -1
  65. package/dist/node/engine/AreHTML.tokenizer.mjs +10 -3
  66. package/dist/node/engine/AreHTML.tokenizer.mjs.map +1 -1
  67. package/dist/node/engine/AreHTML.transformer.js +13 -8
  68. package/dist/node/engine/AreHTML.transformer.js.map +1 -1
  69. package/dist/node/engine/AreHTML.transformer.mjs +9 -2
  70. package/dist/node/engine/AreHTML.transformer.mjs.map +1 -1
  71. package/dist/node/index.d.mts +2 -1
  72. package/dist/node/index.d.ts +2 -1
  73. package/dist/node/index.js +3 -3
  74. package/dist/node/index.mjs +1 -1
  75. package/dist/node/instructions/AddAttribute.instruction.js +3 -4
  76. package/dist/node/instructions/AddAttribute.instruction.js.map +1 -1
  77. package/dist/node/instructions/AddAttribute.instruction.mjs +3 -4
  78. package/dist/node/instructions/AddAttribute.instruction.mjs.map +1 -1
  79. package/dist/node/instructions/AddComment.instruction.js +3 -4
  80. package/dist/node/instructions/AddComment.instruction.js.map +1 -1
  81. package/dist/node/instructions/AddComment.instruction.mjs +3 -4
  82. package/dist/node/instructions/AddComment.instruction.mjs.map +1 -1
  83. package/dist/node/instructions/AddElement.instruction.js +3 -4
  84. package/dist/node/instructions/AddElement.instruction.js.map +1 -1
  85. package/dist/node/instructions/AddElement.instruction.mjs +3 -4
  86. package/dist/node/instructions/AddElement.instruction.mjs.map +1 -1
  87. package/dist/node/instructions/AddInterpolation.instruction.js +3 -4
  88. package/dist/node/instructions/AddInterpolation.instruction.js.map +1 -1
  89. package/dist/node/instructions/AddInterpolation.instruction.mjs +3 -4
  90. package/dist/node/instructions/AddInterpolation.instruction.mjs.map +1 -1
  91. package/dist/node/instructions/AddListener.instruction.js +3 -4
  92. package/dist/node/instructions/AddListener.instruction.js.map +1 -1
  93. package/dist/node/instructions/AddListener.instruction.mjs +3 -4
  94. package/dist/node/instructions/AddListener.instruction.mjs.map +1 -1
  95. package/dist/node/instructions/AddStyle.instruction.js +3 -4
  96. package/dist/node/instructions/AddStyle.instruction.js.map +1 -1
  97. package/dist/node/instructions/AddStyle.instruction.mjs +3 -4
  98. package/dist/node/instructions/AddStyle.instruction.mjs.map +1 -1
  99. package/dist/node/instructions/AddText.instruction.js +3 -4
  100. package/dist/node/instructions/AddText.instruction.js.map +1 -1
  101. package/dist/node/instructions/AddText.instruction.mjs +3 -4
  102. package/dist/node/instructions/AddText.instruction.mjs.map +1 -1
  103. package/dist/node/lib/AreDirective/AreDirective.component.js +5 -0
  104. package/dist/node/lib/AreDirective/AreDirective.component.js.map +1 -1
  105. package/dist/node/lib/AreDirective/AreDirective.component.mjs +5 -0
  106. package/dist/node/lib/AreDirective/AreDirective.component.mjs.map +1 -1
  107. package/dist/node/lib/AreHTMLAttribute/AreHTML.attribute.js +17 -4
  108. package/dist/node/lib/AreHTMLAttribute/AreHTML.attribute.js.map +1 -1
  109. package/dist/node/lib/AreHTMLAttribute/AreHTML.attribute.mjs +10 -3
  110. package/dist/node/lib/AreHTMLAttribute/AreHTML.attribute.mjs.map +1 -1
  111. package/dist/node/lib/AreHTMLNode/AreHTMLNode.js +3 -4
  112. package/dist/node/lib/AreHTMLNode/AreHTMLNode.js.map +1 -1
  113. package/dist/node/lib/AreHTMLNode/AreHTMLNode.mjs +3 -4
  114. package/dist/node/lib/AreHTMLNode/AreHTMLNode.mjs.map +1 -1
  115. package/dist/node/lib/AreRoot/AreRoot.component.js +3 -4
  116. package/dist/node/lib/AreRoot/AreRoot.component.js.map +1 -1
  117. package/dist/node/lib/AreRoot/AreRoot.component.mjs +3 -4
  118. package/dist/node/lib/AreRoot/AreRoot.component.mjs.map +1 -1
  119. package/dist/node/lib/{AreWatcher/AreWatcher.component.d.mts → AreRouteWatcher/AreRouteWatcher.component.d.mts} +2 -2
  120. package/dist/node/lib/{AreWatcher/AreWatcher.component.d.ts → AreRouteWatcher/AreRouteWatcher.component.d.ts} +2 -2
  121. package/dist/node/lib/{AreWatcher/AreWatcher.component.js → AreRouteWatcher/AreRouteWatcher.component.js} +9 -10
  122. package/dist/node/lib/AreRouteWatcher/AreRouteWatcher.component.js.map +1 -0
  123. package/dist/node/lib/{AreWatcher/AreWatcher.component.mjs → AreRouteWatcher/AreRouteWatcher.component.mjs} +10 -11
  124. package/dist/node/lib/AreRouteWatcher/AreRouteWatcher.component.mjs.map +1 -0
  125. package/dist/node/lib/AreStyle/AreStyle.context.js +17 -4
  126. package/dist/node/lib/AreStyle/AreStyle.context.js.map +1 -1
  127. package/dist/node/lib/AreStyle/AreStyle.context.mjs +10 -3
  128. package/dist/node/lib/AreStyle/AreStyle.context.mjs.map +1 -1
  129. package/dist/node/nodes/AreComment.js +17 -4
  130. package/dist/node/nodes/AreComment.js.map +1 -1
  131. package/dist/node/nodes/AreComment.mjs +10 -3
  132. package/dist/node/nodes/AreComment.mjs.map +1 -1
  133. package/dist/node/nodes/AreComponent.js +3 -4
  134. package/dist/node/nodes/AreComponent.js.map +1 -1
  135. package/dist/node/nodes/AreComponent.mjs +3 -4
  136. package/dist/node/nodes/AreComponent.mjs.map +1 -1
  137. package/dist/node/nodes/AreInterpolation.js +17 -4
  138. package/dist/node/nodes/AreInterpolation.js.map +1 -1
  139. package/dist/node/nodes/AreInterpolation.mjs +10 -3
  140. package/dist/node/nodes/AreInterpolation.mjs.map +1 -1
  141. package/dist/node/nodes/AreRoot.js +3 -4
  142. package/dist/node/nodes/AreRoot.js.map +1 -1
  143. package/dist/node/nodes/AreRoot.mjs +3 -4
  144. package/dist/node/nodes/AreRoot.mjs.map +1 -1
  145. package/dist/node/nodes/AreText.js +17 -4
  146. package/dist/node/nodes/AreText.js.map +1 -1
  147. package/dist/node/nodes/AreText.mjs +10 -3
  148. package/dist/node/nodes/AreText.mjs.map +1 -1
  149. package/dist/node/signals/AreRoute.signal.js +18 -5
  150. package/dist/node/signals/AreRoute.signal.js.map +1 -1
  151. package/dist/node/signals/AreRoute.signal.mjs +10 -3
  152. package/dist/node/signals/AreRoute.signal.mjs.map +1 -1
  153. package/docs/SYNTAX.md +714 -0
  154. package/docs/arehtml.monaco.json +235 -0
  155. package/docs/arehtml.monaco.ts +119 -0
  156. package/examples/dashboard/dist/index.html +1 -1
  157. package/examples/dashboard/dist/mpioi5ab-8c3oa9.js +13674 -0
  158. package/examples/jumpstart/dist/index.html +1 -1
  159. package/examples/{dashboard/dist/mnzfypsd-6zjt7w.js → jumpstart/dist/mor90p6y-0plg7g.js} +1869 -1926
  160. package/examples/jumpstart/dist/{mnpl1g4i-nobz9g.js → mor90p7p-1898bz.js} +2797 -2282
  161. package/examples/jumpstart/src/components/List.component.ts +14 -13
  162. package/examples/jumpstart/src/concept.ts +5 -4
  163. package/jest.config.ts +1 -1
  164. package/package.json +10 -6
  165. package/src/attributes/AreBinding.attribute.ts +5 -0
  166. package/src/attributes/AreDirective.attribute.ts +5 -0
  167. package/src/attributes/AreEvent.attribute.ts +5 -0
  168. package/src/attributes/AreStatic.attribute.ts +5 -0
  169. package/src/directives/AreDirectiveFor.directive.ts +97 -60
  170. package/src/directives/AreDirectiveIf.directive.ts +37 -15
  171. package/src/engine/AreHTML.compiler.ts +64 -36
  172. package/src/engine/AreHTML.constants.ts +144 -0
  173. package/src/engine/AreHTML.context.ts +33 -4
  174. package/src/engine/AreHTML.engine.ts +12 -7
  175. package/src/engine/AreHTML.interpreter.ts +195 -68
  176. package/src/engine/AreHTML.lifecycle.ts +5 -0
  177. package/src/engine/AreHTML.tokenizer.ts +6 -1
  178. package/src/engine/AreHTML.transformer.ts +5 -0
  179. package/src/index.ts +2 -2
  180. package/src/instructions/AddAttribute.instruction.ts +3 -4
  181. package/src/instructions/AddComment.instruction.ts +3 -4
  182. package/src/instructions/AddElement.instruction.ts +3 -4
  183. package/src/instructions/AddInterpolation.instruction.ts +3 -4
  184. package/src/instructions/AddListener.instruction.ts +3 -4
  185. package/src/instructions/AddStyle.instruction.ts +3 -4
  186. package/src/instructions/AddText.instruction.ts +3 -4
  187. package/src/lib/AreDirective/AreDirective.component.ts +5 -0
  188. package/src/lib/AreHTMLAttribute/AreHTML.attribute.ts +5 -0
  189. package/src/lib/AreHTMLNode/AreHTMLNode.ts +3 -4
  190. package/src/lib/AreRoot/AreRoot.component.ts +3 -4
  191. package/src/lib/{AreWatcher/AreWatcher.component.ts → AreRouteWatcher/AreRouteWatcher.component.ts} +5 -6
  192. package/src/lib/AreStyle/AreStyle.context.ts +5 -0
  193. package/src/nodes/AreComment.ts +5 -0
  194. package/src/nodes/AreComponent.ts +3 -4
  195. package/src/nodes/AreInterpolation.ts +5 -0
  196. package/src/nodes/AreRoot.ts +3 -4
  197. package/src/nodes/AreText.ts +5 -0
  198. package/src/signals/AreRoute.signal.ts +5 -0
  199. package/dist/node/lib/AreWatcher/AreWatcher.component.js.map +0 -1
  200. package/dist/node/lib/AreWatcher/AreWatcher.component.mjs.map +0 -1
@@ -5,6 +5,7 @@ import { AreStore, AreScene, AreCompilerError } from '@adaas/are';
5
5
  import { AreDirective } from '@adaas/are-html/directive/AreDirective.component';
6
6
  import { AddCommentInstruction } from '@adaas/are-html/instructions/AddComment.instruction';
7
7
  import { AreDirectiveContext } from '@adaas/are-html/directive/AreDirective.context';
8
+ import { A_Frame } from '@adaas/a-frame/core';
8
9
 
9
10
  let AreDirectiveFor = class extends AreDirective {
10
11
  transform(attribute, scope, store, scene, logger, ...args) {
@@ -21,11 +22,9 @@ let AreDirectiveFor = class extends AreDirective {
21
22
  const { key, index, arrayExpr } = this.parseExpression(attribute.content);
22
23
  const array = this.resolveArray(store, arrayExpr, attribute.content);
23
24
  attribute.value = array;
24
- console.log('Initial array for "for" directive:', scene);
25
25
  for (let i = 0; i < array.length; i++) {
26
26
  this.spawnItemNode(attribute.template, attribute.owner, key, index, array[i], i);
27
27
  }
28
- console.log('Template for "for" directive:', forTemplate);
29
28
  }
30
29
  compile(attribute, store, scene, ...args) {
31
30
  const hostInstruction = scene.host;
@@ -36,49 +35,78 @@ let AreDirectiveFor = class extends AreDirective {
36
35
  scene.unPlan(hostInstruction);
37
36
  }
38
37
  update(attribute, store, scene, ...args) {
39
- const { key, index, arrayExpr } = this.parseExpression(attribute.content);
38
+ const { key, index, arrayExpr, trackExpr } = this.parseExpression(attribute.content);
40
39
  const newArray = this.resolveArray(store, arrayExpr, attribute.content);
41
40
  const owner = attribute.owner;
42
41
  const currentChildren = [...owner.children];
43
42
  attribute.value = newArray;
44
- const newLen = newArray.length;
45
- const newItemSet = new Set(newArray);
46
- const keptChildren = [];
47
- const removedChildren = [];
48
- for (const child of currentChildren) {
43
+ const computeKey = this.makeKeyFn(key, index, trackExpr);
44
+ const childByKey = /* @__PURE__ */ new Map();
45
+ const remaining = /* @__PURE__ */ new Set();
46
+ for (let i = 0; i < currentChildren.length; i++) {
47
+ const child = currentChildren[i];
49
48
  const ctx = child.scope.resolveFlat(AreDirectiveContext);
50
- if (ctx && newItemSet.has(ctx.scope[key])) {
51
- keptChildren.push(child);
49
+ const k = ctx ? computeKey(ctx.scope[key], ctx.scope[index || "index"]) : /* @__PURE__ */ Symbol("orphan");
50
+ childByKey.set(k, child);
51
+ remaining.add(child);
52
+ }
53
+ const newOnes = [];
54
+ for (let i = 0; i < newArray.length; i++) {
55
+ const item = newArray[i];
56
+ const k = computeKey(item, i);
57
+ const existing = childByKey.get(k);
58
+ if (existing) {
59
+ remaining.delete(existing);
60
+ let directiveContext = existing.scope.resolveFlat(AreDirectiveContext);
61
+ if (!directiveContext) {
62
+ directiveContext = new AreDirectiveContext(existing.aseid);
63
+ existing.scope.register(directiveContext);
64
+ }
65
+ directiveContext.scope = {
66
+ ...directiveContext.scope,
67
+ [key]: item,
68
+ [index || "index"]: i
69
+ };
52
70
  } else {
53
- removedChildren.push(child);
71
+ const itemNode = this.spawnItemNode(attribute.template, owner, key, index, item, i);
72
+ newOnes.push(itemNode);
54
73
  }
55
74
  }
56
- for (const child of removedChildren) {
75
+ for (const child of remaining) {
57
76
  child.unmount();
58
77
  owner.removeChild(child);
59
78
  }
60
- for (let i = 0; i < keptChildren.length; i++) {
61
- let directiveContext = keptChildren[i].scope.resolveFlat(AreDirectiveContext);
62
- if (!directiveContext) {
63
- directiveContext = new AreDirectiveContext(keptChildren[i].aseid);
64
- keptChildren[i].scope.register(directiveContext);
65
- }
66
- directiveContext.scope = {
67
- ...directiveContext.scope,
68
- [key]: newArray[i],
69
- [index || "index"]: i
70
- };
71
- }
72
- for (let i = keptChildren.length; i < newLen; i++) {
73
- const itemNode = this.spawnItemNode(attribute.template, owner, key, index, newArray[i], i);
74
- itemNode.transform();
75
- itemNode.compile();
76
- itemNode.mount();
79
+ for (const child of newOnes) {
80
+ child.transform();
81
+ child.compile();
82
+ child.mount();
77
83
  }
78
84
  }
79
85
  // ─────────────────────────────────────────────────────────────────────────────
80
86
  // ── Helpers ──────────────────────────────────────────────────────────────────
81
87
  // ─────────────────────────────────────────────────────────────────────────────
88
+ /**
89
+ * Build a key-function that derives a stable identity from each item.
90
+ * If the user provided a `track <expr>` clause, evaluate it as a path on
91
+ * the item; otherwise fall back to the item identity (reference equality).
92
+ */
93
+ makeKeyFn(key, index, trackExpr) {
94
+ if (!trackExpr) {
95
+ return (item, i) => item ?? i;
96
+ }
97
+ const path = trackExpr.startsWith(key + ".") ? trackExpr.slice(key.length + 1) : trackExpr;
98
+ return (item, i) => {
99
+ if (item == null) return i;
100
+ if (path === key || path === "$index") return path === "$index" ? i : item;
101
+ const parts = path.split(".");
102
+ let v = item;
103
+ for (const p of parts) {
104
+ if (v == null) return i;
105
+ v = v[p];
106
+ }
107
+ return v ?? i;
108
+ };
109
+ }
82
110
  /**
83
111
  * Parses the $for expression string into its constituent parts.
84
112
  *
@@ -88,16 +116,29 @@ let AreDirectiveFor = class extends AreDirective {
88
116
  * (item, index) in items
89
117
  * item in filter(items)
90
118
  * item, index in filter(items, 'active')
119
+ * item in items track item.id
120
+ * (item, i) in items track item.id
91
121
  */
92
122
  parseExpression(content) {
93
- const inIndex = content.lastIndexOf(" in ");
94
- const keyAndIndex = content.slice(0, inIndex).trim().replace(/^\(|\)$/g, "");
95
- const arrayExpr = content.slice(inIndex + 4).trim();
123
+ let trackExpr;
124
+ const trackIdx = content.search(/\s+track\s+/);
125
+ let body = content;
126
+ if (trackIdx !== -1) {
127
+ const m = content.slice(trackIdx).match(/\s+track\s+(.+)$/);
128
+ if (m) {
129
+ trackExpr = m[1].trim();
130
+ body = content.slice(0, trackIdx).trim();
131
+ }
132
+ }
133
+ const inIndex = body.lastIndexOf(" in ");
134
+ const keyAndIndex = body.slice(0, inIndex).trim().replace(/^\(|\)$/g, "");
135
+ const arrayExpr = body.slice(inIndex + 4).trim();
96
136
  const keyParts = keyAndIndex.split(",").map((p) => p.trim());
97
137
  return {
98
138
  key: keyParts[0],
99
139
  index: keyParts[1] || void 0,
100
- arrayExpr
140
+ arrayExpr,
141
+ trackExpr
101
142
  };
102
143
  }
103
144
  /**
@@ -188,6 +229,10 @@ __decorateClass([
188
229
  __decorateParam(2, A_Inject(AreScene))
189
230
  ], AreDirectiveFor.prototype, "update", 1);
190
231
  AreDirectiveFor = __decorateClass([
232
+ A_Frame.Define({
233
+ namespace: "a-are-html",
234
+ description: "Built-in $for directive. Iterates over an array expression resolved from the store and renders a cloned template fragment per item, managing per-item subscopes and comment-node anchors. Supports keyed diffing via an optional track clause to minimise DOM mutations on collection updates."
235
+ }),
191
236
  AreDirective.Priority(1)
192
237
  ], AreDirectiveFor);
193
238
 
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/directives/AreDirectiveFor.directive.ts"],"names":[],"mappings":";;;;;;;;AAkBO,IAAM,eAAA,GAAN,cAA8B,YAAA,CAAa;AAAA,EAI9C,UACwB,SAAA,EACD,KAAA,EACC,KAAA,EACA,KAAA,EACA,WACjB,IAAA,EACL;AAEE,IAAA,MAAA,CAAO,MAAM,CAAA,gCAAA,EAAmC,SAAA,CAAU,MAAM,KAAA,CAAM,QAAA,EAAU,CAAA,CAAA,C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A,CAAA;AAAA,EACjB,4BAAS,QAAQ,CAAA,CAAA;AAAA,EACjB,4BAAS,QAAQ,CAAA;AAAA,CAAA,EA/Eb,eAAA,CA4ET,SAAA,EAAA,SAAA,EAAA,CAAA,CAAA;AAsBA,eAAA,CAAA;AAAA,EADC,YAAA,CAAa,MAAA;AAAA,EAET,4BAAS,QAAQ,CAAA,CAAA;AAAA,EACjB,4BAAS,QAAQ,CAAA,CAAA;AAAA,EACjB,4BAAS,QAAQ,CAAA;AAAA,CAAA,EArGb,eAAA,CAkGT,SAAA,EAAA,QAAA,EAAA,CAAA,CAAA;AAlGS,eAAA,GAAN,eAAA,CAAA;AAAA,EADN,YAAA,CAAa,SAAS,CAAC;AAAA,CAAA,EACX,eAAA,CAAA","file":"AreDirectiveFor.directive.mjs","sourcesContent":["import { A_Caller, A_Inject, A_Scope } from \"@adaas/a-concept\";\nimport { A_Logger } from \"@adaas/a-utils/a-logger\";\nimport { AreDirectiveAttribute } from \"@adaas/are-html/attributes/AreDirective.attribute\";\nimport { AreCompilerError, AreScene, AreStore } from \"@adaas/are\";\nimport { AreDirective } from \"@adaas/are-html/directive/AreDirective.component\";\nimport { AddCommentInstruction } from \"@adaas/are-html/instructions/AddComment.instruction\";\nimport { AreHTMLNode } from \"@adaas/are-html/node\";\nimport { AreDirectiveContext } from \"@adaas/are-html/directive/AreDirective.context\";\n\n\ntype AreForExpression = {\n key: string;\n index: string | undefined;\n arrayExpr: string;\n};\n\n\n@AreDirective.Priority(1)\nexport class AreDirectiveFor extends AreDirective {\n\n\n @AreDirective.Transform\n transform(\n @A_Inject(A_Caller) attribute: AreDirectiveAttribute,\n @A_Inject(A_Scope) scope: A_Scope,\n @A_Inject(AreStore) store: AreStore,\n @A_Inject(AreScene) scene: AreScene,\n @A_Inject(A_Logger) logger: A_Logger,\n ...args: any[]\n ) {\n\n logger.debug(`[Transform] directive $FOR for <${attribute.owner.aseid.toString()}>`)\n\n const node = attribute.owner;\n\n /**\n * Transfer the original node's scope (with all registered attributes and children)\n * to the template clone, and give the owner node a fresh empty scope.\n * This mirrors the $if directive's approach, making the owner a lightweight\n * group container whose sole visible presence is a comment placeholder.\n */\n const forTemplate = node.cloneWithScope();\n\n /**\n * Remove the $for attribute from the template so iterative clones do not\n * re-trigger this directive during their own transform phase.\n * Re-register it on the owner so the reactive compile/update pipeline keeps working.\n */\n const forAttr = forTemplate.attributes.find(d => d.name === attribute.name);\n\n if (forAttr) {\n forTemplate.scope.deregister(forAttr);\n node.scope.register(forAttr);\n }\n\n /**\n * Re-initialize the owner node with its fresh scope so it becomes a valid\n * group container that will own the generated item nodes as children.\n */\n node.init();\n\n /**\n * Store the template for use in compile and update.\n */\n attribute.template = forTemplate;\n\n\n /**\n * Parse the $for expression and evaluate the source array.\n */\n const { key, index, arrayExpr } = this.parseExpression(attribute.content);\n const array = this.resolveArray(store, arrayExpr, attribute.content);\n\n attribute.value = array;\n\n console.log('Initial array for \"for\" directive:', scene);\n\n /**\n * For each item in the array, spawn a clone of the template with the\n * item's store values pre-set and its scene activated.\n *\n * The children are added to the owner node before the main compiler's\n * children iteration loop runs, so the main cycle will compile them —\n * no explicit child.compile() call is needed here.\n */\n for (let i = 0; i < array.length; i++) {\n this.spawnItemNode(attribute.template!, attribute.owner, key, index, array[i], i);\n }\n\n console.log('Template for \"for\" directive:', forTemplate);\n }\n\n\n @AreDirective.Compile\n compile(\n @A_Inject(A_Caller) attribute: AreDirectiveAttribute,\n @A_Inject(AreStore) store: AreStore,\n @A_Inject(AreScene) scene: AreScene,\n ...args: any[]\n ): void {\n /**\n * Replace the group node's default host declaration with a comment placeholder\n * so the owner element itself does not render as a DOM element — the item nodes\n * render as its children instead.\n */\n const hostInstruction = scene.host!;\n const commentIdentifier = ` --- for: ${attribute.template!.id} --- `;\n const declaration = new AddCommentInstruction({ content: commentIdentifier });\n\n scene.setHost(declaration);\n scene.planBefore(declaration, hostInstruction);\n scene.unPlan(hostInstruction);\n }\n\n\n @AreDirective.Update\n update(\n @A_Inject(A_Caller) attribute: AreDirectiveAttribute,\n @A_Inject(AreStore) store: AreStore,\n @A_Inject(AreScene) scene: AreScene,\n ...args: any[]\n ): void {\n /**\n * Re-evaluate the source array.\n */\n const { key, index, arrayExpr } = this.parseExpression(attribute.content);\n const newArray = this.resolveArray(store, arrayExpr, attribute.content);\n\n const owner = attribute.owner;\n const currentChildren = [...owner.children] as AreHTMLNode[];\n\n attribute.value = newArray;\n\n const newLen = newArray.length;\n\n /**\n * Build a set of new items for O(1) identity lookup.\n * Uses reference equality — items that exist in newArray are \"kept\".\n */\n const newItemSet = new Set(newArray);\n\n /**\n * Partition existing children by identity: nodes whose stored item is\n * still present in the new array are kept; the rest are removed.\n * This correctly handles deletions from any position, not just the tail.\n */\n const keptChildren: AreHTMLNode[] = [];\n const removedChildren: AreHTMLNode[] = [];\n\n for (const child of currentChildren) {\n const ctx = child.scope.resolveFlat(AreDirectiveContext);\n\n if (ctx && newItemSet.has(ctx.scope[key])) {\n keptChildren.push(child);\n } else {\n removedChildren.push(child);\n }\n }\n\n /**\n * Unmount and detach nodes whose items are no longer in the array.\n */\n for (const child of removedChildren) {\n child.unmount();\n owner.removeChild(child);\n }\n\n /**\n * Update store values on kept nodes so their reactive bindings pick up\n * the latest data and corrected indices without a full re-render.\n */\n for (let i = 0; i < keptChildren.length; i++) {\n let directiveContext = keptChildren[i].scope.resolveFlat(AreDirectiveContext);\n\n if (!directiveContext) {\n directiveContext = new AreDirectiveContext(keptChildren[i].aseid);\n keptChildren[i].scope.register(directiveContext);\n }\n\n directiveContext.scope = {\n ...directiveContext.scope,\n [key]: newArray[i],\n [index || 'index']: i,\n };\n }\n\n /**\n * Append nodes for items added beyond the current kept count.\n * These are created outside the main compile cycle so they need an\n * explicit compile + mount to appear in the DOM.\n */\n for (let i = keptChildren.length; i < newLen; i++) {\n const itemNode = this.spawnItemNode(attribute.template!, owner, key, index, newArray[i], i);\n\n itemNode.transform();\n itemNode.compile();\n itemNode.mount();\n }\n }\n\n\n // ─────────────────────────────────────────────────────────────────────────────\n // ── Helpers ──────────────────────────────────────────────────────────────────\n // ─────────────────────────────────────────────────────────────────────────────\n\n /**\n * Parses the $for expression string into its constituent parts.\n *\n * Supported formats:\n * item in items\n * item, index in items\n * (item, index) in items\n * item in filter(items)\n * item, index in filter(items, 'active')\n */\n private parseExpression(content: string): AreForExpression {\n const inIndex = content.lastIndexOf(' in ');\n const keyAndIndex = content.slice(0, inIndex).trim().replace(/^\\(|\\)$/g, '');\n const arrayExpr = content.slice(inIndex + 4).trim();\n const keyParts = keyAndIndex.split(',').map(p => p.trim());\n\n return {\n key: keyParts[0],\n index: keyParts[1] || undefined,\n arrayExpr,\n };\n }\n\n /**\n * Resolves the array expression against the store.\n * Supports both plain key lookups and function-call expressions:\n * items → store.get('items')\n * filter(items) → store.get('filter')(store.get('items'))\n */\n private resolveArray(store: AreStore, arrayExpr: string, fullContent: string): any[] {\n let result: any;\n const callMatch = arrayExpr.match(/^([^(]+)\\((.+)\\)$/);\n\n if (callMatch) {\n const fnName = callMatch[1].trim();\n const fn = store.get(fnName as any);\n\n if (typeof fn !== 'function')\n throw new AreCompilerError({\n title: 'Invalid \"for\" Directive Function',\n description: `The expression \"${fnName}\" in the \"for\" directive does not resolve to a function in the store. Received: ${typeof fn}`,\n });\n\n const rawArgs = callMatch[2].split(',').map(a => a.trim());\n const resolvedArgs = rawArgs.map(arg => {\n if (arg.startsWith(\"'\") && arg.endsWith(\"'\")) return arg.slice(1, -1);\n if (arg.startsWith('\"') && arg.endsWith('\"')) return arg.slice(1, -1);\n if (!isNaN(Number(arg))) return Number(arg);\n return store.get(arg as any);\n });\n\n result = (fn as Function)(...resolvedArgs);\n } else {\n result = store.get(arrayExpr as any);\n }\n\n if (!Array.isArray(result))\n throw new AreCompilerError({\n title: 'Invalid \"for\" Directive Value',\n description: `The \"for\" directive expects an array but got ${typeof result}. Expression: \"${fullContent}\". Received: ${JSON.stringify(result)}`,\n });\n\n return result;\n }\n\n /**\n * Creates a single item node from the template, registers it as a child of\n * the owner, initialises it, injects item-scoped store values, and activates\n * its scene so the mount/compile cycle will include it.\n *\n * NOTE: This method does NOT call compile() or mount() — the caller is\n * responsible for doing so when the main lifecycle cycle won't cover it\n * (i.e. during update, but not during the initial compile phase).\n */\n private spawnItemNode(\n template: AreHTMLNode,\n owner: AreHTMLNode,\n key: string,\n index: string | undefined,\n item: any,\n i: number,\n ): AreHTMLNode {\n const itemNode = template.clone() as AreHTMLNode;\n\n owner.addChild(itemNode);\n\n const queue = [itemNode];\n\n while (queue.length > 0) {\n const current = queue.shift()!\n\n current.init();\n\n queue.push(...current.children as AreHTMLNode[]);\n }\n\n /**\n * Resolve or create a directive context for the item node. This is needed to hold the item-specific store values (e.g. the \"item\" and \"index\" in a \"for\" loop) that the template's bindings will reference during compile and update. The context is shared among all clones of the same template, but that's fine because each clone gets its own scope values assigned here.\n */\n let directiveContext = itemNode.scope.resolveFlat(AreDirectiveContext);\n\n if (!directiveContext) {\n directiveContext = new AreDirectiveContext(itemNode.aseid);\n itemNode.scope.register(directiveContext);\n }\n\n directiveContext.scope = {\n ...directiveContext.scope,\n [key]: item,\n [index || 'index']: i,\n }\n\n itemNode.scene.activate();\n\n return itemNode;\n }\n}"]}
1
+ {"version":3,"sources":["../../../src/directives/AreDirectiveFor.directive.ts"],"names":[],"mappings":";;;;;;;;;AAyBO,IAAM,eAAA,GAAN,cAA8B,YAAA,CAAa;AAAA,EAI9C,UACwB,SAAA,EACD,KAAA,EACC,KAAA,EACA,KAAA,EACA,WACjB,IAAA,EACL;AAEE,IAAA,MAAA,CAAO,MAAM,CAAA,gCAAA,EAAmC,SAAA,CAAU,MAAM,KAAA,CAAM,QAAA,EAAU,CAAA,CAAA,CAAG,CAAA;AAEnF,IAAA,MAAM,OAAO,SAAA,CAAU,KAAA;AAQvB,IAAA,MAAM,WAAA,GAAc,KAAK,cAAA,EAAe;AAOxC,IAAA,MAAM,OAAA,GAAU,YAAY,UAAA,CAAW,IAAA,CAAK,OAAK,C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AAQ,CAAA,CAAA;AAAA,EACjB,4BAAS,QAAQ,CAAA,CAAA;AAAA,EACjB,4BAAS,QAAQ,CAAA;AAAA,CAAA,EATb,eAAA,CAIT,SAAA,EAAA,WAAA,EAAA,CAAA,CAAA;AAoEA,eAAA,CAAA;AAAA,EADC,YAAA,CAAa,OAAA;AAAA,EAET,4BAAS,QAAQ,CAAA,CAAA;AAAA,EACjB,4BAAS,QAAQ,CAAA,CAAA;AAAA,EACjB,4BAAS,QAAQ,CAAA;AAAA,CAAA,EA3Eb,eAAA,CAwET,SAAA,EAAA,SAAA,EAAA,CAAA,CAAA;AAsBA,eAAA,CAAA;AAAA,EADC,YAAA,CAAa,MAAA;AAAA,EAET,4BAAS,QAAQ,CAAA,CAAA;AAAA,EACjB,4BAAS,QAAQ,CAAA,CAAA;AAAA,EACjB,4BAAS,QAAQ,CAAA;AAAA,CAAA,EAjGb,eAAA,CA8FT,SAAA,EAAA,QAAA,EAAA,CAAA,CAAA;AA9FS,eAAA,GAAN,eAAA,CAAA;AAAA,EALN,QAAQ,MAAA,CAAO;AAAA,IACZ,SAAA,EAAW,YAAA;AAAA,IACX,WAAA,EAAa;AAAA,GAChB,CAAA;AAAA,EACA,YAAA,CAAa,SAAS,CAAC;AAAA,CAAA,EACX,eAAA,CAAA","file":"AreDirectiveFor.directive.mjs","sourcesContent":["import { A_Caller, A_Inject, A_Scope } from \"@adaas/a-concept\";\nimport { A_Logger } from \"@adaas/a-utils/a-logger\";\nimport { AreDirectiveAttribute } from \"@adaas/are-html/attributes/AreDirective.attribute\";\nimport { AreCompilerError, AreScene, AreStore } from \"@adaas/are\";\nimport { AreDirective } from \"@adaas/are-html/directive/AreDirective.component\";\nimport { AddCommentInstruction } from \"@adaas/are-html/instructions/AddComment.instruction\";\nimport { AreHTMLNode } from \"@adaas/are-html/node\";\nimport { AreDirectiveContext } from \"@adaas/are-html/directive/AreDirective.context\";\nimport { A_Frame } from \"@adaas/a-frame/core\";\n\n\ntype AreForExpression = {\n key: string;\n index: string | undefined;\n arrayExpr: string;\n /** Optional `track <expr>` clause, e.g. `track row.id` */\n trackExpr: string | undefined;\n};\n\n\n@A_Frame.Define({\n namespace: 'a-are-html',\n description: 'Built-in $for directive. Iterates over an array expression resolved from the store and renders a cloned template fragment per item, managing per-item subscopes and comment-node anchors. Supports keyed diffing via an optional track clause to minimise DOM mutations on collection updates.'\n})\n@AreDirective.Priority(1)\nexport class AreDirectiveFor extends AreDirective {\n\n\n @AreDirective.Transform\n transform(\n @A_Inject(A_Caller) attribute: AreDirectiveAttribute,\n @A_Inject(A_Scope) scope: A_Scope,\n @A_Inject(AreStore) store: AreStore,\n @A_Inject(AreScene) scene: AreScene,\n @A_Inject(A_Logger) logger: A_Logger,\n ...args: any[]\n ) {\n\n logger.debug(`[Transform] directive $FOR for <${attribute.owner.aseid.toString()}>`)\n\n const node = attribute.owner;\n\n /**\n * Transfer the original node's scope (with all registered attributes and children)\n * to the template clone, and give the owner node a fresh empty scope.\n * This mirrors the $if directive's approach, making the owner a lightweight\n * group container whose sole visible presence is a comment placeholder.\n */\n const forTemplate = node.cloneWithScope();\n\n /**\n * Remove the $for attribute from the template so iterative clones do not\n * re-trigger this directive during their own transform phase.\n * Re-register it on the owner so the reactive compile/update pipeline keeps working.\n */\n const forAttr = forTemplate.attributes.find(d => d.name === attribute.name);\n\n if (forAttr) {\n forTemplate.scope.deregister(forAttr);\n node.scope.register(forAttr);\n }\n\n /**\n * Re-initialize the owner node with its fresh scope so it becomes a valid\n * group container that will own the generated item nodes as children.\n */\n node.init();\n\n /**\n * Store the template for use in compile and update.\n */\n attribute.template = forTemplate;\n\n\n /**\n * Parse the $for expression and evaluate the source array.\n */\n const { key, index, arrayExpr } = this.parseExpression(attribute.content);\n const array = this.resolveArray(store, arrayExpr, attribute.content);\n\n attribute.value = array;\n\n /**\n * For each item in the array, spawn a clone of the template with the\n * item's store values pre-set and its scene activated.\n *\n * The children are added to the owner node before the main compiler's\n * children iteration loop runs, so the main cycle will compile them —\n * no explicit child.compile() call is needed here.\n */\n for (let i = 0; i < array.length; i++) {\n this.spawnItemNode(attribute.template!, attribute.owner, key, index, array[i], i);\n }\n }\n\n\n @AreDirective.Compile\n compile(\n @A_Inject(A_Caller) attribute: AreDirectiveAttribute,\n @A_Inject(AreStore) store: AreStore,\n @A_Inject(AreScene) scene: AreScene,\n ...args: any[]\n ): void {\n /**\n * Replace the group node's default host declaration with a comment placeholder\n * so the owner element itself does not render as a DOM element — the item nodes\n * render as its children instead.\n */\n const hostInstruction = scene.host!;\n const commentIdentifier = ` --- for: ${attribute.template!.id} --- `;\n const declaration = new AddCommentInstruction({ content: commentIdentifier });\n\n scene.setHost(declaration);\n scene.planBefore(declaration, hostInstruction);\n scene.unPlan(hostInstruction);\n }\n\n\n @AreDirective.Update\n update(\n @A_Inject(A_Caller) attribute: AreDirectiveAttribute,\n @A_Inject(AreStore) store: AreStore,\n @A_Inject(AreScene) scene: AreScene,\n ...args: any[]\n ): void {\n /**\n * Re-evaluate the source array.\n */\n const { key, index, arrayExpr, trackExpr } = this.parseExpression(attribute.content);\n const newArray = this.resolveArray(store, arrayExpr, attribute.content);\n\n const owner = attribute.owner;\n const currentChildren = [...owner.children] as AreHTMLNode[];\n\n attribute.value = newArray;\n\n const computeKey = this.makeKeyFn(key, index, trackExpr);\n\n // ── 1. Index existing children by stable key ────────────────────────\n const childByKey = new Map<any, AreHTMLNode>();\n const remaining = new Set<AreHTMLNode>();\n\n for (let i = 0; i < currentChildren.length; i++) {\n const child = currentChildren[i];\n const ctx = child.scope.resolveFlat(AreDirectiveContext);\n const k = ctx ? computeKey(ctx.scope[key], ctx.scope[index || 'index']) : Symbol('orphan');\n childByKey.set(k, child);\n remaining.add(child);\n }\n\n // ── 2. Walk desired list; reuse existing or spawn new ───────────────\n const desired: AreHTMLNode[] = [];\n const newOnes: AreHTMLNode[] = [];\n\n for (let i = 0; i < newArray.length; i++) {\n const item = newArray[i];\n const k = computeKey(item, i);\n const existing = childByKey.get(k);\n\n if (existing) {\n remaining.delete(existing);\n\n let directiveContext = existing.scope.resolveFlat(AreDirectiveContext);\n if (!directiveContext) {\n directiveContext = new AreDirectiveContext(existing.aseid);\n existing.scope.register(directiveContext);\n }\n directiveContext.scope = {\n ...directiveContext.scope,\n [key]: item,\n [index || 'index']: i,\n };\n desired.push(existing);\n } else {\n const itemNode = this.spawnItemNode(attribute.template!, owner, key, index, item, i);\n desired.push(itemNode);\n newOnes.push(itemNode);\n }\n }\n\n // ── 3. Unmount + detach removed children ─────────────────────────────\n for (const child of remaining) {\n child.unmount();\n owner.removeChild(child);\n }\n\n // ── 4. Mount only the new ones (kept children stay where they are). ─\n for (const child of newOnes) {\n child.transform();\n child.compile();\n child.mount();\n }\n }\n\n\n // ─────────────────────────────────────────────────────────────────────────────\n // ── Helpers ──────────────────────────────────────────────────────────────────\n // ─────────────────────────────────────────────────────────────────────────────\n\n /**\n * Build a key-function that derives a stable identity from each item.\n * If the user provided a `track <expr>` clause, evaluate it as a path on\n * the item; otherwise fall back to the item identity (reference equality).\n */\n private makeKeyFn(key: string, index: string | undefined, trackExpr: string | undefined): (item: any, i: number) => any {\n if (!trackExpr) {\n return (item, i) => item ?? i;\n }\n\n // Strip any leading `key.` so users can write `track row.id`.\n const path = trackExpr.startsWith(key + '.') ? trackExpr.slice(key.length + 1) : trackExpr;\n\n return (item, i) => {\n if (item == null) return i;\n if (path === key || path === '$index') return path === '$index' ? i : item;\n\n // dotted path lookup\n const parts = path.split('.');\n let v: any = item;\n for (const p of parts) {\n if (v == null) return i;\n v = v[p];\n }\n return v ?? i;\n };\n }\n\n /**\n * Parses the $for expression string into its constituent parts.\n *\n * Supported formats:\n * item in items\n * item, index in items\n * (item, index) in items\n * item in filter(items)\n * item, index in filter(items, 'active')\n * item in items track item.id\n * (item, i) in items track item.id\n */\n private parseExpression(content: string): AreForExpression {\n // Strip optional `track <expr>` suffix first.\n let trackExpr: string | undefined;\n const trackIdx = content.search(/\\s+track\\s+/);\n let body = content;\n if (trackIdx !== -1) {\n const m = content.slice(trackIdx).match(/\\s+track\\s+(.+)$/);\n if (m) {\n trackExpr = m[1].trim();\n body = content.slice(0, trackIdx).trim();\n }\n }\n\n const inIndex = body.lastIndexOf(' in ');\n const keyAndIndex = body.slice(0, inIndex).trim().replace(/^\\(|\\)$/g, '');\n const arrayExpr = body.slice(inIndex + 4).trim();\n const keyParts = keyAndIndex.split(',').map(p => p.trim());\n\n return {\n key: keyParts[0],\n index: keyParts[1] || undefined,\n arrayExpr,\n trackExpr,\n };\n }\n\n /**\n * Resolves the array expression against the store.\n * Supports both plain key lookups and function-call expressions:\n * items → store.get('items')\n * filter(items) → store.get('filter')(store.get('items'))\n */\n private resolveArray(store: AreStore, arrayExpr: string, fullContent: string): any[] {\n let result: any;\n const callMatch = arrayExpr.match(/^([^(]+)\\((.+)\\)$/);\n\n if (callMatch) {\n const fnName = callMatch[1].trim();\n const fn = store.get(fnName as any);\n\n if (typeof fn !== 'function')\n throw new AreCompilerError({\n title: 'Invalid \"for\" Directive Function',\n description: `The expression \"${fnName}\" in the \"for\" directive does not resolve to a function in the store. Received: ${typeof fn}`,\n });\n\n const rawArgs = callMatch[2].split(',').map(a => a.trim());\n const resolvedArgs = rawArgs.map(arg => {\n if (arg.startsWith(\"'\") && arg.endsWith(\"'\")) return arg.slice(1, -1);\n if (arg.startsWith('\"') && arg.endsWith('\"')) return arg.slice(1, -1);\n if (!isNaN(Number(arg))) return Number(arg);\n return store.get(arg as any);\n });\n\n result = (fn as Function)(...resolvedArgs);\n } else {\n result = store.get(arrayExpr as any);\n }\n\n if (!Array.isArray(result))\n throw new AreCompilerError({\n title: 'Invalid \"for\" Directive Value',\n description: `The \"for\" directive expects an array but got ${typeof result}. Expression: \"${fullContent}\". Received: ${JSON.stringify(result)}`,\n });\n\n return result;\n }\n\n /**\n * Creates a single item node from the template, registers it as a child of\n * the owner, initialises it, injects item-scoped store values, and activates\n * its scene so the mount/compile cycle will include it.\n *\n * NOTE: This method does NOT call compile() or mount() — the caller is\n * responsible for doing so when the main lifecycle cycle won't cover it\n * (i.e. during update, but not during the initial compile phase).\n */\n private spawnItemNode(\n template: AreHTMLNode,\n owner: AreHTMLNode,\n key: string,\n index: string | undefined,\n item: any,\n i: number,\n ): AreHTMLNode {\n const itemNode = template.clone() as AreHTMLNode;\n\n owner.addChild(itemNode);\n\n const queue = [itemNode];\n\n while (queue.length > 0) {\n const current = queue.shift()!\n\n current.init();\n\n queue.push(...current.children as AreHTMLNode[]);\n }\n\n /**\n * Resolve or create a directive context for the item node. This is needed to hold the item-specific store values (e.g. the \"item\" and \"index\" in a \"for\" loop) that the template's bindings will reference during compile and update. The context is shared among all clones of the same template, but that's fine because each clone gets its own scope values assigned here.\n */\n let directiveContext = itemNode.scope.resolveFlat(AreDirectiveContext);\n\n if (!directiveContext) {\n directiveContext = new AreDirectiveContext(itemNode.aseid);\n itemNode.scope.register(directiveContext);\n }\n\n directiveContext.scope = {\n ...directiveContext.scope,\n [key]: item,\n [index || 'index']: i,\n }\n\n itemNode.scene.activate();\n\n return itemNode;\n }\n}"]}
@@ -6,6 +6,24 @@ import { AreDirectiveContext } from '../lib/AreDirective/AreDirective.context.mj
6
6
  import '../lib/AreStyle/AreStyle.context.mjs';
7
7
  import '@adaas/a-utils/a-execution';
8
8
 
9
+ /**
10
+ * `$if` directive — conditionally renders a node based on an expression.
11
+ *
12
+ * ⚠️ Known limitation: do NOT use `$if` and `$for` on the SAME element.
13
+ * Doing so produces duplicated DOM on toggle because the two directives
14
+ * share an owner node and clone its scope independently. Wrap one in a
15
+ * parent element instead, e.g.:
16
+ *
17
+ * <div $if="visible">
18
+ * <li $for="item in items">{{item.name}}</li>
19
+ * </div>
20
+ *
21
+ * or
22
+ *
23
+ * <ul $for="item in items">
24
+ * <li $if="item.visible">{{item.name}}</li>
25
+ * </ul>
26
+ */
9
27
  declare class AreDirectiveIf extends AreDirective {
10
28
  transform(attribute: AreDirectiveAttribute, scope: A_Scope, store: AreStore, scene: AreScene, logger: A_Logger, ...args: any[]): void;
11
29
  compile(attribute: AreDirectiveAttribute, store: AreStore, scene: AreScene, syntax: AreSyntax, directiveContext?: AreDirectiveContext, ...args: any[]): void;
@@ -6,6 +6,24 @@ import { AreDirectiveContext } from '../lib/AreDirective/AreDirective.context.js
6
6
  import '../lib/AreStyle/AreStyle.context.js';
7
7
  import '@adaas/a-utils/a-execution';
8
8
 
9
+ /**
10
+ * `$if` directive — conditionally renders a node based on an expression.
11
+ *
12
+ * ⚠️ Known limitation: do NOT use `$if` and `$for` on the SAME element.
13
+ * Doing so produces duplicated DOM on toggle because the two directives
14
+ * share an owner node and clone its scope independently. Wrap one in a
15
+ * parent element instead, e.g.:
16
+ *
17
+ * <div $if="visible">
18
+ * <li $for="item in items">{{item.name}}</li>
19
+ * </div>
20
+ *
21
+ * or
22
+ *
23
+ * <ul $for="item in items">
24
+ * <li $if="item.visible">{{item.name}}</li>
25
+ * </ul>
26
+ */
9
27
  declare class AreDirectiveIf extends AreDirective {
10
28
  transform(attribute: AreDirectiveAttribute, scope: A_Scope, store: AreStore, scene: AreScene, logger: A_Logger, ...args: any[]): void;
11
29
  compile(attribute: AreDirectiveAttribute, store: AreStore, scene: AreScene, syntax: AreSyntax, directiveContext?: AreDirectiveContext, ...args: any[]): void;
@@ -6,6 +6,7 @@ var are = require('@adaas/are');
6
6
  var AreDirective_component = require('@adaas/are-html/directive/AreDirective.component');
7
7
  var AddComment_instruction = require('@adaas/are-html/instructions/AddComment.instruction');
8
8
  var AreDirective_context = require('@adaas/are-html/directive/AreDirective.context');
9
+ var core = require('@adaas/a-frame/core');
9
10
 
10
11
  var __defProp = Object.defineProperty;
11
12
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
@@ -34,7 +35,6 @@ exports.AreDirectiveIf = class AreDirectiveIf extends AreDirective_component.Are
34
35
  attribute.template = ifTemplate;
35
36
  }
36
37
  compile(attribute, store, scene, syntax, directiveContext, ...args) {
37
- console.log('Compiling directive "if" with attribute content:', attribute);
38
38
  attribute.value = syntax.evaluate(attribute.content, store, {
39
39
  ...directiveContext?.scope || {}
40
40
  });
@@ -50,8 +50,11 @@ exports.AreDirectiveIf = class AreDirectiveIf extends AreDirective_component.Are
50
50
  attribute.template.scene.deactivate();
51
51
  }
52
52
  update(attribute, store, scope, syntax, scene, ...args) {
53
- attribute.value = syntax.evaluate(attribute.content, store);
54
- if (attribute.value) {
53
+ const previous = !!attribute.value;
54
+ const next = !!syntax.evaluate(attribute.content, store);
55
+ attribute.value = next;
56
+ if (previous === next) return;
57
+ if (next) {
55
58
  attribute.template.scene.activate();
56
59
  attribute.template.mount();
57
60
  } else {
@@ -85,6 +88,10 @@ __decorateClass([
85
88
  __decorateParam(4, aConcept.A_Inject(are.AreScene))
86
89
  ], exports.AreDirectiveIf.prototype, "update", 1);
87
90
  exports.AreDirectiveIf = __decorateClass([
91
+ core.A_Frame.Define({
92
+ namespace: "a-are-html",
93
+ description: "Built-in $if directive. Conditionally renders a subtree based on a store expression. Replaces the target element with a stable comment anchor when the condition is false and restores the fully rendered subtree when it becomes true, preventing any leaking of the host element between states."
94
+ }),
88
95
  AreDirective_component.AreDirective.Priority(2)
89
96
  ], exports.AreDirectiveIf);
90
97
  //# sourceMappingURL=AreDirectiveIf.directive.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/directives/AreDirectiveIf.directive.ts"],"names":["AreDirectiveIf","AreDirective","AddCommentInstruction","A_Caller","A_Scope","AreStore","AreScene","A_Logger","AreSyntax","AreDirectiveContext"],"mappings":";;;;;;;;;;;;;;;;;;;;AAWaA,sBAAA,GAAN,6BAA6BC,mCAAA,CAAa;AAAA,EAI7C,UACwB,SAAA,EACD,KAAA,EAEC,KAAA,EACA,KAAA,EACA,WACjB,IAAA,EACL;AACE,IAAA,MAAA,CAAO,MAAM,CAAA,+BAAA,EAAkC,SAAA,CAAU,MAAM,KAAA,CAAM,QAAA,EAAU,CAAA,CAAA,CAAG,CAAA;AAElF,IAAA,MAAM,OAAO,SAAA,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qCAASE,iBAAQ,CAAA,CAAA;AAAA,EACjB,qCAASE,YAAQ,CAAA,CAAA;AAAA,EACjB,qCAASD,gBAAO,CAAA,CAAA;AAAA,EAChB,qCAASI,aAAS,CAAA,CAAA;AAAA,EAClB,qCAASF,YAAQ,CAAA;AAAA,CAAA,EAjGbN,sBAAA,CA4FT,SAAA,EAAA,QAAA,EAAA,CAAA,CAAA;AA5FSA,sBAAA,GAAN,eAAA,CAAA;AAAA,EADNC,mCAAA,CAAa,SAAS,CAAC;AAAA,CAAA,EACXD,sBAAA,CAAA","file":"AreDirectiveIf.directive.js","sourcesContent":["import { A_Caller, A_Inject, A_Scope } from \"@adaas/a-concept\";\nimport { A_Logger } from \"@adaas/a-utils/a-logger\";\nimport { AreDirectiveAttribute } from \"@adaas/are-html/attributes/AreDirective.attribute\";\nimport { AreScene, AreStore, AreSyntax } from \"@adaas/are\";\nimport { AreDirective } from \"@adaas/are-html/directive/AreDirective.component\";\nimport { AddCommentInstruction } from \"@adaas/are-html/instructions/AddComment.instruction\";\nimport { AreDirectiveContext } from \"@adaas/are-html/directive/AreDirective.context\";\n\n\n\n@AreDirective.Priority(2)\nexport class AreDirectiveIf extends AreDirective {\n\n\n @AreDirective.Transform\n transform(\n @A_Inject(A_Caller) attribute: AreDirectiveAttribute,\n @A_Inject(A_Scope) scope: A_Scope,\n\n @A_Inject(AreStore) store: AreStore,\n @A_Inject(AreScene) scene: AreScene,\n @A_Inject(A_Logger) logger: A_Logger,\n ...args: any[]\n ) {\n logger.debug(`[Transform] directive $IF for <${attribute.owner.aseid.toString()}>`)\n\n const node = attribute.owner;\n\n /**\n * We have to keep this node as a group node, and copy all data into the child node that would be actual node. \n */\n const ifTemplate = node.cloneWithScope();\n\n const ifAttr = ifTemplate.attributes.find(d => d.name === attribute.name);\n\n if (ifAttr) {\n ifTemplate.scope.deregister(ifAttr);\n node.scope.register(ifAttr);\n }\n\n node.init();\n\n node.addChild(ifTemplate);\n\n /**\n * Resolve or create a directive context for the item node. This is needed to hold the item-specific store values (e.g. the \"item\" and \"index\" in a \"for\" loop) that the template's bindings will reference during compile and update. The context is shared among all clones of the same template, but that's fine because each clone gets its own scope values assigned here.\n */\n // let directiveContext = ifTemplate.scope.resolveFlat(AreDirectiveContext);\n\n // if (!directiveContext) {\n // directiveContext = new AreDirectiveContext(ifTemplate.aseid);\n // ifTemplate.scope.register(directiveContext);\n // }\n\n ifTemplate.scene.deactivate();\n\n attribute.template = ifTemplate;\n\n }\n\n\n @AreDirective.Compile\n compile(\n @A_Inject(A_Caller) attribute: AreDirectiveAttribute,\n @A_Inject(AreStore) store: AreStore,\n @A_Inject(AreScene) scene: AreScene,\n @A_Inject(AreSyntax) syntax: AreSyntax,\n\n @A_Inject(AreDirectiveContext) directiveContext?: AreDirectiveContext,\n ...args: any[]\n ): void {\n\n console.log('Compiling directive \"if\" with attribute content:', attribute);\n\n /**\n * 1. Extract the value from the store based on the attribute content \n * (which is the path to the value in the store)\n */\n attribute.value = syntax.evaluate(attribute.content, store, {\n ...(directiveContext?.scope || {}),\n });\n\n /**\n * 2. If the value is falsy, remove the node from the scene by planning a RemoveElement instruction.\n * If the value is truthy, ensure the node is in the scene by planning an AddElement instruction if it's not already planned.\n */\n const hostInstruction = scene.host!;\n const commentIdentifier = ` --- if: ${attribute.template!.id} --- `;\n const declaration = new AddCommentInstruction({ content: commentIdentifier })\n\n scene.setHost(declaration);\n scene.planBefore(declaration, hostInstruction);\n scene.unPlan(hostInstruction);\n\n if (attribute.value)\n attribute.template!.scene.activate();\n else\n attribute.template!.scene.deactivate();\n }\n\n\n\n @AreDirective.Update\n update(\n @A_Inject(A_Caller) attribute: AreDirectiveAttribute,\n @A_Inject(AreStore) store: AreStore,\n @A_Inject(A_Scope) scope: A_Scope,\n @A_Inject(AreSyntax) syntax: AreSyntax,\n @A_Inject(AreScene) scene: AreScene,\n ...args: any[]\n ): void {\n /**\n * 1. Extract the value from the store based on the attribute content \n * (which is the path to the value in the store)\n */\n attribute.value = syntax.evaluate(attribute.content, store);\n\n if (attribute.value) {\n attribute.template!.scene.activate();\n\n attribute.template!.mount()\n }\n else {\n attribute.template!.unmount();\n\n attribute.template!.scene.deactivate();\n }\n }\n\n}"]}
1
+ {"version":3,"sources":["../../../src/directives/AreDirectiveIf.directive.ts"],"names":["AreDirectiveIf","AreDirective","AddCommentInstruction","A_Caller","A_Scope","AreStore","AreScene","A_Logger","AreSyntax","AreDirectiveContext","A_Frame"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAkCaA,sBAAA,GAAN,6BAA6BC,mCAAA,CAAa;AAAA,EAI7C,UACwB,SAAA,EACD,KAAA,EAEC,KAAA,EACA,KAAA,EACA,WACjB,IAAA,EACL;AACE,IAAA,MAAA,CAAO,MAAM,CAAA,+BAAA,EAAkC,SAAA,CAAU,MAAM,KAAA,CAAM,QAAA,EAAU,CAAA,CAAA,CAAG,CAAA;AAElF,IAAA,MAAM,OAAO,SAAA,CAAU,KAAA;AAKvB,IAAA,MAAM,UAAA,GAAa,KAAK,cAAA,EAAe;AAEvC,IAAA,MAAM,MAAA,GAAS,WAAW,UAAA,CAAW,IAAA,CAAK,OAAK,CAAA,CAAE,IAAA,KAAS,UAAU,IAAI,CAAA;AAExE,IAAA,IAAI,MAAA,EAAQ;AACR,MAAA,UAAA,CAAW,KAAA,CAAM,WAAW,MAAM,CAAA;AAClC,MAAA,IAAA,CAAK,KAAA,CAAM,SAAS,MAAM,CAAA;AAAA,IAC9B;AAEA,IAAA,IAAA,CAAK,IAAA,EAAK;AAEV,IAAA,IAAA,CAAK,SAAS,UAAU,CAAA;AAYxB,IAAA,UAAA,CAAW,MAAM,UAAA,EAAW;AAE5B,IAAA,SAAA,CAAU,QAAA,GAAW,UAAA;AAAA,EAEzB;AAAA,EAIA,QACwB,SAAA,EACA,KAAA,EACA,KAAA,EACC,MAAA,EAEU,qBAC5B,IAAA,EACC;AAKJ,IAAA,SAAA,CAAU,KAAA,GAAQ,MAAA,CAAO,QAAA,CAAS,SAAA,CA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mCAAA,CAAa,SAAA;AAAA,EAET,qCAASE,iBAAQ,CAAA,CAAA;AAAA,EACjB,qCAASC,gBAAO,CAAA,CAAA;AAAA,EAEhB,qCAASC,YAAQ,CAAA,CAAA;AAAA,EACjB,qCAASC,YAAQ,CAAA,CAAA;AAAA,EACjB,qCAASC,gBAAQ,CAAA;AAAA,CAAA,EAVbP,sBAAA,CAIT,SAAA,EAAA,WAAA,EAAA,CAAA,CAAA;AA+CA,eAAA,CAAA;AAAA,EADCC,mCAAA,CAAa,OAAA;AAAA,EAET,qCAASE,iBAAQ,CAAA,CAAA;AAAA,EACjB,qCAASE,YAAQ,CAAA,CAAA;AAAA,EACjB,qCAASC,YAAQ,CAAA,CAAA;AAAA,EACjB,qCAASE,aAAS,CAAA,CAAA;AAAA,EAElB,qCAASC,wCAAmB,CAAA;AAAA,CAAA,EAzDxBT,sBAAA,CAmDT,SAAA,EAAA,SAAA,EAAA,CAAA,CAAA;AAsCA,eAAA,CAAA;AAAA,EADCC,mCAAA,CAAa,MAAA;AAAA,EAET,qCAASE,iBAAQ,CAAA,CAAA;AAAA,EACjB,qCAASE,YAAQ,CAAA,CAAA;AAAA,EACjB,qCAASD,gBAAO,CAAA,CAAA;AAAA,EAChB,qCAASI,aAAS,CAAA,CAAA;AAAA,EAClB,qCAASF,YAAQ,CAAA;AAAA,CAAA,EA9FbN,sBAAA,CAyFT,SAAA,EAAA,QAAA,EAAA,CAAA,CAAA;AAzFSA,sBAAA,GAAN,eAAA,CAAA;AAAA,EALNU,aAAQ,MAAA,CAAO;AAAA,IACZ,SAAA,EAAW,YAAA;AAAA,IACX,WAAA,EAAa;AAAA,GAChB,CAAA;AAAA,EACAT,mCAAA,CAAa,SAAS,CAAC;AAAA,CAAA,EACXD,sBAAA,CAAA","file":"AreDirectiveIf.directive.js","sourcesContent":["import { A_Caller, A_Inject, A_Scope } from \"@adaas/a-concept\";\nimport { A_Logger } from \"@adaas/a-utils/a-logger\";\nimport { AreDirectiveAttribute } from \"@adaas/are-html/attributes/AreDirective.attribute\";\nimport { AreScene, AreStore, AreSyntax } from \"@adaas/are\";\nimport { AreDirective } from \"@adaas/are-html/directive/AreDirective.component\";\nimport { AddCommentInstruction } from \"@adaas/are-html/instructions/AddComment.instruction\";\nimport { AreDirectiveContext } from \"@adaas/are-html/directive/AreDirective.context\";\nimport { A_Frame } from \"@adaas/a-frame/core\";\n\n\n\n/**\n * `$if` directive — conditionally renders a node based on an expression.\n *\n * ⚠️ Known limitation: do NOT use `$if` and `$for` on the SAME element.\n * Doing so produces duplicated DOM on toggle because the two directives\n * share an owner node and clone its scope independently. Wrap one in a\n * parent element instead, e.g.:\n *\n * <div $if=\"visible\">\n * <li $for=\"item in items\">{{item.name}}</li>\n * </div>\n *\n * or\n *\n * <ul $for=\"item in items\">\n * <li $if=\"item.visible\">{{item.name}}</li>\n * </ul>\n */\n@A_Frame.Define({\n namespace: 'a-are-html',\n description: 'Built-in $if directive. Conditionally renders a subtree based on a store expression. Replaces the target element with a stable comment anchor when the condition is false and restores the fully rendered subtree when it becomes true, preventing any leaking of the host element between states.'\n})\n@AreDirective.Priority(2)\nexport class AreDirectiveIf extends AreDirective {\n\n\n @AreDirective.Transform\n transform(\n @A_Inject(A_Caller) attribute: AreDirectiveAttribute,\n @A_Inject(A_Scope) scope: A_Scope,\n\n @A_Inject(AreStore) store: AreStore,\n @A_Inject(AreScene) scene: AreScene,\n @A_Inject(A_Logger) logger: A_Logger,\n ...args: any[]\n ) {\n logger.debug(`[Transform] directive $IF for <${attribute.owner.aseid.toString()}>`)\n\n const node = attribute.owner;\n\n /**\n * We have to keep this node as a group node, and copy all data into the child node that would be actual node. \n */\n const ifTemplate = node.cloneWithScope();\n\n const ifAttr = ifTemplate.attributes.find(d => d.name === attribute.name);\n\n if (ifAttr) {\n ifTemplate.scope.deregister(ifAttr);\n node.scope.register(ifAttr);\n }\n\n node.init();\n\n node.addChild(ifTemplate);\n\n /**\n * Resolve or create a directive context for the item node. This is needed to hold the item-specific store values (e.g. the \"item\" and \"index\" in a \"for\" loop) that the template's bindings will reference during compile and update. The context is shared among all clones of the same template, but that's fine because each clone gets its own scope values assigned here.\n */\n // let directiveContext = ifTemplate.scope.resolveFlat(AreDirectiveContext);\n\n // if (!directiveContext) {\n // directiveContext = new AreDirectiveContext(ifTemplate.aseid);\n // ifTemplate.scope.register(directiveContext);\n // }\n\n ifTemplate.scene.deactivate();\n\n attribute.template = ifTemplate;\n\n }\n\n\n @AreDirective.Compile\n compile(\n @A_Inject(A_Caller) attribute: AreDirectiveAttribute,\n @A_Inject(AreStore) store: AreStore,\n @A_Inject(AreScene) scene: AreScene,\n @A_Inject(AreSyntax) syntax: AreSyntax,\n\n @A_Inject(AreDirectiveContext) directiveContext?: AreDirectiveContext,\n ...args: any[]\n ): void {\n /**\n * 1. Extract the value from the store based on the attribute content\n * (which is the path to the value in the store)\n */\n attribute.value = syntax.evaluate(attribute.content, store, {\n ...(directiveContext?.scope || {}),\n });\n\n /**\n * 2. If the value is falsy, remove the node from the scene by planning a RemoveElement instruction.\n * If the value is truthy, ensure the node is in the scene by planning an AddElement instruction if it's not already planned.\n */\n const hostInstruction = scene.host!;\n const commentIdentifier = ` --- if: ${attribute.template!.id} --- `;\n const declaration = new AddCommentInstruction({ content: commentIdentifier })\n\n scene.setHost(declaration);\n scene.planBefore(declaration, hostInstruction);\n scene.unPlan(hostInstruction);\n\n if (attribute.value)\n attribute.template!.scene.activate();\n else\n attribute.template!.scene.deactivate();\n }\n\n\n\n @AreDirective.Update\n update(\n @A_Inject(A_Caller) attribute: AreDirectiveAttribute,\n @A_Inject(AreStore) store: AreStore,\n @A_Inject(A_Scope) scope: A_Scope,\n @A_Inject(AreSyntax) syntax: AreSyntax,\n @A_Inject(AreScene) scene: AreScene,\n ...args: any[]\n ): void {\n /**\n * 1. Extract the value from the store based on the attribute content\n * (which is the path to the value in the store)\n */\n const previous = !!attribute.value;\n const next = !!syntax.evaluate(attribute.content, store);\n attribute.value = next;\n\n // Skip when truthiness has not changed — avoids redundant mount/unmount.\n if (previous === next) return;\n\n if (next) {\n attribute.template!.scene.activate();\n attribute.template!.mount();\n } else {\n attribute.template!.unmount();\n attribute.template!.scene.deactivate();\n }\n }\n\n}"]}
@@ -5,6 +5,7 @@ import { AreStore, AreScene, AreSyntax } from '@adaas/are';
5
5
  import { AreDirective } from '@adaas/are-html/directive/AreDirective.component';
6
6
  import { AddCommentInstruction } from '@adaas/are-html/instructions/AddComment.instruction';
7
7
  import { AreDirectiveContext } from '@adaas/are-html/directive/AreDirective.context';
8
+ import { A_Frame } from '@adaas/a-frame/core';
8
9
 
9
10
  let AreDirectiveIf = class extends AreDirective {
10
11
  transform(attribute, scope, store, scene, logger, ...args) {
@@ -22,7 +23,6 @@ let AreDirectiveIf = class extends AreDirective {
22
23
  attribute.template = ifTemplate;
23
24
  }
24
25
  compile(attribute, store, scene, syntax, directiveContext, ...args) {
25
- console.log('Compiling directive "if" with attribute content:', attribute);
26
26
  attribute.value = syntax.evaluate(attribute.content, store, {
27
27
  ...directiveContext?.scope || {}
28
28
  });
@@ -38,8 +38,11 @@ let AreDirectiveIf = class extends AreDirective {
38
38
  attribute.template.scene.deactivate();
39
39
  }
40
40
  update(attribute, store, scope, syntax, scene, ...args) {
41
- attribute.value = syntax.evaluate(attribute.content, store);
42
- if (attribute.value) {
41
+ const previous = !!attribute.value;
42
+ const next = !!syntax.evaluate(attribute.content, store);
43
+ attribute.value = next;
44
+ if (previous === next) return;
45
+ if (next) {
43
46
  attribute.template.scene.activate();
44
47
  attribute.template.mount();
45
48
  } else {
@@ -73,6 +76,10 @@ __decorateClass([
73
76
  __decorateParam(4, A_Inject(AreScene))
74
77
  ], AreDirectiveIf.prototype, "update", 1);
75
78
  AreDirectiveIf = __decorateClass([
79
+ A_Frame.Define({
80
+ namespace: "a-are-html",
81
+ description: "Built-in $if directive. Conditionally renders a subtree based on a store expression. Replaces the target element with a stable comment anchor when the condition is false and restores the fully rendered subtree when it becomes true, preventing any leaking of the host element between states."
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,EAAA,WAAA,EAAA,CAAA,CAAA;AA+CA,eAAA,CAAA;AAAA,EADC,YAAA,CAAa,OAAA;AAAA,EAET,4BAAS,QAAQ,CAAA,CAAA;AAAA,EACjB,4BAAS,QAAQ,CAAA,CAAA;AAAA,EACjB,4BAAS,QAAQ,CAAA,CAAA;AAAA,EACjB,4BAAS,SAAS,CAAA,CAAA;AAAA,EAElB,4BAAS,mBAAmB,CAAA;AAAA,CAAA,EAzDxB,cAAA,CAmDT,SAAA,EAAA,SAAA,EAAA,CAAA,CAAA;AAyCA,eAAA,CAAA;AAAA,EADC,YAAA,CAAa,MAAA;AAAA,EAET,4BAAS,QAAQ,CAAA,CAAA;AAAA,EACjB,4BAAS,QAAQ,CAAA,CAAA;AAAA,EACjB,4BAAS,OAAO,CAAA,CAAA;AAAA,EAChB,4BAAS,SAAS,CAAA,CAAA;AAAA,EAClB,4BAAS,QAAQ,CAAA;AAAA,CAAA,EAjGb,cAAA,CA4FT,SAAA,EAAA,QAAA,EAAA,CAAA,CAAA;AA5FS,cAAA,GAAN,eAAA,CAAA;AAAA,EADN,YAAA,CAAa,SAAS,CAAC;AAAA,CAAA,EACX,cAAA,CAAA","file":"AreDirectiveIf.directive.mjs","sourcesContent":["import { A_Caller, A_Inject, A_Scope } from \"@adaas/a-concept\";\nimport { A_Logger } from \"@adaas/a-utils/a-logger\";\nimport { AreDirectiveAttribute } from \"@adaas/are-html/attributes/AreDirective.attribute\";\nimport { AreScene, AreStore, AreSyntax } from \"@adaas/are\";\nimport { AreDirective } from \"@adaas/are-html/directive/AreDirective.component\";\nimport { AddCommentInstruction } from \"@adaas/are-html/instructions/AddComment.instruction\";\nimport { AreDirectiveContext } from \"@adaas/are-html/directive/AreDirective.context\";\n\n\n\n@AreDirective.Priority(2)\nexport class AreDirectiveIf extends AreDirective {\n\n\n @AreDirective.Transform\n transform(\n @A_Inject(A_Caller) attribute: AreDirectiveAttribute,\n @A_Inject(A_Scope) scope: A_Scope,\n\n @A_Inject(AreStore) store: AreStore,\n @A_Inject(AreScene) scene: AreScene,\n @A_Inject(A_Logger) logger: A_Logger,\n ...args: any[]\n ) {\n logger.debug(`[Transform] directive $IF for <${attribute.owner.aseid.toString()}>`)\n\n const node = attribute.owner;\n\n /**\n * We have to keep this node as a group node, and copy all data into the child node that would be actual node. \n */\n const ifTemplate = node.cloneWithScope();\n\n const ifAttr = ifTemplate.attributes.find(d => d.name === attribute.name);\n\n if (ifAttr) {\n ifTemplate.scope.deregister(ifAttr);\n node.scope.register(ifAttr);\n }\n\n node.init();\n\n node.addChild(ifTemplate);\n\n /**\n * Resolve or create a directive context for the item node. This is needed to hold the item-specific store values (e.g. the \"item\" and \"index\" in a \"for\" loop) that the template's bindings will reference during compile and update. The context is shared among all clones of the same template, but that's fine because each clone gets its own scope values assigned here.\n */\n // let directiveContext = ifTemplate.scope.resolveFlat(AreDirectiveContext);\n\n // if (!directiveContext) {\n // directiveContext = new AreDirectiveContext(ifTemplate.aseid);\n // ifTemplate.scope.register(directiveContext);\n // }\n\n ifTemplate.scene.deactivate();\n\n attribute.template = ifTemplate;\n\n }\n\n\n @AreDirective.Compile\n compile(\n @A_Inject(A_Caller) attribute: AreDirectiveAttribute,\n @A_Inject(AreStore) store: AreStore,\n @A_Inject(AreScene) scene: AreScene,\n @A_Inject(AreSyntax) syntax: AreSyntax,\n\n @A_Inject(AreDirectiveContext) directiveContext?: AreDirectiveContext,\n ...args: any[]\n ): void {\n\n console.log('Compiling directive \"if\" with attribute content:', attribute);\n\n /**\n * 1. Extract the value from the store based on the attribute content \n * (which is the path to the value in the store)\n */\n attribute.value = syntax.evaluate(attribute.content, store, {\n ...(directiveContext?.scope || {}),\n });\n\n /**\n * 2. If the value is falsy, remove the node from the scene by planning a RemoveElement instruction.\n * If the value is truthy, ensure the node is in the scene by planning an AddElement instruction if it's not already planned.\n */\n const hostInstruction = scene.host!;\n const commentIdentifier = ` --- if: ${attribute.template!.id} --- `;\n const declaration = new AddCommentInstruction({ content: commentIdentifier })\n\n scene.setHost(declaration);\n scene.planBefore(declaration, hostInstruction);\n scene.unPlan(hostInstruction);\n\n if (attribute.value)\n attribute.template!.scene.activate();\n else\n attribute.template!.scene.deactivate();\n }\n\n\n\n @AreDirective.Update\n update(\n @A_Inject(A_Caller) attribute: AreDirectiveAttribute,\n @A_Inject(AreStore) store: AreStore,\n @A_Inject(A_Scope) scope: A_Scope,\n @A_Inject(AreSyntax) syntax: AreSyntax,\n @A_Inject(AreScene) scene: AreScene,\n ...args: any[]\n ): void {\n /**\n * 1. Extract the value from the store based on the attribute content \n * (which is the path to the value in the store)\n */\n attribute.value = syntax.evaluate(attribute.content, store);\n\n if (attribute.value) {\n attribute.template!.scene.activate();\n\n attribute.template!.mount()\n }\n else {\n attribute.template!.unmount();\n\n attribute.template!.scene.deactivate();\n }\n }\n\n}"]}
1
+ {"version":3,"sources":["../../../src/directives/AreDirectiveIf.directive.ts"],"names":[],"mappings":";;;;;;;;;AAkCO,IAAM,cAAA,GAAN,cAA6B,YAAA,CAAa;AAAA,EAI7C,UACwB,SAAA,EACD,KAAA,EAEC,KAAA,EACA,KAAA,EACA,WACjB,IAAA,EACL;AACE,IAAA,MAAA,CAAO,MAAM,CAAA,+BAAA,EAAkC,SAAA,CAAU,MAAM,KAAA,CAAM,QAAA,EAAU,CAAA,CAAA,CAAG,CAAA;AAElF,IAAA,MAAM,OAAO,SAAA,CAAU,KAAA;AAKvB,IAAA,MAAM,UAAA,GAAa,KAAK,cAAA,EAAe;AAEvC,IAAA,MAAM,MAAA,GAAS,WAAW,UAAA,CAAW,IAAA,CAAK,OAAK,CAAA,CAAE,IAAA,KAAS,UAAU,IAAI,CAAA;AAExE,IAAA,IAAI,MAAA,EAAQ;AACR,MAAA,UAAA,CAAW,KAAA,CAAM,WAAW,MAAM,CAAA;AAClC,MAAA,IAAA,CAAK,KAAA,CAAM,SAAS,MAAM,CAAA;AAAA,IAC9B;AAEA,IAAA,IAAA,CAAK,IAAA,EAAK;AAEV,IAAA,IAAA,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,YAAA;AAAA,IACX,WAAA,EAAa;AAAA,GAChB,CAAA;AAAA,EACA,YAAA,CAAa,SAAS,CAAC;AAAA,CAAA,EACX,cAAA,CAAA","file":"AreDirectiveIf.directive.mjs","sourcesContent":["import { A_Caller, A_Inject, A_Scope } from \"@adaas/a-concept\";\nimport { A_Logger } from \"@adaas/a-utils/a-logger\";\nimport { AreDirectiveAttribute } from \"@adaas/are-html/attributes/AreDirective.attribute\";\nimport { AreScene, AreStore, AreSyntax } from \"@adaas/are\";\nimport { AreDirective } from \"@adaas/are-html/directive/AreDirective.component\";\nimport { AddCommentInstruction } from \"@adaas/are-html/instructions/AddComment.instruction\";\nimport { AreDirectiveContext } from \"@adaas/are-html/directive/AreDirective.context\";\nimport { A_Frame } from \"@adaas/a-frame/core\";\n\n\n\n/**\n * `$if` directive — conditionally renders a node based on an expression.\n *\n * ⚠️ Known limitation: do NOT use `$if` and `$for` on the SAME element.\n * Doing so produces duplicated DOM on toggle because the two directives\n * share an owner node and clone its scope independently. Wrap one in a\n * parent element instead, e.g.:\n *\n * <div $if=\"visible\">\n * <li $for=\"item in items\">{{item.name}}</li>\n * </div>\n *\n * or\n *\n * <ul $for=\"item in items\">\n * <li $if=\"item.visible\">{{item.name}}</li>\n * </ul>\n */\n@A_Frame.Define({\n namespace: 'a-are-html',\n description: 'Built-in $if directive. Conditionally renders a subtree based on a store expression. Replaces the target element with a stable comment anchor when the condition is false and restores the fully rendered subtree when it becomes true, preventing any leaking of the host element between states.'\n})\n@AreDirective.Priority(2)\nexport class AreDirectiveIf extends AreDirective {\n\n\n @AreDirective.Transform\n transform(\n @A_Inject(A_Caller) attribute: AreDirectiveAttribute,\n @A_Inject(A_Scope) scope: A_Scope,\n\n @A_Inject(AreStore) store: AreStore,\n @A_Inject(AreScene) scene: AreScene,\n @A_Inject(A_Logger) logger: A_Logger,\n ...args: any[]\n ) {\n logger.debug(`[Transform] directive $IF for <${attribute.owner.aseid.toString()}>`)\n\n const node = attribute.owner;\n\n /**\n * We have to keep this node as a group node, and copy all data into the child node that would be actual node. \n */\n const ifTemplate = node.cloneWithScope();\n\n const ifAttr = ifTemplate.attributes.find(d => d.name === attribute.name);\n\n if (ifAttr) {\n ifTemplate.scope.deregister(ifAttr);\n node.scope.register(ifAttr);\n }\n\n node.init();\n\n node.addChild(ifTemplate);\n\n /**\n * Resolve or create a directive context for the item node. This is needed to hold the item-specific store values (e.g. the \"item\" and \"index\" in a \"for\" loop) that the template's bindings will reference during compile and update. The context is shared among all clones of the same template, but that's fine because each clone gets its own scope values assigned here.\n */\n // let directiveContext = ifTemplate.scope.resolveFlat(AreDirectiveContext);\n\n // if (!directiveContext) {\n // directiveContext = new AreDirectiveContext(ifTemplate.aseid);\n // ifTemplate.scope.register(directiveContext);\n // }\n\n ifTemplate.scene.deactivate();\n\n attribute.template = ifTemplate;\n\n }\n\n\n @AreDirective.Compile\n compile(\n @A_Inject(A_Caller) attribute: AreDirectiveAttribute,\n @A_Inject(AreStore) store: AreStore,\n @A_Inject(AreScene) scene: AreScene,\n @A_Inject(AreSyntax) syntax: AreSyntax,\n\n @A_Inject(AreDirectiveContext) directiveContext?: AreDirectiveContext,\n ...args: any[]\n ): void {\n /**\n * 1. Extract the value from the store based on the attribute content\n * (which is the path to the value in the store)\n */\n attribute.value = syntax.evaluate(attribute.content, store, {\n ...(directiveContext?.scope || {}),\n });\n\n /**\n * 2. If the value is falsy, remove the node from the scene by planning a RemoveElement instruction.\n * If the value is truthy, ensure the node is in the scene by planning an AddElement instruction if it's not already planned.\n */\n const hostInstruction = scene.host!;\n const commentIdentifier = ` --- if: ${attribute.template!.id} --- `;\n const declaration = new AddCommentInstruction({ content: commentIdentifier })\n\n scene.setHost(declaration);\n scene.planBefore(declaration, hostInstruction);\n scene.unPlan(hostInstruction);\n\n if (attribute.value)\n attribute.template!.scene.activate();\n else\n attribute.template!.scene.deactivate();\n }\n\n\n\n @AreDirective.Update\n update(\n @A_Inject(A_Caller) attribute: AreDirectiveAttribute,\n @A_Inject(AreStore) store: AreStore,\n @A_Inject(A_Scope) scope: A_Scope,\n @A_Inject(AreSyntax) syntax: AreSyntax,\n @A_Inject(AreScene) scene: AreScene,\n ...args: any[]\n ): void {\n /**\n * 1. Extract the value from the store based on the attribute content\n * (which is the path to the value in the store)\n */\n const previous = !!attribute.value;\n const next = !!syntax.evaluate(attribute.content, store);\n attribute.value = next;\n\n // Skip when truthiness has not changed — avoids redundant mount/unmount.\n if (previous === next) return;\n\n if (next) {\n attribute.template!.scene.activate();\n attribute.template!.mount();\n } else {\n attribute.template!.unmount();\n attribute.template!.scene.deactivate();\n }\n }\n\n}"]}
@@ -1,6 +1,6 @@
1
1
  import { A_Feature } from '@adaas/a-concept';
2
2
  import { A_Logger } from '@adaas/a-utils/a-logger';
3
- import { AreCompiler, AreScene, AreStore } from '@adaas/are';
3
+ import { AreCompiler, AreScene, AreStore, AreSyntax } from '@adaas/are';
4
4
  import { f as AreStaticAttribute, b as AreDirectiveAttribute, c as AreEventAttribute, A as AreBindingAttribute } from '../AreBinding.attribute-C6qrxN8K.mjs';
5
5
  import { AreInterpolation } from '../nodes/AreInterpolation.mjs';
6
6
  import { AreText } from '../nodes/AreText.mjs';
@@ -21,7 +21,7 @@ declare class AreHTMLCompiler extends AreCompiler {
21
21
  compileStaticAttribute(attribute: AreStaticAttribute, scene: AreScene, ...args: any[]): void;
22
22
  compileDirectiveAttribute(directive: AreDirectiveAttribute, store: AreStore, feature: A_Feature, logger?: A_Logger, ...args: any[]): void;
23
23
  compileEventAttribute(attribute: AreEventAttribute, scene: AreScene, ...args: any[]): void;
24
- compileBindingAttribute(attribute: AreBindingAttribute, scene: AreScene, parentStore: AreStore, store: AreStore, ...args: any[]): void;
24
+ compileBindingAttribute(attribute: AreBindingAttribute, scene: AreScene, parentStore: AreStore, store: AreStore, syntax: AreSyntax, ...args: any[]): void;
25
25
  }
26
26
 
27
27
  export { AreHTMLCompiler };
@@ -1,6 +1,6 @@
1
1
  import { A_Feature } from '@adaas/a-concept';
2
2
  import { A_Logger } from '@adaas/a-utils/a-logger';
3
- import { AreCompiler, AreScene, AreStore } from '@adaas/are';
3
+ import { AreCompiler, AreScene, AreStore, AreSyntax } from '@adaas/are';
4
4
  import { f as AreStaticAttribute, b as AreDirectiveAttribute, c as AreEventAttribute, A as AreBindingAttribute } from '../AreBinding.attribute-C6JasbJL.js';
5
5
  import { AreInterpolation } from '../nodes/AreInterpolation.js';
6
6
  import { AreText } from '../nodes/AreText.js';
@@ -21,7 +21,7 @@ declare class AreHTMLCompiler extends AreCompiler {
21
21
  compileStaticAttribute(attribute: AreStaticAttribute, scene: AreScene, ...args: any[]): void;
22
22
  compileDirectiveAttribute(directive: AreDirectiveAttribute, store: AreStore, feature: A_Feature, logger?: A_Logger, ...args: any[]): void;
23
23
  compileEventAttribute(attribute: AreEventAttribute, scene: AreScene, ...args: any[]): void;
24
- compileBindingAttribute(attribute: AreBindingAttribute, scene: AreScene, parentStore: AreStore, store: AreStore, ...args: any[]): void;
24
+ compileBindingAttribute(attribute: AreBindingAttribute, scene: AreScene, parentStore: AreStore, store: AreStore, syntax: AreSyntax, ...args: any[]): void;
25
25
  }
26
26
 
27
27
  export { AreHTMLCompiler };