mirror of
https://github.com/moodle/moodle.git
synced 2025-01-19 14:27:22 +01:00
f20a336b43
Also clean up promises use of done vs then.
1 line
4.3 KiB
JavaScript
1 line
4.3 KiB
JavaScript
define(["core/mustache","jquery","core/ajax","core/str","core/notification","core/url","core/config","core/localstorage","core/event","core/yui","core/log"],function(a,b,c,d,e,f,g,h,i,j,k){var l=0,m={},n=[],o=function(){this.requiredStrings=[],this.requiredJS=[],this.currentThemeName=""};o.prototype.requiredStrings=null,o.prototype.requiredJS=null,o.prototype.currentThemeName="",o.prototype.getTemplate=function(a,d){var e=a.split("/"),f=e.shift(),g=e.shift(),i=this.currentThemeName+"/"+a;if(i in m)return m[i];var j=h.get("core_template/"+i);if(j)return m[i]=b.Deferred().resolve(j).promise(),m[i];var k=c.call([{methodname:"core_output_load_template",args:{component:f,template:g,themename:this.currentThemeName}}],d,!1);return m[i]=k[0].then(function(a){return h.set("core_template/"+i,a),a}),m[i]},o.prototype.partialHelper=function(a){var b="";return this.getTemplate(a,!1).done(function(a){b=a}).fail(e.exception),b},o.prototype.pixHelper=function(b,c,d){var e,g=c.split(","),h="",i="",j="";g.length>0&&(h=g.shift().trim()),g.length>0&&(i=g.shift().trim()),g.length>0&&(j=g.join(",").trim());var k=f.imageUrl(h,i),l={attributes:[{name:"src",value:k},{name:"alt",value:d(j)},{name:"title",value:d(j)},{name:"class",value:"smallicon"}]},m=n[this.currentThemeName];return e=a.render(m,l,this.partialHelper.bind(this)),e.trim()},o.prototype.jsHelper=function(a,b,c){return this.requiredJS.push(c(b,a)),""},o.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+"]]"},o.prototype.quoteHelper=function(a,b,c){var d=c(b.trim(),a);return d=d.replace('"','\\"').replace(/([\{\}]{2,3})/g,"{{=<% %>=}}$1<%={{ }}=%>"),'"'+d+'"'},o.prototype.addHelpers=function(a,b){this.currentThemeName=b,this.requiredStrings=[],this.requiredJS=[],a.uniqid=l++,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.globals={config:g},a.currentTheme=b},o.prototype.getJS=function(a){var b="";return this.requiredJS.length>0&&(b=this.requiredJS.join(";\n")),this.treatStringsInContent(b,a)},o.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&&(k.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},o.prototype.doRender=function(c,e,f){return this.currentThemeName=f,this.getTemplate("core/pix_icon",!0).then(function(a){n[this.currentThemeName]=a}.bind(this)).then(function(){this.addHelpers(e,f);var g=a.render(c,e,this.partialHelper.bind(this));return this.requiredStrings.length>0?d.get_strings(this.requiredStrings).then(function(a){return g=this.treatStringsInContent(g,a),b.Deferred().resolve(g,this.getJS(a)).promise()}.bind(this)):b.Deferred().resolve(g.trim(),this.getJS([])).promise()}.bind(this))};var p=function(a){if(""!==a.trim()){var c=b("<script>").attr("type","text/javascript").html(a);b("head").append(c)}},q=function(a,c,d,e){var f=b(a);if(f.length){var g=b(c),h=null;e?(h=new j.NodeList(f.children().get()),h.destroy(!0),f.empty(),f.append(g)):(h=new j.NodeList(f.get()),h.destroy(!0),f.replaceWith(g)),p(d),i.notifyFilterContentUpdated(g)}};o.prototype.render=function(a,b,c){return"undefined"==typeof c&&(c=g.theme),this.currentThemeName=c,this.getTemplate(a,!0).then(function(a){return this.doRender(a,b,c)}.bind(this))};var r=function(a,c,d){var e=b(a);e.length&&(e.prepend(c),p(d),i.notifyFilterContentUpdated(e))},s=function(a,c,d){var e=b(a);e.length&&(e.append(c),p(d),i.notifyFilterContentUpdated(e))};return{render:function(a,b,c){var d=new o;return d.render(a,b,c)},runTemplateJS:p,replaceNodeContents:function(a,b,c){q(a,b,c,!0)},replaceNode:function(a,b,c){q(a,b,c,!1)},prependNodeContents:function(a,b,c){r(a,b,c)},appendNodeContents:function(a,b,c){s(a,b,c)}}}); |