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.
This commit is contained in:
@@ -33,6 +33,13 @@
|
|||||||
if (missing.length) {
|
if (missing.length) {
|
||||||
console.log("Message missing attributes: " + missing);
|
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 () {
|
(function () {
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
|
window.Whisper = window.Whisper || {};
|
||||||
|
|
||||||
var ErrorView = Backbone.View.extend({
|
var ErrorView = Backbone.View.extend({
|
||||||
className: 'error',
|
className: 'error',
|
||||||
events: {
|
events: {
|
||||||
@@ -30,38 +32,14 @@
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
window.Whisper = window.Whisper || {};
|
|
||||||
|
|
||||||
Whisper.MessageView = Backbone.View.extend({
|
var ContentMessageView = Backbone.View.extend({
|
||||||
tagName: "li",
|
tagName: 'div',
|
||||||
className: "entry",
|
initialize: function() {
|
||||||
|
|
||||||
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();
|
this.template = $('#message').html();
|
||||||
}
|
Mustache.parse(this.template);
|
||||||
Mustache.parse(this.template);
|
},
|
||||||
|
render: function() {
|
||||||
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(
|
this.$el.html(
|
||||||
Mustache.render(this.template, {
|
Mustache.render(this.template, {
|
||||||
message: this.model.get('body'),
|
message: this.model.get('body'),
|
||||||
@@ -89,8 +67,33 @@
|
|||||||
})
|
})
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
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;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user