UNPKG

@codevadmin/jquery-tmpl

Version:

The library was deprecated but some projects are still using it. This is jquery-tmpl commonjs implementation.

10 lines 6.17 kB
/*! * jQuery Templates Plugin 1.0.0pre * http://github.com/jquery/jquery-tmpl * Requires jQuery 1.4.2 * * Copyright 2011, Software Freedom Conservancy, Inc. * Dual licensed under the MIT or GPL Version 2 licenses. * http://jquery.org/license */ t;t=function(t){var e,n=t.fn.domManip,l="_tmplitem",r=/^[^<]*(<[\w\W]+>)[^>]*$|\{\{\! /,a={},p={},i={key:0,data:{}},o=0,u=0,c=[];function f(e,n,l,r){var i={data:r||0===r||!1===r?r:n?n.data:{},_wrap:n?n._wrap:null,tmpl:null,parent:n||null,nodes:[],calls:y,nest:g,wrap:v,html:w,update:j};return e&&t.extend(i,e,{nodes:[],parent:n}),l&&(i.tmpl=l,i._ctnt=i._ctnt||i.tmpl(t,i),i.key=++o,(c.length?p:a)[o]=i),i}function m(e,n,r){var a,p=r?t.map(r,function(t){return"string"==typeof t?e.key?t.replace(/(<\w+)(?=[\s>])(?![^>]*_tmplitem)([^>]*)/g,"$1 "+l+'="'+e.key+'" $2'):t:m(t,e,t._ctnt)}):e;return n?p:((p=p.join("")).replace(/^\s*([^<\s][^<]*)?(<[\w\W]+>)([^>]*[^>\s])?\s*$/,function(e,n,l,r){h(a=t(l).get()),n&&(a=d(n).concat(a)),r&&(a=a.concat(d(r)))}),a||d(p))}function d(e){var n=document.createElement("div");return n.innerHTML=e,t.makeArray(n.childNodes)}function s(e){return new Function("jQuery","$item","var $=jQuery,call,__=[],$data=$item.data;with($data){__.push('"+t.trim(e).replace(/([\\'])/g,"\\$1").replace(/[\r\t\n]/g," ").replace(/\$\{([^\}]*)\}/g,"{{= $1}}").replace(/\{\{(\/?)(\w+|.)(?:\(((?:[^\}]|\}(?!\}))*?)?\))?(?:\s+(.*?)?)?(\(((?:[^\}]|\}(?!\}))*?)\))?\s*\}\}/g,function(e,n,l,r,a,p,i){var o,u,c,f=t.tmpl.tag[l];if(!f)throw"Unknown template tag: "+l;return o=f._default||[],p&&!/\w$/.test(a)&&(a+=p,p=""),a?(a=_(a),i=i?","+_(i)+")":p?")":"",u=p?a.indexOf(".")>-1?a+_(p):"("+a+").call($item"+i:a,c=p?u:"(typeof("+a+")==='function'?("+a+").call($item):("+a+"))"):c=u=o.$1||"null",r=_(r),"');"+f[n?"close":"open"].split("$notnull_1").join(a?"typeof("+a+")!=='undefined' && ("+a+")!=null":"true").split("$1a").join(c).split("$1").join(u).split("$2").join(r||o.$2||"")+"__.push('"})+"');}return __;")}function $(e,n){e._wrap=m(e,!0,t.isArray(n)?n:[r.test(n)?n:t(n).html()]).join("")}function _(t){return t?t.replace(/\\'/g,"'").replace(/\\\\/g,"\\"):null}function h(e){var n,r,i,c,m,d="_"+u,s={};for(i=0,c=e.length;i<c;i++)if(1===(n=e[i]).nodeType){for(m=(r=n.getElementsByTagName("*")).length-1;m>=0;m--)$(r[m]);$(n)}function $(e){var n,r,i,c,m=e;if(c=e.getAttribute(l)){for(;m.parentNode&&1===(m=m.parentNode).nodeType&&!(n=m.getAttribute(l)););n!==c&&(m=m.parentNode?11===m.nodeType?0:m.getAttribute(l)||0:0,(i=a[c])||((i=f(i=p[c],a[m]||p[m])).key=++o,a[o]=i),u&&$(c)),e.removeAttribute(l)}else u&&(i=t.data(e,"tmplItem"))&&($(i.key),a[i.key]=i,m=(m=t.data(e.parentNode,"tmplItem"))?m.key:0);if(i){for(r=i;r&&r.key!=m;)r.nodes.push(e),r=r.parent;delete i._ctnt,delete i._wrap,t.data(e,"tmplItem",i)}function $(t){i=s[t+=d]=s[t]||f(i,a[i.parent.key+d]||i.parent)}}}function y(t,e,n,l){if(!t)return c.pop();c.push({_:t,tmpl:e,item:this,data:n,options:l})}function g(e,n,l){return t.tmpl(t.template(e),n,l,this)}function v(e,n){var l=e.options||{};return l.wrapped=n,t.tmpl(t.template(e.tmpl),e.data,l,e.item)}function w(e,n){var l=this._wrap;return t.map(t(t.isArray(l)?l.join(""):l).filter(e||"*"),function(t){return n?t.innerText||t.textContent:t.outerHTML||(e=t,(l=document.createElement("div")).appendChild(e.cloneNode(!0)),l.innerHTML);var e,l})}function j(){var e=this.nodes;t.tmpl(null,null,null,this).insertBefore(e[0]),t(e).remove()}return t.each({appendTo:"append",prependTo:"prepend",insertBefore:"before",insertAfter:"after",replaceAll:"replaceWith"},function(n,l){t.fn[n]=function(r){var p,i,o,c,f=[],m=t(r),d=1===this.length&&this[0].parentNode;if(e=a||{},d&&11===d.nodeType&&1===d.childNodes.length&&1===m.length)m[l](this[0]),f=this;else{for(i=0,o=m.length;i<o;i++)u=i,p=(i>0?this.clone(!0):this).get(),t(m[i])[l](p),f=f.concat(p);u=0,f=this.pushStack(f,n,m.selector)}return c=e,e=null,t.tmpl.complete(c),f}}),t.fn.extend({tmpl:function(e,n,l){return t.tmpl(this[0],e,n,l)},tmplItem:function(){return t.tmplItem(this[0])},template:function(e){return t.template(e,this[0])},domManip:function(l,r,p,i){if(l[0]&&t.isArray(l[0])){for(var o,c=t.makeArray(arguments),f=l[0],m=f.length,d=0;d<m&&!(o=t.data(f[d++],"tmplItem")););o&&u&&(c[2]=function(e){t.tmpl.afterManip(this,e,p)}),n.apply(this,c)}else n.apply(this,arguments);return u=0,e||t.tmpl.complete(a),this}}),t.extend({tmpl:function(e,n,l,r){var o,u=!r;if(u)r=i,e=t.template[e]||t.template(null,e),p={};else if(!e)return e=r.tmpl,a[r.key]=r,r.nodes=[],r.wrapped&&$(r,r.wrapped),t(m(r,null,r.tmpl(t,r)));return e?("function"==typeof n&&(n=n.call(r||{})),l&&l.wrapped&&$(l,l.wrapped),o=t.isArray(n)?t.map(n,function(t){return t?f(l,r,e,t):null}):[f(l,r,e,n)],u?t(m(r,null,o)):o):[]},tmplItem:function(e){var n;for(e instanceof t&&(e=e[0]);e&&1===e.nodeType&&!(n=t.data(e,"tmplItem"))&&(e=e.parentNode););return n||i},template:function(e,n){return n?("string"==typeof n?n=s(n):n instanceof t&&(n=n[0]||{}),n.nodeType&&(n=t.data(n,"tmpl")||t.data(n,"tmpl",s(n.innerHTML))),"string"==typeof e?t.template[e]=n:n):e?"string"!=typeof e?t.template(null,e):t.template[e]||t.template(null,r.test(e)?e:t(e)):null},encode:function(t){return(""+t).split("<").join("&lt;").split(">").join("&gt;").split('"').join("&#34;").split("'").join("&#39;")}}),t.extend(t.tmpl,{tag:{tmpl:{_default:{$2:"null"},open:"if($notnull_1){__=__.concat($item.nest($1,$2));}"},wrap:{_default:{$2:"null"},open:"$item.calls(__,$1,$2);__=[];",close:"call=$item.calls();__=call._.concat($item.wrap(call,__));"},each:{_default:{$2:"$index, $value"},open:"if($notnull_1){$.each($1a,function($2){with(this){",close:"}});}"},if:{open:"if(($notnull_1) && $1a){",close:"}"},else:{_default:{$1:"true"},open:"}else if(($notnull_1) && $1a){"},html:{open:"if($notnull_1){__.push($1a);}"},"=":{_default:{$1:"$data"},open:"if($notnull_1){__.push($.encode($1a));}"},"!":{open:""}},complete:function(t){a={}},afterManip:function(e,n,l){var r=11===n.nodeType?t.makeArray(n.childNodes):1===n.nodeType?[n]:[];l.call(e,n),h(r),u++}}),t},"function"==typeof define&&define.amd?define(["jquery"],t):"object"==typeof exports&&"undefined"!=typeof module?module.exports=t(require("jquery")):t(jQuery)