2018-07-07 00:48:14 +00:00
|
|
|
/* global Whisper, Backbone, Mustache, _, $ */
|
|
|
|
|
2015-09-07 21:53:43 +00:00
|
|
|
/*
|
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
|
|
|
|
*
|
2015-03-05 23:25:49 +00:00
|
|
|
*/
|
2018-07-07 00:48:14 +00:00
|
|
|
|
|
|
|
// eslint-disable-next-line func-names
|
2018-04-27 21:25:04 +00:00
|
|
|
(function() {
|
|
|
|
'use strict';
|
2018-07-07 00:48:14 +00:00
|
|
|
|
2018-04-27 21:25:04 +00:00
|
|
|
window.Whisper = window.Whisper || {};
|
2015-03-05 23:25:49 +00:00
|
|
|
|
2018-04-27 21:25:04 +00:00
|
|
|
Whisper.View = Backbone.View.extend(
|
|
|
|
{
|
2018-07-07 00:48:14 +00:00
|
|
|
constructor(...params) {
|
|
|
|
Backbone.View.call(this, ...params);
|
2018-04-27 21:25:04 +00:00
|
|
|
Mustache.parse(_.result(this, 'template'));
|
|
|
|
},
|
2018-07-07 00:48:14 +00:00
|
|
|
render_attributes() {
|
2018-04-27 21:25:04 +00:00
|
|
|
return _.result(this.model, 'attributes', {});
|
|
|
|
},
|
2018-07-07 00:48:14 +00:00
|
|
|
render_partials() {
|
2018-04-27 21:25:04 +00:00
|
|
|
return Whisper.View.Templates;
|
|
|
|
},
|
2018-07-07 00:48:14 +00:00
|
|
|
template() {
|
2018-04-27 21:25:04 +00:00
|
|
|
if (this.templateName) {
|
|
|
|
return Whisper.View.Templates[this.templateName];
|
2015-05-26 23:54:21 +00:00
|
|
|
}
|
2018-04-27 21:25:04 +00:00
|
|
|
return '';
|
|
|
|
},
|
2018-07-07 00:48:14 +00:00
|
|
|
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;
|
|
|
|
},
|
2018-07-07 00:48:14 +00:00
|
|
|
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
|
2018-07-07 00:48:14 +00:00
|
|
|
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;
|
|
|
|
})(),
|
|
|
|
}
|
|
|
|
);
|
2015-03-05 23:25:49 +00:00
|
|
|
})();
|