diff options
Diffstat (limited to 'desk/lib/kaji-min.js')
-rw-r--r-- | desk/lib/kaji-min.js | 1 |
1 files changed, 1 insertions, 0 deletions
diff --git a/desk/lib/kaji-min.js b/desk/lib/kaji-min.js new file mode 100644 index 0000000..f683846 --- /dev/null +++ b/desk/lib/kaji-min.js @@ -0,0 +1 @@ +const t=`${Date.now()}${Math.floor(100*Math.random())}`,e=`/~/channel/${`kaji-${t}`}`;let n=0;const o=new DOMParser;function i(t,e){t.innerHTML=e,r(t)}function r(t){const e=t instanceof HTMLElement;if(!t)return;const n=t.querySelectorAll("[kaji]");e&&t.getAttribute("kaji")&&h(t),n.forEach((t=>h(t)))}async function a(t){console.log({channelPath:e,bodies:t},"put body");const n=await fetch(e,{method:"PUT",body:JSON.stringify(t)});return await n}function subscribe(t){document.addEventListener("DOMContentLoaded",(()=>{a([P(window.ship,window.app,t)])}))}function c(t){const n=JSON.parse(t.data);if("diff"!==n.response)return;!async function(t){await fetch(e,{method:"PUT",body:$(t)})}(n.id);const i=n.json;if(i&&0!==i?.length)for(let t of i)if(console.log(t,"sse command"),"refresh"in t&&window.location.reload(!0),"redi"in t&&d(t.redi),"focus"in t&&s(t.focus),"scroll"in t&&l(t.scroll),"url"in t&&u(t.url),"custom"in t)f(t.custom);else{const e=t[Object.keys(t)[0]].manx,n=o.parseFromString(e,"text/html").body.firstChild;"swap"in t&&N(null,n,t.swap.sel,t.swap.inner),"add"in t&&E(null,n,t.add.container,t.add.where),"modal"in t&&p(n),"alert"in t&&m(n,t.alert.duration)}}function s(t){const e=document.querySelector(t);e&&e.focus()}function l(t){const e=document.querySelector(t);e&&e.scrollIntoView()}function u(t){window.history.pushState(null,null,t)}function d(t){console.log(t,"redirecting"),window.location.href=t}function f(t){const e=new CustomEvent("kaji-fact",{detail:t});document.dispatchEvent(e)}function p(t){const e=document.createElement("div");e.id="kaji-modal-bg";const n=document.createElement("div");n.id="kaji-modal-fg",i(n,t),e.appendChild(n),document.body.appendChild(e),window.onclick=function(t){t.target==e&&e.remove()}}function m(t,e){const n=document.createElement("div");n.id="kaji-alert",n.appendChild(t),document.body.appendChild(n),setTimeout((()=>document.body.removeChild(n)),e)}function g(t,e){for(let n of t.attributes){const t=n.name,o=n.value;if("class"===t||"id"===t)continue;const i=e.getAttribute(t);o&&!i&&e.setAttribute(t,o)}}function h(n){const c=n.getAttribute("kaji");if("mobile"===c)!async function(t){const e=t.getAttribute("path");if(!(window.innerWidth<768))return;const n=await fetch(e),i=await n.text(),a=o.parseFromString(i,"text/html").body.firstChild;r(a),t.replaceWith(a)}(n);else if("fetch"===c)!async function(t){const e=await fetch(t.getAttribute("src")),n=await e.text(),i=o.parseFromString(n,"text/html").body.firstChild;t.replaceWith(i)}(n);else if("iscroll"===c)!function(t){let e=new IntersectionObserver(((e,n)=>{e.forEach((e=>{if(e.isIntersecting){const o=function(t){const e=document.createElement("img");return e.src="spinner.svg",e.classList.add("iscroll-spinner"),t.replaceWith(e),e}(e.target);y(C(t)).then((n=>{if(!n)return;const i=e.target.getAttribute("cont");E(t,n,i,v(t)),A({swapMode:"add",container:i,element:n}),o.remove()})),n.unobserve(e.target)}}))}));e.observe(t)}(n);else if("search"===c)!function(t){const e=t.getAttribute("bounce"),n=Number(e||0);if(Number.isNaN(n))return w("bounce setting NaN");const o=e=>async function(t,e){console.log(t,"scry event"),t.preventDefault();const n=C(e);console.log(n,"url"),k(e,!0);const o=e.getAttribute("swap"),i=await y(n);if(!i)return;const r=e.getAttribute("show-params");r&&S(n);let a;if("swap"===o){j(e);const t=e.getAttribute("targ"),n=e.getAttribute("where");a=N(e,i,t,!n||"inner"===n)}else if("add"===o){const t=e.getAttribute("cont");a=await E(e,i,t,v(e))}k(e,!1),a&&A({swapMode:o,container:a})}(e,t),i=((t,e)=>{let n;return(...o)=>{clearTimeout(n),n=setTimeout((()=>t(...o)),e)}})(o,n);t.addEventListener("keyup",i)}(n);else if("clickaway"===c)!function(t){window.onclick=function(e){t.contains(e.target)||(t.hidden=!0)}}(n);else{const o=n.getAttribute("trigger")||function(t){const e=t.tagName.toLowerCase();if("form"===e)return"submit";return"input"===e||"textarea"===e||"select"===e?"change":"click"}(n);n.addEventListener(o,(o=>{!function(n,o,r){"navi"===r&&async function(t,e){t.preventDefault();const n=e.getAttribute("path"),o=L(t.target),r=n||o;if(!r)throw new Error("no fetch path found");const a=await fetch(r),c=await a.text(),s=t.target.closest("#kaji-modal-fg");i(s||document.querySelector("body"),c)}(n,o);"modal"===r&&async function(t,e){const n=e.getAttribute("path"),o=L(t.target),i=n||o;if(!i)throw new Error("no fetch path found");const r=await fetch(i);p(await r.text())}(n,o);"toggle"===r&&function(t,e){t.stopPropagation();const n=t.target.getAttribute("targ");if(!n)return;const o=n.split("/").map((e=>b(t.target,e)));o.forEach((e=>{t.target.getAttribute("modal")&&function(t){const e=document.createElement("div");e.id="kaji-modal-bg";const n=document.createElement("div");n.id="kaji-modal-fg",n.appendChild(t),e.appendChild(n),document.body.appendChild(e),window.onclick=function(t){t.target==e&&e.remove()}}(e),e&&(e.hidden=!e.hidden)}))}(n);"destroy"===r&&function(t,e){t.stopPropagation();const n=t.target.getAttribute("targ");if(!n)return;const o=n.split("/").map((e=>b(t.target,e)));for(let t of o)t&&t.parentElement.removeChild(t)}(n);"scry"===r&&async function(t){console.log(t,"scry event");const e=t.target;!function(t){let e=t.parentElement;for(;e;)g(e,t),e=e.parentElement}(e);const n=e.getAttribute("path")||window.location.href,o=new URLSearchParams(window.location.search),[i,r]=n.split("?");if(r){const t=new URLSearchParams(r);for(let[e,n]of t.entries())o.set(e,n)}const a=e.getAttribute("name");a&&o.set(a,e.value);const c=`${n}?${o.toString()}`;t.preventDefault();const s=e.getAttribute("swap")||"swap";k(e,!0);const l=await y(c);if(console.log(l,"nel"),!l)return;const u=e.getAttribute("show-params");u&&S(c);let d;if("swap"===s){j(e);const t=e.getAttribute("targ"),n=e.getAttribute("where");d=N(e,l,t,!n||"inner"===n)}else if("add"===s){const t=e.getAttribute("cont");d=await E(e,l,t,v(e))}k(e,!1),d&&A({swapMode:s,container:d})}(n);"watch"===r&&async function(t,n){const o=n.getAttribute("ship"),i=n.getAttribute("app"),r=n.getAttribute("path"),a=P(o,i,r);await fetch(e,{method:"PUT",body:a})}(0,o);"poke"===r&&async function(e,n){e.preventDefault();n.getAttribute("ship"),n.getAttribute("app");const o=n.getAttribute("mark")||"kaji",i=n.getAttribute("json");console.log(n.params,"poke params?");const r=e.currentTarget,c=r.getAttribute("action"),s=r.getAttribute("name"),l=r.getAttribute("payload"),u=i||("FORM"===r.tagName?{action:c,...x(r)}:c?{action:c,[s]:l}:{action:s,[s]:l});e.stopPropagation(),"FORM"===r.tagName&&function(t){if(!t.getAttribute("wipe"))return;const e=[...t.getElementsByTagName("input"),...t.getElementsByTagName("textarea")];for(let t of e)"text"===t.type.toLowerCase()&&(t.value=""),"TEXTAREA"===t.tagName&&(t.value="")}(r);const d={...u,origin:window.location.pathname,tab:t};console.log(d,"poke params");const f=[T(window.ship,window.app,o,d)];await a(f)}(n,o);"thread"===r&&async function(t,e){console.log(e.params,"thread params?");const n=e.getAttribute("desk"),o=e.getAttribute("thread"),i=e.getAttribute("input-mark")||"kaji",r=e.getAttribute("output-mark")||"kaji",a=e.getAttribute("noun");fetch(`/spider/${n}/${i}/${o}/${r}.kaji`,{headers:{"Content-type":"application/x-urb-jam"},method:"POST",body:a})}(0,o)}(o,n,c)}))}}function w(t){console.log(t)}function b(t,e){if(console.log(t,"looking above"),console.log(e,"sel"),!e)return console.log(e,"selector not found"),null;if("#"===e[0])return document.getElementById(e.slice(1));if(!t)return null;const n=t.querySelector(e);return n||b(t.parentElement,e)}async function y(t){console.log(t,"router scry");const e=await fetch(t),n=await e.text(),i=o.parseFromString(n,"text/html").body.firstChild;return function(t,e){if(!e)return console.log(t,"No element found scrying"),!0;if("kaji-alert"===e.id){const t=e.getAttribute("dur");return m(e,Number(t)),!0}return!!e.getAttribute("kaji-error")}(n,i)?null:(r(i),function(t){const e=t.querySelectorAll("script");for(let t of e){const e=document.createElement("script");t.src?e.src=t.src:t.textContent&&(e.textContent=t.textContent),document.body.appendChild(e)}}(i),i)}function A(t){const e=new CustomEvent("kaji-scry",{detail:t});document.dispatchEvent(e)}function v(t){const e=t.getAttribute("where");if(!e)return{bottom:null};if("bottom"===e)return{bottom:null};if("top"===e)return{top:null};if("before"===e){const e=t.getAttribute("sibling");return e?{before:e}:null}return null}function E(t,e,n,o){o||w("no where given to addEls");const i=t?b(t,n):document.querySelector(n);for(i||w("no container given to addEls");e.firstChild;)if("top"in o)i.prepend(e.firstChild);else if("bottom"in o)i.appendChild(e.firstChild);else if("before"in o){const t=document.querySelector(o.before);t||w("sibling at addEls doesn't exist"),i.insertBefore(e.firstChild,t)}}function k(t,e){const n=t.getAttribute("indicator"),o=document.querySelector(n);o&&(o.style.display=e?"block":"none")}function C(t){!function(t){const e=function(t){const e=t.tagName.toLowerCase();return"input"===e||"select"===e||"option"===e||"textarea"===e||"button"===e}(t);if(!e)return;let n,o=t.parentElement;for(;!n&&o;)g(o,t),"form"===o.tagName.toLowerCase()?n=o:o=o.parentElement;n&&(t.params=x(n))}(t);const e=t.getAttribute("path")||window.location.href,n=new URLSearchParams(window.location.search),[o,i]=e.split("?");if(i){const t=new URLSearchParams(i);for(let[e,o]of t.entries())n.set(e,o)}const r=t.getAttribute("name"),a=t.params?t.params:"FORM"===t.tagName?x(t):{},c=new URLSearchParams(a),s=new URLSearchParams;for(let[t,e]of n.entries())s.set(t,e);r&&t.value&&s.set(r,t.value);for(let[t,e]of c.entries())s.set(t,e);return`${o}?${s.toString()}`}function S(t){const e=t.split("?")[1]||"";u(`${window.location.pathname}?${e}`)}function L(t){const e=t.closest("a");return e?e.href:null}function j(t){if(!t.classList.contains("tab"))return;t.parentElement;if(!t.classList.contains("active")){t.parentElement.querySelector(".active").classList.remove("active"),t.classList.add("active")}}function N(t,e,n,o){const i=t?b(t,n):document.querySelector(n);if(i){if(o)for(i.innerHTML="";e.firstChild;)i.appendChild(e.firstChild);else i.parentElement.replaceChild(e,i);return i}console.log("no container",n)}function x(t){const e=new FormData(t);return Object.fromEntries(e)}function P(t,e,o){return n++,{id:n,action:"subscribe",ship:t.slice(1),app:e,path:o}}function T(t,e,o,i){return n++,{id:n,action:"poke",ship:t.slice(1),app:e,mark:o,json:i}}function $(t){return n++,JSON.stringify([{id:n,action:"ack","event-id":t}])}addEventListener("DOMContentLoaded",(async()=>{await async function(){console.log(window.ship,"opening channel");const n=P(window.ship,window.app,window.liveUI),o=`/sse/${t}`,i=P(window.ship,window.app,o),r=T(window.ship,"hood","helm-hi","hai"),a=JSON.stringify([n,i,r]);await fetch(e,{method:"PUT",body:a}),eventSource=new EventSource(e),eventSource.addEventListener("message",c)}(),r(document),function(){const t=new URLSearchParams(window.location.search).get("goto");if(t){const e=document.getElementById(t);e?e.scrollIntoView({block:"center"}):console.log(t,"target not found")}}()}));
\ No newline at end of file |