signal-desktop/js/views/whisper_view.js

85 lines
2.5 KiB
JavaScript
Raw Normal View History

/*
2015-10-01 17:54:49 +00:00
* Whisper.View
*
* This is the base for most of our views. The Backbone view is extended
* with some conveniences:
*
* 1. Pre-parses all our mustache templates for performance.
* https://github.com/janl/mustache.js#pre-parsing-and-caching-templates
*
* 2. Defines a default definition for render() which allows sub-classes
* to simply specify a templateName and renderAttributes which are plugged
* into Mustache.render
*
* 3. Makes all the templates available for rendering as partials.
* https://github.com/janl/mustache.js#partials
*
* 4. Provides some common functionality, e.g. confirmation dialog
*
*/
2018-04-27 21:25:04 +00:00
(function() {
'use strict';
window.Whisper = window.Whisper || {};
2018-04-27 21:25:04 +00:00
Whisper.View = Backbone.View.extend(
{
constructor: function() {
Backbone.View.apply(this, arguments);
Mustache.parse(_.result(this, 'template'));
},
render_attributes: function() {
return _.result(this.model, 'attributes', {});
},
render_partials: function() {
return Whisper.View.Templates;
},
template: function() {
if (this.templateName) {
return Whisper.View.Templates[this.templateName];
}
2018-04-27 21:25:04 +00:00
return '';
},
render: function() {
var attrs = _.result(this, 'render_attributes', {});
var template = _.result(this, 'template', '');
var partials = _.result(this, 'render_partials', '');
this.$el.html(Mustache.render(template, attrs, partials));
return this;
},
confirm: function(message, okText) {
return new Promise(
function(resolve, reject) {
var dialog = new Whisper.ConfirmationDialogView({
message: message,
okText: okText,
resolve: resolve,
reject: reject,
});
2018-04-27 21:25:04 +00:00
this.$el.append(dialog.el);
}.bind(this)
);
},
i18n_with_links: function() {
var args = Array.prototype.slice.call(arguments);
for (var i = 1; i < args.length; ++i) {
args[i] =
'class="link" href="' + encodeURI(args[i]) + '" target="_blank"';
}
return i18n(args[0], args.slice(1));
},
},
{
// Class attributes
Templates: (function() {
var templates = {};
$('script[type="text/x-tmpl-mustache"]').each(function(i, el) {
var $el = $(el);
var id = $el.attr('id');
templates[id] = $el.html();
});
return templates;
})(),
}
);
})();