vue-create-api
Version:
A Vue plugin which make Vue component invocated by API.
6 lines • 4.11 kB
JavaScript
/**
* vue-create-api v0.2.3
* (c) 2025 ustbhuangyi
* @license MIT
*/
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?module.exports=n():"function"==typeof define&&define.amd?define(n):e.VueCreateAPI=n()}(this,function(){"use strict";var p=Object.assign||function(e){for(var n=1;n<arguments.length;n++){var t=arguments[n];for(var r in t)Object.prototype.hasOwnProperty.call(t,r)&&(e[r]=t[r])}return e},n=/-(\w)/g;function u(e){return(e+"").replace(n,function(e,n){return n?n.toUpperCase():""})}function w(e){return void 0===e}function g(e){return"function"==typeof e}function b(e,t,n,r,o){var i=void 0,a=void 0,c=new e(p({},o,{render:function(e){var n=a&&a(e);return n&&!Array.isArray(n)&&(n=[n]),e(t,p({},i),n||[])},methods:{init:function(){document.body.appendChild(this.$el)},destroy:function(){this.$destroy(),this.$el&&this.$el.parentNode===document.body&&document.body.removeChild(this.$el)}}}));c.updateRenderData=function(e,n){i=e,a=n},c.updateRenderData(n,r),c.$mount(),c.init();var u=c.$children[0];return u.$updateProps=function(e){p(i.props,e),c.$forceUpdate()},u}function P(){var n,e=0<arguments.length&&void 0!==arguments[0]?arguments[0]:{},t=1<arguments.length&&void 0!==arguments[1]?arguments[1]:{};n={},t.forEach(function(e){n[e]=u("on-"+e)}),t=n;var r=p({},e),o={};for(var i in t)if(t.hasOwnProperty(i)){var a=t[i];r[a]&&(o[i]=r[a],delete r[a])}return{props:r,on:o}}var E=[];var x="hook:beforeDestroy";function f(l){var v=1<arguments.length&&void 0!==arguments[1]?arguments[1]:[],d=2<arguments.length&&void 0!==arguments[2]&&arguments[2],y=this,$={},_=[];function m(n,t,e,r,o){_.forEach(function(e){e(n,t,r)});var i=e.parent?e.parent._uid:-1,a=$[i]?$[i]:{},c=a.comp,u=a.ins;if(r&&c&&u)return u.updateRenderData(n,t),u.$forceUpdate(),c;var p=b(y,l,n,t,e),f=p.$parent,s=p.remove,h=!!o.$on;p.remove=function(){var e;if(h&&(e=o).__unwatchFns__&&(e.__unwatchFns__.forEach(function(e){e()}),e.__unwatchFns__=null),r){if(!$[i])return;$[i]=null}s&&s.apply(this,arguments),f.destroy(),function(e){for(var n=E.length,t=0;t<n;t+=1)E[t]===e&&E.splice(t,1)}(p)};var v=p.show;p.show=function(){return v&&v.apply(this,arguments),this};var d=p.hide;return p.hide=function(){return d&&d.apply(this,arguments),this},r&&($[i]={comp:p,ins:f}),p}return{before:function(e){_.push(e)},create:function(e,n,t){!g(n)&&w(t)&&(t=n,n=null),w(t)&&(t=d);var r=this,o=!!r.$on,i={};o&&((i.parent=r).__unwatchFns__||(r.__unwatchFns__=[]));var a,c,u,p,f,s=P(e,v),h=null;return function(r,t,e,n){var o=t.props.$props;if(o){delete t.props.$props;var i=[],a=[];if(Object.keys(o).forEach(function(e){var n=o[e];"string"==typeof n&&n in r?(t.props[e]=r[n],i.push(e),a.push(n)):t.props[e]=n}),e){var c=r.$watch(function(){var t={};return i.forEach(function(e,n){t[e]=r[a[n]]}),t},n);r.__unwatchFns__.push(c)}}}(r,s,o,function(e){h&&h.$updateProps(e)}),c=r,(u=(a=s).props.$events)&&(delete a.props.$events,Object.keys(u).forEach(function(e){var n=u[e];"string"==typeof n&&(n=c[n]),a.on[e]=n})),f=(p=s).props,Object.keys(f).forEach(function(e){"$"===e.charAt(0)&&(p[e.slice(1)]=f[e],delete f[e])}),h=m(s,n,i,t,r),o&&r.$on(x,h.remove.bind(h)),function(e){for(var n=E.length,t=0;t<n;t+=1)if(E[t]===e)return;E.push(e)}(h),h}}}return{install:function(i){var e=1<arguments.length&&void 0!==arguments[1]?arguments[1]:{},n=e.componentPrefix,a=void 0===n?"":n,t=e.apiPrefix,c=void 0===t?"$create-":t;i.createAPI=function(e,n,t){"boolean"==typeof n&&(t=n,n=[]);var r=f.call(this,e,n,t),o=function(e,n){var t=n.componentPrefix,r=n.apiPrefix,o=e.name;!function(e,n){if(!e)throw new Error("[vue-create-api error]: "+n)}(o,"Component must have name while using create-api!");var i,a=new RegExp("^"+(t+"").replace(new RegExp("[.\\\\+*?\\[\\^\\]$(){}=!<>|:\\"+(i||"")+"-]","g"),"\\$&"),"i"),c=o.replace(a,"");return""+u(""+r+c)}(e,{componentPrefix:a,apiPrefix:c});return i.prototype[o]=e.$create=r.create,r}},batchDestroy:function(e){var n,t=g(e),r=E.slice(),o=t?e(r):r;n=o,"[object Array]"===Object.prototype.toString.call(n)&&o.forEach(function(e){e&&g(e.remove)&&e.remove()})},instantiateComponent:b,version:"0.2.3"}});