// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see .
/**
* Template renderer for Moodle. Load and render Moodle templates with Mustache.
*
* @module core/templates
* @package core
* @class templates
* @copyright 2015 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since 2.9
*/
define(['core/mustache',
'jquery',
'core/ajax',
'core/str',
'core/notification',
'core/url',
'core/config',
'core/localstorage',
'core/event',
'core/yui',
'core/log'
],
function(mustache, $, ajax, str, notification, coreurl, config, storage, event, Y, Log) {
// Module variables.
/** @var {Number} uniqInstances Count of times this constructor has been called. */
var uniqInstances = 0;
/** @var {string[]} templateCache - Cache of already loaded templates */
var templateCache = {};
/**
* Constructor
*
* Each call to templates.render gets it's own instance of this class.
*/
var Renderer = function() {
this.requiredStrings = [];
this.requiredJS = [];
this.currentThemeName = '';
};
// Class variables and functions.
/** @var {string[]} requiredStrings - Collection of strings found during the rendering of one template */
Renderer.prototype.requiredStrings = null;
/** @var {string[]} requiredJS - Collection of js blocks found during the rendering of one template */
Renderer.prototype.requiredJS = null;
/** @var {String} themeName for the current render */
Renderer.prototype.currentThemeName = '';
/**
* Load a template from the cache or local storage or ajax request.
*
* @method getTemplate
* @private
* @param {string} templateName - should consist of the component and the name of the template like this:
* core/menu (lib/templates/menu.mustache) or
* tool_bananas/yellow (admin/tool/bananas/templates/yellow.mustache)
* @param {Boolean} async If false - this function will not return until the promises are resolved.
* @return {Promise} JQuery promise object resolved when the template has been fetched.
*/
Renderer.prototype.getTemplate = function(templateName, async) {
var deferred = $.Deferred();
var parts = templateName.split('/');
var component = parts.shift();
var name = parts.shift();
var searchKey = this.currentThemeName + '/' + templateName;
// First try request variables.
if (searchKey in templateCache) {
deferred.resolve(templateCache[searchKey]);
return deferred.promise();
}
// Now try local storage.
var cached = storage.get('core_template/' + searchKey);
if (cached) {
deferred.resolve(cached);
templateCache[searchKey] = cached;
return deferred.promise();
}
// Oh well - load via ajax.
var promises = ajax.call([{
methodname: 'core_output_load_template',
args: {
component: component,
template: name,
themename: this.currentThemeName
}
}], async, false);
promises[0].done(
function(templateSource) {
storage.set('core_template/' + searchKey, templateSource);
templateCache[searchKey] = templateSource;
deferred.resolve(templateSource);
}
).fail(
function(ex) {
deferred.reject(ex);
}
);
return deferred.promise();
};
/**
* Load a partial from the cache or ajax.
*
* @method partialHelper
* @private
* @param {string} name The partial name to load.
* @return {string}
*/
Renderer.prototype.partialHelper = function(name) {
var template = '';
this.getTemplate(name, false).done(
function(source) {
template = source;
}
).fail(notification.exception);
return template;
};
/**
* Render image icons.
*
* @method pixHelper
* @private
* @param {object} context The mustache context
* @param {string} sectionText The text to parse arguments from.
* @param {function} helper Used to render the alt attribute of the text.
* @return {string}
*/
Renderer.prototype.pixHelper = function(context, sectionText, helper) {
var parts = sectionText.split(',');
var key = '';
var component = '';
var text = '';
var result;
if (parts.length > 0) {
key = parts.shift().trim();
}
if (parts.length > 0) {
component = parts.shift().trim();
}
if (parts.length > 0) {
text = parts.join(',').trim();
}
var url = coreurl.imageUrl(key, component);
var templatecontext = {
attributes: [
{name: 'src', value: url},
{name: 'alt', value: helper(text)},
{name: 'class', value: 'smallicon'}
]
};
// We forced loading of this early, so it will be in the cache.
var template = templateCache[this.currentThemeName + '/core/pix_icon'];
result = mustache.render(template, templatecontext, this.partialHelper.bind(this));
return result.trim();
};
/**
* Render blocks of javascript and save them in an array.
*
* @method jsHelper
* @private
* @param {object} context The current mustache context.
* @param {string} sectionText The text to save as a js block.
* @param {function} helper Used to render the block.
* @return {string}
*/
Renderer.prototype.jsHelper = function(context, sectionText, helper) {
this.requiredJS.push(helper(sectionText, context));
return '';
};
/**
* String helper used to render {{#str}}abd component { a : 'fish'}{{/str}}
* into a get_string call.
*
* @method stringHelper
* @private
* @param {object} context The current mustache context.
* @param {string} sectionText The text to parse the arguments from.
* @param {function} helper Used to render subsections of the text.
* @return {string}
*/
Renderer.prototype.stringHelper = function(context, sectionText, helper) {
var parts = sectionText.split(',');
var key = '';
var component = '';
var param = '';
if (parts.length > 0) {
key = parts.shift().trim();
}
if (parts.length > 0) {
component = parts.shift().trim();
}
if (parts.length > 0) {
param = parts.join(',').trim();
}
if (param !== '') {
// Allow variable expansion in the param part only.
param = helper(param, context);
}
// Allow json formatted $a arguments.
if ((param.indexOf('{') === 0) && (param.indexOf('{{') !== 0)) {
param = JSON.parse(param);
}
var index = this.requiredStrings.length;
this.requiredStrings.push({key: key, component: component, param: param});
return '{{_s' + index + '}}';
};
/**
* Quote helper used to wrap content in quotes, and escape all quotes present in the content.
*
* @method quoteHelper
* @private
* @param {object} context The current mustache context.
* @param {string} sectionText The text to parse the arguments from.
* @param {function} helper Used to render subsections of the text.
* @return {string}
*/
Renderer.prototype.quoteHelper = function(context, sectionText, helper) {
var content = helper(sectionText.trim(), context);
// Escape the {{ and the ".
// This involves wrapping {{, and }} in change delimeter tags.
content = content
.replace('"', '\\"')
.replace(/([\{\}]{2,3})/g, '{{=<% %>=}}$1<%={{ }}=%>')
;
return '"' + content + '"';
};
/**
* Add some common helper functions to all context objects passed to templates.
* These helpers match exactly the helpers available in php.
*
* @method addHelpers
* @private
* @param {Object} context Simple types used as the context for the template.
* @param {String} themeName We set this multiple times, because there are async calls.
*/
Renderer.prototype.addHelpers = function(context, themeName) {
this.currentThemeName = themeName;
this.requiredStrings = [];
this.requiredJS = [];
context.uniqid = (uniqInstances++);
context.str = function() {
return this.stringHelper.bind(this, context);
}.bind(this);
context.pix = function() {
return this.pixHelper.bind(this, context);
}.bind(this);
context.js = function() {
return this.jsHelper.bind(this, context);
}.bind(this);
context.quote = function() {
return this.quoteHelper.bind(this, context);
}.bind(this);
context.globals = {config: config};
context.currentTheme = themeName;
};
/**
* Get all the JS blocks from the last rendered template.
*
* @method getJS
* @private
* @param {string[]} strings Replacement strings.
* @return {string}
*/
Renderer.prototype.getJS = function(strings) {
var js = '';
if (this.requiredJS.length > 0) {
js = this.requiredJS.join(";\n");
}
// Re-render to get the final strings.
return this.treatStringsInContent(js, strings);
};
/**
* Treat strings in content.
*
* The purpose of this method is to replace the placeholders found in a string
* with the their respective translated strings.
*
* Previously we were relying on String.replace() but the complexity increased with
* the numbers of strings to replace. Now we manually walk the string and stop at each
* placeholder we find, only then we replace it. Most of the time we will
* replace all the placeholders in a single run, at times we will need a few
* more runs when placeholders are replaced with strings that contain placeholders
* themselves.
*
* @param {String} content The content in which string placeholders are to be found.
* @param {Array} strings The strings to replace with.
* @return {String} The treated content.
*/
Renderer.prototype.treatStringsInContent = function(content, strings) {
var pattern = /{{_s\d+}}/,
treated,
index,
strIndex,
walker,
char,
strFinal;
do {
treated = '';
index = content.search(pattern);
while (index > -1) {
// Copy the part prior to the placeholder to the treated string.
treated += content.substring(0, index);
content = content.substr(index);
strIndex = '';
walker = 4; // 4 is the length of '{{_s'.
// Walk the characters to manually extract the index of the string from the placeholder.
char = content.substr(walker, 1);
do {
strIndex += char;
walker++;
char = content.substr(walker, 1);
} while (char != '}');
// Get the string, add it to the treated result, and remove the placeholder from the content to treat.
strFinal = strings[parseInt(strIndex, 10)];
if (typeof strFinal === 'undefined') {
Log.debug('Could not find string for pattern {{_s' + strIndex + '}}.');
strFinal = '';
}
treated += strFinal;
content = content.substr(6 + strIndex.length); // 6 is the length of the placeholder without the index: '{{_s}}'.
// Find the next placeholder.
index = content.search(pattern);
}
// The content becomes the treated part with the rest of the content.
content = treated + content;
// Check if we need to walk the content again, in case strings contained placeholders.
index = content.search(pattern);
} while (index > -1);
return content;
};
/**
* Render a template and then call the callback with the result.
*
* @method doRender
* @private
* @param {string} templateSource The mustache template to render.
* @param {Object} context Simple types used as the context for the template.
* @param {String} themeName Name of the current theme.
* @return {Promise} object
*/
Renderer.prototype.doRender = function(templateSource, context, themeName) {
var deferred = $.Deferred();
this.currentThemeName = themeName;
// Make sure we fetch this first.
var loadPixTemplate = this.getTemplate('core/pix_icon', true);
loadPixTemplate.done(
function() {
this.addHelpers(context, themeName);
var result = '';
try {
result = mustache.render(templateSource, context, this.partialHelper.bind(this));
} catch (ex) {
deferred.reject(ex);
}
if (this.requiredStrings.length > 0) {
str.get_strings(this.requiredStrings)
.then(function(strings) {
// Why do we not do another call the render here?
//
// Because that would expose DOS holes. E.g.
// I create an assignment called "{{fish" which
// would get inserted in the template in the first pass
// and cause the template to die on the second pass (unbalanced).
result = this.treatStringsInContent(result, strings);
deferred.resolve(result, this.getJS(strings));
}.bind(this))
.fail(deferred.reject);
} else {
deferred.resolve(result.trim(), this.getJS([]));
}
}.bind(this)
).fail(deferred.reject);
return deferred.promise();
};
/**
* Execute a block of JS returned from a template.
* Call this AFTER adding the template HTML into the DOM so the nodes can be found.
*
* @method runTemplateJS
* @param {string} source - A block of javascript.
*/
var runTemplateJS = function(source) {
if (source.trim() !== '') {
var newscript = $('