Cable-Desktop/js/views/message_view.js

88 lines
3.2 KiB
JavaScript
Raw Normal View History

/*
* vim: ts=4:sw=4:expandtab
*/
(function () {
'use strict';
window.Whisper = window.Whisper || {};
Whisper.MessageView = Whisper.View.extend({
tagName: "li",
template: $('#message').html(),
initialize: function() {
this.listenTo(this.model, 'change:body change:errors', this.render);
this.listenTo(this.model, 'change:delivered', this.renderDelivered);
2015-07-08 04:21:10 +02:00
this.listenTo(this.model, 'change', this.renderSent);
this.listenTo(this.model, 'change:flags change:group_update', this.renderControl);
this.listenTo(this.model, 'destroy', this.remove);
},
events: {
'click .timestamp': 'select'
},
select: function() {
this.$el.trigger('select', {message: this.model});
},
className: function() {
return ["entry", this.model.get('type')].join(' ');
},
2015-07-08 04:21:10 +02:00
renderSent: function() {
if (this.model.isOutgoing()) {
2015-07-08 04:21:10 +02:00
this.$el.toggleClass('sent', !!this.model.get('sent'));
}
},
renderDelivered: function() {
if (this.model.get('delivered')) { this.$el.addClass('delivered'); }
},
renderControl: function() {
if (this.model.isEndSession() || this.model.isGroupUpdate()) {
this.$el.addClass('control');
this.$('.content').text(this.model.getDescription());
} else {
this.$el.removeClass('control');
}
},
render: function() {
var contact = this.model.getContact();
this.$el.html(
Mustache.render(this.template, {
message: this.model.get('body'),
timestamp: moment(this.model.get('sent_at')).fromNow(),
2015-03-18 01:10:18 +01:00
sender: (contact && contact.getTitle()) || '',
avatar: (contact && contact.getAvatar())
}, this.render_partials())
);
twemoji.parse(this.el, { base: '/images/twemoji/', size: 16 });
var content = this.$('.content');
var escaped = content.html();
content.html(escaped.replace(/\n/g, '<br>').replace(/(^|[\s\n]|<br\/?>)((?:https?|ftp):\/\/[\-A-Z0-9+\u0026\u2019@#\/%?=()~_|!:,.;]*[\-A-Z0-9+\u0026@#\/%=~()_|])/gi, "$1<a href='$2' target='_blank'>$2</a>"));
2015-07-08 04:21:10 +02:00
this.renderSent();
this.renderDelivered();
this.renderControl();
this.$('.attachments').append(
this.model.get('attachments').map(function(attachment) {
return new Whisper.AttachmentView({
model: attachment
}).render().el;
})
);
var errors = this.model.get('errors');
if (errors && errors.length) {
this.$('.bubble').prepend(
errors.map(function(error) {
return new Whisper.MessageErrorView({
model: error,
message: this.model
}).render().el;
}.bind(this))
);
}
return this;
}
});
})();