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)
|
|
|
|
//
|
2017-04-21 15:52:16 +02:00
|
|
|
/*global define */
|
2016-03-13 17:16:53 +01:00
|
|
|
|
|
|
|
(function (root, factory) {
|
2017-02-13 15:37:17 +01:00
|
|
|
define([
|
2017-02-14 15:08:39 +01:00
|
|
|
"converse-core",
|
2017-08-16 11:16:22 +02:00
|
|
|
"converse-chatboxes",
|
2017-06-16 11:31:57 +02:00
|
|
|
"emojione",
|
2017-07-15 17:33:25 +02:00
|
|
|
"xss",
|
2017-12-24 16:46:49 +01:00
|
|
|
"tpl!action",
|
2016-09-23 10:54:55 +02:00
|
|
|
"tpl!chatbox",
|
2017-11-17 12:50:16 +01:00
|
|
|
"tpl!chatbox_head",
|
2017-06-16 11:31:57 +02:00
|
|
|
"tpl!emojis",
|
2017-04-21 18:35:34 +02:00
|
|
|
"tpl!help_message",
|
2017-12-24 16:46:49 +01:00
|
|
|
"tpl!info",
|
|
|
|
"tpl!message",
|
|
|
|
"tpl!new_day",
|
|
|
|
"tpl!spinner",
|
|
|
|
"tpl!toolbar"
|
2016-09-23 10:54:55 +02:00
|
|
|
], factory);
|
|
|
|
}(this, function (
|
2016-12-20 11:42:20 +01:00
|
|
|
converse,
|
2017-08-16 11:16:22 +02:00
|
|
|
dummy,
|
2017-06-16 11:31:57 +02:00
|
|
|
emojione,
|
2017-07-15 17:33:25 +02:00
|
|
|
xss,
|
2017-12-24 16:46:49 +01:00
|
|
|
tpl_action,
|
2016-09-23 10:54:55 +02:00
|
|
|
tpl_chatbox,
|
2017-11-17 12:50:16 +01:00
|
|
|
tpl_chatbox_head,
|
2017-06-16 11:31:57 +02:00
|
|
|
tpl_emojis,
|
2017-04-21 18:35:34 +02:00
|
|
|
tpl_help_message,
|
2017-12-24 16:46:49 +01:00
|
|
|
tpl_info,
|
|
|
|
tpl_message,
|
|
|
|
tpl_new_day,
|
|
|
|
tpl_spinner,
|
|
|
|
tpl_toolbar
|
2016-09-23 10:54:55 +02:00
|
|
|
) {
|
2016-03-13 17:16:53 +01:00
|
|
|
"use strict";
|
2018-01-02 21:25:30 +01:00
|
|
|
const { $msg, Backbone, Strophe, _, b64_sha1, sizzle, moment } = converse.env;
|
2017-12-19 21:26:09 +01:00
|
|
|
const u = converse.env.utils;
|
2017-06-12 20:30:58 +02:00
|
|
|
const KEY = {
|
2016-03-13 17:16:53 +01:00
|
|
|
ENTER: 13,
|
|
|
|
FORWARD_SLASH: 47
|
|
|
|
};
|
|
|
|
|
2016-12-20 11:42:20 +01:00
|
|
|
converse.plugins.add('converse-chatview', {
|
2018-01-03 17:08:30 +01:00
|
|
|
/* Optional dependencies are other plugins which might be
|
|
|
|
* overridden or relied upon, and therefore need to be loaded before
|
|
|
|
* this plugin. They are called "optional" because they might not be
|
|
|
|
* available, in which case any overrides applicable to them will be
|
|
|
|
* ignored.
|
|
|
|
*
|
|
|
|
* It's possible however to make optional dependencies non-optional.
|
|
|
|
* If the setting "strict_plugin_dependencies" is set to true,
|
|
|
|
* an error will be raised if the plugin is not found.
|
|
|
|
*
|
|
|
|
* NB: These plugins need to have already been loaded via require.js.
|
|
|
|
*/
|
|
|
|
optional_dependencies: ["converse-chatboxes"],
|
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();
|
2017-07-15 11:03:22 +02:00
|
|
|
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;
|
|
|
|
}
|
2017-12-19 21:26:09 +01:00
|
|
|
u.slideInAllElements(
|
2017-07-15 11:03:22 +02:00
|
|
|
document.querySelectorAll('.toolbar-menu')
|
|
|
|
)
|
2017-06-16 11:31:57 +02:00
|
|
|
}
|
2017-07-15 11:03:22 +02:00
|
|
|
);
|
2017-06-16 11:31:57 +02:00
|
|
|
},
|
2016-03-13 17:16:53 +01:00
|
|
|
|
|
|
|
ChatBoxViews: {
|
2017-07-10 17:46:22 +02:00
|
|
|
onChatBoxAdded (item) {
|
|
|
|
const { _converse } = this.__super__;
|
2017-06-12 20:30:58 +02:00
|
|
|
let view = this.get(item.get('id'));
|
2016-03-21 10:57:38 +01:00
|
|
|
if (!view) {
|
2016-12-20 10:30:20 +01:00
|
|
|
view = new _converse.ChatBoxView({model: item});
|
2016-03-13 17:16:53 +01:00
|
|
|
this.add(item.get('id'), view);
|
2016-03-19 22:56:55 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02: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.
|
|
|
|
*/
|
2017-07-10 17:46:22 +02:00
|
|
|
const { _converse } = this,
|
|
|
|
{ __ } = _converse;
|
2016-12-20 11:42:20 +01:00
|
|
|
|
2017-07-05 11:03:13 +02:00
|
|
|
_converse.api.settings.update({
|
2017-07-16 12:27:21 +02:00
|
|
|
'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,
|
2016-10-26 12:50:00 +02:00
|
|
|
'call': false,
|
2016-10-27 13:30:58 +02:00
|
|
|
'clear': true
|
2016-10-26 12:50:00 +02:00
|
|
|
},
|
2016-03-13 17:16:53 +01:00
|
|
|
});
|
2017-07-16 12:27:21 +02:00
|
|
|
emojione.imagePathPNG = _converse.emojione_image_path;
|
2017-07-16 12:50:59 +02:00
|
|
|
emojione.ascii = true;
|
2016-03-13 17:16:53 +01:00
|
|
|
|
2017-06-12 20:30:58 +02:00
|
|
|
function onWindowStateChanged (data) {
|
2017-05-03 09:06:28 +02:00
|
|
|
_converse.chatboxviews.each(function (chatboxview) {
|
2017-06-12 20:30:58 +02:00
|
|
|
chatboxview.onWindowStateChanged(data.state);
|
|
|
|
});
|
|
|
|
}
|
2017-05-03 09:06:28 +02:00
|
|
|
_converse.api.listen.on('windowStateChanged', onWindowStateChanged);
|
|
|
|
|
2017-12-24 17:47:02 +01:00
|
|
|
_converse.EmojiPicker = Backbone.Model.extend({
|
2017-06-16 11:31:57 +02:00
|
|
|
defaults: {
|
2017-07-16 11:06:34 +02:00
|
|
|
'current_category': 'people',
|
2017-07-16 11:36:31 +02:00
|
|
|
'current_skintone': '',
|
|
|
|
'scroll_position': 0
|
2017-07-16 14:12:17 +02:00
|
|
|
},
|
|
|
|
initialize () {
|
|
|
|
const id = `converse.emoji-${_converse.bare_jid}`;
|
|
|
|
this.id = id;
|
|
|
|
this.browserStorage = new Backbone.BrowserStorage[_converse.storage](id);
|
2017-06-16 11:31:57 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-01-03 20:02:05 +01:00
|
|
|
_converse.EmojiPickerView = Backbone.NativeView.extend({
|
2017-07-15 11:03:22 +02:00
|
|
|
className: 'emoji-picker-container toolbar-menu collapsed',
|
2017-06-16 11:31:57 +02:00
|
|
|
events: {
|
2017-07-16 13:41:01 +02:00
|
|
|
'click .emoji-category-picker li.emoji-category': 'chooseCategory',
|
2017-07-16 14:52:19 +02:00
|
|
|
'click .emoji-skintone-picker li.emoji-skintone': 'chooseSkinTone'
|
2017-06-16 11:31:57 +02:00
|
|
|
},
|
|
|
|
|
2017-07-16 11:36:31 +02:00
|
|
|
initialize () {
|
|
|
|
this.model.on('change:current_skintone', this.render, this);
|
|
|
|
this.model.on('change:current_category', this.render, this);
|
2017-08-09 14:07:46 +02:00
|
|
|
this.setScrollPosition = _.debounce(this.setScrollPosition, 50);
|
2017-06-16 11:31:57 +02:00
|
|
|
},
|
|
|
|
|
2017-07-16 14:52:19 +02:00
|
|
|
render () {
|
2017-06-16 11:31:57 +02:00
|
|
|
var emojis_html = tpl_emojis(
|
|
|
|
_.extend(
|
|
|
|
this.model.toJSON(), {
|
2017-07-16 11:06:34 +02:00
|
|
|
'transform': _converse.use_emojione ? emojione.shortnameToImage : emojione.shortnameToUnicode,
|
2017-12-19 21:26:09 +01:00
|
|
|
'emojis_by_category': u.getEmojisByCategory(_converse, emojione),
|
|
|
|
'toned_emojis': u.getTonedEmojis(_converse),
|
2017-07-16 11:06:34 +02:00
|
|
|
'skintones': ['tone1', 'tone2', 'tone3', 'tone4', 'tone5'],
|
2017-07-16 12:14:56 +02:00
|
|
|
'shouldBeHidden': this.shouldBeHidden
|
2017-06-16 11:31:57 +02:00
|
|
|
}
|
|
|
|
));
|
|
|
|
this.el.innerHTML = emojis_html;
|
2017-08-08 15:32:53 +02:00
|
|
|
_.forEach(this.el.querySelectorAll('.emoji-picker'), (el) => {
|
2017-08-09 14:07:46 +02:00
|
|
|
el.addEventListener('scroll', this.setScrollPosition.bind(this));
|
2017-07-16 11:36:31 +02:00
|
|
|
});
|
|
|
|
this.restoreScrollPosition();
|
2017-06-16 11:31:57 +02:00
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2017-07-16 12:14:56 +02:00
|
|
|
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;
|
|
|
|
},
|
|
|
|
|
2017-07-16 11:36:31 +02:00
|
|
|
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');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-08-08 15:32:53 +02:00
|
|
|
setScrollPosition (ev) {
|
2017-07-16 14:12:17 +02:00
|
|
|
this.model.save('scroll_position', ev.target.scrollTop);
|
2017-07-16 11:36:31 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
chooseSkinTone (ev) {
|
2017-07-16 11:06:34 +02:00
|
|
|
ev.preventDefault();
|
|
|
|
ev.stopPropagation();
|
2017-07-16 13:41:01 +02:00
|
|
|
const target = ev.target.nodeName === 'IMG' ?
|
|
|
|
ev.target.parentElement : ev.target;
|
|
|
|
const skintone = target.getAttribute("data-skintone").trim();
|
2017-07-16 12:14:56 +02:00
|
|
|
if (this.model.get('current_skintone') === skintone) {
|
2017-07-16 14:12:17 +02:00
|
|
|
this.model.save({'current_skintone': ''});
|
2017-07-16 12:14:56 +02:00
|
|
|
} else {
|
2017-07-16 14:12:17 +02:00
|
|
|
this.model.save({'current_skintone': skintone});
|
2017-07-16 12:14:56 +02:00
|
|
|
}
|
2017-07-16 11:06:34 +02:00
|
|
|
},
|
|
|
|
|
2017-07-16 11:36:31 +02:00
|
|
|
chooseCategory (ev) {
|
2017-06-16 11:31:57 +02:00
|
|
|
ev.preventDefault();
|
|
|
|
ev.stopPropagation();
|
2017-07-16 13:41:01 +02:00
|
|
|
const target = ev.target.nodeName === 'IMG' ?
|
|
|
|
ev.target.parentElement : ev.target;
|
|
|
|
const category = target.getAttribute("data-category").trim();
|
2017-07-16 14:12:17 +02:00
|
|
|
this.model.save({
|
2017-07-16 11:36:31 +02:00
|
|
|
'current_category': category,
|
|
|
|
'scroll_position': 0
|
|
|
|
});
|
2017-06-16 11:31:57 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-01-03 20:02:05 +01:00
|
|
|
_converse.ChatBoxHeading = Backbone.NativeView.extend({
|
2017-11-17 12:50:16 +01:00
|
|
|
|
|
|
|
initialize () {
|
2017-12-02 18:07:31 +01:00
|
|
|
this.model.on('change:image', this.render, this);
|
2017-11-17 12:50:16 +01:00
|
|
|
this.model.on('change:status', this.onStatusMessageChanged, this);
|
|
|
|
this.model.on('change:fullname', this.render, this);
|
|
|
|
},
|
|
|
|
|
2017-11-17 13:19:21 +01:00
|
|
|
render () {
|
|
|
|
this.el.innerHTML = tpl_chatbox_head(
|
2017-11-17 12:50:16 +01:00
|
|
|
_.extend(this.model.toJSON(), {
|
|
|
|
'avatar_width': _converse.chatview_avatar_width,
|
|
|
|
'avatar_height': _converse.chatview_avatar_height,
|
|
|
|
'info_close': __('Close this chat box'),
|
|
|
|
})
|
|
|
|
);
|
2017-11-17 13:19:21 +01:00
|
|
|
return this;
|
2017-11-17 12:50:16 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
onStatusMessageChanged (item) {
|
|
|
|
this.render();
|
|
|
|
_converse.emit('contactStatusMessageChanged', {
|
|
|
|
'contact': item.attributes,
|
|
|
|
'message': item.get('status')
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-01-03 20:02:05 +01:00
|
|
|
_converse.ChatBoxView = Backbone.NativeView.extend({
|
2016-03-13 17:16:53 +01:00
|
|
|
length: 200,
|
2016-11-22 17:42:58 +01:00
|
|
|
className: 'chatbox hidden',
|
2016-10-26 12:50:00 +02:00
|
|
|
is_chatroom: false, // Leaky abstraction from MUC
|
2016-03-13 17:16:53 +01:00
|
|
|
|
|
|
|
events: {
|
|
|
|
'click .close-chatbox-button': 'close',
|
2016-10-28 15:31:53 +02:00
|
|
|
'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
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
initialize () {
|
2018-01-02 21:44:46 +01:00
|
|
|
this.scrollDown = _.debounce(this._scrollDown, 250);
|
2017-12-25 12:58:30 +01:00
|
|
|
this.markScrolled = _.debounce(this._markScrolled, 100);
|
2017-07-16 14:12:17 +02:00
|
|
|
this.createEmojiPicker();
|
2016-03-13 17:16:53 +01:00
|
|
|
this.model.messages.on('add', this.onMessageAdded, this);
|
|
|
|
this.model.on('show', this.show, this);
|
2017-11-17 12:50:16 +01:00
|
|
|
this.model.on('destroy', this.remove, this);
|
2016-03-13 17:16:53 +01:00
|
|
|
// 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('showHelpMessages', this.showHelpMessages, this);
|
|
|
|
this.model.on('sendMessage', this.sendMessage, this);
|
2017-11-17 12:50:16 +01:00
|
|
|
|
|
|
|
this.render().renderToolbar().insertHeading().fetchMessages();
|
|
|
|
_converse.emit('chatBoxOpened', this);
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.emit('chatBoxInitialized', this);
|
2016-03-13 17:16:53 +01:00
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
render () {
|
2017-11-17 12:50:16 +01:00
|
|
|
this.el.setAttribute('id', this.model.get('box_id'));
|
|
|
|
this.el.innerHTML = tpl_chatbox(
|
|
|
|
_.extend(this.model.toJSON(), {
|
|
|
|
label_personal_message: __('Personal message'),
|
2017-11-17 14:41:54 +01:00
|
|
|
label_send: __('Send'),
|
|
|
|
show_send_button: _converse.show_send_button,
|
|
|
|
show_textarea: true,
|
|
|
|
show_toolbar: _converse.show_toolbar,
|
|
|
|
unread_msgs: __('You have unread messages')
|
2017-11-17 12:50:16 +01:00
|
|
|
}
|
2017-11-17 14:41:54 +01:00
|
|
|
));
|
2017-12-02 14:57:10 +01:00
|
|
|
this.content = this.el.querySelector('.chat-content');
|
2017-11-17 12:50:16 +01:00
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
insertHeading () {
|
2017-11-17 14:41:54 +01:00
|
|
|
this.heading = new _converse.ChatBoxHeading({'model': this.model});
|
|
|
|
this.heading.render();
|
|
|
|
this.heading.chatview = this;
|
|
|
|
|
2017-11-17 12:50:16 +01:00
|
|
|
const flyout = this.el.querySelector('.flyout');
|
|
|
|
flyout.insertBefore(this.heading.el, flyout.querySelector('.chat-body'));
|
|
|
|
return this;
|
2016-03-13 17:16:53 +01:00
|
|
|
},
|
|
|
|
|
2017-07-16 14:12:17 +02:00
|
|
|
createEmojiPicker () {
|
|
|
|
if (_.isUndefined(_converse.emojipicker)) {
|
|
|
|
_converse.emojipicker = new _converse.EmojiPicker();
|
|
|
|
_converse.emojipicker.fetch();
|
|
|
|
}
|
|
|
|
this.emoji_picker_view = new _converse.EmojiPickerView({
|
|
|
|
'model': _converse.emojipicker
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
afterMessagesFetched () {
|
2017-02-24 22:18:41 +01:00
|
|
|
this.insertIntoDOM();
|
2017-02-25 22:17:14 +01:00
|
|
|
this.scrollDown();
|
2017-12-24 16:46:49 +01:00
|
|
|
this.content.addEventListener('scroll', this.markScrolled.bind(this));
|
2017-07-21 15:01:35 +02:00
|
|
|
_converse.emit('afterMessagesFetched', this);
|
2016-03-13 17:16:53 +01:00
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
fetchMessages () {
|
2016-03-13 17:16:53 +01:00
|
|
|
this.model.messages.fetch({
|
2017-05-15 18:04:03 +02:00
|
|
|
'add': true,
|
2017-02-24 22:18:41 +01:00
|
|
|
'success': this.afterMessagesFetched.bind(this),
|
|
|
|
'error': this.afterMessagesFetched.bind(this),
|
2016-03-13 17:16:53 +01:00
|
|
|
});
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
insertIntoDOM () {
|
2017-08-29 11:08:37 +02:00
|
|
|
/* This method gets overridden in src/converse-controlbox.js
|
|
|
|
* as well as src/converse-muc.js (if those plugins are
|
|
|
|
* enabled).
|
2016-03-19 23:16:00 +01:00
|
|
|
*/
|
2017-06-12 20:30:58 +02:00
|
|
|
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;
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
clearStatusNotification () {
|
2018-01-02 21:44:46 +01:00
|
|
|
u.removeElement(this.content.querySelector('.chat-event'));
|
2016-03-13 17:16:53 +01:00
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
showStatusNotification (message, keep_old, permanent) {
|
2016-03-13 17:16:53 +01:00
|
|
|
if (!keep_old) {
|
|
|
|
this.clearStatusNotification();
|
|
|
|
}
|
2017-12-24 16:46:49 +01:00
|
|
|
this.content.insertAdjacentHTML(
|
|
|
|
'beforeend',
|
|
|
|
tpl_info({
|
|
|
|
'extra_classes': !permanent ? 'chat-event' : '',
|
|
|
|
'message': message,
|
|
|
|
'isodate': moment().format(),
|
|
|
|
'data': ''
|
|
|
|
}));
|
2016-05-25 15:14:12 +02:00
|
|
|
this.scrollDown();
|
2016-03-13 17:16:53 +01:00
|
|
|
},
|
|
|
|
|
2017-12-02 14:57:10 +01:00
|
|
|
addSpinner (append=false) {
|
2017-04-04 09:56:18 +02:00
|
|
|
if (_.isNull(this.el.querySelector('.spinner'))) {
|
2017-12-02 14:57:10 +01:00
|
|
|
if (append) {
|
|
|
|
this.content.insertAdjacentHTML('beforeend', tpl_spinner());
|
|
|
|
this.scrollDown();
|
|
|
|
} else {
|
|
|
|
this.content.insertAdjacentHTML('afterbegin', tpl_spinner());
|
|
|
|
}
|
2016-03-13 17:16:53 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
clearSpinner () {
|
2017-12-02 14:57:10 +01:00
|
|
|
_.each(
|
|
|
|
this.content.querySelectorAll('span.spinner'),
|
|
|
|
(el) => el.parentNode.removeChild(el)
|
|
|
|
);
|
2016-03-13 17:16:53 +01:00
|
|
|
},
|
|
|
|
|
2018-01-03 13:04:06 +01:00
|
|
|
insertDayIndicator (next_msg_el) {
|
|
|
|
/* Inserts an indicator into the chat area, showing the
|
|
|
|
* day as given by the passed in date.
|
2016-03-19 23:16:00 +01:00
|
|
|
*
|
2018-01-03 13:55:57 +01:00
|
|
|
* The indicator is only inserted if necessary.
|
|
|
|
*
|
2016-03-19 23:16:00 +01:00
|
|
|
* Parameters:
|
2018-01-03 13:04:06 +01:00
|
|
|
* (HTMLElement) next_msg_el - The message element before
|
|
|
|
* which the day indicator element must be inserted.
|
|
|
|
* This element must have a "data-isodate" attribute
|
|
|
|
* which specifies its creation date.
|
2016-03-19 23:16:00 +01:00
|
|
|
*/
|
2018-01-03 13:55:57 +01:00
|
|
|
const prev_msg_el = this.getPreviousMessageElement(next_msg_el),
|
|
|
|
prev_msg_date = _.isNull(prev_msg_el) ? null : prev_msg_el.getAttribute('data-isodate'),
|
|
|
|
next_msg_date = next_msg_el.getAttribute('data-isodate');
|
|
|
|
|
|
|
|
if (_.isNull(prev_msg_date) || moment(next_msg_date).isAfter(prev_msg_date, 'day')) {
|
|
|
|
const day_date = moment(next_msg_date).startOf('day');
|
|
|
|
next_msg_el.insertAdjacentHTML('beforeBegin',
|
|
|
|
tpl_new_day({
|
|
|
|
'isodate': day_date.format(),
|
|
|
|
'datestring': day_date.format("dddd MMM Do YYYY")
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
2018-01-03 13:04:06 +01:00
|
|
|
|
2018-01-03 13:55:57 +01:00
|
|
|
getPreviousMessageElement (el) {
|
|
|
|
let prev_msg_el = el.previousSibling;
|
|
|
|
while (!_.isNull(prev_msg_el) &&
|
2018-01-03 20:02:05 +01:00
|
|
|
!u.hasClass('message', prev_msg_el) &&
|
|
|
|
!u.hasClass('chat-info', prev_msg_el)) {
|
2018-01-03 13:55:57 +01:00
|
|
|
prev_msg_el = prev_msg_el.previousSibling
|
|
|
|
}
|
|
|
|
return prev_msg_el;
|
2016-03-13 17:16:53 +01:00
|
|
|
},
|
|
|
|
|
2018-01-02 21:25:30 +01:00
|
|
|
getLastMessageElement () {
|
|
|
|
let last_msg_el = this.content.lastElementChild;
|
|
|
|
while (!_.isNull(last_msg_el) &&
|
2018-01-03 20:02:05 +01:00
|
|
|
!u.hasClass('message', last_msg_el) &&
|
|
|
|
!u.hasClass('chat-info', last_msg_el)) {
|
2018-01-02 21:25:30 +01:00
|
|
|
last_msg_el = last_msg_el.previousSibling
|
|
|
|
}
|
|
|
|
return last_msg_el;
|
|
|
|
},
|
|
|
|
|
|
|
|
getFirstMessageElement () {
|
|
|
|
let first_msg_el = this.content.firstElementChild;
|
|
|
|
while (!_.isNull(first_msg_el) &&
|
2018-01-03 20:02:05 +01:00
|
|
|
!u.hasClass('message', first_msg_el) &&
|
|
|
|
!u.hasClass('chat-info', first_msg_el)) {
|
2018-01-02 21:25:30 +01:00
|
|
|
first_msg_el = first_msg_el.nextSibling
|
|
|
|
}
|
|
|
|
return first_msg_el;
|
|
|
|
},
|
|
|
|
|
2017-12-24 17:47:02 +01:00
|
|
|
getLastMessageDate (cutoff) {
|
|
|
|
/* Return the ISO8601 format date of the latest message.
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* (Object) cutoff: Moment Date cutoff date. The last
|
|
|
|
* message received cutoff this date will be returned.
|
|
|
|
*/
|
2018-01-02 21:25:30 +01:00
|
|
|
const first_msg = this.getFirstMessageElement(),
|
|
|
|
oldest_date = first_msg ? first_msg.getAttribute('data-isodate') : null;
|
|
|
|
if (!_.isNull(oldest_date) && moment(oldest_date).isAfter(cutoff)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
const last_msg = this.getLastMessageElement(),
|
|
|
|
most_recent_date = last_msg ? last_msg.getAttribute('data-isodate') : null;
|
|
|
|
if (_.isNull(most_recent_date) || moment(most_recent_date).isBefore(cutoff)) {
|
|
|
|
return most_recent_date;
|
2017-12-24 17:47:02 +01:00
|
|
|
}
|
|
|
|
const msg_dates = _.invokeMap(
|
2018-01-02 21:25:30 +01:00
|
|
|
sizzle('.message, .chat-info', this.content),
|
|
|
|
Element.prototype.getAttribute, 'data-isodate'
|
2017-12-24 17:47:02 +01:00
|
|
|
)
|
2017-12-25 12:58:30 +01:00
|
|
|
if (_.isObject(cutoff)) {
|
|
|
|
cutoff = cutoff.format();
|
|
|
|
}
|
|
|
|
msg_dates.push(cutoff);
|
2017-12-24 17:47:02 +01:00
|
|
|
msg_dates.sort();
|
2018-01-02 21:25:30 +01:00
|
|
|
const idx = msg_dates.lastIndexOf(cutoff);
|
|
|
|
if (idx === 0) {
|
|
|
|
return null;
|
|
|
|
} else {
|
|
|
|
return msg_dates[idx-1];
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
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:
|
2017-02-24 22:18:41 +01:00
|
|
|
* (Object) attrs: An object containing the message
|
|
|
|
* attributes.
|
2016-03-19 23:16:00 +01:00
|
|
|
*/
|
2017-12-24 17:47:02 +01:00
|
|
|
const current_msg_date = moment(attrs.time) || moment,
|
2018-01-02 21:25:30 +01:00
|
|
|
prepend_html = _.bind(this.content.insertAdjacentHTML, this.content, 'afterbegin'),
|
2018-01-03 12:54:28 +01:00
|
|
|
previous_msg_date = this.getLastMessageDate(current_msg_date),
|
|
|
|
message_el = this.renderMessage(attrs);
|
2017-12-24 17:47:02 +01:00
|
|
|
|
2018-01-02 21:25:30 +01:00
|
|
|
if (_.isNull(previous_msg_date)) {
|
2018-01-03 12:54:28 +01:00
|
|
|
this.content.insertAdjacentElement('afterbegin', message_el);
|
2017-12-25 12:58:30 +01:00
|
|
|
} else {
|
2018-01-02 21:25:30 +01:00
|
|
|
const previous_msg_el = sizzle(`[data-isodate="${previous_msg_date}"]:last`, this.content).pop();
|
2018-01-03 12:54:28 +01:00
|
|
|
previous_msg_el.insertAdjacentElement('afterend', message_el);
|
2017-12-25 12:58:30 +01:00
|
|
|
}
|
2018-01-03 13:55:57 +01:00
|
|
|
this.insertDayIndicator(message_el);
|
2018-01-03 12:54:28 +01:00
|
|
|
this.scrollDown();
|
2017-01-26 15:49:02 +01:00
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
getExtraMessageTemplateAttributes () {
|
2017-01-26 15:49:02 +01:00
|
|
|
/* Provides a hook for sending more attributes to the
|
|
|
|
* message template.
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* (Object) attrs: An object containing message attributes.
|
|
|
|
*/
|
2016-07-01 14:27:26 +02:00
|
|
|
return {};
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
getExtraMessageClasses (attrs) {
|
2017-02-15 21:30:32 +01:00
|
|
|
return attrs.delayed && 'delayed' || '';
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
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.
|
|
|
|
*/
|
2017-06-12 20:30:58 +02:00
|
|
|
let text = attrs.message,
|
2016-03-13 17:16:53 +01:00
|
|
|
fullname = this.model.get('fullname') || attrs.fullname,
|
|
|
|
template, username;
|
|
|
|
|
2017-06-12 20:30:58 +02:00
|
|
|
const match = text.match(/^\/(.*?)(?: (.*))?$/);
|
2016-03-13 17:16:53 +01:00
|
|
|
if ((match) && (match[1] === 'me')) {
|
|
|
|
text = text.replace(/^\/me/, '');
|
2017-02-19 10:58:30 +01:00
|
|
|
template = tpl_action;
|
2017-02-15 22:03:02 +01:00
|
|
|
if (attrs.sender === 'me') {
|
2017-02-28 08:58:17 +01:00
|
|
|
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 {
|
2017-02-19 10:58:30 +01:00
|
|
|
template = tpl_message;
|
2016-03-13 17:16:53 +01:00
|
|
|
username = attrs.sender === 'me' && __('me') || fullname;
|
|
|
|
}
|
2018-01-02 21:44:46 +01:00
|
|
|
this.clearStatusNotification();
|
2016-03-13 17:16:53 +01:00
|
|
|
|
2016-09-12 13:12:14 +02:00
|
|
|
if (text.length > 8000) {
|
|
|
|
text = text.substring(0, 10) + '...';
|
|
|
|
this.showStatusNotification(
|
2017-11-23 12:07:49 +01:00
|
|
|
__("A very large message has been received. "+
|
|
|
|
"This might be due to an attack meant to degrade the chat performance. "+
|
2016-09-12 13:12:14 +02:00
|
|
|
"Output has been shortened."),
|
|
|
|
true, true);
|
|
|
|
}
|
2017-06-12 20:30:58 +02:00
|
|
|
const msg_time = moment(attrs.time) || moment;
|
2018-01-02 21:44:46 +01:00
|
|
|
const msg = u.stringToElement(template(
|
2016-11-02 14:42:20 +01:00
|
|
|
_.extend(this.getExtraMessageTemplateAttributes(attrs), {
|
|
|
|
'msgid': attrs.msgid,
|
|
|
|
'sender': attrs.sender,
|
2017-03-12 11:32:38 +01:00
|
|
|
'time': msg_time.format(_converse.time_format),
|
2016-11-02 14:42:20 +01:00
|
|
|
'isodate': msg_time.format(),
|
|
|
|
'username': username,
|
2017-02-15 21:30:32 +01:00
|
|
|
'extra_classes': this.getExtraMessageClasses(attrs)
|
2016-11-02 14:42:20 +01:00
|
|
|
})
|
|
|
|
));
|
2018-01-02 21:44:46 +01:00
|
|
|
const msg_content = msg.querySelector('.chat-msg-content');
|
2017-12-19 21:26:09 +01:00
|
|
|
msg_content.innerHTML = u.addEmoji(
|
|
|
|
_converse, emojione, u.addHyperlinks(xss.filterXSS(text, {'whiteList': {}}))
|
2017-07-15 20:13:44 +02:00
|
|
|
);
|
2017-12-19 21:26:09 +01:00
|
|
|
u.renderImageURLs(msg_content);
|
2018-01-02 21:44:46 +01:00
|
|
|
return msg;
|
2016-03-13 17:16:53 +01:00
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
showHelpMessages (msgs, type, spinner) {
|
2017-06-12 20:30:58 +02:00
|
|
|
_.each(msgs, (msg) => {
|
2017-12-06 17:01:17 +01:00
|
|
|
this.content.insertAdjacentHTML(
|
|
|
|
'beforeend',
|
|
|
|
tpl_help_message({
|
|
|
|
'type': type||'info',
|
|
|
|
'message': xss.filterXSS(msg, {'whiteList': {'strong': []}})
|
|
|
|
})
|
|
|
|
);
|
2017-06-12 20:30:58 +02:00
|
|
|
});
|
2016-03-13 17:16:53 +01:00
|
|
|
if (spinner === true) {
|
2018-01-02 21:44:46 +01:00
|
|
|
this.addSpinner();
|
2016-03-13 17:16:53 +01:00
|
|
|
} else if (spinner === false) {
|
2018-01-02 21:25:30 +01:00
|
|
|
this.clearSpinner();
|
2016-03-13 17:16:53 +01:00
|
|
|
}
|
|
|
|
return this.scrollDown();
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
handleChatStateMessage (message) {
|
2016-12-20 10:30:20 +01:00
|
|
|
if (message.get('chat_state') === _converse.COMPOSING) {
|
2017-03-31 04:06:00 +02:00
|
|
|
if (message.get('sender') === 'me') {
|
2017-03-16 16:28:59 +01:00
|
|
|
this.showStatusNotification(__('Typing from another device'));
|
2017-03-31 04:06:00 +02:00
|
|
|
} else {
|
2017-03-16 16:28:59 +01:00
|
|
|
this.showStatusNotification(message.get('fullname')+' '+__('is typing'));
|
|
|
|
}
|
2017-12-19 20:17:38 +01:00
|
|
|
this.clear_status_timeout = window.setTimeout(
|
|
|
|
this.clearStatusNotification.bind(this),
|
|
|
|
30000
|
|
|
|
);
|
2016-12-20 10:30:20 +01:00
|
|
|
} else if (message.get('chat_state') === _converse.PAUSED) {
|
2017-03-31 04:06:00 +02:00
|
|
|
if (message.get('sender') === 'me') {
|
2017-03-16 16:28:59 +01:00
|
|
|
this.showStatusNotification(__('Stopped typing on the other device'));
|
2017-03-31 04:06:00 +02:00
|
|
|
} else {
|
2017-03-16 16:28:59 +01:00
|
|
|
this.showStatusNotification(message.get('fullname')+' '+__('has stopped typing'));
|
|
|
|
}
|
2016-12-20 10:30:20 +01:00
|
|
|
} else if (_.includes([_converse.INACTIVE, _converse.ACTIVE], message.get('chat_state'))) {
|
2018-01-02 21:44:46 +01:00
|
|
|
this.clearStatusNotification();
|
2016-12-20 10:30:20 +01:00
|
|
|
} else if (message.get('chat_state') === _converse.GONE) {
|
2016-03-13 17:16:53 +01:00
|
|
|
this.showStatusNotification(message.get('fullname')+' '+__('has gone away'));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
shouldShowOnTextMessage () {
|
2018-01-02 21:44:46 +01:00
|
|
|
return !u.isVisible(this.el);
|
2016-03-28 16:08:50 +02:00
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
handleTextMessage (message) {
|
2016-06-16 10:36:55 +02:00
|
|
|
this.showMessage(_.clone(message.attributes));
|
2017-12-25 12:58:30 +01:00
|
|
|
if (u.isNewMessage(message)) {
|
|
|
|
if (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 (this.model.get('scrolled', true)) {
|
|
|
|
this.showNewMessagesIndicator();
|
2017-06-14 19:18:14 +02:00
|
|
|
}
|
2016-03-13 17:16:53 +01:00
|
|
|
}
|
2016-03-28 16:08:50 +02:00
|
|
|
if (this.shouldShowOnTextMessage()) {
|
2016-03-13 17:16:53 +01:00
|
|
|
this.show();
|
2016-05-30 16:32:05 +02:00
|
|
|
} else {
|
|
|
|
this.scrollDown();
|
2016-03-13 17:16:53 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
handleErrorMessage (message) {
|
2017-12-24 16:46:49 +01:00
|
|
|
const message_el = this.content.querySelector(`[data-msgid="${message.get('msgid')}"]`);
|
|
|
|
if (!_.isNull(message_el)) {
|
|
|
|
message_el.insertAdjacentHTML(
|
|
|
|
'afterend',
|
|
|
|
tpl_info({
|
|
|
|
'extra_classes': 'chat-error',
|
|
|
|
'message': message.get('message'),
|
|
|
|
'isodate': moment().format(),
|
|
|
|
'data': ''
|
|
|
|
}));
|
2016-06-22 18:47:07 +02:00
|
|
|
this.scrollDown();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
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.
|
|
|
|
*/
|
2017-01-26 15:49:02 +01:00
|
|
|
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;
|
|
|
|
}
|
2016-06-22 18:47:07 +02:00
|
|
|
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);
|
|
|
|
}
|
2017-04-20 21:25:58 +02:00
|
|
|
_converse.emit('messageAdded', {
|
|
|
|
'message': message,
|
|
|
|
'chatbox': this.model
|
|
|
|
});
|
2016-03-13 17:16:53 +01:00
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
createMessageStanza (message) {
|
2016-03-13 17:16:53 +01:00
|
|
|
return $msg({
|
2016-12-20 10:30:20 +01:00
|
|
|
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()
|
2016-12-20 10:30:20 +01:00
|
|
|
.c(_converse.ACTIVE, {'xmlns': Strophe.NS.CHATSTATES}).up();
|
2016-03-13 17:16:53 +01:00
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02: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.
|
2017-06-12 20:30:58 +02:00
|
|
|
const messageStanza = this.createMessageStanza(message);
|
2016-12-20 10:30:20 +01:00
|
|
|
_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.
|
2016-12-20 10:30:20 +01:00
|
|
|
_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())
|
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
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.
|
|
|
|
*/
|
2016-12-20 10:30:20 +01:00
|
|
|
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'
|
|
|
|
);
|
|
|
|
}
|
2017-06-12 20:30:58 +02:00
|
|
|
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") {
|
2017-06-12 20:30:58 +02:00
|
|
|
const msgs = [
|
2017-07-10 17:46:22 +02:00
|
|
|
`<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;
|
|
|
|
}
|
|
|
|
}
|
2017-06-12 20:30:58 +02:00
|
|
|
let fullname = _converse.xmppstatus.get('fullname');
|
2016-12-20 10:30:20 +01:00
|
|
|
fullname = _.isEmpty(fullname)? _converse.bare_jid: fullname;
|
2017-06-12 20:30:58 +02:00
|
|
|
|
|
|
|
const message = this.model.messages.create({
|
2017-07-10 17:46:22 +02:00
|
|
|
fullname,
|
2016-03-13 17:16:53 +01:00
|
|
|
sender: 'me',
|
|
|
|
time: moment().format(),
|
|
|
|
message: text
|
|
|
|
});
|
|
|
|
this.sendMessage(message);
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
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.
|
|
|
|
*/
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.connection.send(
|
2016-03-13 17:16:53 +01:00
|
|
|
$msg({'to':this.model.get('jid'), 'type': 'chat'})
|
2016-05-30 18:53:31 +02:00
|
|
|
.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
|
|
|
);
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02: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.
|
|
|
|
*/
|
2017-01-26 15:49:02 +01:00
|
|
|
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;
|
|
|
|
}
|
2016-12-20 10:30:20 +01:00
|
|
|
if (state === _converse.COMPOSING) {
|
2016-03-13 17:16:53 +01:00
|
|
|
this.chat_state_timeout = window.setTimeout(
|
2017-12-19 20:17:38 +01:00
|
|
|
this.setChatState.bind(this),
|
|
|
|
_converse.TIMEOUTS.PAUSED,
|
|
|
|
_converse.PAUSED
|
|
|
|
);
|
2016-12-20 10:30:20 +01:00
|
|
|
} else if (state === _converse.PAUSED) {
|
2016-03-13 17:16:53 +01:00
|
|
|
this.chat_state_timeout = window.setTimeout(
|
2017-12-19 20:17:38 +01:00
|
|
|
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) {
|
2017-03-30 12:40:17 +02:00
|
|
|
ev.preventDefault();
|
2017-06-12 20:30:58 +02:00
|
|
|
const textarea = this.el.querySelector('.chat-textarea'),
|
2017-07-15 15:58:11 +02:00
|
|
|
message = textarea.value;
|
2017-03-30 12:40:17 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
clearMessages (ev) {
|
2016-03-13 17:16:53 +01:00
|
|
|
if (ev && ev.preventDefault) { ev.preventDefault(); }
|
2017-06-12 20:30:58 +02:00
|
|
|
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) {
|
2017-12-24 16:46:49 +01:00
|
|
|
this.content.innerHTML = '';
|
2016-03-13 17:16:53 +01:00
|
|
|
this.model.messages.reset();
|
|
|
|
this.model.messages.browserStorage._clear();
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
insertIntoTextArea (value) {
|
2018-01-02 21:44:46 +01:00
|
|
|
const textbox_el = this.el.querySelector('.chat-textarea');
|
|
|
|
let existing = textbox_el.value;
|
2016-07-26 11:04:05 +02:00
|
|
|
if (existing && (existing[existing.length-1] !== ' ')) {
|
|
|
|
existing = existing + ' ';
|
|
|
|
}
|
2018-01-02 21:44:46 +01:00
|
|
|
textbox_el.value = existing+value+' ';
|
|
|
|
textbox_el.focus()
|
2016-07-26 11:04:05 +02:00
|
|
|
},
|
|
|
|
|
2017-07-15 11:55:07 +02:00
|
|
|
insertEmoji (ev) {
|
2016-03-13 17:16:53 +01:00
|
|
|
ev.stopPropagation();
|
2017-08-09 12:33:00 +02:00
|
|
|
const target = ev.target.nodeName === 'IMG' ? ev.target.parentElement : ev.target;
|
|
|
|
this.insertIntoTextArea(target.getAttribute('data-emoji'));
|
2016-03-13 17:16:53 +01:00
|
|
|
},
|
|
|
|
|
2017-07-15 11:55:07 +02:00
|
|
|
toggleEmojiMenu (ev) {
|
2018-01-03 20:02:05 +01:00
|
|
|
if (u.hasClass('insert-emoji', ev.target)) {
|
|
|
|
return;
|
|
|
|
}
|
2017-06-16 11:31:57 +02:00
|
|
|
if (!_.isUndefined(ev)) {
|
|
|
|
ev.stopPropagation();
|
2017-06-24 11:34:01 +02:00
|
|
|
if (ev.target.classList.contains('emoji-category-picker') ||
|
2017-07-16 14:52:19 +02:00
|
|
|
ev.target.classList.contains('emoji-skintone-picker') ||
|
2017-06-24 11:34:01 +02:00
|
|
|
ev.target.classList.contains('emoji-category')) {
|
|
|
|
return;
|
|
|
|
}
|
2017-06-16 11:31:57 +02:00
|
|
|
}
|
2017-07-15 11:03:22 +02:00
|
|
|
const elements = _.difference(
|
|
|
|
document.querySelectorAll('.toolbar-menu'),
|
|
|
|
[this.emoji_picker_view.el]
|
|
|
|
);
|
2017-12-19 21:26:09 +01:00
|
|
|
u.slideInAllElements(elements)
|
2017-11-12 20:08:37 +01:00
|
|
|
.then(_.partial(
|
2017-12-19 21:26:09 +01:00
|
|
|
u.slideToggleElement,
|
2017-11-12 20:08:37 +01:00
|
|
|
this.emoji_picker_view.el))
|
|
|
|
.then(this.focus.bind(this));
|
2016-03-13 17:16:53 +01:00
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
toggleCall (ev) {
|
2016-03-13 17:16:53 +01:00
|
|
|
ev.stopPropagation();
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.emit('callButtonClicked', {
|
|
|
|
connection: _converse.connection,
|
2016-03-13 17:16:53 +01:00
|
|
|
model: this.model
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
onChatStatusChanged (item) {
|
2017-06-12 20:30:58 +02:00
|
|
|
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;
|
2018-01-02 21:44:46 +01:00
|
|
|
if (u.isVisible(this.el)) {
|
2016-03-13 17:16:53 +01:00
|
|
|
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') {
|
2018-01-02 21:44:46 +01:00
|
|
|
this.clearStatusNotification();
|
2016-03-13 17:16:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
close (ev) {
|
2016-03-13 17:16:53 +01:00
|
|
|
if (ev && ev.preventDefault) { ev.preventDefault(); }
|
2017-10-31 23:04:46 +01:00
|
|
|
if (Backbone.history.getFragment() === "converse/chat?jid="+this.model.get('jid')) {
|
|
|
|
_converse.router.navigate('');
|
|
|
|
}
|
2016-12-20 10:30:20 +01:00
|
|
|
if (_converse.connection.connected) {
|
2016-03-28 13:42:33 +02:00
|
|
|
// Immediately sending the chat state, because the
|
|
|
|
// model is going to be destroyed afterwards.
|
2017-12-19 20:17:38 +01:00
|
|
|
this.setChatState(_converse.INACTIVE);
|
2016-03-28 13:42:33 +02:00
|
|
|
this.sendChatState();
|
2016-03-13 17:16:53 +01:00
|
|
|
}
|
2017-04-19 15:40:27 +02:00
|
|
|
try {
|
|
|
|
this.model.destroy();
|
|
|
|
} catch (e) {
|
2017-07-05 11:33:55 +02:00
|
|
|
_converse.log(e, Strophe.LogLevel.ERROR);
|
2017-04-19 15:40:27 +02:00
|
|
|
}
|
2016-03-28 13:42:33 +02:00
|
|
|
this.remove();
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.emit('chatBoxClosed', this);
|
2016-03-13 17:16:53 +01:00
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
getToolbarOptions (options) {
|
2016-10-27 13:30:58 +02:00
|
|
|
return _.extend(options || {}, {
|
|
|
|
'label_clear': __('Clear all messages'),
|
|
|
|
'label_insert_smiley': __('Insert a smiley'),
|
|
|
|
'label_start_call': __('Start a call'),
|
2016-12-20 10:30:20 +01:00
|
|
|
'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
|
|
|
});
|
2016-10-27 13:30:58 +02:00
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
renderToolbar (toolbar, options) {
|
2016-12-20 10:30:20 +01:00
|
|
|
if (!_converse.show_toolbar) { return; }
|
2017-02-19 10:58:30 +01:00
|
|
|
toolbar = toolbar || tpl_toolbar;
|
2017-06-16 11:31:57 +02:00
|
|
|
options = _.assign(
|
2016-10-27 13:30:58 +02:00
|
|
|
this.model.toJSON(),
|
|
|
|
this.getToolbarOptions(options || {})
|
|
|
|
);
|
2017-06-16 11:31:57 +02:00
|
|
|
this.el.querySelector('.chat-toolbar').innerHTML = toolbar(options);
|
|
|
|
|
2017-12-19 22:37:51 +01:00
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
renderEmojiPicker () {
|
2017-06-16 11:31:57 +02:00
|
|
|
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
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
focus () {
|
2017-12-20 11:02:01 +01:00
|
|
|
const textarea_el = this.el.querySelector('.chat-textarea');
|
|
|
|
if (!_.isNull(textarea_el)) {
|
|
|
|
textarea_el.focus();
|
|
|
|
_converse.emit('chatBoxFocused', this);
|
|
|
|
}
|
2016-03-13 17:16:53 +01:00
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
hide () {
|
2016-11-22 17:42:58 +01:00
|
|
|
this.el.classList.add('hidden');
|
2016-03-13 17:16:53 +01:00
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
afterShown (focus) {
|
2017-12-19 21:26:09 +01:00
|
|
|
if (u.isPersistableModel(this.model)) {
|
2016-03-28 16:52:00 +02:00
|
|
|
this.model.save();
|
|
|
|
}
|
2016-12-20 10:30:20 +01:00
|
|
|
this.setChatState(_converse.ACTIVE);
|
2017-12-19 22:37:51 +01:00
|
|
|
this.renderEmojiPicker();
|
2016-03-28 16:52:00 +02:00
|
|
|
this.scrollDown();
|
|
|
|
if (focus) {
|
|
|
|
this.focus();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-12-20 11:02:01 +01:00
|
|
|
_show (focus) {
|
|
|
|
/* Inner show method that gets debounced */
|
|
|
|
if (u.isVisible(this.el)) {
|
2016-04-01 14:46:19 +02:00
|
|
|
if (focus) { this.focus(); }
|
|
|
|
return;
|
|
|
|
}
|
2017-12-19 21:26:09 +01:00
|
|
|
const that = this;
|
|
|
|
u.fadeIn(this.el, function () {
|
|
|
|
that.afterShown();
|
|
|
|
if (focus) { that.focus(); }
|
|
|
|
});
|
2016-04-01 14:46:19 +02:00
|
|
|
},
|
|
|
|
|
2017-12-20 11:02:01 +01:00
|
|
|
show (focus) {
|
|
|
|
if (_.isUndefined(this.debouncedShow)) {
|
|
|
|
/* 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.
|
|
|
|
*/
|
|
|
|
this.debouncedShow = _.debounce(this._show, 250, {'leading': true});
|
|
|
|
}
|
|
|
|
this.debouncedShow.apply(this, arguments);
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2017-12-25 12:58:30 +01:00
|
|
|
showNewMessagesIndicator () {
|
|
|
|
u.showElement(this.el.querySelector('.new-msgs-indicator'));
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
hideNewMessagesIndicator () {
|
2017-06-12 20:30:58 +02:00
|
|
|
const new_msgs_indicator = this.el.querySelector('.new-msgs-indicator');
|
2017-03-03 15:11:25 +01:00
|
|
|
if (!_.isNull(new_msgs_indicator)) {
|
|
|
|
new_msgs_indicator.classList.add('hidden');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-12-25 12:58:30 +01:00
|
|
|
_markScrolled: function (ev) {
|
2016-05-25 15:14:12 +02:00
|
|
|
/* 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(); }
|
2017-03-03 14:55:03 +01:00
|
|
|
if (this.model.get('auto_scrolled')) {
|
|
|
|
this.model.set({
|
|
|
|
'scrolled': false,
|
|
|
|
'auto_scrolled': false
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
2017-06-12 20:30:58 +02:00
|
|
|
let scrolled = true;
|
|
|
|
const is_at_bottom =
|
2018-01-02 21:44:46 +01:00
|
|
|
(this.content.scrollTop + this.content.clientHeight) >=
|
|
|
|
this.content.scrollHeight - 62; // sigh...
|
2017-08-09 14:07:46 +02:00
|
|
|
|
2016-05-25 15:14:12 +02:00
|
|
|
if (is_at_bottom) {
|
2017-06-19 11:08:57 +02:00
|
|
|
scrolled = false;
|
2017-05-03 09:06:28 +02:00
|
|
|
this.onScrolledDown();
|
2016-05-25 15:14:12 +02:00
|
|
|
}
|
2017-12-19 21:26:09 +01:00
|
|
|
u.safeSave(this.model, {'scrolled': scrolled});
|
2017-08-09 14:07:46 +02:00
|
|
|
},
|
2016-05-25 15:14:12 +02:00
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
viewUnreadMessages () {
|
2017-02-24 22:18:41 +01:00
|
|
|
this.model.save('scrolled', false);
|
2016-05-28 14:25:44 +02:00
|
|
|
this.scrollDown();
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
_scrollDown () {
|
2017-02-02 16:06:49 +01:00
|
|
|
/* Inner method that gets debounced */
|
2017-12-20 11:02:01 +01:00
|
|
|
if (_.isUndefined(this.content)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (u.isVisible(this.content) && !this.model.get('scrolled')) {
|
|
|
|
this.content.scrollTop = this.content.scrollHeight;
|
2017-05-03 09:06:28 +02:00
|
|
|
this.onScrolledDown();
|
2017-03-03 14:55:03 +01:00
|
|
|
this.model.save({'auto_scrolled': true});
|
2016-03-13 17:16:53 +01:00
|
|
|
}
|
2017-02-02 16:06:49 +01:00
|
|
|
},
|
|
|
|
|
2017-12-25 12:58:30 +01:00
|
|
|
onScrolledDown() {
|
|
|
|
this.hideNewMessagesIndicator();
|
|
|
|
if (_converse.windowState !== 'hidden') {
|
|
|
|
this.model.clearUnreadMsgCounter();
|
|
|
|
}
|
|
|
|
_converse.emit('chatBoxScrolledDown', {'chatbox': this.model});
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
onWindowStateChanged (state) {
|
2017-05-16 09:21:51 +02:00
|
|
|
if (this.model.get('num_unread', 0) && !this.model.newMessageWillBeHidden()) {
|
2017-05-03 09:06:28 +02:00
|
|
|
this.model.clearUnreadMsgCounter();
|
|
|
|
}
|
2016-03-13 17:16:53 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2017-02-14 16:34:32 +01:00
|
|
|
|
|
|
|
return converse;
|
2016-03-13 17:16:53 +01:00
|
|
|
}));
|