weekly release 4.0dev
[moodle.git] / lib / amd / build / templates.min.js
CommitLineData
1b7ce2dd 1define ("core/templates",["core/mustache","jquery","core/ajax","core/str","core/notification","core/url","core/config","core/localstorage","core/icon_system","core/event","core/yui","core/log","core/truncate","core/user_date","core/pending"],function(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o){var p=0,q={},r={},s={},t={},u=[],v=!1,w=["js"],x=function(a){if(a){if("moodle"!==a&&"core"!==a){return a}}return"core"},y=function(a){if(a in r){return r[a]}if(a in q){r[a]=b.Deferred().resolve(q[a]).promise();return r[a]}if(0>=M.cfg.templaterev){return null}var c=h.get("core_template/"+M.cfg.templaterev+":"+a);if(c){q[a]=c;r[a]=b.Deferred().resolve(c).promise();return r[a]}return null},z=function(){if(!u.length){return}if(v){return}v=!0;var a=u.slice(),e=b.Deferred(),f=[],g=a.map(function(a){var c=x(a.component),i=a.name,j=a.searchKey,k=a.theme,l=a.deferred,m=null,n=y(j);if(n){m=n}else{f.push({methodname:"core_output_load_template_with_dependencies",args:{component:c,template:i,themename:k,lang:b("html").attr("lang").replace(/-/g,"_")}});var o=f.length-1;m=e.promise().then(function(a){g[j]=a[o].then(function(a){var b=null;a.templates.forEach(function(a){a.component=x(a.component);var d=[k,a.component,a.name].join("/");q[d]=a.value;if(0<M.cfg.templaterev){h.set("core_template/"+M.cfg.templaterev+":"+d,a.value)}if(a.component==c&&a.name==i){b=a.value}});if(a.strings.length){d.cache_strings(a.strings.map(function(a){return{component:x(a.component),key:a.name,value:a.value}}))}return b});return g[j]})}return m.then(function(a){return l.resolve(a)}).catch(function(a){l.reject(a);throw a})});if(f.length){e.resolve(c.call(f,!0,!1,!1,0,M.cfg.templaterev))}else{e.resolve()}b.when.apply(null,g).then(function(){u.splice(0,a.length);v=!1;z()}).catch(function(){u.splice(0,a.length);v=!1;z()})},A=function(){this.requiredStrings=[];this.requiredJS=[];this.requiredDates=[];this.currentThemeName=""};A.prototype.requiredStrings=null;A.prototype.requiredDates=[];A.prototype.requiredJS=null;A.prototype.currentThemeName="";A.prototype.getTemplate=function(a){var c=this.currentThemeName,d=c+"/"+a,e=y(d);if(e){return e}var f=u.filter(function(a){return a.searchKey==d});if(f.length){return f[0].deferred.promise()}var g=a.split("/"),h=x(g.shift()),i=g.join("/"),j=b.Deferred();u.push({component:h,name:i,theme:c,searchKey:d,deferred:j});z();return j.promise()};A.prototype.prefetchTemplates=function(a,c){a.forEach(function(a){var d=c+"/"+a;if(y(d)){return}var e=u.filter(function(a){return a.searchKey==d});if(e.length){return}var f=a.split("/"),g=x(f.shift()),h=f.join("/");u.push({component:g,name:h,theme:c,searchKey:d,deferred:b.Deferred()})});z()};A.prototype.partialHelper=function(a){var b=this.currentThemeName+"/"+a;if(!(b in q)){e.exception(new Error("Failed to pre-fetch the template: "+a))}return q[b]};A.prototype.renderIcon=function(a,c,d){var f=g.iconsystemmodule;c=x(c);var h=b.Deferred();require([f],function(a){var b=new a;if(!(b instanceof i)){h.reject("Invalid icon system specified"+g.iconsystemmodule)}else{t=b;b.init().then(h.resolve).catch(e.exception)}});return h.then(function(a){return this.getTemplate(a.getTemplateName())}.bind(this)).then(function(b){return t.renderIcon(a,c,d,b)})};A.prototype.pixHelper=function(a,b,c){var d=b.split(","),e="",f="",g="";if(0<d.length){e=c(d.shift().trim(),a)}if(0<d.length){f=c(d.shift().trim(),a)}if(0<d.length){g=c(d.join(",").trim(),a)}var h=t.getTemplateName(),i=this.currentThemeName+"/"+h,j=q[i];f=x(f);e=e.replace(/&#x2F;/gi,"/");return t.renderIcon(e,f,g,j)};A.prototype.jsHelper=function(a,b,c){this.requiredJS.push(c(b,a));return""};A.prototype.stringHelper=function(a,b,c){var d=b.split(","),e="",f="",g="";if(0<d.length){e=d.shift().trim()}if(0<d.length){f=d.shift().trim()}if(0<d.length){g=d.join(",").trim()}f=x(f);if(""!==g){g=c(g,a)}if(0===g.indexOf("{")&&0!==g.indexOf("{{")){g=JSON.parse(g)}var h=this.requiredStrings.length;this.requiredStrings.push({key:e,component:f,param:g});return"[[_s"+h+"]]"};A.prototype.quoteHelper=function(a,b,c){var d=c(b.trim(),a);d=d.replace(/"/g,"\\\"").replace(/([{}]{2,3})/g,"{{=<% %>=}}$1<%={{ }}=%>").replace(/(\r\n|\r|\n)/g,"&#x0a;");return"\""+d+"\""};A.prototype.shortenTextHelper=function(a,b,c){var d=b.match(/(.*?),(.*)/),e=d[1].trim(),f=d[2].trim(),g=c(f,a);return m.truncate(g,{length:e,words:!0,ellipsis:"..."})};A.prototype.userDateHelper=function(a,b,c){var d=b.match(/(.*?),(.*)/),e=c(d[1].trim(),a),f=c(d[2].trim(),a),g=this.requiredDates.length;this.requiredDates.push({timestamp:e,format:f});return"[[_t_"+g+"]]"};A.prototype.addHelperFunction=function(a,b){return function(){return function(c,d){var e=w.reduce(function(a,c){if(b.hasOwnProperty(c)){a[c]=b[c]}return a},{});w.forEach(function(a){b[a]=function(){return""}});var f=a.apply(this,[b,c,d]);for(var g in e){b[g]=e[g]}return f}.bind(this)}.bind(this)};A.prototype.addHelpers=function(a,b){this.currentThemeName=b;this.requiredStrings=[];this.requiredJS=[];a.uniqid=p++;a.str=this.addHelperFunction(this.stringHelper,a);a.pix=this.addHelperFunction(this.pixHelper,a);a.js=this.addHelperFunction(this.jsHelper,a);a.quote=this.addHelperFunction(this.quoteHelper,a);a.shortentext=this.addHelperFunction(this.shortenTextHelper,a);a.userdate=this.addHelperFunction(this.userDateHelper,a);a.globals={config:g};a.currentTheme=b};A.prototype.getJS=function(){var a="";if(0<this.requiredJS.length){a=this.requiredJS.join(";\n")}return a};A.prototype.treatStringsInContent=function(a,b){var c=/\[\[_s\d+\]\]/,d,e,f,g,h,i;do{d="";e=a.search(c);while(-1<e){d+=a.substring(0,e);a=a.substr(e);f="";g=4;h=a.substr(g,1);do{f+=h;g++;h=a.substr(g,1)}while("]"!=h);i=b[parseInt(f,10)];if("undefined"==typeof i){l.debug("Could not find string for pattern [[_s"+f+"]].");i=""}d+=i;a=a.substr(6+f.length);e=a.search(c)}a=d+a;e=a.search(c)}while(-1<e);return a};A.prototype.treatDatesInContent=function(a,b){b.forEach(function(b,c){var d=new RegExp("\\[\\[_t_"+c+"\\]\\]","g");a=a.replace(d,b)});return a};A.prototype.doRender=function(c,e,f){this.currentThemeName=f;var g=t.getTemplateName(),h=new o("core/templates:doRender");return this.getTemplate(g).then(function(){this.addHelpers(e,f);var d=a.render(c,e,this.partialHelper.bind(this));return b.Deferred().resolve(d.trim(),this.getJS()).promise()}.bind(this)).then(function(a,c){if(0<this.requiredStrings.length){return d.get_strings(this.requiredStrings).then(function(d){this.requiredDates=this.requiredDates.map(function(a){return{timestamp:this.treatStringsInContent(a.timestamp,d),format:this.treatStringsInContent(a.format,d)}}.bind(this));a=this.treatStringsInContent(a,d);c=this.treatStringsInContent(c,d);return b.Deferred().resolve(a,c).promise()}.bind(this))}return b.Deferred().resolve(a,c).promise()}.bind(this)).then(function(a,c){if(0<this.requiredDates.length){return n.get(this.requiredDates).then(function(d){a=this.treatDatesInContent(a,d);c=this.treatDatesInContent(c,d);return b.Deferred().resolve(a,c).promise()}.bind(this))}return b.Deferred().resolve(a,c).promise()}.bind(this)).then(function(a,c){h.resolve();return b.Deferred().resolve(a,c).promise()})};var B=function(a){if(""!==a.trim()){var c=b("<script>").attr("type","text/javascript").html(a);b("head").append(c)}},C=function(a,c,d,e){var f=b(a);if(f.length){var g=b(c),h=null;if(e){h=new k.NodeList(f.children().get());h.destroy(!0);f.empty();f.append(g)}else{h=new k.NodeList(f.get());h.destroy(!0);f.replaceWith(g)}B(d);j.notifyFilterContentUpdated(g);return g.get()}return[]};A.prototype.scanForPartials=function(b){var c=a.parse(b),d=[],e=function(a,b){var c,d;for(c=0;c<a.length;c++){d=a[c];if(">"==d[0]||"<"==d[0]){b.push(d[1])}if(4<d.length){e(d[4],b)}}};e(c,d);return d};A.prototype.cachePartials=function(a,c){var d=this.currentThemeName+"/"+a;if(d in s){return s[d]}c=c||[d];s[d]=b.Deferred();this.getTemplate(a).then(function(e){var f=this.scanForPartials(e),g=f.filter(function(b){if(0<=c.indexOf(this.currentThemeName+"/"+b)){return!1}return b!=a}.bind(this)),h=g.map(function(a){c.push(this.currentThemeName+"/"+a);return this.cachePartials(a,c)}.bind(this));return b.when.apply(b,h).then(function(){return s[d].resolve(e)})}.bind(this)).catch(s[d].reject);return s[d]};A.prototype.render=function(a,c,d){if("undefined"==typeof d){d=g.theme}this.currentThemeName=d;var f=g.iconsystemmodule,h=b.Deferred();require([f],function(a){var b=new a;if(!(b instanceof i)){h.reject("Invalid icon system specified"+g.iconsystem)}else{t=b;b.init().then(h.resolve).catch(e.exception)}});return h.then(function(){return this.cachePartials(a)}.bind(this)).then(function(a){return this.doRender(a,c,d)}.bind(this))};var D=function(a,c,d){var e=b(a);if(e.length){var f=b(c);e.prepend(f);B(d);j.notifyFilterContentUpdated(e);return f.get()}return[]},E=function(a,c,d){var e=b(a);if(e.length){var f=b(c);e.append(f);B(d);j.notifyFilterContentUpdated(e);return f.get()}return[]};return{render:function render(a,b,c){var d=new A;return d.render(a,b,c)},prefetchTemplates:function prefetchTemplates(a,b){var c=new A;if("undefined"==typeof b){b=g.theme}return c.prefetchTemplates(a,b)},renderForPromise:function renderForPromise(a,b,c){var d=new A;return d.render(a,b,c).then(function(a,b){return{html:a,js:b}})},renderPix:function renderPix(a,b,c){var d=new A;return d.renderIcon(a,x(b),c)},runTemplateJS:B,replaceNodeContents:function replaceNodeContents(a,b,c){return C(a,b,c,!0)},replaceNode:function replaceNode(a,b,c){return C(a,b,c,!1)},prependNodeContents:function prependNodeContents(a,b,c){return D(a,b,c)},appendNodeContents:function appendNodeContents(a,b,c){return E(a,b,c)}}});
195ea448 2//# sourceMappingURL=templates.min.js.map