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.
pull/749/head
lilia 10 years ago
parent 85bec04010
commit 94c94eb7c9

@ -33,6 +33,13 @@
if (missing.length) {
console.log("Message missing attributes: " + missing);
}
},
isEndSession: function() {
var flag = textsecure.protobuf.PushMessageContent.Flags.END_SESSION;
return !!(this.get('flags') & flag);
},
isGroupUpdate: function() {
return !!(this.get('group_update'));
}
});

@ -16,6 +16,8 @@
(function () {
'use strict';
window.Whisper = window.Whisper || {};
var ErrorView = Backbone.View.extend({
className: 'error',
events: {
@ -30,38 +32,14 @@
}
});
window.Whisper = window.Whisper || {};
Whisper.MessageView = Backbone.View.extend({
tagName: "li",
className: "entry",
var ContentMessageView = Backbone.View.extend({
tagName: 'div',
initialize: function() {
var groupUpdate = this.model.get('group_update');
this.$el.addClass(this.model.get('type'));
if (groupUpdate) {
this.group_update_view = new Whisper.GroupUpdateView({
model: groupUpdate
}).render();
} else if (this.model.get('flags') === textsecure.protobuf.PushMessageContent.Flags.END_SESSION) {
this.end_session_view = new Whisper.EndSessionView().render();
} else {
this.template = $('#message').html();
}
Mustache.parse(this.template);
this.listenTo(this.model, 'change', this.render); // auto update
this.listenTo(this.model, 'destroy', this.remove); // auto update
},
render: function() {
if (this.group_update_view) {
this.$el.append(this.group_update_view.$el);
} else if (this.end_session_view) {
this.$el.append(this.end_session_view.$el);
} else {
this.$el.html(
Mustache.render(this.template, {
message: this.model.get('body'),
@ -90,7 +68,32 @@
);
}
}
});
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;
}

Loading…
Cancel
Save