2016-03-09 09:14:09 +01:00
|
|
|
// Converse.js (A browser based XMPP chat client)
|
|
|
|
// http://conversejs.org
|
|
|
|
//
|
2017-02-13 15:37:17 +01:00
|
|
|
// Copyright (c) 2012-2017, Jan-Carel Brand <jc@opkode.com>
|
2016-03-09 09:14:09 +01:00
|
|
|
// Licensed under the Mozilla Public License (MPLv2)
|
|
|
|
//
|
2016-12-19 08:46:48 +01:00
|
|
|
/*global define */
|
2016-03-09 09:14:09 +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-02-19 10:58:30 +01:00
|
|
|
"tpl!chatbox",
|
|
|
|
"converse-chatview",
|
2016-03-13 17:16:53 +01:00
|
|
|
], factory);
|
2017-02-19 10:58:30 +01:00
|
|
|
}(this, function (converse, tpl_chatbox) {
|
2016-03-09 09:14:09 +01:00
|
|
|
"use strict";
|
2017-07-10 17:46:22 +02:00
|
|
|
const { _, utils } = converse.env;
|
2017-12-06 22:10:21 +01:00
|
|
|
const HEADLINES_TYPE = 'headline';
|
2016-03-20 00:03:00 +01:00
|
|
|
|
2016-12-19 08:02:49 +01:00
|
|
|
converse.plugins.add('converse-headline', {
|
2018-01-10 14:26:35 +01:00
|
|
|
/* Plugin dependencies are other plugins which might be
|
|
|
|
* overridden or relied upon, and therefore need to be loaded before
|
|
|
|
* this plugin.
|
|
|
|
*
|
|
|
|
* If the setting "strict_plugin_dependencies" is set to true,
|
|
|
|
* an error will be raised if the plugin is not found. By default it's
|
|
|
|
* false, which means these plugins are only loaded opportunistically.
|
|
|
|
*
|
|
|
|
* NB: These plugins need to have already been loaded via require.js.
|
|
|
|
*/
|
|
|
|
dependencies: ["converse-chatview"],
|
2016-12-20 11:42:20 +01:00
|
|
|
|
2016-03-20 00:03:00 +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.
|
2016-05-28 10:55:03 +02:00
|
|
|
|
2017-12-06 22:10:21 +01:00
|
|
|
ChatBoxes: {
|
|
|
|
model (attrs, options) {
|
|
|
|
const { _converse } = this.__super__;
|
|
|
|
if (attrs.type == HEADLINES_TYPE) {
|
|
|
|
return new _converse.HeadlinesBox(attrs, options);
|
|
|
|
} else {
|
|
|
|
return this.__super__.model.apply(this, arguments);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2016-03-20 00:03:00 +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-20 00:03:00 +01:00
|
|
|
if (!view && item.get('type') === 'headline') {
|
2016-12-20 10:30:20 +01:00
|
|
|
view = new _converse.HeadlinesBoxView({model: item});
|
2016-03-20 00:03:00 +01:00
|
|
|
this.add(item.get('id'), view);
|
|
|
|
return view;
|
|
|
|
} else {
|
2016-08-31 12:06:17 +02:00
|
|
|
return this.__super__.onChatBoxAdded.apply(this, arguments);
|
2016-03-20 00:03:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-12-06 22:10:21 +01:00
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
initialize () {
|
2016-03-09 09:14:09 +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-12-06 22:10:21 +01:00
|
|
|
_converse.HeadlinesBox = _converse.ChatBox.extend({
|
|
|
|
defaults: {
|
|
|
|
'type': 'headline',
|
|
|
|
'show_avatar': false,
|
|
|
|
'bookmarked': false,
|
|
|
|
'chat_state': undefined,
|
|
|
|
'num_unread': 0,
|
|
|
|
'url': ''
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.HeadlinesBoxView = _converse.ChatBoxView.extend({
|
2016-03-20 00:03:00 +01:00
|
|
|
className: 'chatbox headlines',
|
|
|
|
|
|
|
|
events: {
|
|
|
|
'click .close-chatbox-button': 'close',
|
|
|
|
'click .toggle-chatbox-button': 'minimize',
|
2016-12-19 08:46:48 +01:00
|
|
|
'keypress textarea.chat-textarea': 'keyPressed'
|
2016-03-20 00:03:00 +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);
|
2018-01-02 21:25:30 +01:00
|
|
|
this.markScrolled = _.debounce(this._markScrolled, 100);
|
2016-03-31 11:17:39 +02:00
|
|
|
this.disable_mam = true; // Don't do MAM queries for this box
|
2016-03-20 00:03:00 +01:00
|
|
|
this.model.messages.on('add', this.onMessageAdded, this);
|
|
|
|
this.model.on('show', this.show, this);
|
|
|
|
this.model.on('destroy', this.hide, this);
|
|
|
|
this.model.on('change:minimized', this.onMinimizedChanged, this);
|
2017-11-17 14:41:54 +01:00
|
|
|
|
|
|
|
this.render().insertHeading().fetchMessages().insertIntoDOM().hide();
|
|
|
|
_converse.emit('chatBoxOpened', this);
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.emit('chatBoxInitialized', this);
|
2016-03-20 00:03:00 +01:00
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
render () {
|
2018-01-03 20:02:05 +01:00
|
|
|
this.el.setAttribute('id', this.model.get('box_id'))
|
2017-11-17 14:41:54 +01:00
|
|
|
this.el.innerHTML = tpl_chatbox(
|
|
|
|
_.extend(this.model.toJSON(), {
|
|
|
|
info_close: '',
|
|
|
|
label_personal_message: '',
|
|
|
|
show_send_button: false,
|
|
|
|
show_toolbar: false,
|
|
|
|
unread_msgs: ''
|
|
|
|
}
|
|
|
|
));
|
2018-01-02 21:44:46 +01:00
|
|
|
this.content = this.el.querySelector('.chat-content');
|
2016-03-20 00:03:00 +01:00
|
|
|
return this;
|
2017-12-20 13:17:58 +01:00
|
|
|
},
|
|
|
|
|
2018-02-04 18:33:39 +01:00
|
|
|
// Override to avoid the methods in converse-chatview.js
|
|
|
|
'renderMessageForm': _.noop,
|
2017-12-20 13:17:58 +01:00
|
|
|
'afterShown': _.noop
|
2016-03-20 00:03:00 +01:00
|
|
|
});
|
|
|
|
|
2017-06-12 20:30:58 +02:00
|
|
|
function onHeadlineMessage (message) {
|
2017-02-24 15:03:26 +01:00
|
|
|
/* Handler method for all incoming messages of type "headline". */
|
2017-06-12 20:30:58 +02:00
|
|
|
const from_jid = message.getAttribute('from');
|
2018-01-29 15:00:11 +01:00
|
|
|
if (utils.isHeadlineMessage(_converse, message)) {
|
2017-02-24 15:03:26 +01:00
|
|
|
if (_.includes(from_jid, '@') && !_converse.allow_non_roster_messaging) {
|
|
|
|
return;
|
|
|
|
}
|
2017-06-12 20:30:58 +02:00
|
|
|
const chatbox = _converse.chatboxes.create({
|
2016-12-20 11:42:20 +01:00
|
|
|
'id': from_jid,
|
|
|
|
'jid': from_jid,
|
|
|
|
'fullname': from_jid,
|
2017-12-06 22:10:21 +01:00
|
|
|
'type': 'headline',
|
2017-04-21 12:11:33 +02:00
|
|
|
});
|
|
|
|
chatbox.createMessage(message, undefined, message);
|
|
|
|
_converse.emit('message', {'chatbox': chatbox, 'stanza': message});
|
2016-12-20 11:42:20 +01:00
|
|
|
}
|
|
|
|
return true;
|
2017-06-12 20:30:58 +02:00
|
|
|
}
|
2016-12-20 11:42:20 +01:00
|
|
|
|
2017-06-12 20:30:58 +02:00
|
|
|
function registerHeadlineHandler () {
|
2018-02-08 17:06:53 +01:00
|
|
|
_converse.connection.addHandler(onHeadlineMessage, null, 'message');
|
2017-06-12 20:30:58 +02:00
|
|
|
}
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.on('connected', registerHeadlineHandler);
|
|
|
|
_converse.on('reconnected', registerHeadlineHandler);
|
2016-03-09 09:14:09 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}));
|