-
Notifications
You must be signed in to change notification settings - Fork 0
/
penjs.min.js
1 lines (1 loc) · 15.1 KB
/
penjs.min.js
1
!function(e){function t(e){function t(t,i,o){if(!(o<=i)){var a={id:(m++).toString(36),type:t,pos:i,endpos:o};return"text"!==t&&"comment"!==t||(a.value=e.slice(i,o)),r=o,n.push(a),a}}for(var n=[],r=0;r<e.length;){var i=e.slice(r).match(/([^\S\n]*)(?:<(!--)|<\/(:?[\w_]+[\w_-]*[\w_]|[\w_]+)\s*>|<(:?[\w_]+[\w_-]*[\w_]|[\w_]+)\s*)/);if(!i)break;t("text",r,r+i.index);var o=i[0].length,a=i[1];if(i[2]){if(!(i=e.slice(r+o).match(/-->/))){t("comment",r,e.length).indent=a;break}o+=i.index+i[0].length;t("comment",r,r+o).indent=a}else{var u=i[3];if(u){var s=t("right",r,r+o);s.tag=u,s.indent=a}else{u=i[4];for(var c=[];;){if(!(i=e.slice(r+o).match(/^\s*([:@]?[\w_]+[\w_\-.]*[\w_]|[\w_]+)\s*/)))break;o+=i[0].length;var l=i[1],d="",f="";switch(i=e.slice(r+o).match(/^\s*=\s*((')([^']*)'|(")([^"]*)"|([^'"\s\/>]+))\s*/),i&&(o+=i[0].length,d=i[1],f=i[2]||i[4]||""),f){case'"':case"'":d=d.slice(1,-1)}c.push({name:l,value:d,quoted:f})}if(!(i=e.slice(r+o).match(/^\s*(\/?)>/)))break;o+=i[0].length;var p=i[1]||v.indexOf(u)>=0,h=t(p?"single":"left",r,r+o);h.tag=u,h.attrs=c,h.indent=a,h.selfClosing=v.indexOf(u)>=0}}}return t("text",r,e.length),n}function n(e){var n={id:(m++).toString(36),type:"root",pos:0,endpos:e.length,children:[]},r=n,i=t(e),o=[];return i.forEach(function(t){switch(t.type){case"comment":case"single":case"text":r.children.push(t),r.endpos=t.endpos;break;case"left":t.children=[],o.push(t),r.children.push(t),r=t;break;case"right":var i=void 0,a=void 0;if(o.length<=0)throw i=e.slice(0,t.endpos).split("\n"),i.length,i[i.length-1].length+1,a="No start tag. (line:"+t.line+" col:"+t.col+")",console.error(a),a;for(var u=o.length-1;u>=0;u--){var s=o[u],c=o[u-1];if(s.tag===t.tag){s.type="block",s.endpos=t.endpos,r=c||n,r.endpos=s.endpos,o=o.slice(0,u);break}if(!c)throw i=e.slice(0,t.endpos).split("\n"),i.length,i[i.length-1].length+1,a="No start tag. (line:"+t.line+" col:"+t.col+")",console.error(a),a;s.type="text",delete s.children,delete s.tag,delete s.attrs}}}),n}function r(e,t,n){if(!e)return"";var i=e.indent||"";if(n&&n(e,t),e.overwriteNode)return e.overwriteNode;var o="";return e.beforebegin&&(o+=e.beforebegin),"text"===e.type||"comment"===e.type?o+=e.value:e.tag&&(o&&"\n"!==o[o.length-1]||(o+=i),o+="<"+e.tag,"string"==typeof e.overwriteAttrs?e.overwriteAttrs&&(o+=" "+e.overwriteAttrs):e.attrs.forEach(function(e){o+=" "+e.name,e.value&&(o+="="+e.quoted+e.value+e.quoted)}),"single"===e.type?(e.selfClosing||(o+="/"),o+=">"):o+=">"),e.selfClosing||"single"===e.type||(e.beforeend&&(o+=e.beforeend),e.children&&e.children.forEach(function(i){i.parent=e,o+=r(i,t,n)}),e.afterbegin&&(o+=e.afterbegin),e.tag&&("\n"===o[o.length-1]&&(o+=i),o+="</"+e.tag+">")),e.afterend&&(o+=e.afterend),o}function i(e,t,n){function r(r,i){if(!n||n(r)){var o=Object.getOwnPropertyDescriptor(e,r);if(!o||!1!==o.configurable){var a=o&&o.get,u=o&&o.set;Object.defineProperty(e,r,{enumerable:!0,configurable:!0,get:function(){return a?a.call(e):i},set:function(n){n!==(a?a.call(e):i)&&(u?u.call(e,n):i=n,t(e))}})}}}t&&(Array.isArray(e)?["push","pop","shift","unshift","splice","sort","reverse"].forEach(function(n){var r=e[n];Object.defineProperty(e,n,{value:function(){var n=r.apply(this,arguments);return t(e),n},enumerable:!1,writable:!0,configurable:!0})}):Object.keys(e).forEach(function(t){r(t,e[t])}))}function o(e,t){var n=e.indent||"";if("root"===e.type)return e.beforebegin=n+"/***/ var _rootScope_ = "+t+".bind(this, { root: true }, null, function (_output_, _scope_) {",void(e.afterend="\n"+n+"/***/ }); _rootScope_.innerRender(_output_); "+t+".$$scope = _rootScope_;");if(e.tag&&e.attrs&&e.attrs.length){if(":template"===e.tag)return void e.attrs.some(function(r){if("name"===r.name)return e.overwriteNode="\n"+n+"/***/ _output_.push("+t+".templateRender("+JSON.stringify(r.value)+", _scope_, "+t+".bind));",!0});var r,i="\n"+n+"/***/ var _attrs_ = [\n";e.attrs.forEach(function(o){var a;if(":"===o.name[0])":bind"===o.name?(e.beforebegin="\n"+n+"/***/ "+t+".bind("+o.value+", _scope_, function (_output_, _scope_, holdInner) {\n",e.beforeend="\n"+n+"/***/ _scope_.innerRender = function(_output_) {\n",e.afterbegin="\n"+n+"/***/ }; if (holdInner) { _scope_.innerRender(_output_); }\n",e.afterend="\n"+n+"/***/ }).outerRender(_output_, true);\n"):":depend"===o.name&&(e.beforebegin="\n"+n+"/***/ "+t+".depend("+o.value+", _scope_, function (_output_, _scope_) {\n",e.afterend="\n"+n+"/***/ }).outerRender(_output_);\n"),r=!0,a=o.value;else if("@"===o.name[0]){var u=o.name.split("."),s=u[1];a=s?"function (event) { if ("+t+".eventChecker(event, "+JSON.stringify(s)+")) { with ("+t+"._imports || {}) { "+o.value+" }}}":"function (event) { with ("+t+"._imports || {}) { "+o.value+" }}",r=!0}else a=JSON.stringify(o.value);i+=n+"/***/ { name: "+JSON.stringify(o.name)+", value: "+a+", quoted: "+JSON.stringify(o.quoted)+"},\n"}),r&&(e.beforebegin=e.beforebegin||"",i+=n+"/***/ ];\n",e.beforebegin+=i,e.overwriteAttrs="!#{"+t+"._attrsRender(_scope_, _attrs_, { tag: "+JSON.stringify(e.tag)+", id: "+JSON.stringify(e.id)+" })}")}}function a(e){return!!/^.*#\{[^}]*\}.*$/.test(e)||(!!/^[ \t]*[&=:|].*$/.test(e)||(!!/^[ \w\t_$]*([^&\^?|\n\w\/'"{}\[\]+\-():;,!` \t=\.$_]|:\/\/).*$/.test(e)||(!!/^\s*[`\-+'"]{3,}/.test(e)||!!/^(?!\s*(else|do|try|finally|void|typeof\s[\w$_]*)\s*$)[^'"`!:;{}()\[\],\n|=&\/^?]+$/.test(e))))}function u(e){if(!e)return function(){return""};var t=String(e).split(/\n\r?/).map(function(e,t,n){if(/^\s*$/.test(e))return e;if(a(e)){var r=[],i=0;e.replace(/(!?#)\{((?:"(?:[^\\"]|(?:\\.))*"|'(?:[^\\']|(?:\\.))*'|(?:[^{}]*\{[^}]*\})?|[^}]*)*)\}/g,function(t,n,o,a){var u=e.slice(i,a);if(u&&r.push(JSON.stringify(u)),i=a+t.length,o)switch(/^[a-z$][\w$]+$/i.test(o)&&!/^(true|false|NaN|null|this)$/.test(o)&&(o="typeof "+o+"==='undefined'?'':"+o),n){case"#":r.push("_encode_("+o+")");break;case"!#":r.push("("+o+")")}});var o=e.slice(i,e.length);return o&&r.push(JSON.stringify(o)),t<n.length-1&&r.push('"\\n"'),"_output_.push("+r+");"}return e});return t.unshift("with(this){"),t.push("}"),new Function("_output_","_encode_","helper",t.join("\n"))}function s(e){return String(e).replace(/["<>& ']/g,function(e){return"&"+y[e]+";"})}function c(e,t,n){"function"==typeof e&&(e=String(e).replace(/[^]*\/\*!?\s*|\s*\*\/[^]*/g,""));var r=u(e),i=function(e,t){var n=[];return void 0===t&&(t=function(e){r.call(e,n,s,t)}),r.call(e,n,s,t),n.join("")};return arguments.length<=1?i:i(t,n)}function l(e,t,n){function r(n){var c=[].slice.call(e.querySelectorAll(t));for(a=n.target;a&&c.indexOf(a)<0;)a=a.parentNode;if(a)if(u=Date.now(),/touchstart/i.test(n.type)){s||(s=!0,e.removeEventListener("mousedown",r),document.removeEventListener("mouseup",i));var l=(n.touches||{})[0]||{};o=[l.clientX||0,l.clientY||0]}else o=[n.clientX,n.clientY]}function i(e){if(o){var t;if(/touchend/i.test(e.type)){var r=(e.touches||{})[0]||(e.changedTouches||{})[0]||{};t=[r.clientX||0,r.clientY||0]}else t=[e.clientX,e.clientY];Math.sqrt(Math.pow(o[0]-t[0],2)+Math.pow(o[1]-t[1],2))<50&&n(a,Date.now()-u),o=null,a=null}}"string"==typeof e&&(e=document.querySelector(e));var o,a,u,s;e.addEventListener("touchstart",r,!1),document.addEventListener("touchend",i,!1),e.addEventListener("mousedown",r,!1),document.addEventListener("mouseup",i,!1),window.addEventListener("scroll",function(){o=null,a=null})}function d(e,t){E[e]=t}function f(e,t,n,r){r=r||function(){};var i=new XMLHttpRequest;i.open(t,e,!0),i.onreadystatechange=function(){if(4==i.readyState){var e=i.responseText;try{e=JSON.parse(e),r(null,e,i)}catch(t){r(t.message,e,i)}}},i.send(n?n(i):null)}function p(e,t,n){f(e,"POST",function(e){return e.setRequestHeader("Content-type","application/x-www-form-urlencoded"),Object.keys(t).map(function(e){return e+"="+encodeURIComponent(t[e])}).join("&")},n)}function h(e,t){f(e,"GET",null,t)}var v=["area","base","br","col","embed","hr","img","input","keygen","link","menuitem","meta","param","source","track","wbr"],m=0,g={parse:n,build:r},b=0,_=function(){function e(e){var t=this;this._adapters={},e=e||{},this._binds={},this._templates={},this._adapters={},this._bindObjectName=e.bindObjectName||"jnodes.binder",this._bindAttributeName=e.bindAttributeName||"bind",this._dependAttributeName=e.dependAttributeName||"depend",this._scopeAttributeName=e.scopeAttributeName||"data-jnodes-scope",this._eventAttributePrefix=e.eventAttributePrefix||"data-jnodes-event-",this._imports=e.imports,this._templates={},this._checkers={},this._findElement=e.findElement||function(e){return document.querySelector("["+t._scopeAttributeName+'="'+e.id+'"]')},this._updateElement=e.updateElement||function(e,n){if(e&&(n.outerRender||n.innerRender)){t.lifecycleEvent(n,"destroy"),t.cleanChildren(n);var r=[];if(n.innerRender)if(n.outerRender){n.outerRender(r,!1);var i=r.join("");r=[],n.shell===i?(n.innerRender(r),e.innerHTML=r.join("")):(n.shell=i,n.outerRender(r,!0),e.outerHTML=r.join(""))}else n.innerRender(r),e.innerHTML=r.join("");else n.outerRender(r,!0),e.outerHTML=r.join("");t.lifecycleEvent(n,"create"),t.lifecycleEvent(n,"update")}},this._attrsRender=e.attrsRender||function(e,n,r){if(!n)return"";var i={},o={},a=!1;return n.filter(function(n){if(":"!==n.name[0]&&"@"!==n.name[0])return!0;var r=n.name.slice(1);if(r!==t._bindAttributeName&&r!==t._dependAttributeName)return!0;r=t._scopeAttributeName,o[r]=n.quoted,i[r]=[e.id],a=!0,e.methods=e.methods||{},Object.keys(e.methods).forEach(function(t){"function"==typeof e.methods[t]&&(e.methods[t].$$scope||delete e.methods[t])})}).filter(function(n){if(":"!==n.name[0]&&"@"!==n.name[0])return!0;var r=n.name.slice(1);if("@"===n.name[0]){r=r.split(".")[0],"create"===r?e.lifecycleCreate=!0:"destroy"===r?e.lifecycleDestroy=!0:"update"===r&&(e.lifecycleUpdate=!0),r=t._eventAttributePrefix+r}var u=i[r]=i[r]||[];if(o[r]=n.quoted,""!==n.value&&null!==n.value&&void 0!==n.value&&!1!==n.value)switch(typeof n.value){case"boolean":case"number":case"string":u.push(n.value);break;case"object":Object.keys(n.value).forEach(function(e){n.value[e]&&u.push(e)});break;case"function":e.methods=e.methods||{};var s=void 0;a&&(s=e.methods["v:"+n.value]),s||(s="@"+(b++).toString(36),e.methods[s]=n.value,a&&(n.value.$$scope=e,e.methods["v:"+n.value]=s)),u.push(s)}}).forEach(function(e){var t=i[e.name]=i[e.name]||[];o[e.name]=e.quoted,""===e.value||t.indexOf(e.value)>=0||t.push(e.value)}),Object.keys(i).map(function(e){var t=i[e];if(t.length<=0)return null;var n=o[e]||"";return 1===t.length&&!0===t[0]?""+e:e+"="+n+t.join(" ")+n}).join(" ")}}return e.prototype.registerTemplate=function(e,t){this._templates[e]=t},e.prototype.templateRender=function(e,t){var n=this._templates[e];if(n)return n(t)},e.prototype.registerChecker=function(e,t){this._checkers[e]=t},e.prototype.eventChecker=function(e,t){var n=this._checkers[e.type];if(n)return n(e,t)},e.prototype.templateAdapter=function(e,t){var n=this._adapters[e];if(n)return n(t,this._bindObjectName)},e.prototype.registerAdapter=function(e,t){this._adapters[e]=t},e.prototype.cleanChildren=function(e){var t=this;e.children&&e.children.forEach(function(n){var r=n.model&&n.model.$$binds&&n.model.$$binds();if(r){var i=r.indexOf(n);i>=0&&r.splice(i,1)}delete t._binds[n.id],t.cleanChildren(n),e.children=[],delete e.methods,delete e.attrs})},e.prototype.lifecycleEvent=function(e,t){function n(e){return"update"!==t?!!("create"===t&&e.lifecycleCreate||"destroy"===t&&e.lifecycleDestroy)||(e.children?e.children.some(n):void 0):!!e.lifecycleUpdate||void 0}var r=this;if(n(e)){var i=this.element(e);if("update"===t)this.triggerScopeEvent({type:t,target:i});else{[].slice.apply(i.querySelectorAll("["+this._eventAttributePrefix+t+"]")).forEach(function(e){r.triggerScopeEvent({type:t,target:e}),e.removeAttribute(""+r._eventAttributePrefix+t)})}}},e.prototype.bind=function(e,t,n,r){var i=this,o={type:"bind",model:e,parent:t,binder:this};return Object.defineProperty(o,"element",{enumerable:!0,configurable:!0,get:function(){return o._element},set:function(e){o._element=e,e&&(e.setAttribute(i._scopeAttributeName,o.id),i.lifecycleEvent(o,"create"))}}),n&&(o.outerRender=function(e,t){return n(e,o,t)}),r&&(o.innerRender=function(e){return r(e,o)}),o.id=(b++).toString(36),this._binds[o.id]=o,this.observer(e,o),o},e.prototype.observer=function(e,t){function n(e,t){var r=t.parent;r.model.$$binds&&r.model.$$binds().forEach(function(t){"depend"!==t.type?e.indexOf(t)<0&&e.push(t):n(e,t)})}var r=t.parent;if(r&&(r.children=r.children||[],r.children.push(t)),e&&"object"==typeof e)if(e.$$binds)e.$$binds().push(t);else{var o=[t];e.$$binds=function(){return o},i(e,function(){var t=[];e.$$binds().forEach(function(e){"depend"!==e.type?e.binder.update(e):n(t,e)}),t.forEach(function(e){e.binder.update(e)})},function(e){return e&&"$$"!==e.slice(2)})}},e.prototype.depend=function(e,t,n){var r={type:"depend",model:e,parent:t,binder:this,outerRender:function(e){return n(e,r,!0)}};return this.observer(e,r),r.id=(b++).toString(36),this._binds[r.id]=r,r},e.prototype.element=function(e){return e._element||this._findElement(e)},e.prototype.update=function(e){e&&this._updateElement(this.element(e),e)},e.prototype.scope=function(e){var t;if("string"==typeof e){if(t=this._binds[e])return t}else for(var n=e;n&&n.getAttribute;){var r=this.scope(n.getAttribute(this._scopeAttributeName));if(r)return r;n=n.parentNode}},e.prototype.triggerScopeEvent=function(e,t){if(t=t||e.target){var n=t.getAttribute(this._eventAttributePrefix+e.type);if(n&&"@"===n[0]){var r=this.scope(t);if(!r)return;n.replace(/@\w+/g,function(n){var i=(r.methods||{})[n];return i&&i.call(t,e),""})}}},e}(),y={'"':"#34","'":"#39","<":"lt",">":"gt","&":"amp"," ":"nbsp"},w=0,E={},A=function(e,t){function n(e,t,n){for(var r=[].slice.call(e.querySelectorAll(n));t&&r.indexOf(t)<0;)t=t.parentNode;return t}function r(e){c.triggerScopeEvent(e)}function i(e,t){return({esc:[27],tab:[9],enter:[13],space:[32],up:[38],left:[37],right:[39],down:[40],delete:[8,46]}[t]||[]).indexOf(e.keyCode)>=0}if(t=t||{},t.data=t.data||{},"string"==typeof e&&(e=document.querySelector(e)),e){var o,a;if(/script/i.test(e.tagName)?(a=e,o=e.parentNode):(a=e.querySelector("script"),o=e),a&&o){var u=String(a.getAttribute("type")).match(/text\/([\w]+)/);if(u){var s="binder"+(w++).toString(36),c=A[s]=new _({bindObjectName:"penjs."+s,imports:function(e,t){if(e){var n={};return Object.keys(e).forEach(function(r){var i=e[r];n[r]="function"==typeof i?function(){i.apply(t,arguments)}:i}),n}}(t.methods,t.data)});"function"==typeof t.init&&t.init.call(t.data,c),["click","dblclick","keydown","keyup","focusin","focusout","change"].forEach(function(e){o.addEventListener(e,function(t){t.target.getAttribute(c._eventAttributePrefix+"input")&&("focusin"===e?t.target.addEventListener("input",r):"focusout"===e&&t.target.removeEventListener("input",r));var i=n(o,t.target,"["+c._eventAttributePrefix+e+"]");i&&c.triggerScopeEvent(t,i)})}),l(o,"["+c._eventAttributePrefix+"tap]",function(e,t){c.triggerScopeEvent({type:"tap",target:e})}),c.registerChecker("keyup",i),c.registerChecker("keydown",i);var d=u[1],f=E[d];f&&f(c);var p=c.templateAdapter(d,a.innerHTML);return p&&(o.innerHTML=p(t.data),c.$$scope.element=o),t.data}}}};A.Parser=g,A.registerAdapter=d,d("jhtmls",function(e){e.registerAdapter("jhtmls",function(e,t){return c(g.build(g.parse(e),t,o))})}),A.Ajax={send:f,post:p,get:h},"undefined"!=typeof window&&(window.penjs=A),"function"==typeof define?(define.amd||define.cmd)&&define(function(){return A}):"undefined"!=typeof module&&module.exports&&(module.exports=A)}();