You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1 line
8.3 KiB
JavaScript

!function(){var e={prefix:"jugl",namespaceURI:null,loadTemplate:function(e){o(e.url,(function(t){var n,o;if(!t.status||t.status>=200&&t.status<300){try{n=t.responseXML,o=new r(n.documentElement)}catch(e){(n=document.createElement("div")).innerHTML=t.responseText,o=new r(n.firstChild)}e.callback&&e.callback.call(e.scope,o)}else e.failure&&e.failure.call(e.scope,t)}))}},t=function(e,t){for(var n in e=e||{},t=t||{})e[n]=t[n];return e},n=function(e,t){var n,o,i,r,s;if("string"==typeof e){if(!(n=document.getElementById(e)))throw Error("Element id not found: "+e);e=n}if("string"==typeof t){if(!(n=document.getElementById(t)))throw Error("Element id not found: "+t);t=n}if(t.namespaceURI&&t.xml)for((o=document.createElement("div")).innerHTML=t.xml,r=0,s=(i=o.childNodes).length;r<s;++r)e.appendChild(i[r]);else e.ownerDocument&&e.ownerDocument.importNode&&e.ownerDocument!==t.ownerDocument&&(t=e.ownerDocument.importNode(t,!0)),e.appendChild(t);return t},o=function(e,t,n){var o;if("undefined"!=typeof XMLHttpRequest)o=new XMLHttpRequest;else{if("undefined"==typeof ActiveXObject)throw new Error("XMLHttpRequest not supported");o=new ActiveXObject("Microsoft.XMLHTTP")}o.open("GET",e),o.onreadystatechange=function(){4===o.readyState&&t.call(n,o)},o.send(null)},i=function(e,t){this.template=e,this.node=t,this.scope={},this.scope.repeat={}};t(i.prototype,{clone:function(){var e=this.node.cloneNode(!0);e.removeAttribute("id");var n=new i(this.template,e);return t(n.scope,this.scope),n},getAttribute:function(t){var n;return 1===this.node.nodeType&&(n=this.template.usingNS?this.node.getAttributeNodeNS(e.namespaceURI,t):this.node.getAttributeNode(e.prefix+":"+t))&&!n.specified&&(n=!1),n?new s(this,n,t):n},setAttribute:function(e,t){this.node.setAttribute(e,t)},removeAttributeNode:function(e){this.node.removeAttributeNode(e.node)},getChildNodes:function(){for(var e,n=this.node.childNodes.length,o=new Array(n),r=0;r<n;++r)(e=new i(this.template,this.node.childNodes[r])).scope=t({},this.scope),o[r]=e;return o},removeChildNodes:function(){for(;this.node.hasChildNodes();)this.node.removeChild(this.node.firstChild)},removeChild:function(e){return this.node.removeChild(e.node),node},removeSelf:function(){this.node.parentNode.removeChild(this.node)},importNode:function(e){this.node.ownerDocument&&this.node.ownerDocument.importNode&&e.node.ownerDocument!==this.node.ownerDocument&&(e.node=this.node.ownerDocument.importNode(e.node,!0))},appendChild:function(e){this.importNode(e),this.node.appendChild(e.node)},insertAfter:function(e){this.importNode(e);var t=this.node.parentNode,n=this.node.nextSibling;n?t.insertBefore(e.node,n):t.appendChild(e.node)},insertBefore:function(e){this.importNode(e),this.node.parentNode.insertBefore(e.node,this.node)},process:function(){for(var e,t=["define","condition","repeat"],n=0,o=t.length;n<o;++n)if((e=this.getAttribute(t[n]))&&!e.process())return;var i=this.getAttribute("content");if(i)i.process();else{var r=this.getAttribute("replace");r&&r.process()}var s=this.getAttribute("attributes");s&&s.process(),i||r||this.processChildNodes();var l=this.getAttribute("omit-tag");l&&l.process();var d=this.getAttribute("reflow");d&&d.process()},processChildNodes:function(){for(var e=this.getChildNodes(),t=0,n=e.length;t<n;++t)e[t].process()}});var r=function(e){if("string"!=typeof(e=e||{})&&1!==e.nodeType||(e={node:e}),"string"==typeof e.node&&(e.node=document.getElementById(e.node),!e.node))throw Error("Element id not found: "+e.node);e.node?(this.node=e.node,this.loaded=!0):e.url&&this.load({url:e.url,callback:e.callback,scope:e.scope})};t(r.prototype,{node:null,usingNS:!1,xmldom:window.ActiveXObject?new ActiveXObject("Microsoft.XMLDOM"):null,trimSpace:/^\s*(\w+)\s+(.*?)\s*$/,loaded:!1,loading:!1,process:function(o){var r,s;return o=t({context:null,clone:!1,string:!1},o),this.usingNS=this.node.getAttributeNodeNS&&e.namespaceURI,r=new i(this,this.node),(o.clone||o.string)&&(r=r.clone()),o.context&&(r.scope=o.context),r.process(),o.string?s=r.node.innerHTML?r.node.innerHTML:this.xmldom?r.node.xml:(new XMLSerializer).serializeToString(r.node):(s=r.node,o.parent&&(o.clone?s=n(o.parent,r.node):this.appendTo(o.parent))),s},load:function(t){"string"==typeof t&&(t={url:t}),t=t||{},this.loading=!0;var n;t.failure&&(n=function(e){t.failure.call(t.scope,e)}),e.loadTemplate({url:t.url,callback:function(e){this.node=e.node,this.loading=!1,this.loaded=!0,t.callback&&t.callback.apply(t.scope,[e])},failure:n,scope:this})},appendTo:function(e){return this.node=n(e,this.node),this}});var s=function(e,t,n){this.element=e,this.node=t,this.type=n,this.nodeValue=t.nodeValue,this.nodeName=t.nodeName,this.template=e.template};t(s.prototype,{splitAttributeValue:function(e){e=null!=e?e:this.nodeValue;var t=this.template.trimSpace.exec(e);return t&&3===t.length&&[t[1],t[2]]},splitExpressionPrefix:function(){var e=this.splitAttributeValue();return(!e||"structure"!=e[0]&&"text"!=e[0])&&(e=[null,this.nodeValue]),e},getAttributeValues:function(){return this.nodeValue.replace(/[\t\n]/g,"").replace(/;\s*$/,"").replace(/;;/g,"\t").split(";").join("\n").replace(/\t/g,";").split(/\n/g)},removeSelf:function(){this.element.removeAttributeNode(this)},process:function(){return this.processAttribute[this.type].apply(this,[])},evalInScope:function(e){var t=this.element.scope,n=[],o=[];for(key in t)n.push(key),o.push(t[key]);return new Function(n.join(","),"return "+e).apply({},o)},processAttribute:{define:function(){var e,t,n,o=this.getAttributeValues();for(t=0,n=o.length;t<n;++t)e=this.splitAttributeValue(o[t]),this.element.scope[e[0]]=this.evalInScope(e[1]);return this.removeSelf(),!0},condition:function(){var e=!!this.evalInScope(this.nodeValue);return this.removeSelf(),e||this.element.removeSelf(),e},repeat:function(){var e,t=this.splitAttributeValue(),n=t[0],o=this.evalInScope(t[1]);if(this.removeSelf(),!(o instanceof Array)){var i=new Array;for(var r in o)i.push(r);o=i}for(var s=this.element,l=0,d=o.length;l<d;++l)(e=this.element.clone()).scope[n]=o[l],e.scope.repeat[n]={index:l,number:l+1,even:!(l%2),odd:!!(l%2),start:0===l,end:l===d-1,length:d},s.insertAfter(e),e.process(),s=e;return this.element.removeSelf(),!1},content:function(){var e=this.splitExpressionPrefix(),t=this.evalInScope(e[1]);if(this.removeSelf(),"structure"===e[0])try{this.element.node.innerHTML=t}catch(e){var n=document.createElement("div");if(n.innerHTML=t,this.element.node.xml&&this.template.xmldom){for(;this.element.node.firstChild;)this.element.node.removeChild(this.element.node.firstChild);this.template.xmldom.loadXML(n.outerHTML);for(var o=this.template.xmldom.firstChild.childNodes,r=0,s=o.length;r<s;++r)this.element.node.appendChild(o[r])}else this.element.node.innerHTML=n.innerHTML}else{var l;l=this.element.node.xml&&this.template.xmldom?this.template.xmldom.createTextNode(t):document.createTextNode(t);var d=new i(this.template,l);this.element.removeChildNodes(),this.element.appendChild(d)}return!0},replace:function(){var e=this.splitExpressionPrefix(),t=this.evalInScope(e[1]);if(this.removeSelf(),"structure"===e[0]){var n=document.createElement("div");for(n.innerHTML=t,this.element.node.xml&&this.template.xmldom&&(this.template.xmldom.loadXML(n.outerHTML),n=this.template.xmldom.firstChild);n.firstChild;){var o=n.removeChild(n.firstChild);this.element.node.ownerDocument&&this.element.node.ownerDocument.importNode&&o.ownerDocument!=this.element.node.ownerDocument&&(o=this.element.node.ownerDocument.importNode(o,!0)),this.element.node.parentNode.insertBefore(o,this.element.node)}}else{var r;r=this.element.node.xml&&this.template.xmldom?this.template.xmldom.createTextNode(t):document.createTextNode(t);var s=new i(this.template,r);this.element.insertBefore(s)}return this.element.removeSelf(),!0},attributes:function(){for(var e,t,n,o=this.getAttributeValues(),i=0,r=o.length;i<r;++i)t=(e=this.splitAttributeValue(o[i]))[0],!1!==(n=this.evalInScope(e[1]))&&this.element.setAttribute(t,n);return this.removeSelf(),!0},"omit-tag":function(){var e=""===this.nodeValue||!!this.evalInScope(this.nodeValue);if(this.removeSelf(),e){for(var t=this.element.getChildNodes(),n=0,o=t.length;n<o;++n)this.element.insertBefore(t[n]);this.element.removeSelf()}},reflow:function(){var e=""===this.nodeValue||!!this.evalInScope(this.nodeValue);this.removeSelf(),e&&(this.element.node.outerHTML?this.element.node.outerHTML=this.element.node.outerHTML:this.element.node.innerHTML=this.element.node.innerHTML)}}}),window.jugl=t(e,{Template:r})}();