2015-09-07 14:53:43 -07:00
|
|
|
/*
|
|
|
|
* vim: ts=4:sw=4:expandtab
|
2014-11-20 15:43:51 -08:00
|
|
|
*/
|
2014-05-16 21:48:46 -07:00
|
|
|
(function () {
|
2015-02-17 23:58:00 -08:00
|
|
|
'use strict';
|
|
|
|
window.Whisper = window.Whisper || {};
|
2015-02-17 11:54:15 -08:00
|
|
|
|
2015-03-23 18:30:16 -07:00
|
|
|
Whisper.MessageView = Whisper.View.extend({
|
|
|
|
tagName: "li",
|
2015-12-08 15:01:29 -08:00
|
|
|
templateName: 'message',
|
2015-03-05 16:13:53 -08:00
|
|
|
initialize: function() {
|
2015-11-06 17:37:12 -08:00
|
|
|
this.listenTo(this.model, 'change:errors', this.onErrorsChanged);
|
2015-11-05 20:11:28 -08:00
|
|
|
this.listenTo(this.model, 'change:body', this.render);
|
2015-03-05 16:13:53 -08:00
|
|
|
this.listenTo(this.model, 'change:delivered', this.renderDelivered);
|
2015-07-07 19:21:10 -07:00
|
|
|
this.listenTo(this.model, 'change', this.renderSent);
|
2015-03-23 18:30:16 -07:00
|
|
|
this.listenTo(this.model, 'change:flags change:group_update', this.renderControl);
|
|
|
|
this.listenTo(this.model, 'destroy', this.remove);
|
2015-10-28 13:57:32 -07:00
|
|
|
this.listenTo(this.model, 'pending', this.renderPending);
|
|
|
|
this.listenTo(this.model, 'done', this.renderDone);
|
2015-11-06 14:58:26 -08:00
|
|
|
this.timeStampView = new Whisper.MessageTimestampView();
|
2015-03-23 18:30:16 -07:00
|
|
|
},
|
|
|
|
events: {
|
2015-10-23 17:20:24 -07:00
|
|
|
'click .timestamp': 'select',
|
|
|
|
'click .error': 'select'
|
2015-03-23 18:30:16 -07:00
|
|
|
},
|
2015-10-26 17:00:21 -07:00
|
|
|
select: function(e) {
|
2015-03-23 18:30:16 -07:00
|
|
|
this.$el.trigger('select', {message: this.model});
|
2015-10-26 17:00:21 -07:00
|
|
|
e.stopPropagation();
|
2015-03-05 16:13:53 -08:00
|
|
|
},
|
2015-02-17 23:58:00 -08:00
|
|
|
className: function() {
|
2015-03-23 18:30:16 -07:00
|
|
|
return ["entry", this.model.get('type')].join(' ');
|
2015-02-17 23:58:00 -08:00
|
|
|
},
|
2015-10-28 13:57:32 -07:00
|
|
|
renderPending: function() {
|
|
|
|
this.$el.addClass('pending');
|
|
|
|
},
|
|
|
|
renderDone: function() {
|
|
|
|
this.$el.removeClass('pending');
|
|
|
|
},
|
2015-07-07 19:21:10 -07:00
|
|
|
renderSent: function() {
|
2015-05-26 13:30:51 -07:00
|
|
|
if (this.model.isOutgoing()) {
|
2015-07-07 19:21:10 -07:00
|
|
|
this.$el.toggleClass('sent', !!this.model.get('sent'));
|
2015-05-26 13:30:51 -07:00
|
|
|
}
|
|
|
|
},
|
2015-03-05 16:13:53 -08:00
|
|
|
renderDelivered: function() {
|
|
|
|
if (this.model.get('delivered')) { this.$el.addClass('delivered'); }
|
|
|
|
},
|
2015-11-06 17:37:12 -08:00
|
|
|
onErrorsChanged: function() {
|
|
|
|
if (this.model.isIncoming()) {
|
|
|
|
this.render();
|
|
|
|
} else {
|
|
|
|
this.renderErrors();
|
|
|
|
}
|
|
|
|
},
|
2015-09-30 14:27:18 -07:00
|
|
|
renderErrors: function() {
|
|
|
|
var errors = this.model.get('errors');
|
2015-09-30 15:22:59 -07:00
|
|
|
if (_.size(errors) > 0) {
|
2015-10-23 17:20:24 -07:00
|
|
|
this.$('.bubble').addClass('error');
|
2015-09-30 15:22:59 -07:00
|
|
|
if (this.model.isIncoming()) {
|
|
|
|
this.$('.content').text(this.model.getDescription()).addClass('error-message');
|
|
|
|
}
|
2015-10-02 12:14:47 -07:00
|
|
|
} else {
|
2015-11-05 20:11:28 -08:00
|
|
|
this.$('.bubble').removeClass('error');
|
2015-09-30 15:22:59 -07:00
|
|
|
}
|
2015-09-30 14:27:18 -07:00
|
|
|
},
|
2015-03-23 18:30:16 -07:00
|
|
|
renderControl: function() {
|
|
|
|
if (this.model.isEndSession() || this.model.isGroupUpdate()) {
|
|
|
|
this.$el.addClass('control');
|
2015-03-27 18:47:58 -07:00
|
|
|
this.$('.content').text(this.model.getDescription());
|
2015-03-23 18:30:16 -07:00
|
|
|
} else {
|
|
|
|
this.$el.removeClass('control');
|
|
|
|
}
|
|
|
|
},
|
2015-02-17 23:58:00 -08:00
|
|
|
render: function() {
|
2015-03-11 17:49:01 -07:00
|
|
|
var contact = this.model.getContact();
|
2015-01-09 12:53:39 -10:00
|
|
|
this.$el.html(
|
2015-12-08 15:01:29 -08:00
|
|
|
Mustache.render(_.result(this, 'template', ''), {
|
2015-07-04 23:08:25 -07:00
|
|
|
message: this.model.get('body'),
|
2015-11-06 14:58:26 -08:00
|
|
|
timestamp: this.model.get('sent_at'),
|
2015-03-17 17:10:18 -07:00
|
|
|
sender: (contact && contact.getTitle()) || '',
|
2015-06-18 17:05:00 -07:00
|
|
|
avatar: (contact && contact.getAvatar())
|
2015-03-23 14:01:18 -07:00
|
|
|
}, this.render_partials())
|
2015-01-09 12:53:39 -10:00
|
|
|
);
|
2015-11-06 14:58:26 -08:00
|
|
|
this.timeStampView.setElement(this.$('.timestamp'));
|
|
|
|
this.timeStampView.update();
|
2014-12-21 21:01:21 -08:00
|
|
|
|
2015-10-23 16:03:51 -07:00
|
|
|
this.renderControl();
|
|
|
|
|
2015-03-19 16:46:58 -07:00
|
|
|
twemoji.parse(this.el, { base: '/images/twemoji/', size: 16 });
|
2015-03-10 12:11:32 -07:00
|
|
|
|
2015-03-27 18:47:58 -07:00
|
|
|
var content = this.$('.content');
|
2015-07-04 23:08:25 -07:00
|
|
|
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-03-06 17:01:04 -08:00
|
|
|
|
2015-07-07 19:21:10 -07:00
|
|
|
this.renderSent();
|
2015-03-05 16:13:53 -08:00
|
|
|
this.renderDelivered();
|
2015-09-30 15:22:59 -07:00
|
|
|
this.renderErrors();
|
2015-11-12 10:35:29 -08:00
|
|
|
this.loadAttachments();
|
2015-01-09 12:53:39 -10:00
|
|
|
|
2015-02-17 23:58:00 -08:00
|
|
|
return this;
|
2015-11-12 10:35:29 -08:00
|
|
|
},
|
|
|
|
loadAttachments: function() {
|
|
|
|
this.model.get('attachments').forEach(function(attachment) {
|
|
|
|
var view = new Whisper.AttachmentView({ model: attachment }).render();
|
|
|
|
this.listenTo(view, 'update', function() {
|
|
|
|
this.trigger('beforeChangeHeight');
|
|
|
|
this.$('.attachments').append(view.el);
|
|
|
|
this.trigger('afterChangeHeight');
|
|
|
|
});
|
|
|
|
}.bind(this));
|
2015-02-17 23:58:00 -08:00
|
|
|
}
|
|
|
|
});
|
2014-05-16 21:48:46 -07:00
|
|
|
|
|
|
|
})();
|