Cable-Desktop/js/views/message_view.js
lilia 94c94eb7c9 Refactor message view
The message view has three flavors so far, a normal text+attachments
message, a group update, and an end session message. This changeset
extracts the normal message rendering into its own subview, and adds
some convenience functions to the message model in order to simplify
some of that flavoring logic.
2015-02-17 12:07:46 -08:00

102 lines
3.2 KiB
JavaScript

/* vim: ts=4:sw=4:expandtab
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
(function () {
'use strict';
window.Whisper = window.Whisper || {};
var ErrorView = Backbone.View.extend({
className: 'error',
events: {
'click' : 'replay'
},
replay: function() {
new window.textsecure.ReplayableError(this.model).replay();
},
render: function() {
this.$el.text(this.model.message);
return this;
}
});
var ContentMessageView = Backbone.View.extend({
tagName: 'div',
initialize: function() {
this.template = $('#message').html();
Mustache.parse(this.template);
},
render: function() {
this.$el.html(
Mustache.render(this.template, {
message: this.model.get('body'),
timestamp: moment(this.model.get('received_at')).fromNow(),
bubble_class: this.model.get('type') === 'outgoing' ? 'sent' : 'incoming',
sender: this.model.get('source')
})
);
this.$el.find('.attachments').append(
this.model.get('attachments').map(function(attachment) {
return new Whisper.AttachmentView({model: attachment}).render().el;
})
);
if (this.model.get('delivered')) {
this.$el.addClass('delivered');
}
var errors = this.model.get('errors');
if (errors && errors.length) {
this.$el.find('.bubble').append(
errors.map(function(error) {
return new ErrorView({model: error}).render().el;
})
);
}
}
});
Whisper.MessageView = Backbone.View.extend({
tagName: "li",
className: function() {
return ["entry", this.model.get('type')].join(' ');
},
initialize: function() {
if (this.model.isEndSession()) {
this.view = new Whisper.EndSessionView();
} else if (this.model.isGroupUpdate()) {
this.view = new Whisper.GroupUpdateView({
model: this.model.get('group_update')
});
} else {
this.view = new ContentMessageView({model: this.model});
}
this.$el.append(this.view.el);
this.listenTo(this.model, 'change', this.render); // auto update
this.listenTo(this.model, 'destroy', this.remove); // auto update
},
render: function() {
this.view.render();
return this;
}
});
})();