Commit adf34c84 authored by JC Brand's avatar JC Brand

Render chatbox avatar via the vcards collection

parent 71ed5bbd
...@@ -213,6 +213,11 @@ ...@@ -213,6 +213,11 @@
}, },
initialize () { initialize () {
this.vcard = _converse.vcards.findWhere({'jid': this.get('jid')});
if (_.isNil(this.vcard)) {
this.vcard = _converse.vcards.create({'jid': this.get('jid')});
}
this.messages = new _converse.Messages(); this.messages = new _converse.Messages();
this.messages.browserStorage = new Backbone.BrowserStorage[_converse.message_storage]( this.messages.browserStorage = new Backbone.BrowserStorage[_converse.message_storage](
b64_sha1(`converse.messages${this.get('jid')}${_converse.bare_jid}`)); b64_sha1(`converse.messages${this.get('jid')}${_converse.bare_jid}`));
......
...@@ -102,8 +102,6 @@ ...@@ -102,8 +102,6 @@
_converse.api.settings.update({ _converse.api.settings.update({
'use_emojione': false, 'use_emojione': false,
'emojione_image_path': emojione.imagePathPNG, 'emojione_image_path': emojione.imagePathPNG,
'chatview_avatar_height': 32,
'chatview_avatar_width': 32,
'show_toolbar': true, 'show_toolbar': true,
'time_format': 'HH:mm', 'time_format': 'HH:mm',
'visible_toolbar_buttons': { 'visible_toolbar_buttons': {
...@@ -204,23 +202,24 @@ ...@@ -204,23 +202,24 @@
} }
}); });
_converse.ChatBoxHeading = Backbone.NativeView.extend({ _converse.ChatBoxHeading = _converse.ViewWithAvatar.extend({
initialize () { initialize () {
this.model.on('change:image', this.render, this);
this.model.on('change:status', this.onStatusMessageChanged, this); this.model.on('change:status', this.onStatusMessageChanged, this);
this.model.on('change:fullname', this.render, this); this.model.vcard.on('change', this.render, this);
}, },
render () { render () {
this.el.innerHTML = tpl_chatbox_head( this.el.innerHTML = tpl_chatbox_head(
_.extend(this.model.toJSON(), { _.extend(
'_converse': _converse, this.model.toJSON(),
'avatar_width': _converse.chatview_avatar_width, this.model.vcard.toJSON(),
'avatar_height': _converse.chatview_avatar_height, { '_converse': _converse,
'info_close': __('Close this chat box'), 'info_close': __('Close this chat box')
}) }
)
); );
this.renderAvatar();
return this; return this;
}, },
......
...@@ -41,7 +41,33 @@ ...@@ -41,7 +41,33 @@
const { _converse } = this, const { _converse } = this,
{ __ } = _converse; { __ } = _converse;
_converse.MessageView = Backbone.NativeView.extend({ _converse.ViewWithAvatar = Backbone.NativeView.extend({
renderAvatar () {
const canvas_el = this.el.querySelector('canvas');
if (_.isNull(canvas_el)) {
return;
}
const image_type = this.model.vcard.get('image_type'),
image = this.model.vcard.get('image'),
img_src = "data:" + image_type + ";base64," + image,
img = new Image();
img.onload = () => {
const ctx = canvas_el.getContext('2d'),
ratio = img.width / img.height;
ctx.clearRect(0, 0, canvas_el.width, canvas_el.height);
if (ratio < 1) {
ctx.drawImage(img, 0, 0, canvas_el.width, canvas_el.height * (1 / ratio));
} else {
ctx.drawImage(img, 0, 0, canvas_el.width, canvas_el.height * ratio);
}
};
img.src = img_src;
},
});
_converse.MessageView = _converse.ViewWithAvatar.extend({
initialize () { initialize () {
this.chatbox = this.model.collection.chatbox; this.chatbox = this.model.collection.chatbox;
...@@ -53,7 +79,7 @@ ...@@ -53,7 +79,7 @@
}) })
} }
}); });
this.model.vcard.on('change:image', () => this.renderAvatar()); this.model.vcard.on('change:image', this.renderAvatar, this);
this.model.on('change:fullname', this.render, this); this.model.on('change:fullname', this.render, this);
this.model.on('change:progress', this.renderFileUploadProgresBar, this); this.model.on('change:progress', this.renderFileUploadProgresBar, this);
this.model.on('change:type', this.render, this); this.model.on('change:type', this.render, this);
...@@ -121,29 +147,6 @@ ...@@ -121,29 +147,6 @@
} }
}, },
renderAvatar () {
const canvas_el = this.el.querySelector('canvas');
if (_.isNull(canvas_el)) {
return;
}
const image_type = this.model.vcard.get('image_type'),
image = this.model.vcard.get('image'),
img_src = "data:" + image_type + ";base64," + image,
img = new Image();
img.onload = () => {
const ctx = canvas_el.getContext('2d'),
ratio = img.width / img.height;
ctx.clearRect(0, 0, canvas_el.width, canvas_el.height);
if (ratio < 1) {
ctx.drawImage(img, 0, 0, canvas_el.width, canvas_el.height * (1 / ratio));
} else {
ctx.drawImage(img, 0, 0, canvas_el.width, canvas_el.height * ratio);
}
};
img.src = img_src;
},
replaceElement (msg) { replaceElement (msg) {
if (!_.isNil(this.el.parentElement)) { if (!_.isNil(this.el.parentElement)) {
this.el.parentElement.replaceChild(msg, this.el); this.el.parentElement.replaceChild(msg, this.el);
......
<div class="chat-head chat-head-chatbox row no-gutters"> <div class="chat-head chat-head-chatbox row no-gutters">
<div class="col"> <div class="col">
<div class="row no-gutters"> <div class="row no-gutters">
<canvas class="avatar" height="36" width="36"></canvas>
<div class="col chat-title" title="{{{o.jid}}}"> <div class="col chat-title" title="{{{o.jid}}}">
{[ if (o.url) { ]} {[ if (o.url) { ]}
<a href="{{{o.url}}}" target="_blank" rel="noopener" class="user"> <a href="{{{o.url}}}" target="_blank" rel="noopener" class="user">
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment