UNPKG

8.41 kBJavaScriptView Raw
1!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e(t.composi={})}(this,function(t){"use strict";var e="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},n=(function(){function t(t){this.value=t}function e(e){var n,o;function i(n,o){try{var l=e[n](o),u=l.value;u instanceof t?Promise.resolve(u.value).then(function(t){i("next",t)},function(t){i("throw",t)}):r(l.done?"return":"normal",l.value)}catch(t){r("throw",t)}}function r(t,e){switch(t){case"return":n.resolve({value:e,done:!0});break;case"throw":n.reject(e);break;default:n.resolve({value:e,done:!1})}(n=n.next)?i(n.key,n.arg):o=null}this._invoke=function(t,e){return new Promise(function(r,l){var u={key:t,arg:e,resolve:r,reject:l,next:null};o?o=o.next=u:(n=o=u,i(t,e))})},"function"!=typeof e.return&&(this.return=void 0)}"function"==typeof Symbol&&Symbol.asyncIterator&&(e.prototype[Symbol.asyncIterator]=function(){return this}),e.prototype.next=function(t){return this._invoke("next",t)},e.prototype.throw=function(t){return this._invoke("throw",t)},e.prototype.return=function(t){return this._invoke("return",t)}}(),function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}),o=function(){function t(t,e){for(var n=0;n<e.length;n++){var o=e[n];o.enumerable=o.enumerable||!1,o.configurable=!0,"value"in o&&(o.writable=!0),Object.defineProperty(t,o.key,o)}}return function(e,n,o){return n&&t(e.prototype,n),o&&t(e,o),e}}(),i="http://www.w3.org/1999/xlink",r="http://www.w3.org/2000/svg";function l(t,e){var n={};for(var o in t)n[o]=t[o];for(var i in e)n[i]=e[i];return n}function u(t){return!Array.isArray(t)&&"object"===(void 0===t?"undefined":e(t))}function s(t){return t.currentTarget.events[t.type](t)}var a=function(t){return t?t.key:null};function c(t,n,o,r,u){if("key"!==n)if("style"!==n||"object"!==(void 0===o?"undefined":e(o))||Array.isArray(o))"o"===(n=n.toLowerCase())[0]&&"n"===n[1]&&(t.events||(t.events={}),t.events[n=n.slice(2)]=o,null==o?t.removeEventListener(n,s):null==r&&t.addEventListener(n,s)),"classname"===n&&(n="class"),"dangerouslysetinnerhtml"===n&&(n="innerHTML"),n in t&&"list"!==n&&"type"!==n&&"draggable"!==n&&"spellcheck"!==n&&"translate"!==n&&!u?t[n]=null==o?"":o:null!=o&&"no"!==o&&"off"!==o&&("xlink-href"===n?(t.setAttributeNS(i,"href",o),t.setAttribute("href",o)):("true"===o&&(o=""),"dangerouslysetinnerhtml"!==n&&t.setAttribute(n,o))),null!=o&&"no"!==o&&"off"!==o||t.removeAttribute(n);else for(var a in l(r,o)){var c=null==o||null==o[a]?"":o[a];"-"===a[0]?t[n].setProperty(a,c):t[n][a]=c}}var p=function(t,e,n){var o=function(){t.removeChild(e)},i=n.props&&n.props.onunmount||n.props&&n.props.onComponentWillUnmount;null!=i?i(e,o):o()};function f(t,e,n,o,i){if(o!==n){if(null==n||n.type!==o.type){var u=function t(e,n){var o=void 0;"number"==typeof e&&(e=e.toString()),o="string"==typeof e?document.createTextNode(e):(n=n||"svg"===e.type)?document.createElementNS(r,e.type):document.createElement(e.type);var i=e.props;if(i){for(var l=0;l<e.children.length;l++)o.appendChild(t(e.children[l],n));for(var u in i)c(o,u,i[u],null,n)}return o}(o,i);t&&(t.insertBefore(u,e),null!=n&&p(t,e,n)),e=u}else if(null==n.type)e.nodeValue=o;else{!function(t,e,n,o){for(var i in l(e,n))n[i]!==("value"===i||"checked"===i?t[i]:e[i])&&c(t,i,n[i],e[i],o);t.mounted&&n&&n.onComponentDidUpdate&&n.onComponentDidUpdate.call(n.onComponentDidUpdate,e,n,t),t.mounted&&n&&n.onupdate&&n.onupdate.call(n.onupdate,e,n,t)}(e,n.props,o.props,i=i||"svg"===o.type);for(var s={},h={},d=[],m=n.children,y=o.children,v=0;v<m.length;v++){d[v]=e.childNodes[v];var g=a(m[v]);null!=g&&(s[g]=[d[v],m[v]])}for(var b=0,k=0;k<y.length;){var S=a(m[b]),U=a(y[k]);if(h[S])b++;else if(null==U||U!==a(m[b+1]))if(null==U)null==S&&(f(e,d[b],m[b],y[k],i),k++),b++;else{var w=s[U]||[];S===U?(f(e,w[0],w[1],y[k],i),b++):w[0]?f(e,e.insertBefore(w[0],d[b]),w[1],y[k],i):f(e,d[b],null,y[k],i),h[U]=y[k],k++}else null==S&&p(e,d[b],m[b]),b++}for(;b<m.length;)null==a(m[b])&&p(e,d[b],m[b]),b++;for(var D in s)h[D]||p(e,s[D][0],s[D][1])}return e}}function h(t,e){return e?f(e.parentNode,e,e&&e.vnode,t):e=f(null,null,null,t),e.vnode=t,e}var d=(new Date).getTime(),m=function(){function t(e){n(this,t),e||(e={}),this.props=e,this.selector=e.container||"body",e.render&&(this.render=e.render),e.state&&(this.state=e.state),this.selector&&(this.container=document.querySelector(this.selector)),this.componentShouldUpdate=!0,this.mounted=!1,this.element=null,e.componentWillMount&&(this.componentWillMount=e.componentWillMount),e.componentDidMount&&(this.componentDidMount=e.componentDidMount),e.componentWillUpdate&&(this.componentWillUpdate=e.componentWillUpdate),e.componentDidUpdate&&(this.componentDidUpdate=e.componentDidUpdate),e.componentWillUnmount&&(this.componentWillUnmount=e.componentWillUnmount)}return o(t,[{key:"componentWillMount",value:function(t){t&&"function"==typeof t&&t.call(t,this)}},{key:"componentDidMount",value:function(t){t&&"function"==typeof t&&t.call(t,this)}},{key:"componentWillUpdate",value:function(t){t&&"function"==typeof t&&t.call(t,this)}},{key:"componentDidUpdate",value:function(t){t&&"function"==typeof t&&t.call(t,this)}},{key:"componentWillUnmount",value:function(t){t&&"function"==typeof t&&t.call(t,this)}},{key:"render",value:function(t){return t}},{key:"setState",value:function(t){if("function"==typeof t){var e=t.call(this,this.state);e&&(this.state=e)}else if(u(this.state)&&u(t)){var n=this.state;this.state=l(n,t)}else this.state=t}},{key:"update",value:function(t){if(this.render&&(this.componentShouldUpdate||!this.mounted)){var e=this.state;!0!==t&&t&&(e=t),this.container&&"string"==typeof this.container&&(this.selector=this.container,this.container=document.querySelector(this.container));var n=this.render(e),o=void 0;n&&n.props&&n.props.id&&this.container&&(o=this.container&&this.container.querySelector("#"+n.props.id)),o&&!this.mounted&&o.parentNode.removeChild(o);var i=this.element&&this.element.vnode;if(r=i,l=e,!(u=this)||JSON.stringify(r)!==JSON.stringify(u.render(l))){var r,l,u;if(this.element=h(this.render(e),this.element),!this.mounted)return this.componentWillMount&&this.componentWillMount(),this.container&&1===this.container.nodeType||console.error("The container for a class component is not a valid DOM node. Check the selector provided for the class to make sure it is a valid CSS selector and that the container exists in the DOM. You might be targeting a nonexistent node."),this.container.appendChild(this.element),this.mounted=!0,void(this.componentDidMount&&this.componentDidMount());this.componentWillUpdate&&this.componentWillUpdate(),this.componentDidUpdate&&this.componentDidUpdate()}}}},{key:"unmount",value:function(){if(this.element){this.componentWillUnmount&&this.componentWillUnmount(),this.container.removeChild(this.element),this.container=null;for(var t in this)delete this[t];delete this.state,this.update=null,this.unmount=null}}},{key:"state",get:function(){return this[d]},set:function(t){var e=this;this[d]=t,setTimeout(function(){return e.update()})}}]),t}();t.h=function(t,e){for(var n=arguments.length,o=Array(n>2?n-2:0),i=2;i<n;i++)o[i-2]=arguments[i];var r=[],l=[],u=o.length,s=(e=e||{}).key||null;for(delete e.key;u-- >0;)r.push(o[u]);for(;r.length;){var a=r.pop();if(a&&a.pop)for(u=a.length;u--;)r.push(a[u]);else null!=a&&!0!==a&&!1!==a&&l.push(a)}return o=l,"function"==typeof t?t(e||{},l):{type:t,props:e,children:o,key:s}},t.mount=function(t,e,o){if("string"==typeof e&&(e=document.querySelector(e)),e||(e=document.body),Array.isArray(t))throw new function t(){n(this,t),this.message="Cannot insert Fragment tag directly into DOM.",this.toString=function(){return this.message}};var i=h(t);return t.props&&t.props.onComponentDidMount&&t.props.onComponentDidMount.call(t.props.onComponentDidMount,i),t.props&&t.props.onmount&&t.props.onmount.call(t.props.onmount,i),i.mounted=!0,o?("string"==typeof o&&(o=document.querySelector(o)),o.parentNode.appendChild(i),o.parentNode.removeChild(o),i):e.appendChild(i)},t.render=function(t,e){return h(t,e)},t.Component=m,t.Fragment=function(t,e){return e},Object.defineProperty(t,"__esModule",{value:!0})});
2//# sourceMappingURL=composi.js.map