xmpp.chapril.org-conversejs/src/converse-chatview.js

1022 lines
46 KiB
JavaScript
Raw Normal View History

2016-03-13 17:16:53 +01:00
// Converse.js (A browser based XMPP chat client)
// http://conversejs.org
//
2017-02-02 19:29:38 +01:00
// Copyright (c) 2012-2017, Jan-Carel Brand <jc@opkode.com>
2016-03-13 17:16:53 +01:00
// Licensed under the Mozilla Public License (MPLv2)
//
/*global define */
2016-03-13 17:16:53 +01:00
(function (root, factory) {
define([
2017-07-10 21:14:48 +02:00
"jquery.noconflict",
"converse-core",
2017-06-16 11:31:57 +02:00
"emojione",
"xss",
"tpl!chatbox",
"tpl!new_day",
"tpl!action",
2017-06-16 11:31:57 +02:00
"tpl!emojis",
"tpl!message",
2017-04-21 18:35:34 +02:00
"tpl!help_message",
"tpl!toolbar",
2017-06-14 18:09:22 +02:00
"tpl!avatar",
"tpl!spinner"
], factory);
}(this, function (
2017-07-10 21:14:48 +02:00
$,
converse,
2017-06-16 11:31:57 +02:00
emojione,
xss,
tpl_chatbox,
tpl_new_day,
tpl_action,
2017-06-16 11:31:57 +02:00
tpl_emojis,
tpl_message,
2017-04-21 18:35:34 +02:00
tpl_help_message,
tpl_toolbar,
2017-06-14 18:09:22 +02:00
tpl_avatar,
tpl_spinner
) {
2016-03-13 17:16:53 +01:00
"use strict";
2017-07-10 21:14:48 +02:00
const { $msg, Backbone, Strophe, _, moment, utils } = converse.env;
const KEY = {
2016-03-13 17:16:53 +01:00
ENTER: 13,
FORWARD_SLASH: 47
};
converse.plugins.add('converse-chatview', {
2016-03-13 17:16:53 +01:00
overrides: {
// Overrides mentioned here will be picked up by converse.js's
// plugin architecture they will replace existing methods on the
// relevant objects or classes.
//
// New functions which don't exist yet can also be added.
2017-06-16 11:31:57 +02:00
//
registerGlobalEventHandlers: function () {
this.__super__.registerGlobalEventHandlers();
document.addEventListener(
2017-07-15 15:15:37 +02:00
'click', function (ev) {
if (_.includes(ev.target.classList, 'toggle-toolbar-menu') ||
_.includes(ev.target.classList, 'insert-emoji')) {
return;
}
utils.slideInAllElements(
document.querySelectorAll('.toolbar-menu')
)
2017-06-16 11:31:57 +02:00
}
);
2017-06-16 11:31:57 +02:00
},
2016-03-13 17:16:53 +01:00
ChatBoxViews: {
onChatBoxAdded (item) {
const { _converse } = this.__super__;
let view = this.get(item.get('id'));
if (!view) {
view = new _converse.ChatBoxView({model: item});
2016-03-13 17:16:53 +01:00
this.add(item.get('id'), view);
return view;
2016-03-13 17:16:53 +01:00
} else {
2016-08-31 12:06:17 +02:00
return this.__super__.onChatBoxAdded.apply(this, arguments);
2016-03-13 17:16:53 +01:00
}
}
}
},
initialize () {
2016-03-13 17:16:53 +01:00
/* The initialize function gets called as soon as the plugin is
* loaded by converse.js's plugin machinery.
*/
const { _converse } = this,
{ __ } = _converse;
_converse.api.settings.update({
'use_emojione': true,
'emojione_image_path': emojione.imagePathPNG,
'chatview_avatar_height': 32,
'chatview_avatar_width': 32,
'show_toolbar': true,
'time_format': 'HH:mm',
'visible_toolbar_buttons': {
2017-06-24 11:00:44 +02:00
'emoji': true,
'call': false,
'clear': true
},
2016-03-13 17:16:53 +01:00
});
emojione.imagePathPNG = _converse.emojione_image_path;
emojione.ascii = true;
2016-03-13 17:16:53 +01:00
function onWindowStateChanged (data) {
_converse.chatboxviews.each(function (chatboxview) {
chatboxview.onWindowStateChanged(data.state);
});
}
_converse.api.listen.on('windowStateChanged', onWindowStateChanged);
2017-06-16 11:31:57 +02:00
_converse.EmojiPicker = Backbone.Model.extend({
defaults: {
'current_category': 'people',
'current_skintone': '',
'scroll_position': 0
2017-06-16 11:31:57 +02:00
}
});
_converse.EmojiPickerView = Backbone.View.extend({
className: 'emoji-picker-container toolbar-menu collapsed',
2017-06-16 11:31:57 +02:00
events: {
'click .emoji-category-picker li.emoji-category': 'chooseCategory',
'click .emoji-category-picker li.emoji-skintone': 'chooseSkinTone'
2017-06-16 11:31:57 +02:00
},
initialize () {
this.model.on('change:current_skintone', this.render, this);
this.model.on('change:current_category', this.render, this);
this.setScrollPosition = _.debounce(this.setScrollPosition, 50);
2017-06-16 11:31:57 +02:00
},
render () {
2017-06-16 11:31:57 +02:00
var emojis_html = tpl_emojis(
_.extend(
this.model.toJSON(), {
'transform': _converse.use_emojione ? emojione.shortnameToImage : emojione.shortnameToUnicode,
'emojis_by_category': utils.getEmojisByCategory(_converse, emojione),
'toned_emojis': utils.getTonedEmojis(_converse),
'skintones': ['tone1', 'tone2', 'tone3', 'tone4', 'tone5'],
'shouldBeHidden': this.shouldBeHidden
2017-06-16 11:31:57 +02:00
}
));
this.el.innerHTML = emojis_html;
this.el.querySelectorAll('.emoji-picker').forEach((el) => {
el.addEventListener(
'scroll', this.setScrollPosition.bind(this)
);
});
this.restoreScrollPosition();
2017-06-16 11:31:57 +02:00
return this;
},
shouldBeHidden (shortname, current_skintone, toned_emojis) {
/* Helper method for the template which decides whether an
* emoji should be hidden, based on which skin tone is
* currently being applied.
*/
if (_.includes(shortname, '_tone')) {
if (!current_skintone || !_.includes(shortname, current_skintone)) {
return true;
}
} else {
if (current_skintone && _.includes(toned_emojis, shortname)) {
return true;
}
}
return false;
},
restoreScrollPosition () {
const current_picker = _.difference(
this.el.querySelectorAll('.emoji-picker'),
this.el.querySelectorAll('.emoji-picker.hidden')
);
if (current_picker.length === 1 && this.model.get('scroll_position')) {
current_picker[0].scrollTop = this.model.get('scroll_position');
}
},
setScrollPosition (ev, position) {
this.model.set('scroll_position', ev.target.scrollTop);
},
chooseSkinTone (ev) {
ev.preventDefault();
ev.stopPropagation();
const target = ev.target.nodeName === 'IMG' ?
ev.target.parentElement : ev.target;
const skintone = target.getAttribute("data-skintone").trim();
if (this.model.get('current_skintone') === skintone) {
this.model.set({'current_skintone': ''});
} else {
this.model.set({'current_skintone': skintone});
}
},
chooseCategory (ev) {
2017-06-16 11:31:57 +02:00
ev.preventDefault();
ev.stopPropagation();
const target = ev.target.nodeName === 'IMG' ?
ev.target.parentElement : ev.target;
const category = target.getAttribute("data-category").trim();
this.model.set({
'current_category': category,
'scroll_position': 0
});
2017-06-16 11:31:57 +02:00
}
});
_converse.ChatBoxView = Backbone.View.extend({
2016-03-13 17:16:53 +01:00
length: 200,
tagName: 'div',
className: 'chatbox hidden',
is_chatroom: false, // Leaky abstraction from MUC
2016-03-13 17:16:53 +01:00
events: {
'click .close-chatbox-button': 'close',
'keypress .chat-textarea': 'keyPressed',
2017-07-15 15:58:11 +02:00
'click .send-button': 'onFormSubmitted',
2017-07-15 11:55:07 +02:00
'click .toggle-smiley': 'toggleEmojiMenu',
'click .toggle-smiley ul.emoji-picker li': 'insertEmoji',
2016-03-13 17:16:53 +01:00
'click .toggle-clear': 'clearMessages',
2016-05-28 14:25:44 +02:00
'click .toggle-call': 'toggleCall',
'click .new-msgs-indicator': 'viewUnreadMessages'
2016-03-13 17:16:53 +01:00
},
initialize () {
2017-07-15 07:09:59 +02:00
this.emoji_picker_view = new _converse.EmojiPickerView({
'model': new _converse.EmojiPicker()
});
2016-03-13 17:16:53 +01:00
this.model.messages.on('add', this.onMessageAdded, this);
this.model.on('show', this.show, this);
this.model.on('destroy', this.hide, this);
// TODO check for changed fullname as well
this.model.on('change:chat_state', this.sendChatState, this);
this.model.on('change:chat_status', this.onChatStatusChanged, this);
this.model.on('change:image', this.renderAvatar, this);
this.model.on('change:status', this.onStatusChanged, this);
this.model.on('showHelpMessages', this.showHelpMessages, this);
this.model.on('sendMessage', this.sendMessage, this);
this.render().fetchMessages();
_converse.emit('chatBoxInitialized', this);
2016-03-13 17:16:53 +01:00
},
render () {
2016-03-13 17:16:53 +01:00
this.$el.attr('id', this.model.get('box_id'))
.html(tpl_chatbox(
2016-03-13 17:16:53 +01:00
_.extend(this.model.toJSON(), {
show_toolbar: _converse.show_toolbar,
2016-03-13 17:16:53 +01:00
show_textarea: true,
show_send_button: _converse.show_send_button,
2016-03-13 17:16:53 +01:00
title: this.model.get('fullname'),
unread_msgs: __('You have unread messages'),
2016-03-13 17:16:53 +01:00
info_close: __('Close this chat box'),
label_personal_message: __('Personal message'),
label_send: __('Send')
2016-03-13 17:16:53 +01:00
}
)
)
);
this.$content = this.$el.find('.chat-content');
this.renderToolbar().renderAvatar();
_converse.emit('chatBoxOpened', this);
utils.refreshWebkit();
2016-03-13 17:16:53 +01:00
return this.showStatusMessage();
},
afterMessagesFetched () {
this.insertIntoDOM();
2017-02-25 22:17:14 +01:00
this.scrollDown();
// We only start listening for the scroll event after
// cached messages have been fetched
this.$content.on('scroll', this.markScrolled.bind(this));
2016-03-13 17:16:53 +01:00
},
fetchMessages () {
2016-03-13 17:16:53 +01:00
this.model.messages.fetch({
'add': true,
'success': this.afterMessagesFetched.bind(this),
'error': this.afterMessagesFetched.bind(this),
2016-03-13 17:16:53 +01:00
});
return this;
},
insertIntoDOM () {
2016-03-13 17:16:53 +01:00
/* This method gets overridden in src/converse-controlbox.js if
2016-03-19 23:16:00 +01:00
* the controlbox plugin is active.
*/
const container = document.querySelector('#conversejs');
2017-02-25 22:17:14 +01:00
if (this.el.parentNode !== container) {
container.insertBefore(this.el, container.firstChild);
}
2016-03-13 17:16:53 +01:00
return this;
},
clearStatusNotification () {
2016-03-13 17:16:53 +01:00
this.$content.find('div.chat-event').remove();
},
showStatusNotification (message, keep_old, permanent) {
2016-03-13 17:16:53 +01:00
if (!keep_old) {
this.clearStatusNotification();
}
const $el = $('<div class="chat-info"></div>').text(message);
if (!permanent) {
$el.addClass('chat-event');
}
this.$content.append($el);
this.scrollDown();
2016-03-13 17:16:53 +01:00
},
addSpinner () {
if (_.isNull(this.el.querySelector('.spinner'))) {
2017-06-14 18:09:22 +02:00
this.$content.prepend(tpl_spinner);
2016-03-13 17:16:53 +01:00
}
},
clearSpinner () {
2016-03-13 17:16:53 +01:00
if (this.$content.children(':first').is('span.spinner')) {
this.$content.children(':first').remove();
}
},
insertDayIndicator (date, prepend) {
/* Appends (or prepends if "prepend" is truthy) an indicator
* into the chat area, showing the day as given by the
* passed in date.
2016-03-19 23:16:00 +01:00
*
* Parameters:
* (String) date - An ISO8601 date string.
*/
const day_date = moment(date).startOf('day');
const insert = prepend ? this.$content.prepend: this.$content.append;
insert.call(this.$content, tpl_new_day({
2016-03-13 17:16:53 +01:00
isodate: day_date.format(),
datestring: day_date.format("dddd MMM Do YYYY")
}));
},
insertMessage (attrs, prepend) {
/* Helper method which appends a message (or prepends if the
* 2nd parameter is set to true) to the end of the chat box's
* content area.
2016-03-19 23:16:00 +01:00
*
* Parameters:
* (Object) attrs: An object containing the message attributes.
*/
const insert = prepend ? this.$content.prepend : this.$content.append;
_.flow(($el) => {
insert.call(this.$content, $el);
return $el;
},
this.scrollDown.bind(this)
2016-03-13 17:16:53 +01:00
)(this.renderMessage(attrs));
},
showMessage (attrs) {
2016-03-13 17:16:53 +01:00
/* Inserts a chat message into the content area of the chat box.
2016-03-19 23:16:00 +01:00
* Will also insert a new day indicator if the message is on a
* different day.
*
* The message to show may either be newer than the newest
* message, or older than the oldest message.
*
* Parameters:
* (Object) attrs: An object containing the message
* attributes.
2016-03-19 23:16:00 +01:00
*/
let current_msg_date = moment(attrs.time) || moment;
const $first_msg = this.$content.find('.chat-message:first'),
first_msg_date = $first_msg.data('isodate');
2016-03-13 17:16:53 +01:00
if (!first_msg_date) {
// This is the first received message, so we insert a
// date indicator before it.
this.insertDayIndicator(current_msg_date);
this.insertMessage(attrs);
2016-03-13 17:16:53 +01:00
return;
}
const last_msg_date = this.$content.find('.chat-message:last').data('isodate');
if (current_msg_date.isAfter(last_msg_date) ||
current_msg_date.isSame(last_msg_date)) {
2016-03-13 17:16:53 +01:00
// The new message is after the last message
if (current_msg_date.isAfter(last_msg_date, 'day')) {
// Append a new day indicator
this.insertDayIndicator(current_msg_date);
2016-03-13 17:16:53 +01:00
}
this.insertMessage(attrs);
2016-03-13 17:16:53 +01:00
return;
}
if (current_msg_date.isBefore(first_msg_date) ||
current_msg_date.isSame(first_msg_date)) {
// The message is before the first, but on the same day.
// We need to prepend the message immediately before the
// first message (so that it'll still be after the day
// indicator).
this.insertMessage(attrs, 'prepend');
2016-03-13 17:16:53 +01:00
if (current_msg_date.isBefore(first_msg_date, 'day')) {
// This message is also on a different day, so
// we prepend a day indicator.
this.insertDayIndicator(current_msg_date, 'prepend');
2016-03-13 17:16:53 +01:00
}
return;
2016-03-13 17:16:53 +01:00
}
// Find the correct place to position the message
current_msg_date = current_msg_date.format();
const msg_dates = _.map(
this.$content.find('.chat-message'),
(el) => $(el).data('isodate')
);
msg_dates.push(current_msg_date);
msg_dates.sort();
const idx = msg_dates.indexOf(current_msg_date)-1;
const $latest_message = this.$content.find(`.chat-message[data-isodate="${msg_dates[idx]}"]:last`);
_.flow(($el) => {
$el.insertAfter($latest_message);
return $el;
},
this.scrollDown.bind(this)
)(this.renderMessage(attrs));
},
getExtraMessageTemplateAttributes () {
/* Provides a hook for sending more attributes to the
* message template.
*
* Parameters:
* (Object) attrs: An object containing message attributes.
*/
return {};
},
getExtraMessageClasses (attrs) {
2017-02-15 21:30:32 +01:00
return attrs.delayed && 'delayed' || '';
},
renderMessage (attrs) {
2016-03-13 17:16:53 +01:00
/* Renders a chat message based on the passed in attributes.
2016-03-19 23:16:00 +01:00
*
* Parameters:
* (Object) attrs: An object containing the message attributes.
*
* Returns:
* The DOM element representing the message.
*/
let text = attrs.message,
2016-03-13 17:16:53 +01:00
fullname = this.model.get('fullname') || attrs.fullname,
template, username;
const match = text.match(/^\/(.*?)(?: (.*))?$/);
2016-03-13 17:16:53 +01:00
if ((match) && (match[1] === 'me')) {
text = text.replace(/^\/me/, '');
template = tpl_action;
2017-02-15 22:03:02 +01:00
if (attrs.sender === 'me') {
fullname = _converse.xmppstatus.get('fullname') || attrs.fullname;
2017-02-15 22:03:02 +01:00
username = _.isNil(fullname)? _converse.bare_jid: fullname;
} else {
username = attrs.fullname;
}
2016-03-13 17:16:53 +01:00
} else {
template = tpl_message;
2016-03-13 17:16:53 +01:00
username = attrs.sender === 'me' && __('me') || fullname;
}
this.$content.find('div.chat-event').remove();
if (text.length > 8000) {
text = text.substring(0, 10) + '...';
this.showStatusNotification(
__("A very large message has been received."+
"This might be due to an attack meant to degrade the chat performance."+
"Output has been shortened."),
true, true);
}
const msg_time = moment(attrs.time) || moment;
const $msg = $(template(
_.extend(this.getExtraMessageTemplateAttributes(attrs), {
'msgid': attrs.msgid,
'sender': attrs.sender,
'time': msg_time.format(_converse.time_format),
'isodate': msg_time.format(),
'username': username,
2017-02-15 21:30:32 +01:00
'extra_classes': this.getExtraMessageClasses(attrs)
})
));
const msg_content = $msg[0].querySelector('.chat-msg-content');
msg_content.innerHTML = utils.addEmoji(
_converse, emojione, utils.addHyperlinks(xss.filterXSS(text, {'whiteList': {}}))
);
utils.renderImageURLs(msg_content);
return $msg;
2016-03-13 17:16:53 +01:00
},
showHelpMessages (msgs, type, spinner) {
_.each(msgs, (msg) => {
2017-04-21 18:35:34 +02:00
this.$content.append($(tpl_help_message({
'type': type||'info',
'message': msgs
2017-04-21 18:35:34 +02:00
})));
});
2016-03-13 17:16:53 +01:00
if (spinner === true) {
2017-06-14 18:09:22 +02:00
this.$content.append(tpl_spinner);
2016-03-13 17:16:53 +01:00
} else if (spinner === false) {
this.$content.find('span.spinner').remove();
}
return this.scrollDown();
},
handleChatStateMessage (message) {
if (message.get('chat_state') === _converse.COMPOSING) {
if (message.get('sender') === 'me') {
this.showStatusNotification(__('Typing from another device'));
} else {
this.showStatusNotification(message.get('fullname')+' '+__('is typing'));
}
2016-08-18 12:19:43 +02:00
this.clear_status_timeout = window.setTimeout(this.clearStatusNotification.bind(this), 30000);
} else if (message.get('chat_state') === _converse.PAUSED) {
if (message.get('sender') === 'me') {
this.showStatusNotification(__('Stopped typing on the other device'));
} else {
this.showStatusNotification(message.get('fullname')+' '+__('has stopped typing'));
}
} else if (_.includes([_converse.INACTIVE, _converse.ACTIVE], message.get('chat_state'))) {
2016-03-13 17:16:53 +01:00
this.$content.find('div.chat-event').remove();
} else if (message.get('chat_state') === _converse.GONE) {
2016-03-13 17:16:53 +01:00
this.showStatusNotification(message.get('fullname')+' '+__('has gone away'));
}
},
shouldShowOnTextMessage () {
return !this.$el.is(':visible');
},
handleTextMessage (message) {
this.showMessage(_.clone(message.attributes));
if (utils.isNewMessage(message) && message.get('sender') === 'me') {
// We remove the "scrolled" flag so that the chat area
// gets scrolled down. We always want to scroll down
// when the user writes a message as opposed to when a
// message is received.
this.model.set('scrolled', false);
} else {
if (utils.isNewMessage(message) && this.model.get('scrolled', true)) {
this.$el.find('.new-msgs-indicator').removeClass('hidden');
}
2016-03-13 17:16:53 +01:00
}
if (this.shouldShowOnTextMessage()) {
2016-03-13 17:16:53 +01:00
this.show();
} else {
this.scrollDown();
2016-03-13 17:16:53 +01:00
}
},
handleErrorMessage (message) {
const $message = $(`[data-msgid=${message.get('msgid')}]`);
if ($message.length) {
$message.after($('<div class="chat-info chat-error"></div>').text(message.get('message')));
this.scrollDown();
}
},
onMessageAdded (message) {
2016-03-13 17:16:53 +01:00
/* Handler that gets called when a new message object is created.
2016-03-19 23:16:00 +01:00
*
* Parameters:
* (Object) message - The message Backbone object that was added.
*/
if (!_.isUndefined(this.clear_status_timeout)) {
2016-03-13 17:16:53 +01:00
window.clearTimeout(this.clear_status_timeout);
delete this.clear_status_timeout;
}
if (message.get('type') === 'error') {
this.handleErrorMessage(message);
} else if (!message.get('message')) {
2016-03-13 17:16:53 +01:00
this.handleChatStateMessage(message);
} else {
this.handleTextMessage(message);
}
_converse.emit('messageAdded', {
'message': message,
'chatbox': this.model
});
2016-03-13 17:16:53 +01:00
},
createMessageStanza (message) {
2016-03-13 17:16:53 +01:00
return $msg({
from: _converse.connection.jid,
2016-03-13 17:16:53 +01:00
to: this.model.get('jid'),
type: 'chat',
id: message.get('msgid')
}).c('body').t(message.get('message')).up()
.c(_converse.ACTIVE, {'xmlns': Strophe.NS.CHATSTATES}).up();
2016-03-13 17:16:53 +01:00
},
sendMessage (message) {
2016-03-13 17:16:53 +01:00
/* Responsible for sending off a text message.
2016-03-19 23:16:00 +01:00
*
* Parameters:
* (Message) message - The chat message
*/
2016-03-13 17:16:53 +01:00
// TODO: We might want to send to specfic resources.
// Especially in the OTR case.
const messageStanza = this.createMessageStanza(message);
_converse.connection.send(messageStanza);
if (_converse.forward_messages) {
2016-03-13 17:16:53 +01:00
// Forward the message, so that other connected resources are also aware of it.
_converse.connection.send(
$msg({ to: _converse.bare_jid, type: 'chat', id: message.get('msgid') })
2016-03-13 17:16:53 +01:00
.c('forwarded', {xmlns:'urn:xmpp:forward:0'})
.c('delay', {xmns:'urn:xmpp:delay',stamp:(new Date()).getTime()}).up()
.cnode(messageStanza.tree())
);
}
},
onMessageSubmitted (text) {
2016-03-13 17:16:53 +01:00
/* This method gets called once the user has typed a message
2016-03-19 23:16:00 +01:00
* and then pressed enter in a chat box.
*
* Parameters:
* (string) text - The chat message text.
*/
if (!_converse.connection.authenticated) {
2016-03-13 17:16:53 +01:00
return this.showHelpMessages(
['Sorry, the connection has been lost, '+
'and your message could not be sent'],
'error'
);
}
const match = text.replace(/^\s*/, "").match(/^\/(.*)\s*$/);
2016-03-13 17:16:53 +01:00
if (match) {
if (match[1] === "clear") {
return this.clearMessages();
}
else if (match[1] === "help") {
const msgs = [
`<strong>/help</strong>:${__('Show this menu')}`,
`<strong>/me</strong>:${__('Write in the third person')}`,
`<strong>/clear</strong>:${__('Remove messages')}`
2016-03-13 17:16:53 +01:00
];
this.showHelpMessages(msgs);
return;
}
}
let fullname = _converse.xmppstatus.get('fullname');
fullname = _.isEmpty(fullname)? _converse.bare_jid: fullname;
const message = this.model.messages.create({
fullname,
2016-03-13 17:16:53 +01:00
sender: 'me',
time: moment().format(),
message: text
});
this.sendMessage(message);
},
sendChatState () {
2016-03-13 17:16:53 +01:00
/* Sends a message with the status of the user in this chat session
2016-03-19 23:16:00 +01:00
* as taken from the 'chat_state' attribute of the chat box.
* See XEP-0085 Chat State Notifications.
*/
_converse.connection.send(
2016-03-13 17:16:53 +01:00
$msg({'to':this.model.get('jid'), 'type': 'chat'})
.c(this.model.get('chat_state'), {'xmlns': Strophe.NS.CHATSTATES}).up()
.c('no-store', {'xmlns': Strophe.NS.HINTS}).up()
.c('no-permanent-store', {'xmlns': Strophe.NS.HINTS})
2016-03-13 17:16:53 +01:00
);
},
setChatState (state, no_save) {
2016-03-13 17:16:53 +01:00
/* Mutator for setting the chat state of this chat session.
2016-03-19 23:16:00 +01:00
* Handles clearing of any chat state notification timeouts and
* setting new ones if necessary.
* Timeouts are set when the state being set is COMPOSING or PAUSED.
* After the timeout, COMPOSING will become PAUSED and PAUSED will become INACTIVE.
* See XEP-0085 Chat State Notifications.
*
* Parameters:
* (string) state - The chat state (consts ACTIVE, COMPOSING, PAUSED, INACTIVE, GONE)
* (Boolean) no_save - Just do the cleanup or setup but don't actually save the state.
*/
if (!_.isUndefined(this.chat_state_timeout)) {
2016-03-13 17:16:53 +01:00
window.clearTimeout(this.chat_state_timeout);
delete this.chat_state_timeout;
}
if (state === _converse.COMPOSING) {
2016-03-13 17:16:53 +01:00
this.chat_state_timeout = window.setTimeout(
this.setChatState.bind(this), _converse.TIMEOUTS.PAUSED, _converse.PAUSED);
} else if (state === _converse.PAUSED) {
2016-03-13 17:16:53 +01:00
this.chat_state_timeout = window.setTimeout(
this.setChatState.bind(this), _converse.TIMEOUTS.INACTIVE, _converse.INACTIVE);
2016-03-13 17:16:53 +01:00
}
if (!no_save && this.model.get('chat_state') !== state) {
this.model.set('chat_state', state);
}
return this;
},
2017-07-15 15:58:11 +02:00
onFormSubmitted (ev) {
ev.preventDefault();
const textarea = this.el.querySelector('.chat-textarea'),
2017-07-15 15:58:11 +02:00
message = textarea.value;
textarea.value = '';
textarea.focus();
if (message !== '') {
this.onMessageSubmitted(message);
_converse.emit('messageSend', message);
}
this.setChatState(_converse.ACTIVE);
},
2017-07-15 15:58:11 +02:00
keyPressed (ev) {
/* Event handler for when a key is pressed in a chat box textarea.
*/
if (ev.keyCode === KEY.ENTER) {
this.onFormSubmitted(ev);
} else {
// Set chat state to composing if keyCode is not a forward-slash
// (which would imply an internal command and not a message).
this.setChatState(_converse.COMPOSING, ev.keyCode === KEY.FORWARD_SLASH);
}
},
clearMessages (ev) {
2016-03-13 17:16:53 +01:00
if (ev && ev.preventDefault) { ev.preventDefault(); }
const result = confirm(__("Are you sure you want to clear the messages from this chat box?"));
2016-03-13 17:16:53 +01:00
if (result === true) {
this.$content.empty();
this.model.messages.reset();
this.model.messages.browserStorage._clear();
}
return this;
},
insertIntoTextArea (value) {
const $textbox = this.$el.find('textarea.chat-textarea');
let existing = $textbox.val();
if (existing && (existing[existing.length-1] !== ' ')) {
existing = existing + ' ';
}
$textbox.focus().val(existing+value+' ');
},
2017-07-15 11:55:07 +02:00
insertEmoji (ev) {
2016-03-13 17:16:53 +01:00
ev.stopPropagation();
2017-07-15 11:55:07 +02:00
this.toggleEmojiMenu();
2017-07-15 07:09:59 +02:00
const target = ev.target.nodeName === 'IMG' ?
ev.target.parentElement : ev.target;
2017-07-15 11:55:07 +02:00
var shortname = target.getAttribute('data-emoji');
2017-07-15 07:09:59 +02:00
this.insertIntoTextArea(
emojione.shortnameToUnicode(shortname)
);
2016-03-13 17:16:53 +01:00
},
2017-07-15 11:55:07 +02:00
toggleEmojiMenu (ev) {
2017-06-16 11:31:57 +02:00
if (!_.isUndefined(ev)) {
ev.stopPropagation();
if (ev.target.classList.contains('emoji-category-picker') ||
ev.target.classList.contains('emoji-category')) {
return;
}
2017-06-16 11:31:57 +02:00
}
const elements = _.difference(
document.querySelectorAll('.toolbar-menu'),
[this.emoji_picker_view.el]
);
utils.slideInAllElements(elements).then(
_.partial(
utils.slideToggleElement,
this.emoji_picker_view.el
)
);
2016-03-13 17:16:53 +01:00
},
toggleCall (ev) {
2016-03-13 17:16:53 +01:00
ev.stopPropagation();
_converse.emit('callButtonClicked', {
connection: _converse.connection,
2016-03-13 17:16:53 +01:00
model: this.model
});
},
onChatStatusChanged (item) {
const chat_status = item.get('chat_status');
let fullname = item.get('fullname');
2016-03-13 17:16:53 +01:00
fullname = _.isEmpty(fullname)? item.get('jid'): fullname;
if (this.$el.is(':visible')) {
if (chat_status === 'offline') {
this.showStatusNotification(fullname+' '+__('has gone offline'));
} else if (chat_status === 'away') {
this.showStatusNotification(fullname+' '+__('has gone away'));
} else if ((chat_status === 'dnd')) {
this.showStatusNotification(fullname+' '+__('is busy'));
} else if (chat_status === 'online') {
this.$el.find('div.chat-event').remove();
}
}
},
onStatusChanged (item) {
2016-03-13 17:16:53 +01:00
this.showStatusMessage();
_converse.emit('contactStatusMessageChanged', {
2016-03-13 17:16:53 +01:00
'contact': item.attributes,
'message': item.get('status')
});
},
showStatusMessage (msg) {
2016-03-13 17:16:53 +01:00
msg = msg || this.model.get('status');
if (_.isString(msg)) {
2016-03-13 17:16:53 +01:00
this.$el.find('p.user-custom-message').text(msg).attr('title', msg);
}
return this;
},
close (ev) {
2016-03-13 17:16:53 +01:00
if (ev && ev.preventDefault) { ev.preventDefault(); }
if (_converse.connection.connected) {
// Immediately sending the chat state, because the
// model is going to be destroyed afterwards.
this.model.set('chat_state', _converse.INACTIVE);
this.sendChatState();
2016-03-13 17:16:53 +01:00
}
2017-04-19 15:40:27 +02:00
try {
this.model.destroy();
} catch (e) {
_converse.log(e, Strophe.LogLevel.ERROR);
2017-04-19 15:40:27 +02:00
}
this.remove();
_converse.emit('chatBoxClosed', this);
2016-03-13 17:16:53 +01:00
return this;
},
getToolbarOptions (options) {
return _.extend(options || {}, {
'label_clear': __('Clear all messages'),
'label_insert_smiley': __('Insert a smiley'),
'label_start_call': __('Start a call'),
'show_call_button': _converse.visible_toolbar_buttons.call,
'show_clear_button': _converse.visible_toolbar_buttons.clear,
2017-06-24 11:00:44 +02:00
'use_emoji': _converse.visible_toolbar_buttons.emoji,
2016-03-13 17:16:53 +01:00
});
},
renderToolbar (toolbar, options) {
if (!_converse.show_toolbar) { return; }
toolbar = toolbar || tpl_toolbar;
2017-06-16 11:31:57 +02:00
options = _.assign(
this.model.toJSON(),
this.getToolbarOptions(options || {})
);
2017-06-16 11:31:57 +02:00
this.el.querySelector('.chat-toolbar').innerHTML = toolbar(options);
var toggle = this.el.querySelector('.toggle-smiley');
toggle.innerHTML = '';
toggle.appendChild(this.emoji_picker_view.render().el);
2016-03-13 17:16:53 +01:00
return this;
},
renderAvatar () {
2016-03-13 17:16:53 +01:00
if (!this.model.get('image')) {
return;
}
const width = _converse.chatview_avatar_width;
const height = _converse.chatview_avatar_height;
const img_src = `data:${this.model.get('image_type')};base64,${this.model.get('image')}`,
canvas = $(tpl_avatar({
'width': width,
'height': height
})).get(0);
2016-03-13 17:16:53 +01:00
if (!(canvas.getContext && canvas.getContext('2d'))) {
return this;
}
const ctx = canvas.getContext('2d');
const img = new Image(); // Create new Image object
2016-03-13 17:16:53 +01:00
img.onload = function () {
const ratio = img.width/img.height;
2016-03-13 17:16:53 +01:00
if (ratio < 1) {
ctx.drawImage(img, 0,0, width, height*(1/ratio));
2016-03-13 17:16:53 +01:00
} else {
ctx.drawImage(img, 0,0, width, height*ratio);
2016-03-13 17:16:53 +01:00
}
};
img.src = img_src;
this.$el.find('.chat-title').before(canvas);
return this;
},
focus () {
2016-03-13 17:16:53 +01:00
this.$el.find('.chat-textarea').focus();
_converse.emit('chatBoxFocused', this);
2016-03-13 17:16:53 +01:00
return this;
},
hide () {
this.el.classList.add('hidden');
utils.refreshWebkit();
2016-03-13 17:16:53 +01:00
return this;
},
afterShown (focus) {
if (utils.isPersistableModel(this.model)) {
this.model.save();
}
this.setChatState(_converse.ACTIVE);
this.scrollDown();
if (focus) {
this.focus();
}
},
_show (focus) {
/* Inner show method that gets debounced */
if (this.$el.is(':visible') && this.$el.css('opacity') === "1") {
if (focus) { this.focus(); }
return;
}
2017-02-17 22:07:11 +01:00
utils.fadeIn(this.el, _.bind(this.afterShown, this, focus));
},
show (focus) {
if (_.isUndefined(this.debouncedShow)) {
2016-03-13 17:16:53 +01:00
/* We wrap the method in a debouncer and set it on the
2016-03-19 23:16:00 +01:00
* instance, so that we have it debounced per instance.
* Debouncing it on the class-level is too broad.
*/
this.debouncedShow = _.debounce(this._show, 250, {'leading': true});
2016-03-13 17:16:53 +01:00
}
this.debouncedShow.apply(this, arguments);
return this;
},
hideNewMessagesIndicator () {
const new_msgs_indicator = this.el.querySelector('.new-msgs-indicator');
if (!_.isNull(new_msgs_indicator)) {
new_msgs_indicator.classList.add('hidden');
}
},
markScrolled: _.debounce(function (ev) {
/* Called when the chat content is scrolled up or down.
* We want to record when the user has scrolled away from
* the bottom, so that we don't automatically scroll away
* from what the user is reading when new messages are
* received.
*/
if (ev && ev.preventDefault) { ev.preventDefault(); }
if (this.model.get('auto_scrolled')) {
this.model.set({
'scrolled': false,
'auto_scrolled': false
});
return;
}
let scrolled = true;
const is_at_bottom =
(this.$content.scrollTop() + this.$content.innerHeight()) >=
this.$content[0].scrollHeight-10;
if (is_at_bottom) {
scrolled = false;
this.onScrolledDown();
}
utils.safeSave(this.model, {'scrolled': scrolled});
}, 150),
viewUnreadMessages () {
this.model.save('scrolled', false);
2016-05-28 14:25:44 +02:00
this.scrollDown();
},
_scrollDown () {
2017-02-02 16:06:49 +01:00
/* Inner method that gets debounced */
if (this.$content.is(':visible') && !this.model.get('scrolled')) {
2016-03-13 17:16:53 +01:00
this.$content.scrollTop(this.$content[0].scrollHeight);
this.onScrolledDown();
this.model.save({'auto_scrolled': true});
2016-03-13 17:16:53 +01:00
}
2017-02-02 16:06:49 +01:00
},
onScrolledDown() {
this.hideNewMessagesIndicator();
if (_converse.windowState !== 'hidden') {
this.model.clearUnreadMsgCounter();
}
_converse.emit('chatBoxScrolledDown', {'chatbox': this.model});
},
scrollDown () {
2017-02-02 16:06:49 +01:00
if (_.isUndefined(this.debouncedScrollDown)) {
/* We wrap the method in a debouncer and set it on the
* instance, so that we have it debounced per instance.
* Debouncing it on the class-level is too broad.
*/
2017-02-25 22:17:14 +01:00
this.debouncedScrollDown = _.debounce(this._scrollDown, 250);
2017-02-02 16:06:49 +01:00
}
this.debouncedScrollDown.apply(this, arguments);
2016-03-13 17:16:53 +01:00
return this;
},
onWindowStateChanged (state) {
if (this.model.get('num_unread', 0) && !this.model.newMessageWillBeHidden()) {
this.model.clearUnreadMsgCounter();
}
2016-03-13 17:16:53 +01:00
}
});
}
});
return converse;
2016-03-13 17:16:53 +01:00
}));