forked from MathiasWP/TeroyJS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTeroy.polyfill.js
More file actions
1 lines (1 loc) · 5.95 KB
/
Teroy.polyfill.js
File metadata and controls
1 lines (1 loc) · 5.95 KB
1
"use strict";function _instanceof(left,right){if(right!=null&&typeof Symbol!=="undefined"&&right[Symbol.hasInstance]){return!!right[Symbol.hasInstance](left)}else{return left instanceof right}}function _createForOfIteratorHelper(o){if(typeof Symbol==="undefined"||o[Symbol.iterator]==null){if(Array.isArray(o)||(o=_unsupportedIterableToArray(o))){var i=0;var F=function F(){};return{s:F,n:function n(){if(i>=o.length)return{done:true};return{done:false,value:o[i++]}},e:function e(_e){throw _e},f:F}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var it,normalCompletion=true,didErr=false,err;return{s:function s(){it=o[Symbol.iterator]()},n:function n(){var step=it.next();normalCompletion=step.done;return step},e:function e(_e2){didErr=true;err=_e2},f:function f(){try{if(!normalCompletion&&it.return!=null)it.return()}finally{if(didErr)throw err}}}}function _toConsumableArray(arr){return _arrayWithoutHoles(arr)||_iterableToArray(arr)||_unsupportedIterableToArray(arr)||_nonIterableSpread()}function _nonIterableSpread(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function _unsupportedIterableToArray(o,minLen){if(!o)return;if(typeof o==="string")return _arrayLikeToArray(o,minLen);var n=Object.prototype.toString.call(o).slice(8,-1);if(n==="Object"&&o.constructor)n=o.constructor.name;if(n==="Map"||n==="Set")return Array.from(o);if(n==="Arguments"||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n))return _arrayLikeToArray(o,minLen)}function _iterableToArray(iter){if(typeof Symbol!=="undefined"&&Symbol.iterator in Object(iter))return Array.from(iter)}function _arrayWithoutHoles(arr){if(Array.isArray(arr))return _arrayLikeToArray(arr)}function _arrayLikeToArray(arr,len){if(len==null||len>arr.length)len=arr.length;for(var i=0,arr2=new Array(len);i<len;i++){arr2[i]=arr[i]}return arr2}function _classCallCheck(instance,Constructor){if(!_instanceof(instance,Constructor)){throw new TypeError("Cannot call a class as a function")}}function _defineProperties(target,props){for(var i=0;i<props.length;i++){var descriptor=props[i];descriptor.enumerable=descriptor.enumerable||false;descriptor.configurable=true;if("value"in descriptor)descriptor.writable=true;Object.defineProperty(target,descriptor.key,descriptor)}}function _createClass(Constructor,protoProps,staticProps){if(protoProps)_defineProperties(Constructor.prototype,protoProps);if(staticProps)_defineProperties(Constructor,staticProps);return Constructor}(function(){var Teroy=function(){function Teroy(element,component){_classCallCheck(this,Teroy);this.element=document.querySelector(element);if(!this.element)throw"TEROY: ".concat(element," not found.");if(!component.render||typeof component.render!=="function")throw"TEROY: No render() function found in component.";if(typeof component.render()!=="string")throw"TEROY: Please make sure that the return from the render() function is wrapped in template literals (or any other string primitive).";this.html=component.render;this.rendered=false;this.data=new Proxy(component.data||{},this.handler())}_createClass(Teroy,[{key:"handler",value:function handler(){var component=this;return{get:function get(obj,prop){if(["[object Object]","[object Array]"].indexOf(Object.prototype.toString.call(obj[prop]))>-1)return new Proxy(obj[prop],component.handler());if(component.proxyPaused)return obj[prop];if(component.rendered)window.requestAnimationFrame((function(){return component.update()}));return obj[prop]},set:function set(obj,prop,value){component.update();obj[prop]=value;return true}}}},{key:"select",value:function select(selector){return this.element.querySelector(selector)}},{key:"selectAll",value:function selectAll(selector){return this.element.querySelectorAll(selector)}},{key:"parse",value:function parse(string){return(new DOMParser).parseFromString(string,"text/html")}},{key:"show",value:function show(){var _this=this;if(this.rendered)return console.warn("TEROY: Component is already showing on page, no need to show it again.");this.DOM=this.parse(this.html());Array.from(this.DOM.body.childNodes).forEach((function(child){return _this.element.appendChild(child)}));this.rendered=true}},{key:"diffAttributes",value:function diffAttributes(newAttrs,oldAttrs,node){if(newAttrs===oldAttrs)return;var allAttrs=new Set([].concat(_toConsumableArray(newAttrs),_toConsumableArray(oldAttrs)).map((function(i){return i.name})));var _iterator=_createForOfIteratorHelper(allAttrs),_step;try{for(_iterator.s();!(_step=_iterator.n()).done;){var attr=_step.value;var _o=oldAttrs.getNamedItem(attr);var _n=newAttrs.getNamedItem(attr);if(!_n)node.removeAttribute(attr);else if(!_o)node.setAttribute(attr,_n.value);else if(_n.value!==_o.value)node.setAttribute(attr,_n.value)}}catch(err){_iterator.e(err)}finally{_iterator.f()}}},{key:"diff",value:function diff(newNode,oldNode,root){if(!oldNode)return root.appendChild(n);if(!newNode)return root.removeChild(o);if(newNode.isEqualNode(oldNode))return;var OLD_CHILDREN=oldNode.childNodes;var NEW_CHILDREN=newNode.childNodes;var MAX=Math.max(OLD_CHILDREN.length,NEW_CHILDREN.length);var cur_idx=-1;while(++cur_idx<MAX){var _o2=OLD_CHILDREN[cur_idx];var _n2=NEW_CHILDREN[cur_idx];if(_o2===_n2||_o2&&_n2&&_n2.isEqualNode(_o2))continue;if(!_o2)root.appendChild(_n2);else if(!_n2)root.removeChild(_o2);else if(_n2.nodeType!==_o2.nodeType||_n2.nodeName!==_o2.nodeName||_n2.nodeValue!==_o2.nodeValue)root.replaceChild(_n2,_o2);else{this.diffAttributes(_n2.attributes,_o2.attributes,_o2);this.diff(_n2,_o2,_o2)}}}},{key:"update",value:function update(){this.proxyPaused=true;this.DOM=this.parse(this.html.apply(this));this.diff(this.DOM.body,this.element,this.element);delete this.proxyPaused}}]);return Teroy}();if(typeof define==="function"&&define.amd)define((function(){return Teroy}));else if(typeof module!=="undefined"&&module.exports)module.exports=Teroy;else window.Teroy=Teroy}).call(void 0);