'core/event',
'core/yui',
'core/log',
- 'core/truncate'
++ 'core/truncate',
+ 'core/user_date'
],
- function(mustache, $, ajax, str, notification, coreurl, config, storage, event, Y, Log, Truncate) {
- function(mustache, $, ajax, str, notification, coreurl, log,
- config, storage, event, Y, Log, UserDate) {
++ function(mustache, $, ajax, str, notification, coreurl, config, storage, event, Y, Log, Truncate, UserDate) {
// Module variables.
/** @var {Number} uniqInstances Count of times this constructor has been called. */
return '"' + content + '"';
};
+ /**
+ * Shorten text helper to truncate text and append a trailing ellipsis.
+ *
+ * @method shortenTextHelper
+ * @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.shortenTextHelper = function(context, sectionText, helper) {
+ // Non-greedy split on comma to grab section text into the length and
+ // text parts.
+ var regex = /(.*?),(.*)/;
+ var parts = sectionText.match(regex);
+ // The length is the part matched in the first set of parethesis.
+ var length = parts[1].trim();
+ // The length is the part matched in the second set of parethesis.
+ var text = parts[2].trim();
+ var content = helper(text, context);
+ return Truncate.truncate(content, {
+ length: length,
+ words: true,
+ ellipsis: '...'
+ });
+ };
+
+ /**
+ * User date helper to render user dates from timestamps.
+ *
+ * @method userDateHelper
+ * @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.userDateHelper = function(context, sectionText, helper) {
+ // Non-greedy split on comma to grab the timestamp and format.
+ var regex = /(.*?),(.*)/;
+ var parts = sectionText.match(regex);
+ var timestamp = helper(parts[1].trim(), context);
+ var format = helper(parts[2].trim(), context);
+ var index = this.requiredDates.length;
+
+ this.requiredDates.push({
+ timestamp: timestamp,
+ format: format
+ });
+
+ return '[[_t_' + index + ']]';
+ };
+
/**
* Add some common helper functions to all context objects passed to templates.
* These helpers match exactly the helpers available in php.
context.quote = function() {
return this.quoteHelper.bind(this, context);
}.bind(this);
+ context.shortentext = function() {
+ return this.shortenTextHelper.bind(this, context);
+ }.bind(this);
+ context.userdate = function() {
+ return this.userDateHelper.bind(this, context);
+ }.bind(this);
context.globals = {config: config};
context.currentTheme = themeName;
};
$quotehelper = new \core\output\mustache_quote_helper();
$jshelper = new \core\output\mustache_javascript_helper($this->page->requires);
$pixhelper = new \core\output\mustache_pix_helper($this);
+ $shortentexthelper = new \core\output\mustache_shorten_text_helper();
+ $userdatehelper = new \core\output\mustache_user_date_helper();
// We only expose the variables that are exposed to JS templates.
$safeconfig = $this->page->requires->get_config_for_javascript($this->page, $this);
'quote' => array($quotehelper, 'quote'),
'js' => array($jshelper, 'help'),
'pix' => array($pixhelper, 'pix'),
- 'shortentext' => array($shortentexthelper, 'shorten'));
- 'userdate' => array($userdatehelper, 'transform'));
++ 'shortentext' => array($shortentexthelper, 'shorten'),
++ 'userdate' => array($userdatehelper, 'transform'),
++ );
$this->mustache = new Mustache_Engine(array(
'cache' => $cachedir,