Merge branch 'MDL-57636_master' of https://github.com/dasistwas/moodle
[moodle.git] / lib / amd / build / templates.min.js
CommitLineData
c77ba489 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=[],v=!1,w=function(a){if(a in r)return r[a];if(a in q)return r[a]=b.Deferred().resolve(q[a]).promise(),r[a];var c=h.get("core_template/"+a);return c?(q[a]=c,r[a]=b.Deferred().resolve(c).promise(),r[a]):null},x=function(){if(u.length&&!v){v=!0;var a=u.slice(),e=b.Deferred(),f=[],g=a.map(function(a){var b=a.component,c=a.name,i=a.searchKey,j=a.theme,k=a.deferred,l=null,m=w(i);if(m)l=m;else{f.push({methodname:"core_output_load_template_with_dependencies",args:{component:b,template:c,themename:j}});var n=f.length-1;l=e.promise().then(function(a){return g[i]=a[n].then(function(a){var e=null;return a.templates.forEach(function(a){var d=[j,a.component,a.name].join("/");q[d]=a.value,h.set("core_template/"+d,a.value),a.component==b&&a.name==c&&(e=a.value)}),a.strings.length&&d.cache_strings(a.strings.map(function(a){return{component:a.component,key:a.name,value:a.value}})),e}),g[i]})}return l.then(function(a){return k.resolve(a)})["catch"](function(a){throw k.reject(a),a})});f.length?e.resolve(c.call(f,!0,!1)):e.resolve(),b.when.apply(null,g).then(function(){u.splice(0,a.length),v=!1,x()})["catch"](function(){u.splice(0,a.length),v=!1,x()})}},y=function(){this.requiredStrings=[],this.requiredJS=[],this.requiredDates=[],this.currentThemeName=""};y.prototype.requiredStrings=null,y.prototype.requiredDates=[],y.prototype.requiredJS=null,y.prototype.currentThemeName="",y.prototype.getTemplate=function(a){var c=this.currentThemeName,d=c+"/"+a,e=w(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=g.shift(),i=g.shift(),j=b.Deferred();return u.push({component:h,name:i,theme:c,searchKey:d,deferred:j}),x(),j.promise()},y.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]},y.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)})},y.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)},y.prototype.jsHelper=function(a,b,c){return this.requiredJS.push(c(b,a)),""},y.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+"]]"},y.prototype.quoteHelper=function(a,b,c){var d=c(b.trim(),a);return d=d.replace('"','\\"').replace(/([\{\}]{2,3})/g,"{{=<% %>=}}$1<%={{ }}=%>"),'"'+d+'"'},y.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:"..."})},y.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+"]]"},y.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},y.prototype.getJS=function(){var a="";return this.requiredJS.length>0&&(a=this.requiredJS.join(";\n")),a},y.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},y.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},y.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 z=function(a){if(""!==a.trim()){var c=b("<script>").attr("type","text/javascript").html(a);b("head").append(c)}},A=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)),z(d),j.notifyFilterContentUpdated(g)}};y.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},y.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])},y.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 B=function(a,c,d){var e=b(a);e.length&&(e.prepend(c),z(d),j.notifyFilterContentUpdated(e))},C=function(a,c,d){var e=b(a);e.length&&(e.append(c),z(d),j.notifyFilterContentUpdated(e))};return{render:function(a,b,c){var d=new y;return d.render(a,b,c)},renderPix:function(a,b,c){var d=new y;return d.renderIcon(a,b,c)},runTemplateJS:z,replaceNodeContents:function(a,b,c){A(a,b,c,!0)},replaceNode:function(a,b,c){A(a,b,c,!1)},prependNodeContents:function(a,b,c){B(a,b,c)},appendNodeContents:function(a,b,c){C(a,b,c)}}});