vue-custom-element
Version:
Custom Elements for Vue.js
6 lines • 9.46 kB
JavaScript
/**
* vue-custom-element v3.3.0
* (c) 2021 Karol Fabjańczuk
* @license MIT
*/
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):e.VueCustomElement=t()}(this,function(){"use strict";function e(e,t){return e.__proto__=t,e}Object.setPrototypeOf=Object.setPrototypeOf||e;e.bind(Object);var f="undefined"!=typeof Symbol&&"undefined"!=typeof Reflect&&"undefined"!=typeof Proxy&&!Object.isSealed(Proxy),d=function(e,t,n){return t&&o(e.prototype,t),n&&o(e,n),e};function o(e,t){for(var n=0;n<t.length;n++){var o=t[n];o.enumerable=o.enumerable||!1,o.configurable=!0,"value"in o&&(o.writable=!0),Object.defineProperty(e,o.key,o)}}function p(e,t){if(!e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!t||"object"!=typeof t&&"function"!=typeof t?e:t}function _(){return Reflect.construct(HTMLElement,[],this.__proto__.constructor)}function n(e,t){var o=1<arguments.length&&void 0!==t?t:{};if("undefined"!=typeof customElements){if(f){var n=(function(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function, not "+typeof t);e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}}),t&&(Object.setPrototypeOf?Object.setPrototypeOf(e,t):e.__proto__=t)}(s,_),d(s,null,[{key:"observedAttributes",get:function(){return o.observedAttributes||[]}}]),s);return n.prototype.connectedCallback=c,n.prototype.disconnectedCallback=i,n.prototype.attributeChangedCallback=l,u(e,n),n}var r=function(e){var t=e?HTMLElement.call(e):this;return a.call(t),t};return r.observedAttributes=o.observedAttributes||[],(r.prototype=Object.create(HTMLElement.prototype,{constructor:{configurable:!0,writable:!0,value:r}})).connectedCallback=c,r.prototype.disconnectedCallback=i,r.prototype.attributeChangedCallback=l,u(e,r),r}function a(){!0===o.shadow&&HTMLElement.prototype.attachShadow&&this.attachShadow({mode:"open"}),"function"==typeof o.constructorCallback&&o.constructorCallback.call(this)}function c(){"function"==typeof o.connectedCallback&&o.connectedCallback.call(this)}function i(){"function"==typeof o.disconnectedCallback&&o.disconnectedCallback.call(this)}function l(e,t,n){"function"==typeof o.attributeChangedCallback&&o.attributeChangedCallback.call(this,e,t,n)}function u(e,t){var n=customElements.get(e);return void 0!==n?n:customElements.define(e,t)}function s(e){!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,s);var t=p(this,(s.__proto__||Object.getPrototypeOf(s)).call(this)),n=e?HTMLElement.call(e):t;return a.call(n),p(t,n)}}Object.setPrototypeOf(_.prototype,HTMLElement.prototype),Object.setPrototypeOf(_,HTMLElement);var t=/-(\w)/g,u=function(e){return e.replace(t,function(e,t){return t?t.toUpperCase():""})},r=/([^-])([A-Z])/g,a=function(e){return e.replace(r,"$1-$2").replace(r,"$1-$2").toLowerCase()};function h(e,t){for(var n=1<arguments.length&&void 0!==t?t:0,o=e.length-n,r=new Array(o);o--;)r[o]=e[o+n];return r}var m="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e};function v(e,t){if(null==e)return t!==Boolean&&void 0;var n=e,o=-1<["true","false"].indexOf(e),r=parseFloat(n,10),a=!isNaN(r)&&isFinite(n)&&"string"==typeof n&&!n.match(/^0+[^.]\d*$/g);return t&&t!==Boolean&&(void 0===n?"undefined":m(n))!==t?n=t(e):o||t===Boolean?n=""===n||("true"===n||!0===n):a&&(n=r),n}function c(e,n){if(e&&e.length)e.forEach(function(e){var t=u(e);-1===n.camelCase.indexOf(t)&&n.camelCase.push(t)});else if(e&&"object"===(void 0===e?"undefined":m(e)))for(var t in e){var o=u(t);-1===n.camelCase.indexOf(o)&&n.camelCase.push(o),e[o]&&e[o].type&&(n.types[t]=[].concat(e[o].type)[0])}}function s(e){var t=0<arguments.length&&void 0!==e?e:{},n={camelCase:[],hyphenate:[],types:{}};t.mixins&&t.mixins.forEach(function(e){c(e.props,n)}),t.extends&&t.extends.props&&c(t.extends.props,n);return c(t.props,n),n.camelCase.forEach(function(e){n.hyphenate.push(a(e))}),n}function b(e){var t={};return h(e.attributes).forEach(function(e){t["vue-slot"===e.nodeName?"slot":e.nodeName]=e.nodeValue}),t}function y(t,e,n){var o=h(function(e){if(e.childNodes.length)return e.childNodes;if(e.content&&e.content.childNodes&&e.content.childNodes.length)return e.content.childNodes;var t=document.createElement("div");return t.innerHTML=e.innerHTML,t.childNodes}(e)).map(function(e){return"#text"===e.nodeName?e.nodeValue:t(e.tagName,{attrs:b(e),domProps:{innerHTML:e.innerHTML}})});return n.slot=e.id,t("template",n,o)}function C(e,t){for(var n=arguments.length,o=Array(2<n?n-2:0),r=2;r<n;r++)o[r-2]=arguments[r];var a=function(e,t){var n={bubbles:!1,cancelable:!1,detail:t},o=void 0;return"function"==typeof window.CustomEvent?o=new CustomEvent(e,n):(o=document.createEvent("CustomEvent")).initCustomEvent(e,n.bubbles,n.cancelable,n.detail),o}(t,[].concat(o));e.dispatchEvent(a)}function w(r,t,e,o,n){if(r.__vue_custom_element__)return Promise.resolve(r);var a=t.util.extend({},e),c=function(a,e,c){var i=e.propsData||{};return c.hyphenate.forEach(function(e,t){var n=c.camelCase[t],o=a.attributes[e]||a[n],r=null;c.types[n]&&(r=c.types[n]),o instanceof Attr?i[n]=v(o.value,r):void 0!==o&&(i[n]=o)}),i}(r,a,o),i=t.version&&parseInt(t.version.split(".")[0],10)||0;if(a.beforeCreate=[].concat(a.beforeCreate||[],function(){this.$emit=function(){for(var e,t=arguments.length,n=Array(t),o=0;o<t;o++)n[o]=arguments[o];C.apply(void 0,[r].concat(n)),this.__proto__&&(e=this.__proto__.$emit).call.apply(e,[this].concat(n))}}),a._compiled){var l={},u=a._Ctor;u&&(l=Object.keys(u).map(function(e){return u[e]})[0].options),l.beforeCreate=a.beforeCreate}var s=void 0;if(2<=i){var f=r.cloneNode(!0).childNodes;s={propsData:c,props:o.camelCase,computed:{reactiveProps:function(){var t=this,n={};return o.camelCase.forEach(function(e){void 0!==t[e]&&(n[e]=t[e])}),n}},render:function(e){var t={props:this.reactiveProps};return e(a,t,function(e,t){var r=t,a=[];return h(0<arguments.length&&void 0!==e?e:[]).forEach(function(e){if("#text"===e.nodeName)e.nodeValue.trim()&&a.push(r("span",e.nodeValue));else if("#comment"!==e.nodeName){var t=b(e),n={attrs:t,domProps:{innerHTML:""===e.innerHTML?e.innerText:e.innerHTML}};t.slot&&(n.slot=t.slot,t.slot=void 0);var o="TEMPLATE"===e.tagName?y(r,e,n):r(e.tagName,n);a.push(o)}}),a}(f,e))}}}else if(1===i)(s=a).propsData=c;else{s=a;var d={};Object.keys(c).forEach(function(e){d[e]={default:c[e]}}),s.props=d}var p=2<=i?"<div></div>":("<div>"+r.innerHTML+"</div>").replace(/vue-slot=/g,"slot=");if(n.shadow&&r.shadowRoot?(r.shadowRoot.innerHTML=p,s.el=r.shadowRoot.children[0]):(r.innerHTML=p,s.el=r.children[0]),n.shadow&&n.shadowCss&&r.shadowRoot){var _=document.createElement("style");_.type="text/css",_.appendChild(document.createTextNode(n.shadowCss)),r.shadowRoot.appendChild(_)}return function(t,r){r.camelCase.forEach(function(e,o){Object.defineProperty(t,e,{get:function(){return this.__vue_custom_element__[e]},set:function(e){if("object"!==(void 0===e?"undefined":m(e))&&"function"!=typeof e||!this.__vue_custom_element__){var t=r.types[r.camelCase[o]];this.setAttribute(r.hyphenate[o],v(e,t))}else{var n=r.camelCase[o];this.__vue_custom_element__[n]=e}}})})}(r,o),"function"==typeof n.beforeCreateVueInstance&&(s=n.beforeCreateVueInstance(s)||s),Promise.resolve(s).then(function(e){return r.__vue_custom_element__=new t(e),r.__vue_custom_element_props__=o,r.getVueInstance=function(){var e=r.__vue_custom_element__;return e.$children.length?e.$children[0]:e},r.removeAttribute("vce-cloak"),r.setAttribute("vce-ready",""),C(r,"vce-ready"),r})}function i(l){l.customElement=function(o,r,e){var a=2<arguments.length&&void 0!==e?e:{},c="function"==typeof r,t=c&&{props:a.props||[]},i=s(c?t:r);return n(o,{constructorCallback:function(){"function"==typeof a.constructorCallback&&a.constructorCallback.call(this)},connectedCallback:function(){var n=this,e=c&&r(),t=e&&e.then&&"function"==typeof e.then;if("function"==typeof a.connectedCallback&&a.connectedCallback.call(this),c&&!t)throw new Error("Async component "+o+" do not returns Promise");this.__detached__||(t?e.then(function(e){var t=s(e);w(n,l,e,t,a).then(function(){"function"==typeof a.vueInstanceCreatedCallback&&a.vueInstanceCreatedCallback.call(n)})}):w(this,l,r,i,a).then(function(){"function"==typeof a.vueInstanceCreatedCallback&&a.vueInstanceCreatedCallback.call(n)})),this.__detached__=!1},disconnectedCallback:function(){var e=this;this.__detached__=!0,"function"==typeof a.disconnectedCallback&&a.disconnectedCallback.call(this),null!==a.destroyTimeout&&setTimeout(function(){e.__detached__&&e.__vue_custom_element__&&(e.__detached__=!1,e.__vue_custom_element__.$destroy(!0),delete e.__vue_custom_element__,delete e.__vue_custom_element_props__)},a.destroyTimeout||3e3)},attributeChangedCallback:function(e,t,n){if(this.__vue_custom_element__&&void 0!==n){var o=u(e);"function"==typeof a.attributeChangedCallback&&a.attributeChangedCallback.call(this,e,t,n);var r=this.__vue_custom_element_props__.types[o];this.__vue_custom_element__[o]=v(n,r)}},observedAttributes:i.hyphenate,shadow:!!a.shadow&&!!HTMLElement.prototype.attachShadow})}}return"undefined"!=typeof window&&window.Vue&&(window.Vue.use(i),i.installed&&(i.installed=!1)),i});