Welcome to mirror list, hosted at ThFree Co, Russian Federation.

cdn.js « dist « morph « packages « alpinejs - github.com/gohugoio/hugo-mod-jslibs-dist.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3971815cdb7e1fb8c730957268330f42668f928f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
(() => {
  // packages/morph/src/dom.js
  var DomManager = class {
    el = void 0;
    constructor(el) {
      this.el = el;
    }
    traversals = {
      first: "firstElementChild",
      next: "nextElementSibling",
      parent: "parentElement"
    };
    nodes() {
      this.traversals = {
        first: "firstChild",
        next: "nextSibling",
        parent: "parentNode"
      };
      return this;
    }
    first() {
      return this.teleportTo(this.el[this.traversals["first"]]);
    }
    next() {
      return this.teleportTo(this.teleportBack(this.el[this.traversals["next"]]));
    }
    before(insertee) {
      this.el[this.traversals["parent"]].insertBefore(insertee, this.el);
      return insertee;
    }
    replace(replacement) {
      this.el[this.traversals["parent"]].replaceChild(replacement, this.el);
      return replacement;
    }
    append(appendee) {
      this.el.appendChild(appendee);
      return appendee;
    }
    teleportTo(el) {
      if (!el)
        return el;
      if (el._x_teleport)
        return el._x_teleport;
      return el;
    }
    teleportBack(el) {
      if (!el)
        return el;
      if (el._x_teleportBack)
        return el._x_teleportBack;
      return el;
    }
  };
  function dom(el) {
    return new DomManager(el);
  }
  function createElement(html) {
    const template = document.createElement("template");
    template.innerHTML = html;
    return template.content.firstElementChild;
  }
  function textOrComment(el) {
    return el.nodeType === 3 || el.nodeType === 8;
  }

  // packages/morph/src/morph.js
  var resolveStep = () => {
  };
  var logger = () => {
  };
  async function morph(from, toHtml, options) {
    let fromEl;
    let toEl;
    let key, lookahead, updating, updated, removing, removed, adding, added, debug;
    function breakpoint(message) {
      if (!debug)
        return;
      logger((message || "").replace("\n", "\\n"), fromEl, toEl);
      return new Promise((resolve) => resolveStep = () => resolve());
    }
    function assignOptions(options2 = {}) {
      let defaultGetKey = (el) => el.getAttribute("key");
      let noop = () => {
      };
      updating = options2.updating || noop;
      updated = options2.updated || noop;
      removing = options2.removing || noop;
      removed = options2.removed || noop;
      adding = options2.adding || noop;
      added = options2.added || noop;
      key = options2.key || defaultGetKey;
      lookahead = options2.lookahead || false;
      debug = options2.debug || false;
    }
    async function patch(from2, to) {
      if (differentElementNamesTypesOrKeys(from2, to)) {
        let result = patchElement(from2, to);
        await breakpoint("Swap elements");
        return result;
      }
      let updateChildrenOnly = false;
      if (shouldSkip(updating, from2, to, () => updateChildrenOnly = true))
        return;
      window.Alpine && initializeAlpineOnTo(from2, to, () => updateChildrenOnly = true);
      if (textOrComment(to)) {
        await patchNodeValue(from2, to);
        updated(from2, to);
        return;
      }
      if (!updateChildrenOnly) {
        await patchAttributes(from2, to);
      }
      updated(from2, to);
      await patchChildren(from2, to);
    }
    function differentElementNamesTypesOrKeys(from2, to) {
      return from2.nodeType != to.nodeType || from2.nodeName != to.nodeName || getKey(from2) != getKey(to);
    }
    function patchElement(from2, to) {
      if (shouldSkip(removing, from2))
        return;
      let toCloned = to.cloneNode(true);
      if (shouldSkip(adding, toCloned))
        return;
      dom(from2).replace(toCloned);
      removed(from2);
      added(toCloned);
    }
    async function patchNodeValue(from2, to) {
      let value = to.nodeValue;
      if (from2.nodeValue !== value) {
        from2.nodeValue = value;
        await breakpoint("Change text node to: " + value);
      }
    }
    async function patchAttributes(from2, to) {
      if (from2._x_isShown && !to._x_isShown) {
        return;
      }
      if (!from2._x_isShown && to._x_isShown) {
        return;
      }
      let domAttributes = Array.from(from2.attributes);
      let toAttributes = Array.from(to.attributes);
      for (let i = domAttributes.length - 1; i >= 0; i--) {
        let name = domAttributes[i].name;
        if (!to.hasAttribute(name)) {
          from2.removeAttribute(name);
          await breakpoint("Remove attribute");
        }
      }
      for (let i = toAttributes.length - 1; i >= 0; i--) {
        let name = toAttributes[i].name;
        let value = toAttributes[i].value;
        if (from2.getAttribute(name) !== value) {
          from2.setAttribute(name, value);
          await breakpoint(`Set [${name}] attribute to: "${value}"`);
        }
      }
    }
    async function patchChildren(from2, to) {
      let domChildren = from2.childNodes;
      let toChildren = to.childNodes;
      let toKeyToNodeMap = keyToMap(toChildren);
      let domKeyDomNodeMap = keyToMap(domChildren);
      let currentTo = dom(to).nodes().first();
      let currentFrom = dom(from2).nodes().first();
      let domKeyHoldovers = {};
      while (currentTo) {
        let toKey = getKey(currentTo);
        let domKey = getKey(currentFrom);
        if (!currentFrom) {
          if (toKey && domKeyHoldovers[toKey]) {
            let holdover = domKeyHoldovers[toKey];
            dom(from2).append(holdover);
            currentFrom = holdover;
            await breakpoint("Add element (from key)");
          } else {
            let added2 = addNodeTo(currentTo, from2) || {};
            await breakpoint("Add element: " + (added2.outerHTML || added2.nodeValue));
            currentTo = dom(currentTo).nodes().next();
            continue;
          }
        }
        if (lookahead) {
          let nextToElementSibling = dom(currentTo).next();
          let found = false;
          while (!found && nextToElementSibling) {
            if (currentFrom.isEqualNode(nextToElementSibling)) {
              found = true;
              currentFrom = addNodeBefore(currentTo, currentFrom);
              domKey = getKey(currentFrom);
              await breakpoint("Move element (lookahead)");
            }
            nextToElementSibling = dom(nextToElementSibling).next();
          }
        }
        if (toKey !== domKey) {
          if (!toKey && domKey) {
            domKeyHoldovers[domKey] = currentFrom;
            currentFrom = addNodeBefore(currentTo, currentFrom);
            domKeyHoldovers[domKey].remove();
            currentFrom = dom(currentFrom).nodes().next();
            currentTo = dom(currentTo).nodes().next();
            await breakpoint('No "to" key');
            continue;
          }
          if (toKey && !domKey) {
            if (domKeyDomNodeMap[toKey]) {
              currentFrom = dom(currentFrom).replace(domKeyDomNodeMap[toKey]);
              await breakpoint('No "from" key');
            }
          }
          if (toKey && domKey) {
            domKeyHoldovers[domKey] = currentFrom;
            let domKeyNode = domKeyDomNodeMap[toKey];
            if (domKeyNode) {
              currentFrom = dom(currentFrom).replace(domKeyNode);
              await breakpoint('Move "from" key');
            } else {
              domKeyHoldovers[domKey] = currentFrom;
              currentFrom = addNodeBefore(currentTo, currentFrom);
              domKeyHoldovers[domKey].remove();
              currentFrom = dom(currentFrom).next();
              currentTo = dom(currentTo).next();
              await breakpoint("Swap elements with keys");
              continue;
            }
          }
        }
        let currentFromNext = currentFrom && dom(currentFrom).nodes().next();
        await patch(currentFrom, currentTo);
        currentTo = currentTo && dom(currentTo).nodes().next();
        currentFrom = currentFromNext;
      }
      let removals = [];
      while (currentFrom) {
        if (!shouldSkip(removing, currentFrom))
          removals.push(currentFrom);
        currentFrom = dom(currentFrom).nodes().next();
      }
      while (removals.length) {
        let domForRemoval = removals.shift();
        domForRemoval.remove();
        await breakpoint("remove el");
        removed(domForRemoval);
      }
    }
    function getKey(el) {
      return el && el.nodeType === 1 && key(el);
    }
    function keyToMap(els) {
      let map = {};
      els.forEach((el) => {
        let theKey = getKey(el);
        if (theKey) {
          map[theKey] = el;
        }
      });
      return map;
    }
    function addNodeTo(node, parent) {
      if (!shouldSkip(adding, node)) {
        let clone = node.cloneNode(true);
        dom(parent).append(clone);
        added(clone);
        return clone;
      }
      return null;
    }
    function addNodeBefore(node, beforeMe) {
      if (!shouldSkip(adding, node)) {
        let clone = node.cloneNode(true);
        dom(beforeMe).before(clone);
        added(clone);
        return clone;
      }
      return beforeMe;
    }
    assignOptions(options);
    fromEl = from;
    toEl = createElement(toHtml);
    if (window.Alpine && window.Alpine.closestDataStack && !from._x_dataStack) {
      toEl._x_dataStack = window.Alpine.closestDataStack(from);
      toEl._x_dataStack && window.Alpine.clone(from, toEl);
    }
    await breakpoint();
    await patch(from, toEl);
    fromEl = void 0;
    toEl = void 0;
    return from;
  }
  morph.step = () => resolveStep();
  morph.log = (theLogger) => {
    logger = theLogger;
  };
  function shouldSkip(hook, ...args) {
    let skip = false;
    hook(...args, () => skip = true);
    return skip;
  }
  function initializeAlpineOnTo(from, to, childrenOnly) {
    if (from.nodeType !== 1)
      return;
    if (from._x_dataStack) {
      window.Alpine.clone(from, to);
    }
  }

  // packages/morph/src/index.js
  function src_default(Alpine) {
    Alpine.morph = morph;
  }

  // packages/morph/builds/cdn.js
  document.addEventListener("alpine:init", () => {
    window.Alpine.plugin(src_default);
  });
})();