hele
Version:
A front-end UI lib.
1 lines • 9.8 kB
JavaScript
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t(e.HEle={})}(this,function(e){"use strict";var t="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},n=function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")},o=function(){function defineProperties(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)}}return function(e,t,n){return t&&defineProperties(e.prototype,t),n&&defineProperties(e,n),e}}(),r=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)},i=function(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},a=function(e){if(Array.isArray(e)){for(var t=0,n=Array(e.length);t<e.length;t++)n[t]=e[t];return n}return Array.from(e)},c=function Reference(){n(this,Reference),this.current=void 0};function isEqual(e,t){if(e===t||e!=e&&t!=t)return!0;if(!(e instanceof Object&&t instanceof Object)||e+""!=t+"")return!1;for(var n in e)if(!(n in t&&isEqual(e[n],t[n])))return!1;for(var o in t)if(!(o in e))return!1;return!0}function _clrChd(e){var t=arguments.length>1&&void 0!==arguments[1]&&arguments[1];[].concat(a(e.childNodes)).forEach(function(n){t&&_clrChd(n,!0),e.removeChild(n)})}function _flatten(e){var t=[];return e.forEach(function(e){e instanceof Array?e.forEach(function(e){t.push(e)}):t.push(e)}),t}function _copy(e){return e instanceof Object?Object.create(e):e}var u=function(e){return e instanceof Object&&e+""=="[object Object]"},l=new Map,f=new Map([["svg","http://www.w3.org/2000/svg"]]);function _toEle(e){if(e instanceof s)return e;var n=void 0===e?"undefined":t(e);return"string"===n?new s(null,{children:[e]}):"number"===n?new s(null,{children:[e.toString()]}):e instanceof Array?e.map(_toEle):null}function _toNode(e){return e instanceof s?e.toNode():e instanceof Array?_flatten(e.map(_toNode)):document.createTextNode("")}var s=function(){function HElement(e,t){n(this,HElement),this.type=e,this.parent=void 0,this.node=void 0,this.context=void 0,this.props=e&&"string"!=typeof e&&e.prototype instanceof m?Object.assign({},e.defaultProps,t):t}return o(HElement,[{key:"toNode",value:function(){var e=this,t=this.type,n=this.props,o=void 0;if(null===t)o=document.createTextNode(n.children[0]);else if("string"==typeof t){var r=_copy(this.context),i=!n["no-xmlns"]&&(n.xmlns||f.get(t)||r._xmlns);o=i?document.createElementNS(i,t):document.createElement(t),i&&(r._xmlns=i),_crtNode(n,o,r)}else{var a=_getCom(t,n,_copy(this.context)),c=a.element,u=a.component,s=_toEle(c);if(t===v&&(this.context=u.state),s){var d=this.context;_flatten([s]).forEach(function(t){t&&(t.parent=e,t.context=_copy(d))})}u&&l.set(u,this);try{o=s instanceof Array?_flatten(s.map(_toNode)):_toNode(s),u&&u.onDidMount()}catch(e){if(o=document.createTextNode(""),!u)throw e;u.onCaughtError(e)}}return this.node=o}}]),HElement}();function _upCom(e){var t=l.get(e);if(t){var n=t.node;if(n){var o=t.parent,r=_flatten([n]);r.forEach(function(n,i){var c=n.parentNode;if(_clrChd(n,!0),c)if(0===i){var u=e.toElement();if(u instanceof s){u.parent=o,u.context=t.context,l.set(e,u);var f=u.toNode(),d=_flatten([f]),p=document.createDocumentFragment();o&&(o.node instanceof Array?r.forEach(function(e,t){var n,r=o.node.indexOf(e);0===t?(n=o.node).splice.apply(n,[r,1].concat(a(d))):o.node.splice(r,1)}):o.node=f),d.forEach(function(e){p.appendChild(e)}),c.replaceChild(p,n)}else l.delete(e),c.removeChild(n)}else c.removeChild(n)})}}}var d=new Set,p=function(e){requestAnimationFrame(e)},h={tickMethod:p,maxUpdateTime:12,maxClearTime:3,_willTick:!1,_tick:function(){h._willTick||(h.tickMethod(function(){h._willTick=!1;var e=h.maxUpdateTime,t=h.maxClearTime,n=Date.now();d.forEach(function(t){if(Date.now()-n<e){d.delete(t);var o=t.state,r=t.updateRequestCallbacks,i=t._forceUp,a=r.slice(0),c=a.length,u=_copy(o),l=void 0;a.forEach(function(e){void 0!==(l=e(u))&&(u=l)}),t.updateRequestCallbacks=r.slice(c);try{if(i||t.shouldUpdate(o,u)){t._forceUp=!1;var f=t.onWillUpdate(o);t.state=u,_upCom(t),t.onDidUpdate(f)}}catch(e){t.onCaughtError(e)}}}),d.size&&h._tick(),n=Date.now();for(var o=!0;o&&Date.now()-n<t;)o=!1,l.forEach(function(e,t){var n=e.node;if(n){var r=n instanceof Array?n[0]:n;if(!r||!r.parentNode){o=!0;try{t.onWillUnmount(),l.delete(t),t.onDidUnmount()}catch(e){t.onCaughtError(e)}}}})}),h._willTick=!0)},_mark:function(e){d.add(e),h._tick()}},m=function(){function Component(e,t){n(this,Component),this.context=t,this.state={},this.refs=new Map,this.updateRequestCallbacks=[],this._forceUp=!1,this.props=e}return o(Component,[{key:"toElement",value:function(){this.refs.forEach(function(e){e.current=void 0});try{var e=this.render(),n=void 0===e?"undefined":t(e);return"string"===n?new s(null,{children:[e]}):"number"===n?new s(null,{children:[e.toString()]}):e}catch(e){return this.onCaughtError(e),null}}},{key:"onWillMount",value:function(){}},{key:"onDidMount",value:function(){}},{key:"shouldUpdate",value:function(e,t){return!isEqual(e,t)}},{key:"onWillUpdate",value:function(e){}},{key:"onDidUpdate",value:function(e){}},{key:"onWillUnmount",value:function(){}},{key:"onDidUnmount",value:function(){}},{key:"onCaughtError",value:function(e){throw e}},{key:"createRef",value:function(e){var t=this.refs;if(t.has(e))return t.get(e);var n=new c;return t.set(e,n),n}},{key:"requestUpdate",value:function(e){return this.updateRequestCallbacks.push(e),h._mark(this),this}},{key:"update",value:function(e){return this.requestUpdate(function(t){if(!u(e)||!u(t))return e;Object.assign(t,e)})}},{key:"forceUpdate",value:function(e){return this._forceUp=!0,arguments.length?this.update(e):h._mark(this),this}}]),Component}();m.defaultProps={};var v=function(e){function Context(e,t){n(this,Context);var o=i(this,(Context.__proto__||Object.getPrototypeOf(Context)).call(this,e,t));return o.state=Object.assign({},t,e.value),o}return r(Context,m),o(Context,[{key:"render",value:function(){return _flatten(this.props.children)}}]),Context}();function render(e,n){var o=!(arguments.length>2&&void 0!==arguments[2])||arguments[2];_clrChd(n,o),h._tick();var r=n.context;_flatten([e]).forEach(function(e){if(e instanceof s)e.context=r,_flatten([e.toNode()]).forEach(function(e){n.appendChild(e)});else{var o=void 0===e?"undefined":t(e);"string"===o?n.appendChild(document.createTextNode(e)):"number"===o&&n.appendChild(document.createTextNode(e.toString()))}})}var _=new Map([["children",function(e,t){render(e,t,!1)}],["style",function(e,t){if(t instanceof HTMLElement)if(e instanceof Object)for(var n in e)t.style[n]=e[n];else t.style=e}],["ref",function(e,t){e.current=t}],["class",function(e,t){"setAttribute"in t&&t.setAttribute("class","string"==typeof e?e:e.filter(function(e){return"string"==typeof e}).join(" "))}],["no-xmlns",function(){}]]),y=new Map([["ref",function(e,t){e.current=t}]]),E=new Map([["ref",function(e){e.current=void 0}]]),g=/^on(\w+)$/i,b=/capture/i,C=/nonpassive/i,x=/once/i;function _getEName(e,t,n,o){var r=0;return t&&(r+=7),n&&(r+=7),o&&(r+=4),r>0?e.slice(0,-r):e}function _getEOpt(e,t,n){return!!(t||e||n)&&{capture:e,passive:!t,once:n}}function _crtNode(e,t,n){for(var o in t.context=n,e){var r=e[o],i=_.get(o);if(i)i(r,t);else if(o.match(g)){var a=RegExp.$1,c=b.test(a),u=C.test(a),l=x.test(a),f=_getEName(a,c,u,l);t.addEventListener(f,r,_getEOpt(c,u,l))}else if(!(o in t)&&"setAttribute"in t)t.setAttribute(o,r);else try{t[o]=r}catch(e){t.setAttribute(o,r)}}}function _getCom(e,t,n){var o={element:null,component:null};if(e.prototype instanceof m){var r=o.component=new e(t,n);for(var i in t){var a=y.get(i);a&&a(t[i],r)}try{r.onWillMount()}catch(e){r.onCaughtError(e)}o.element=r.toElement()}else{for(var c in t){var u=E.get(c);u&&u(t[c],void 0)}var l=e(t,n);o.element=l}return o}function createElement(e,t){for(var n=arguments.length,o=Array(n>2?n-2:0),r=2;r<n;r++)o[r-2]=arguments[r];return new s(e,Object.assign({},t||{},{children:_flatten(o)}))}var k=function(e){function Portal(e,t){n(this,Portal);var o=i(this,(Portal.__proto__||Object.getPrototypeOf(Portal)).call(this,e,t));return e.container?o.container=e.container:(o.container=document.createElement("div"),document.body.appendChild(o.container)),o}return r(Portal,m),o(Portal,[{key:"render",value:function(){return null}},{key:"onWillMount",value:function(){render(createElement(v,{value:this.context},this.props.children),this.container,this.props.deepClear)}}]),Portal}();e.specialNodePropProcessors=_,e.specialComponentPropProcessors=y,e.specialFactoryPropProcessors=E,e._getEName=_getEName,e._getEOpt=_getEOpt,e._crtNode=_crtNode,e._getCom=_getCom,e.isEqual=isEqual,e._clrChd=_clrChd,e._flatten=_flatten,e._copy=_copy,e._isNorObj=u,e._upCom=_upCom,e._expired=d,e.defaultTickMethod=p,e.Ticker=h,e.Component=m,e.Fragment=function(e){return _flatten(e.children)},e.Context=v,e._eleMap=l,e.namespaces=f,e._nsCtxName="_xmlns",e._toEle=_toEle,e._toNode=_toNode,e.HElement=s,e.Reference=c,e.render=render,e.Portal=k,e.createElement=createElement,e.createFactory=function(e){return function(t){for(var n=arguments.length,o=Array(n>1?n-1:0),r=1;r<n;r++)o[r-1]=arguments[r];return createElement.apply(void 0,[e,t].concat(o))}},Object.defineProperty(e,"__esModule",{value:!0})});