-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshadowQuery.min.mjs
2 lines (2 loc) · 8.25 KB
/
shadowQuery.min.mjs
1
2
/* eslint-disable */
const aExp=/^(attr:)|@/,pExp=/^(prop:)|\./,tExp=/^((text:)|§)$/;export class ShadowQuery extends Array{constructor(e,t){if(0===e)return super(0);let o;o=Array.isArray(e)?e:"string"==typeof e?[shadowQuery.template(e)]:e instanceof ShadowQuery?e:e instanceof NodeList||e instanceof HTMLCollection?Array.from(e):[e.shadowRoot||e],t&&(o=find(o,t)),super(...o)}access(e,t){const[,o,n]=e.match(/^(.)(.*)$/),r=arguments.length>1;switch(o){case"@":return r?this.attr(n,t):this.map(e=>e.getAttribute(n));case".":return r?this.prop(n,t):this.map(e=>e[n]);case"§":return r?this.text(t):this.map(e=>e.nodeType===Node.TEXT_NODE&&e.nodeValue||textNode(e)&&textNode(e).nodeValue);default:throw new Error('Key in calls to "access" must start with "@", ".", or "§"')}}addClass(e){for(const t of this)t.classList.add(e);return this}after(e){for(const t of this){const o=t.parentNode;t===o.lastChild?toNodes(o,e,e=>o.appendChild(e)):toNodes(o,e,e=>o.insertBefore(e,t.nextSibling))}return this}append(e){for(const t of this)toNodes(t,e,e=>t.appendChild(e));return this}attr(e,t){if(1===arguments.length)return this[0]&&this[0].getAttribute(e);if(void 0===t||!1===t||null===t)for(const t of this)t.removeAttribute(e);else{"string"!=typeof t&&(t=JSON.stringify(t));for(const o of this)o.setAttribute(e,t)}return this}before(e){for(const t of this)toNodes(t.parentNode,e,e=>t.parentNode.insertBefore(e,t));return this}call(e,...t){return this.map(o=>o[e](...t))}ccall(e,...t){for(const o of this)o[e](...t);return this}emit(e,t){if(e instanceof Event&&t)throw new Error("EITHER pass an Event instance OR optionally pass customEventInit");"string"==typeof e&&(e=new CustomEvent(e,t||{}));for(const t of this)t.dispatchEvent(e);return this}hasClass(e){for(const t of this)if(t.classList.contains(e))return!0;return!1}off(e,t){for(const o of this)if(aExp.test(e))try{o[obsKey(attrFilter(e))][t].disconnect()}catch(e){}else if(pExp.test(e))offProp(o,e,t);else if(tExp.test(e))try{o[obsKey(textFilter())][t].disconnect()}catch(e){}else o.removeEventListener(e,t._shadowQueryNoSelf||t);return this}on(e,t,o){const n=3===arguments.length&&"noSelf"===t;2===arguments.length&&(o=t);for(const t of this)tExp.test(e)?observer(t,n,o,textFilter()):aExp.test(e)?observer(t,n,o,attrFilter(e)):pExp.test(e)?onProp(t,e,n,o):t.addEventListener(e,n?noSelf(o):o);return this}once(e,t){for(const o of this)tExp.test(e)?onceObserver(o,t,textFilter()):aExp.test(e)?onceObserver(o,t,attrFilter(e)):pExp.test(e)?onceProp(o,e,t):o.addEventListener(e,t,{once:!0});return this}prepend(e){for(const t of this)t.firstChild?toNodes(t,e,e=>t.insertBefore(e,t.firstChild)):toNodes(t,e,e=>t.appendChild(e));return this}prop(e,t){if(1===arguments.length)return this[0]&&this[0][e];for(const o of this)o[e]=t;return this}query(e){return new ShadowQuery(find(this,e))}remove(){console.warn('remove method is deprecated and will be removed soon, use call("remove") instead (and optionally use a polyfill for IE)');for(const e of this)e.parentElement.removeChild(e);return this}removeClass(e){for(const t of this)t.classList.remove(e);return this}shadow(e,t={mode:"open"}){for(const o of this){const n=o.attachShadow(t);e&&n.appendChild($.template(e))}return this}text(e){if(!arguments.length)return this[0]&&this[0].firstChild&&(this[0].firstChild.nodeType===Node.TEXT_NODE&&this[0].firstChild.nodeValue||textNode(this[0])&&textNode(this[0]).nodeValue);for(const t of this)t.firstChild&&t.firstChild.nodeType===Node.TEXT_NODE?t.firstChild.nodeValue=e:textNode(t,!0).nodeValue=e;return this}toggleClass(e,t){if(1===arguments.length)for(const t of this)t.classList.toggle(e);else{t=!!t;for(const o of this)o.classList.toggle(e,t)}return this}when(e){return new Promise(t=>this.once(e,t))}};export function shadowQuery(e,t){return new ShadowQuery(e,t)};export default shadowQuery;const $=shadowQuery;function toNodes(e,t,o){if(t instanceof HTMLTemplateElement)return o(t.content.cloneNode(!0));if(t instanceof Node)return o(t);if(t.constructor===Object&&(t=shadowQuery.template(t)),"function"==typeof t)return isProcessDynNodes(t)?t(e,o):o(t());t instanceof Array||(t=new ShadowQuery(t));for(const e of t)o(e)}function find(e,t){const o=[];for(let n=0;n<e.length;n++)if(/:host/.test(t))for(let r of t.split(","))if(/^\s*:host\s*/.test(r)){r=r.replace(/^\s*:host\s*/,"");let t=e[n].host||e[n];"string"==typeof t&&(t=e[n]),/[^\s]/.test(r)?o.push(...t.querySelectorAll(r)):o.push(t)}else o.push(...e[n].querySelectorAll(r));else e[n].querySelectorAll&&o.push(...e[n].querySelectorAll(t));return o}function textNode(e,t){for(let t=0;t<e.childNodes.length;t++)if(e.childNodes[t].nodeType===Node.TEXT_NODE)return e.childNodes[t];if(t)return console.warn&&console.warn('ShadowQuery is creating a text node. For performance reason you should put the text node into your DOM from the start (e.g. as an empty space or zero width space "​")'),e.appendChild(document.createTextNode("")),e.lastChild}function attrFilter(e){return{attributes:!0,attributeFilter:[e.replace(aExp,"")]}}function textFilter(){return{characterData:!0,subtree:!0}}function onceObserver(e,t,o){const n=observer(e,!1,function(...e){n.disconnect(),t(...e)},o,t)}function observer(e,t,o,n,r=o){const s=new MutationObserver(t?noSelf(o,!0):o);return s.observe(e,n),e[obsKey(n)]?e[obsKey(n)][r]=s:e[obsKey(n)]={[r]:s},s}function obsKey(e){return`_shadowQueryObserver${JSON.stringify(e)}`}function offProp(e,t,o){const n=t.replace(pExp,""),r=propKey(n);if(!e[r])return;const s=e[r].listener,i=s.indexOf(o._shadowQueryNoSelf||o);if(-1!==i&&s.splice(i,1),s.length)return;const c=e[r].value;delete e[r],delete e[n],e[n]=c}function onceProp(e,t,o){onProp(e,t,!1,function _onceListener(...n){offProp(e,t,_onceListener);o(...n)})}function onProp(e,t,o,n){const r=t.replace(pExp,""),s=propKey(r);let i;e[s]?i=!0:e[s]={listener:[]},e[s].listener.push(o?noSelf(n):n),i||(e instanceof HTMLInputElement&&("value"===r||"checked"===r)?onInputValueChange(e,r,s,t):e instanceof HTMLSelectElement&&"value"===r&&onInputValueChange(e,r,s,t),onPropertyChange(e,r,s))}function onInputValueChange(e,t,o,n){const r=`${o}-Listener`;if(!e[r])switch(t){case"value":e[r]=!0,e.addEventListener("change",()=>{e[o].value=e.value,tell(e,e[o],n)});break;case"checked":e[r]=!0,e.addEventListener("change",()=>{e[o].checked=e.checked,tell(e,e[o],n)})}}function onPropertyChange(e,t,o,n){(Object.prototype.hasOwnProperty.call(e,t)||void 0!==e[t])&&(e[o].value=e[t],delete e[t]);const r=htmlElementProperty(e,t);Object.defineProperty(e,t,{get:function(){return r?r.get.call(this):e[o].value},set:function(t){return r?(r.set.call(this,t),e[o].value=r.get.call(this)):e[o].value=t,tell(e,e[o],n),t},enumerable:!0,configurable:!0})}function htmlElementProperty(e,t){try{for(;e;){e=Object.getPrototypeOf(e);const o=Object.getOwnPropertyDescriptor(e,t);if(o)return o}}catch(e){return}}function propKey(e){return`_shadowQueryProp${e}`}function tell(e,t,o){for(const n of t.listener)n(t.value,e,o)}function noSelf(e,t){let o;return e._shadowQueryNoSelf=function(){o||(o=!0,e(),t?setTimeout(function(){o=!1}):o=!1)}}const templates={};let processDynNodesRef;function dynTemplate(e){const{array:t=[void 0],chunks:o,id:n="default",update:r}=e,s=e.template,i=`_shadowQueryChildArrayDynNode${n}`,c=`_shadowQueryChildArrayTimeout${n}`;function processDynNodes(n,l){n[c]&&(clearTimeout(n[c]),delete n[c]),n[i]||(n[i]=[]);const a=n[i];if(Object.prototype.hasOwnProperty.call(e,"condition")&&!e.condition)return removeNodes(0);function removeNodes(e){for(let t=e;t<a.length;t++)for(let e=0;e<a[t].length;e++)n.removeChild(a[t][e]);a.splice(e)}function iterDynNodeChunk(r){r>=t.length?e.done&&e.done():!o||r%o?iterNodeArray(r):n[c]=setTimeout(()=>iterNodeArray(r))}function iterNodeArray(e){let o=n[i][e];if(!o){const e=$.template(s);o=$(e.childNodes),l(e),n[i].push(o)}r&&r($(o),t[e],e),delete n[c],iterDynNodeChunk(++e)}a.length>t.length&&removeNodes(t.length),iterDynNodeChunk(0)}return processDynNodesRef=processDynNodes.prototype.constructor,processDynNodes}function isProcessDynNodes(e){return processDynNodesRef&&e&&e.prototype&&e.prototype.constructor===processDynNodesRef}shadowQuery.template=function(e){if("string"==typeof e){if(templates[e])return templates[e].content.cloneNode(!0);const t=document.createElement("template");return t.innerHTML=e,templates[e]=t,t.content.cloneNode(!0)}return e instanceof HTMLTemplateElement?e.content.cloneNode(!0):dynTemplate(e)};