MDL-64348 javascript: change template loading to also load dependencies
[moodle.git] / lib / amd / build / templates.min.js
CommitLineData
2b92891a 1define(["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=function(){this.requiredStrings=[],this.requiredJS=[],this.requiredDates=[],this.currentThemeName=""};u.prototype.requiredStrings=null,u.prototype.requiredDates=[],u.prototype.requiredJS=null,u.prototype.currentThemeName="",u.prototype.getTemplate=function(a){var e=a.split("/"),f=e.shift(),g=e.shift(),i=this.currentThemeName,j=i+"/"+a;if(j in r)return r[j];if(j in q)return r[j]=b.Deferred().resolve(q[j]).promise(),r[j];var k=h.get("core_template/"+j);if(k)return q[j]=k,r[j]=b.Deferred().resolve(k).promise(),r[j];var l=c.call([{methodname:"core_output_load_template_with_dependencies",args:{component:f,template:g,themename:i}}],!0,!1);return r[j]=l[0].then(function(a){var b=null;return a.templates.forEach(function(a){var c=[i,a.component,a.name].join("/");q[c]=a.value,h.set("core_template/"+c,a.value),a.component==f&&a.name==g&&(b=a.value)}),a.strings.length&&d.cache_strings(a.strings.map(function(a){return{component:a.component,key:a.name,value:a.value}})),b}),r[j]},u.prototype.partialHelper=function(a){var b=this.currentThemeName+"/"+a;return b in q||e.exception(new Error("Failed to pre-fetch the template: "+a)),q[b]},u.prototype.renderIcon=function(a,c,d){var f=g.iconsystemmodule,h=b.Deferred();return require([f],function(a){var b=new a;b instanceof i?(t=b,b.init().then(h.resolve)["catch"](e.exception)):h.reject("Invalid icon system specified"+g.iconsystemmodule)}),h.then(function(a){return this.getTemplate(a.getTemplateName())}.bind(this)).then(function(b){return t.renderIcon(a,c,d,b)})},u.prototype.pixHelper=function(a,b,c){var d=b.split(","),e="",f="",g="";d.length>0&&(e=c(d.shift().trim(),a)),d.length>0&&(f=c(d.shift().trim(),a)),d.length>0&&(g=c(d.join(",").trim(),a));var h=t.getTemplateName(),i=this.currentThemeName+"/"+h,j=q[i];return e=e.replace(/&#x2F;/gi,"/"),t.renderIcon(e,f,g,j)},u.prototype.jsHelper=function(a,b,c){return this.requiredJS.push(c(b,a)),""},u.prototype.stringHelper=function(a,b,c){var d=b.split(","),e="",f="",g="";d.length>0&&(e=d.shift().trim()),d.length>0&&(f=d.shift().trim()),d.length>0&&(g=d.join(",").trim()),""!==g&&(g=c(g,a)),0===g.indexOf("{")&&0!==g.indexOf("{{")&&(g=JSON.parse(g));var h=this.requiredStrings.length;return this.requiredStrings.push({key:e,component:f,param:g}),"[[_s"+h+"]]"},u.prototype.quoteHelper=function(a,b,c){var d=c(b.trim(),a);return d=d.replace('"','\\"').replace(/([\{\}]{2,3})/g,"{{=<% %>=}}$1<%={{ }}=%>"),'"'+d+'"'},u.prototype.shortenTextHelper=function(a,b,c){var d=/(.*?),(.*)/,e=b.match(d),f=e[1].trim(),g=e[2].trim(),h=c(g,a);return m.truncate(h,{length:f,words:!0,ellipsis:"..."})},u.prototype.userDateHelper=function(a,b,c){var d=/(.*?),(.*)/,e=b.match(d),f=c(e[1].trim(),a),g=c(e[2].trim(),a),h=this.requiredDates.length;return this.requiredDates.push({timestamp:f,format:g}),"[[_t_"+h+"]]"},u.prototype.addHelpers=function(a,b){this.currentThemeName=b,this.requiredStrings=[],this.requiredJS=[],a.uniqid=p++,a.str=function(){return this.stringHelper.bind(this,a)}.bind(this),a.pix=function(){return this.pixHelper.bind(this,a)}.bind(this),a.js=function(){return this.jsHelper.bind(this,a)}.bind(this),a.quote=function(){return this.quoteHelper.bind(this,a)}.bind(this),a.shortentext=function(){return this.shortenTextHelper.bind(this,a)}.bind(this),a.userdate=function(){return this.userDateHelper.bind(this,a)}.bind(this),a.globals={config:g},a.currentTheme=b},u.prototype.getJS=function(){var a="";return this.requiredJS.length>0&&(a=this.requiredJS.join(";\n")),a},u.prototype.treatStringsInContent=function(a,b){var c,d,e,f,g,h,i=/\[\[_s\d+\]\]/;do{for(c="",d=a.search(i);d>-1;){c+=a.substring(0,d),a=a.substr(d),e="",f=4,g=a.substr(f,1);do e+=g,f++,g=a.substr(f,1);while("]"!=g);h=b[parseInt(e,10)],"undefined"==typeof h&&(l.debug("Could not find string for pattern [[_s"+e+"]]."),h=""),c+=h,a=a.substr(6+e.length),d=a.search(i)}a=c+a,d=a.search(i)}while(d>-1);return a},u.prototype.treatDatesInContent=function(a,b){return b.forEach(function(b,c){var d="\\[\\[_t_"+c+"\\]\\]",e=new RegExp(d,"g");a=a.replace(e,b)}),a},u.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){return this.requiredStrings.length>0?d.get_strings(this.requiredStrings).then(function(d){return 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),b.Deferred().resolve(a,c).promise()}.bind(this)):b.Deferred().resolve(a,c).promise()}.bind(this)).then(function(a,c){return this.requiredDates.length>0?n.get(this.requiredDates).then(function(d){return a=this.treatDatesInContent(a,d),c=this.treatDatesInContent(c,d),b.Deferred().resolve(a,c).promise()}.bind(this)):b.Deferred().resolve(a,c).promise()}.bind(this)).then(function(a,c){return h.resolve(),b.Deferred().resolve(a,c).promise()})};var v=function(a){if(""!==a.trim()){var c=b("<script>").attr("type","text/javascript").html(a);b("head").append(c)}},w=function(a,c,d,e){var f=b(a);if(f.length){var g=b(c),h=null;e?(h=new k.NodeList(f.children().get()),h.destroy(!0),f.empty(),f.append(g)):(h=new k.NodeList(f.get()),h.destroy(!0),f.replaceWith(g)),v(d),j.notifyFilterContentUpdated(g)}};u.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],">"!=d[0]&&"<"!=d[0]||b.push(d[1]),d.length>4&&e(d[4],b)};return e(c,d),d},u.prototype.cachePartials=function(a,c){var d=this.currentThemeName+"/"+a;return d in s?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){return!(c.indexOf(this.currentThemeName+"/"+b)>=0)&&b!=a}.bind(this)),h=g.map(function(a){return c.push(this.currentThemeName+"/"+a),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),s[d])},u.prototype.render=function(a,c,d){"undefined"==typeof d&&(d=g.theme),this.currentThemeName=d;var f=g.iconsystemmodule,h=b.Deferred();return require([f],function(a){var b=new a;b instanceof i?(t=b,b.init().then(h.resolve)["catch"](e.exception)):h.reject("Invalid icon system specified"+g.iconsystem)}),h.then(function(){return this.cachePartials(a)}.bind(this)).then(function(a){return this.doRender(a,c,d)}.bind(this))};var x=function(a,c,d){var e=b(a);e.length&&(e.prepend(c),v(d),j.notifyFilterContentUpdated(e))},y=function(a,c,d){var e=b(a);e.length&&(e.append(c),v(d),j.notifyFilterContentUpdated(e))};return{render:function(a,b,c){var d=new u;return d.render(a,b,c)},renderPix:function(a,b,c){var d=new u;return d.renderIcon(a,b,c)},runTemplateJS:v,replaceNodeContents:function(a,b,c){w(a,b,c,!0)},replaceNode:function(a,b,c){w(a,b,c,!1)},prependNodeContents:function(a,b,c){x(a,b,c)},appendNodeContents:function(a,b,c){y(a,b,c)}}});