UNPKG

agile-core

Version:

Agile Core - A powerful component based micro front-end framework

2 lines (1 loc) 13.2 kB
"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const e=require("vue"),t=require("ts-deepmerge");var n,o;const r=function(){try{return __AUI_CONFIG__}catch(e){return{tagPrefix:"aui"}}}().tagPrefix||"aui",i="data-v-aui",s="auix",l=Symbol("shadow"),a=class t extends(o=HTMLElement,n=l,o){constructor(e){super(),this.__hasVue__=!1,this[n]={needHook:!1,slot:{}},this[t.shadowSymbol].options=e||{},this.$initAttrs()}connectedCallback(){this.$needInit()&&(this.$initVue(),this.$needHook(!0))}set innerHTML(e){this.$needHook()?this.$innerHTML("default",e):Reflect.set(HTMLElement.prototype,"innerHTML",e,this)}get innerHTML(){return this.$needHook()?this.$innerHTML("default"):Reflect.get(HTMLElement.prototype,"innerHTML",this)||""}set textContent(e){this.$needHook()?this.$textContent("default",e):Reflect.set(HTMLElement.prototype,"innerHTML",e,this)}get textContent(){return this.$needHook()?this.$textContent("default"):Reflect.get(HTMLElement.prototype,"innerHTML",this)||""}set innerText(e){this.$ignoreLog("innerText"),Reflect.set(HTMLElement.prototype,"innerText",e,this)}set value(e){let n=e;const o=this[t.shadowSymbol].value;let r;this.$isArrayValue()?(n=t.$anyToArray(e),r=JSON.stringify(n)):r=n.toString(),o!==r&&(this[t.shadowSymbol].value=r,this[t.shadowSymbol].el&&this.$attrChange("value"))}get value(){const e=this[t.shadowSymbol].value;return this.$isArrayValue()&&"string"==typeof e?JSON.parse(e):e}prepend(e){if(!this.$needHook())return HTMLElement.prototype.prepend.call(this,e);this.$prepend("default",e)}appendChild(e){return this.$needHook()?this.$appendChild("default",e):HTMLElement.prototype.appendChild.call(this,e)}insertBefore(e,t){return this.$needHook()?this.$insertBefore("default",e,t):HTMLElement.prototype.insertBefore.call(this,e,t)}hasAttribute(e){const n=this[t.shadowSymbol].el;return n?n.hasAttribute(e):HTMLElement.prototype.hasAttribute.call(this,e)}setAttribute(e,n){n=n.toString();const o=this[t.shadowSymbol].el;if(o){if(o.getAttribute(e)===n)return;o.setAttribute(e,n)}this.$isShowProp(c.vueAttrName(e))&&HTMLElement.prototype.setAttribute.call(this,e,n),"value"===e&&o?this.value=o.getAttribute(e):this.$attrChange(e)}getAttribute(e){const n=this[t.shadowSymbol].el;return n?n.getAttribute(e):super.getAttribute(e)}removeAttribute(e){const n=this[t.shadowSymbol].el;n&&n.removeAttribute(e),this.$isShowProp(c.vueAttrName(e))&&HTMLElement.prototype.removeAttribute.call(this,e),this.$attrChange(e)}attributeChangedCallback(e,n,o){const r=this[t.shadowSymbol].el;r&&n!==o&&(this.$isShowProp(c.vueAttrName(e))||HTMLElement.prototype.removeAttribute.call(this,e),r.setAttribute(e,o||""),"value"!==e&&this.$attrChange(e))}static $anyToArray(e){let t=[];if("string"==typeof e){try{const t=JSON.parse(e);if(t instanceof Array)return t}catch(n){}t=e.split(",").filter(e=>!!e&&e.trim())}else e instanceof Array&&(t=e);return t}$resetShadowSymbol(){this[t.shadowSymbol]={needHook:!1,slot:{}}}$customEvent(e,t){this.dispatchEvent(new CustomEvent(e,Object.assign({bubbles:!1,composed:!1,cancelable:!0},t||{})))}$forceUpdate(){let e=null;const n=this[t.shadowSymbol].slot;for(const t in n){const o=this.$slotCollection(n[t]);e||(e={}),e[t]=o}e&&(this[t.shadowSymbol].slotCollection=e),this.$customEvent("aui-force-update")}$slotCollection(e){var t;const{start:n,end:o}=e,r=[];let i;for(;n&&(i=n.nextSibling)&&i!==o;)null==(t=n.parentNode)||t.removeChild(i),r.push(i);return r}$slotReplace(){const e=this[t.shadowSymbol].slotCollection;if(e){delete this[t.shadowSymbol].slotCollection;for(const t in e)this.querySelector(`aui-slot[name="${t}"]`).$replace(this),this.$replaceChildren(t,e[t])}}$textContent(e,...t){if(0===t.length){const t=[],{start:n,end:o}=this.$slot(e)||{};let r=n;for(;r&&(r=r.nextSibling)&&r!==o;)t.push(r.textContent);return t.join("")}return this.$hasSlot()?(this.$replaceChildren(e,[document.createTextNode(t[0]||"")]),""):""}$innerHTML(e,...t){if(0===t.length){const t=[],{start:n,end:o}=this.$slot(e)||{};let r=n;for(;r&&(r=r.nextSibling)&&r!==o;)if(r.outerHTML)t.push(r.outerHTML);else{let e=r.textContent;r instanceof Comment&&(e=`\x3c!--${e}--\x3e`),t.push(e)}return t.join("")}{if(!this.$hasSlot())return"";const n=document.createElement("div");n.innerHTML=t[0]||"";const o=this.$moveChildren(n);this.$replaceChildren(e,o.default)}return""}$prepend(e,t){var n;const{start:o}=this.$slot(e)||{};null==(n=null==o?void 0:o.parentNode)||n.insertBefore(t,o.nextSibling)}$appendChild(e,t){var n;const{end:o}=this.$slot(e)||{};return null==(n=null==o?void 0:o.parentNode)||n.insertBefore(t,o),t}$insertBefore(e,t,n){const{start:o}=this.$slot(e)||{},r=(null==o?void 0:o.parentNode)||this;return n?r===this?HTMLElement.prototype.insertBefore.call(this,t,n):r.insertBefore(t,n):this.appendChild(t),t}$childNodes(e){const t=[],{start:n,end:o}=this.$slot(e)||{};let r=n;for(;r&&(r=r.nextSibling)&&r!==o;)t.push(r);return t}$children(e){const t=[],{start:n,end:o}=this.$slot(e)||{};let r=n;for(;r&&(r=r.nextSibling)&&r!==o;)r instanceof HTMLElement&&t.push(r);return t}$replaceChildren(e,t){var n,o;const{start:r,end:i}=this.$slot(e)||{};let s=null;for(;r&&(s=r.nextSibling)&&s!==i;)null==(n=s.parentNode)||n.removeChild(s);if(i)for(;s=t.shift();)null==(o=i.parentNode)||o.insertBefore(s,i)}$slot(e,n){if(e=e||"default",!n)return this[t.shadowSymbol].slot[e];this[t.shadowSymbol].slot[e]=n}$hasSlot(){return Object.keys(this[t.shadowSymbol].slot).length>0}$needHook(e){return"boolean"==typeof e&&(this[t.shadowSymbol].needHook=e),this[t.shadowSymbol].needHook}$ignoreLog(e){}$moveChildren(e){var t;const n=(e=e||this).childNodes,o={default:[]};for(;n.length>0;){const r=n[0];if(null==(t=r.parentNode)||t.removeChild(r),h.$isForAuiSlot(r)){const t=r.getAttribute("for")||"default";o[t]=o[t]||[];const n=Array.prototype.slice.call(r.childNodes);h.prototype.$bind.call(r,e),o[t].push(...n)}else o.default.push(r)}return o}$isArrayValue(){var e;return(null==(e=this[t.shadowSymbol].options)?void 0:e.valueType)===Array}$attrChange(e){if("value"===e){const e=this.value;HTMLElement.prototype.hasAttribute.call(this,i)?this.$call("$emit","update:value",e):this.$customEvent("input",{bubbles:!0,composed:!0})}this.$customEvent("aui-attr-change",{bubbles:!0,composed:!0,detail:{attributeName:c.vueAttrName(e)}})}$initAttrs(){const e=this.attributes,n=document.createElement("div");for(let t=0;t<e.length;t++){const o=e[t];n.setAttribute(o.name,o.value),this.$isShowProp(c.vueAttrName(o.name))||HTMLElement.prototype.removeAttribute.call(this,o.name),"value"===o.name&&(this.value=o.value)}Object.defineProperty(this,"attributes",{get value(){return n.attributes},writable:!1}),this[t.shadowSymbol].el=n}$isShowProp(e){if(c.isVueAttr(e))return!0;const t=this.$getProp(e);return null==t?void 0:t.show}$getProp(e){var t;const n=c.cache.getAuiVueDefined(this.tagName);return((null==(t=null==n?void 0:n.origin)?void 0:t.props)||{})[e]}$initVue(){if(this.__hasVue__)return;this.__hasVue__=!0;const t=this.tagName.toLowerCase();c.cache.getAuiVueDefined(t)&&e.createApp(c.createVueComponent(t,this)).mount(this)}$needInit(){return!HTMLElement.prototype.hasAttribute.call(this,i)}$bindVue(e){var n,o;if(this[t.shadowSymbol].ref)return;this[t.shadowSymbol].ref=e,this.__hasVue__=!0,this.$customEvent("aui-init",{bubbles:!1,detail:{code:0}});const r=null==(o=null==(n=c.cache.getAuiVueDefined(this.tagName))?void 0:n.origin)?void 0:o.auiInit;r&&r.call(e)}$call(e,...n){const o=this[t.shadowSymbol].ref;return c.$call(o,e,...n)}};a.shadowSymbol=l;let u=a;class h extends HTMLElement{constructor(){super()}connectedCallback(){this.style.display="none"}set textContent(e){var t;this._name_?null==(t=this._el_)||t.$textContent(this._name_,e):Reflect.set(HTMLElement.prototype,"textContent",e,this)}set innerHTML(e){var t;this._name_?null==(t=this._el_)||t.$innerHTML(this._name_,e):Reflect.set(HTMLElement.prototype,"innerHTML",e,this)}prepend(e){var t;this._name_?null==(t=this._el_)||t.$prepend(this._name_,e):HTMLElement.prototype.prepend.call(this,e)}appendChild(e){var t;return this._name_?(null==(t=this._el_)||t.$appendChild(this._name_,e),e):HTMLElement.prototype.appendChild.call(this,e)}static $isAuiSlot(e){return!(!e||e.nodeType!==Node.ELEMENT_NODE)&&"aui-slot"===e.tagName.toLowerCase()}static $isForAuiSlot(e){return!(!h.$isAuiSlot(e)||!e.hasAttribute("for"))}$bind(e){this._el_=e,this._name_=this.getAttribute("for")||"default"}$replace(e){const t=this.getAttribute("name")||"default",n=document.createComment(`aui-slot-${t}-start`),o=document.createComment(`aui-slot-${t}-end`);e.$slot(t,{start:n,end:o}),this.$replaceWith(n,o)}$replaceWith(...e){var t;e.forEach(e=>{var t;null==(t=this.parentNode)||t.insertBefore(e,this)}),null==(t=this.parentNode)||t.removeChild(this)}}const c={init:()=>(c.defineSlot(),c),defineSlot(){c.defineCustomElement("aui-slot",h)},$call(e,t,...n){if(!e)return new Error("not init");if(!t)return e;if("function"==typeof e[t])return e[t].call(e,...n);const o=t.split(".");let r=e,i=null;const s=o.pop()||"";for(;i=o.shift();){if(!Object.prototype.hasOwnProperty.call(r,i))return new Error("no match name");r=r[i]}return 0===n.length?r[s]:(r[s]=n[0],null)},formatName:e=>e?(e=e.replace(/[A-Z]/g,function(e){return"-"+e.toLowerCase()}),r+e):"",cache:{defined:{},getAuiVueDefined:e=>(e=e.toLowerCase(),c.cache.defined[e]),getAuiPropNames(e){var t;const n=c.cache.getAuiVueDefined(e),o=(null==(t=null==n?void 0:n.origin)?void 0:t.props)||{},r=[];for(const i in o)r.push(c.auiAttrName(i));return-1===r.indexOf(s)&&r.push(s),r},addHooks(e,t){e=e.toLowerCase();const n=c.cache.getAuiVueDefined(e);n?(n.hooks=n.hooks||[],n.hooks.push(t)):c.cache.defined[e]={hooks:[t]}}},createExtendComp:t=>{var n,o;return e.defineComponent({props:{[s]:{type:String},value:{type:(null==(o=null==(n=t.props)?void 0:n.value)?void 0:o.type)||[String,Number,Array]}},methods:{$anyToArray:u.$anyToArray,$value(...e){const t=this.$aui();if(0===e.length)return null==t?void 0:t.value;t&&(t.value=e[0])},$call(e,...t){const n=this.$aui();return n?c.$call(n,e,...t):null},$aui(){var e;return null==(e=this.$el)?void 0:e.parentNode}},data:()=>({})})},patchDefaultProps(e){var t,n;const o=null==(n=null==(t=e.props)?void 0:t.value)?void 0:n.type;return e.mixins||(e.mixins=[]),e.mixins.push(c.createExtendComp(e)),o},defineAuiComponent(e,t,n){var o,r;const i=c.patchDefaultProps(n);if(null==(o=c.cache.defined[e])?void 0:o.origin)c.cache.defined[e].origin=n;else{class o extends u{constructor(){super({valueType:i})}static get observedAttributes(){return c.cache.getAuiPropNames(e)}}c.cache.defined[e]={aui:o,vue:t,origin:n,hooks:(null==(r=c.cache.defined[e])?void 0:r.hooks)||[]},c.defineCustomElement(e,o)}},defineCustomElement(e,t){try{customElements.define(e,t)}catch(n){}},sameComponent(e,t){if(!Object.prototype.hasOwnProperty.call(t,"__hmrId"))return!1;const n=c.cache.getAuiVueDefined(e),o=null==n?void 0:n.origin;return!!o&&t.__hmrId===o.__hmrId},notifyComponentUpdate(e){document.querySelectorAll(e).forEach(e=>{e.$forceUpdate()})},define(e,t=!0){const n=c.formatName(e.name);if(!n)return e;const o=c.cache.getAuiVueDefined(n);if(o&&o.vue)return(t||c.sameComponent(n,e))&&(c.defineAuiComponent(n,o.vue,e),c.notifyComponentUpdate(n)),o.vue;const r=c.createVueComponent(n);return c.defineAuiComponent(n,r,e),r},createTempSlot(t){const n={};return Object.keys(t).forEach(t=>{n[t]=()=>e.h("aui-slot",{name:t})}),n},replaceTempSlot(e,t){Object.keys(t).forEach(n=>{const o=e.querySelector(`aui-slot[name="${n}"]`);o&&(h.prototype.$replace.call(o,e),e.$replaceChildren(n,t[n]))})},createVueComponent(t,n){const o=n?n.$moveChildren():{default:[]};return{setup(){const r=e.useAttrs(),s=e.shallowRef(null),l=e.shallowRef(null),a=e.ref({}),u=e.shallowRef(n?c.createTempSlot(o):e.useSlots()),h=e.shallowRef(null);return e.onMounted(()=>{s.value&&l.value&&l.value.$bindVue&&(l.value.$bindVue(s.value),n&&c.replaceTempSlot(n,o),l.value.addEventListener("aui-attr-change",()=>{l.value&&(a.value=c.getAuiAttrs(l.value))}),l.value.addEventListener("aui-force-update",()=>{h.value=null}),l.value.$call("$emit","update:value",l.value.value))}),e.onUpdated(()=>{l.value&&l.value.$slotReplace()}),()=>{if(n&&(l.value=n,a.value=c.getAuiAttrs(n)),h.value||(h.value=c.formatStructure(t)),!h.value)return null;const o=e.h(h.value,{...r,...a.value,ref:s},u.value||{});return n?o:e.h(t,{ref:l,[i]:""},[o])}}}},getAuiAttrs(e){const t={},n=e.attributes;for(let o=0;o<n.length;o++){const e=n[o];e.name===r||c.isVueAttr(e.name)||(t[e.name]=e.value)}return null!==e.value&&(t.value=e.value),t},isVueAttr:e=>0===e.indexOf("data-v-"),auiAttrName:e=>e.replace(/([A-Z])/g,e=>"-"+e.toLowerCase()),vueAttrName:e=>c.isVueAttr(e)?e:e.replace(/-([a-z])/g,(e,t)=>t.toUpperCase()),hookStructure(e,t){c.cache.addHooks(e,t)},getHookStructure(e){const t=c.cache.getAuiVueDefined(e);return((null==t?void 0:t.hooks)||[]).slice(0)},formatStructure(e){const n=c.cache.getAuiVueDefined(e);if(!(null==n?void 0:n.origin))return null;let o=t.merge({},n.origin);const r=c.getHookStructure(e);let i;for(;i=r.shift();){const e=i(o);e&&(o=e)}return o}},d=c.init();exports.AuiComponent=u,exports.AuiSlot=h,exports.default=d;//# sourceMappingURL=core.cjs.js.map