signal-desktop/js/views/whisper_view.js

80 lines
2.2 KiB
JavaScript
Raw Normal View History

/* global Whisper, Backbone, Mustache, _, $ */
/*
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
*
*/
// eslint-disable-next-line func-names
2018-04-27 21:25:04 +00:00
(function() {
'use strict';
2018-04-27 21:25:04 +00:00
window.Whisper = window.Whisper || {};
2018-04-27 21:25:04 +00:00
Whisper.View = Backbone.View.extend(
{
constructor(...params) {
Backbone.View.call(this, ...params);
2018-04-27 21:25:04 +00:00
Mustache.parse(_.result(this, 'template'));
},
render_attributes() {
2018-04-27 21:25:04 +00:00
return _.result(this.model, 'attributes', {});
},
render_partials() {
2018-04-27 21:25:04 +00:00
return Whisper.View.Templates;
},
template() {
2018-04-27 21:25:04 +00:00
if (this.templateName) {
return Whisper.View.Templates[this.templateName];
}
2018-04-27 21:25:04 +00:00
return '';
},
render() {
const attrs = _.result(this, 'render_attributes', {});
const template = _.result(this, 'template', '');
const partials = _.result(this, 'render_partials', '');
2018-04-27 21:25:04 +00:00
this.$el.html(Mustache.render(template, attrs, partials));
return this;
},
confirm(message, okText) {
return new Promise((resolve, reject) => {
const dialog = new Whisper.ConfirmationDialogView({
message,
okText,
resolve,
reject,
});
this.$el.append(dialog.el);
});
2018-04-27 21:25:04 +00:00
},
},
{
// Class attributes
Templates: (() => {
const templates = {};
$('script[type="text/x-tmpl-mustache"]').each((i, el) => {
const $el = $(el);
const id = $el.attr('id');
2018-04-27 21:25:04 +00:00
templates[id] = $el.html();
});
return templates;
})(),
}
);
})();