sub/js/idiomorph-ext.min.js

1 line
7.4 KiB
JavaScript

(function(e,t){if(typeof define==="function"&&define.amd){define([],t)}else{e.Idiomorph=e.Idiomorph||t()}})(typeof self!=="undefined"?self:this,function(){return function(){"use strict";let o=new Set;function e(e,t,n={}){if(e instanceof Document){e=e.documentElement}if(typeof t==="string"){t=S(t)}let l=y(t);let r=c(e,l,n);return d(e,l,r)}function d(r,i,o){if(o.head.block){let t=r.querySelector("head");let n=i.querySelector("head");if(t&&n){let e=s(n,t,o);Promise.all(e).then(function(){d(r,i,Object.assign(o,{head:{block:false,ignore:true}}))});return}}if(o.morphStyle==="innerHTML"){l(i,r,o);return r.children}else if(o.morphStyle==="outerHTML"||o.morphStyle==null){let e=M(i,r,o);let t=e?.previousSibling;let n=e?.nextSibling;let l=u(r,e,o);if(e){return A(t,l,n)}else{return[]}}else{throw"Do not understand how to morph style "+o.morphStyle}}function f(e,t){return t.ignoreActiveValue&&e===document.activeElement}function u(e,t,n){if(n.ignoreActive&&e===document.activeElement){}else if(t==null){if(n.callbacks.beforeNodeRemoved(e)===false)return;e.remove();n.callbacks.afterNodeRemoved(e);return null}else if(!m(e,t)){if(n.callbacks.beforeNodeRemoved(e)===false)return;if(n.callbacks.beforeNodeAdded(t)===false)return;e.parentElement.replaceChild(t,e);n.callbacks.afterNodeAdded(t);n.callbacks.afterNodeRemoved(e);return t}else{if(n.callbacks.beforeNodeMorphed(e,t)===false)return;if(e instanceof HTMLHeadElement&&n.head.ignore){}else if(e instanceof HTMLHeadElement&&n.head.style!=="morph"){s(t,e,n)}else{r(t,e,n);if(!f(e,n)){l(t,e,n)}}n.callbacks.afterNodeMorphed(e,t);return e}}function l(n,l,r){let i=n.firstChild;let o=l.firstChild;let d;while(i){d=i;i=d.nextSibling;if(o==null){if(r.callbacks.beforeNodeAdded(d)===false)return;l.appendChild(d);r.callbacks.afterNodeAdded(d);x(r,d);continue}if(h(d,o,r)){u(o,d,r);o=o.nextSibling;x(r,d);continue}let e=g(n,l,d,o,r);if(e){o=b(o,e,r);u(e,d,r);x(r,d);continue}let t=v(n,l,d,o,r);if(t){o=b(o,t,r);u(t,d,r);x(r,d);continue}if(r.callbacks.beforeNodeAdded(d)===false)return;l.insertBefore(d,o);r.callbacks.afterNodeAdded(d);x(r,d)}while(o!==null){let e=o;o=o.nextSibling;k(e,r)}}function r(e,t,n){let l=e.nodeType;if(l===1){const r=e.attributes;const i=t.attributes;for(const o of r){if(o.name==="value"&&f(t,n)){continue}if(t.getAttribute(o.name)!==o.value){t.setAttribute(o.name,o.value)}}for(const d of i){if(!e.hasAttribute(d.name)){t.removeAttribute(d.name)}}}if(l===8||l===3){if(t.nodeValue!==e.nodeValue){t.nodeValue=e.nodeValue}}if(!f(t,n)){a(e,t)}}function t(e,t,n){if(e[n]!==t[n]){if(e[n]){t.setAttribute(n,e[n])}else{t.removeAttribute(n)}}}function a(n,l){if(n instanceof HTMLInputElement&&l instanceof HTMLInputElement&&n.type!=="file"){l.value=n.value||"";t(n,l,"value");t(n,l,"checked");t(n,l,"disabled")}else if(n instanceof HTMLOptionElement){t(n,l,"selected")}else if(n instanceof HTMLTextAreaElement&&l instanceof HTMLTextAreaElement){let e=n.value;let t=l.value;if(e!==t){l.value=e}if(l.firstChild&&l.firstChild.nodeValue!==e){l.firstChild.nodeValue=e}}}function s(e,t,l){let r=[];let i=[];let o=[];let d=[];let f=l.head.style;let u=new Map;for(const n of e.children){u.set(n.outerHTML,n)}for(const s of t.children){let e=u.has(s.outerHTML);let t=l.head.shouldReAppend(s);let n=l.head.shouldPreserve(s);if(e||n){if(t){i.push(s)}else{u.delete(s.outerHTML);o.push(s)}}else{if(f==="append"){if(t){i.push(s);d.push(s)}}else{if(l.head.shouldRemove(s)!==false){i.push(s)}}}}d.push(...u.values());p("to append: ",d);let a=[];for(const c of d){p("adding: ",c);let n=document.createRange().createContextualFragment(c.outerHTML).firstChild;p(n);if(l.callbacks.beforeNodeAdded(n)!==false){if(n.href||n.src){let t=null;let e=new Promise(function(e){t=e});n.addEventListener("load",function(){t()});a.push(e)}t.appendChild(n);l.callbacks.afterNodeAdded(n);r.push(n)}}for(const h of i){if(l.callbacks.beforeNodeRemoved(h)!==false){t.removeChild(h);l.callbacks.afterNodeRemoved(h)}}l.head.afterHeadMorphed(t,{added:r,kept:o,removed:i});return a}function p(){}function i(){}function c(e,t,n){return{target:e,newContent:t,config:n,morphStyle:n.morphStyle,ignoreActive:n.ignoreActive,ignoreActiveValue:n.ignoreActiveValue,idMap:L(e,t),deadIds:new Set,callbacks:Object.assign({beforeNodeAdded:i,afterNodeAdded:i,beforeNodeMorphed:i,afterNodeMorphed:i,beforeNodeRemoved:i,afterNodeRemoved:i},n.callbacks),head:Object.assign({style:"merge",shouldPreserve:function(e){return e.getAttribute("im-preserve")==="true"},shouldReAppend:function(e){return e.getAttribute("im-re-append")==="true"},shouldRemove:i,afterHeadMorphed:i},n.head)}}function h(e,t,n){if(e==null||t==null){return false}if(e.nodeType===t.nodeType&&e.tagName===t.tagName){if(e.id!==""&&e.id===t.id){return true}else{return E(n,e,t)>0}}return false}function m(e,t){if(e==null||t==null){return false}return e.nodeType===t.nodeType&&e.tagName===t.tagName}function b(t,e,n){while(t!==e){let e=t;t=t.nextSibling;k(e,n)}x(n,e);return e.nextSibling}function g(n,e,l,r,i){let o=E(i,l,e);let t=null;if(o>0){let e=r;let t=0;while(e!=null){if(h(l,e,i)){return e}t+=E(i,e,n);if(t>o){return null}e=e.nextSibling}}return t}function v(e,t,n,l,r){let i=l;let o=n.nextSibling;let d=0;while(i!=null){if(E(r,i,e)>0){return null}if(m(n,i)){return i}if(m(o,i)){d++;o=o.nextSibling;if(d>=2){return null}}i=i.nextSibling}return i}function S(n){let l=new DOMParser;let e=n.replace(/<svg(\s[^>]*>|>)([\s\S]*?)<\/svg>/gim,"");if(e.match(/<\/html>/)||e.match(/<\/head>/)||e.match(/<\/body>/)){let t=l.parseFromString(n,"text/html");if(e.match(/<\/html>/)){t.generatedByIdiomorph=true;return t}else{let e=t.firstChild;if(e){e.generatedByIdiomorph=true;return e}else{return null}}}else{let e=l.parseFromString("<body><template>"+n+"</template></body>","text/html");let t=e.body.querySelector("template").content;t.generatedByIdiomorph=true;return t}}function y(e){if(e==null){const t=document.createElement("div");return t}else if(e.generatedByIdiomorph){return e}else if(e instanceof Node){const t=document.createElement("div");t.append(e);return t}else{const t=document.createElement("div");for(const n of[...e]){t.append(n)}return t}}function A(e,t,n){let l=[];let r=[];while(e!=null){l.push(e);e=e.previousSibling}while(l.length>0){let e=l.pop();r.push(e);t.parentElement.insertBefore(e,t)}r.push(t);while(n!=null){l.push(n);r.push(n);n=n.nextSibling}while(l.length>0){t.parentElement.insertBefore(l.pop(),t.nextSibling)}return r}function M(e,t,n){let l;l=e.firstChild;let r=l;let i=0;while(l){let e=N(l,t,n);if(e>i){r=l;i=e}l=l.nextSibling}return r}function N(e,t,n){if(m(e,t)){return.5+E(n,e,t)}return 0}function k(e,t){x(t,e);if(t.callbacks.beforeNodeRemoved(e)===false)return;e.remove();t.callbacks.afterNodeRemoved(e)}function w(e,t){return!e.deadIds.has(t)}function T(e,t,n){let l=e.idMap.get(n)||o;return l.has(t)}function x(e,t){let n=e.idMap.get(t)||o;for(const l of n){e.deadIds.add(l)}}function E(e,t,n){let l=e.idMap.get(t)||o;let r=0;for(const i of l){if(w(e,i)&&T(e,i,n)){++r}}return r}function H(e,n){let l=e.parentElement;let t=e.querySelectorAll("[id]");for(const r of t){let t=r;while(t!==l&&t!=null){let e=n.get(t);if(e==null){e=new Set;n.set(t,e)}e.add(r.id);t=t.parentElement}}}function L(e,t){let n=new Map;H(e,n);H(t,n);return n}return{morph:e}}()});htmx.defineExtension("morph",{isInlineSwap:function(e){return e==="morph"},handleSwap:function(e,t,n){if(e==="morph"||e==="morph:outerHTML"){return Idiomorph.morph(t,n.children)}else if(e==="morph:innerHTML"){return Idiomorph.morph(t,n.children,{morphStyle:"innerHTML"})}}});