vue-create-api
Version:
A Vue plugin which make Vue component invocated by API.
6 lines • 3.74 kB
JavaScript
/**
* vue-create-api v0.2.3
* (c) 2019 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 c(e){return(e+"").replace(n,function(e,n){return n?n.toUpperCase():""})}function m(e){return void 0===e}function w(e,t,n,r,o){var i=void 0,a=void 0,u=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)}}}));u.updateRenderData=function(e,n){i=e,a=n},u.updateRenderData(n,r),u.$mount(),u.init();var c=u.$children[0];return c.$updateProps=function(e){p(i.props,e),u.$forceUpdate()},c}function g(){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]=c("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 b="hook:beforeDestroy";function f(d){var v=1<arguments.length&&void 0!==arguments[1]?arguments[1]:[],l=2<arguments.length&&void 0!==arguments[2]&&arguments[2],y=this,_={},$=[];return{before:function(e){$.push(e)},create:function(e,n,t){"function"!=typeof n&&m(t)&&(t=n,n=null),m(t)&&(t=l);var r=this,o=!!r.$on,i={};o&&((i.parent=r).__unwatchFns__||(r.__unwatchFns__=[]));var a,u,c,p,f,s=g(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 u=r.$watch(function(){var t={};return i.forEach(function(e,n){t[e]=r[a[n]]}),t},n);r.__unwatchFns__.push(u)}}}(r,s,o,function(e){h&&h.$updateProps(e)}),u=r,(c=(a=s).props.$events)&&(delete a.props.$events,Object.keys(c).forEach(function(e){var n=c[e];"string"==typeof n&&(n=u[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=function(n,t,e,r){$.forEach(function(e){e(n,t,r)});var o=e.parent?e.parent._uid:-1,i=_[o]?_[o]:{},a=i.comp,u=i.ins;if(r&&a&&u)return u.updateRenderData(n,t),u.$forceUpdate(),a;var c=w(y,d,n,t,e),p=c.$parent,f=c.remove;c.remove=function(){if(r){if(!_[o])return;_[o]=null}f&&f.apply(this,arguments),p.destroy()};var s=c.show;c.show=function(){return s&&s.apply(this,arguments),this};var h=c.hide;return c.hide=function(){return h&&h.apply(this,arguments),this},r&&(_[o]={comp:c,ins:p}),c}(s,n,i,t),o&&r.$on(b,function(){e=r,e.__unwatchFns__&&(e.__unwatchFns__.forEach(function(e){e()}),e.__unwatchFns__=null),h.remove(),h=null;var e}),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,u=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"),u=o.replace(a,"");return""+c(""+r+u)}(e,{componentPrefix:a,apiPrefix:u});return i.prototype[o]=e.$create=r.create,r}},instantiateComponent:w,version:"0.2.3"}});