2019-07-11 10:48:52 +02:00
|
|
|
/**
|
|
|
|
* @module converse-controlbox
|
2020-01-26 16:21:20 +01:00
|
|
|
* @copyright 2020, the Converse.js contributors
|
|
|
|
* @license Mozilla Public License (MPLv2)
|
2019-07-11 10:48:52 +02:00
|
|
|
*/
|
2018-10-23 03:41:38 +02:00
|
|
|
import "converse-chatview";
|
2020-01-30 01:45:01 +01:00
|
|
|
import bootstrap from "bootstrap.native";
|
2019-11-06 11:01:34 +01:00
|
|
|
import log from "@converse/headless/log";
|
2018-10-23 03:41:38 +02:00
|
|
|
import tpl_brand_heading from "templates/converse_brand_heading.html";
|
|
|
|
import tpl_controlbox from "templates/controlbox.html";
|
|
|
|
import tpl_controlbox_toggle from "templates/controlbox_toggle.html";
|
2020-01-30 01:45:01 +01:00
|
|
|
import tpl_login_panel from "templates/login_panel.js";
|
2020-05-18 10:54:37 +02:00
|
|
|
import { Model } from 'skeletor.js/src/model.js';
|
|
|
|
import { View } from "skeletor.js/src/view";
|
|
|
|
import { __ } from '@converse/headless/i18n';
|
|
|
|
import { _converse, api, converse } from "@converse/headless/converse-core";
|
2018-10-23 03:41:38 +02:00
|
|
|
|
2020-01-30 01:45:01 +01:00
|
|
|
const { Strophe, dayjs } = converse.env;
|
2018-10-23 03:41:38 +02:00
|
|
|
const u = converse.env.utils;
|
|
|
|
|
|
|
|
const CONNECTION_STATUS_CSS_CLASS = {
|
|
|
|
'Error': 'error',
|
|
|
|
'Connecting': 'info',
|
|
|
|
'Connection failure': 'error',
|
|
|
|
'Authenticating': 'info',
|
|
|
|
'Authentication failure': 'error',
|
|
|
|
'Connected': 'info',
|
|
|
|
'Disconnected': 'error',
|
|
|
|
'Disconnecting': 'warn',
|
|
|
|
'Attached': 'info',
|
|
|
|
'Redirect': 'info',
|
|
|
|
'Reconnecting': 'warn'
|
|
|
|
};
|
|
|
|
|
|
|
|
const PRETTY_CONNECTION_STATUS = {
|
|
|
|
0: 'Error',
|
|
|
|
1: 'Connecting',
|
|
|
|
2: 'Connection failure',
|
|
|
|
3: 'Authenticating',
|
|
|
|
4: 'Authentication failure',
|
|
|
|
5: 'Connected',
|
|
|
|
6: 'Disconnected',
|
|
|
|
7: 'Disconnecting',
|
|
|
|
8: 'Attached',
|
|
|
|
9: 'Redirect',
|
|
|
|
10: 'Reconnecting'
|
|
|
|
};
|
|
|
|
|
|
|
|
const REPORTABLE_STATUSES = [
|
|
|
|
0, // ERROR'
|
|
|
|
1, // CONNECTING
|
|
|
|
2, // CONNFAIL
|
|
|
|
3, // AUTHENTICATING
|
|
|
|
4, // AUTHFAIL
|
|
|
|
7, // DISCONNECTING
|
|
|
|
10 // RECONNECTING
|
|
|
|
];
|
|
|
|
|
|
|
|
converse.plugins.add('converse-controlbox', {
|
|
|
|
/* 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.
|
|
|
|
*/
|
2019-11-01 16:04:55 +01:00
|
|
|
dependencies: ["converse-modal", "converse-chatboxes", "converse-chat", "converse-rosterview", "converse-chatview"],
|
2018-10-23 03:41:38 +02:00
|
|
|
|
2019-12-11 08:09:29 +01:00
|
|
|
|
2018-12-12 14:08:00 +01:00
|
|
|
enabled (_converse) {
|
2020-03-30 16:29:09 +02:00
|
|
|
return !_converse.api.settings.get("singleton");
|
2018-12-12 14:08:00 +01:00
|
|
|
},
|
|
|
|
|
2019-12-11 08:09:29 +01:00
|
|
|
|
2018-10-23 03:41:38 +02: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.
|
|
|
|
|
|
|
|
ChatBoxes: {
|
2018-12-12 14:08:00 +01:00
|
|
|
model (attrs, options) {
|
|
|
|
const { _converse } = this.__super__;
|
2019-10-24 14:29:15 +02:00
|
|
|
if (attrs && attrs.id == 'controlbox') {
|
2018-12-12 14:08:00 +01:00
|
|
|
return new _converse.ControlBox(attrs, options);
|
|
|
|
} else {
|
|
|
|
return this.__super__.model.apply(this, arguments);
|
|
|
|
}
|
2019-04-16 13:38:46 +02:00
|
|
|
}
|
2018-10-23 03:41:38 +02:00
|
|
|
}
|
|
|
|
},
|
2016-02-28 20:24:06 +01:00
|
|
|
|
2019-12-11 08:09:29 +01:00
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
initialize () {
|
|
|
|
/* The initialize function gets called as soon as the plugin is
|
|
|
|
* loaded by converse.js's plugin machinery.
|
|
|
|
*/
|
2020-03-31 13:15:57 +02:00
|
|
|
api.settings.update({
|
2018-10-23 03:41:38 +02:00
|
|
|
allow_logout: true,
|
|
|
|
default_domain: undefined,
|
|
|
|
locked_domain: undefined,
|
|
|
|
show_controlbox_by_default: false,
|
|
|
|
sticky_controlbox: false
|
|
|
|
});
|
|
|
|
|
2020-03-31 13:15:57 +02:00
|
|
|
api.promises.add('controlBoxInitialized');
|
2018-10-23 03:41:38 +02:00
|
|
|
|
2018-11-18 19:14:22 +01:00
|
|
|
|
2018-12-12 14:08:00 +01:00
|
|
|
_converse.ControlBox = _converse.ChatBox.extend({
|
|
|
|
|
2019-04-17 11:52:41 +02:00
|
|
|
defaults () {
|
|
|
|
return {
|
|
|
|
'bookmarked': false,
|
|
|
|
'box_id': 'controlbox',
|
|
|
|
'chat_state': undefined,
|
2020-03-31 13:15:57 +02:00
|
|
|
'closed': !api.settings.get('show_controlbox_by_default'),
|
2019-04-17 11:52:41 +02:00
|
|
|
'num_unread': 0,
|
2019-05-05 19:05:45 +02:00
|
|
|
'time_opened': this.get('time_opened') || (new Date()).getTime(),
|
2019-04-17 11:52:41 +02:00
|
|
|
'type': _converse.CONTROLBOX_TYPE,
|
|
|
|
'url': ''
|
|
|
|
}
|
2019-05-22 14:03:13 +02:00
|
|
|
},
|
|
|
|
|
2019-12-11 08:09:29 +01:00
|
|
|
initialize () {
|
|
|
|
if (this.get('id') === 'controlbox') {
|
|
|
|
this.set({'time_opened': dayjs(0).valueOf()});
|
|
|
|
} else {
|
|
|
|
_converse.ChatBox.prototype.initialize.apply(this, arguments);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
validate (attrs) {
|
|
|
|
if (attrs.type === _converse.CONTROLBOX_TYPE) {
|
2020-03-31 13:15:57 +02:00
|
|
|
if (api.settings.get("view_mode") === 'embedded' && api.settings.get("singleton")) {
|
2019-12-11 08:09:29 +01:00
|
|
|
return 'Controlbox not relevant in embedded view mode';
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
return _converse.ChatBox.prototype.validate.call(this, attrs);
|
|
|
|
},
|
|
|
|
|
|
|
|
maybeShow (force) {
|
|
|
|
if (!force && this.get('id') === 'controlbox') {
|
|
|
|
// Must return the chatbox
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
return _converse.ChatBox.prototype.maybeShow.call(this, force);
|
|
|
|
},
|
|
|
|
|
|
|
|
onReconnection: function onReconnection () {}
|
2018-12-12 14:08:00 +01:00
|
|
|
});
|
2018-02-22 18:41:01 +01:00
|
|
|
|
2016-02-28 20:24:06 +01:00
|
|
|
|
2019-12-11 08:09:29 +01:00
|
|
|
function addControlBox () {
|
|
|
|
const m = new _converse.ControlBox({'id': 'controlbox'});
|
|
|
|
return _converse.chatboxes.add(m);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
_converse.ControlBoxView = _converse.ChatBoxView.extend({
|
|
|
|
tagName: 'div',
|
|
|
|
className: 'chatbox',
|
|
|
|
id: 'controlbox',
|
|
|
|
events: {
|
|
|
|
'click a.close-chatbox-button': 'close'
|
|
|
|
},
|
|
|
|
|
|
|
|
initialize () {
|
2019-07-29 10:19:05 +02:00
|
|
|
if (_converse.controlboxtoggle === undefined) {
|
2018-10-23 03:41:38 +02:00
|
|
|
_converse.controlboxtoggle = new _converse.ControlBoxToggle();
|
|
|
|
}
|
|
|
|
_converse.controlboxtoggle.el.insertAdjacentElement('afterend', this.el);
|
|
|
|
|
2019-09-06 14:34:59 +02:00
|
|
|
this.listenTo(this.model, 'change:connected', this.onConnected)
|
|
|
|
this.listenTo(this.model, 'destroy', this.hide)
|
|
|
|
this.listenTo(this.model, 'hide', this.hide)
|
|
|
|
this.listenTo(this.model, 'show', this.show)
|
|
|
|
this.listenTo(this.model, 'change:closed', this.ensureClosedState)
|
2018-10-23 03:41:38 +02:00
|
|
|
this.render();
|
2019-03-29 15:47:23 +01:00
|
|
|
/**
|
|
|
|
* Triggered when the _converse.ControlBoxView has been initialized and therefore
|
|
|
|
* exists. The controlbox contains the login and register forms when the user is
|
|
|
|
* logged out and a list of the user's contacts and group chats when logged in.
|
2019-08-05 11:10:32 +02:00
|
|
|
* @event _converse#controlBoxInitialized
|
2019-03-29 15:47:23 +01:00
|
|
|
* @type { _converse.ControlBoxView }
|
2019-08-05 11:10:32 +02:00
|
|
|
* @example _converse.api.listen.on('controlBoxInitialized', view => { ... });
|
2019-03-29 15:47:23 +01:00
|
|
|
*/
|
2020-03-31 13:15:57 +02:00
|
|
|
api.trigger('controlBoxInitialized', this);
|
2018-10-23 03:41:38 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
render () {
|
|
|
|
if (this.model.get('connected')) {
|
2019-07-29 10:19:05 +02:00
|
|
|
if (this.model.get('closed') === undefined) {
|
2020-03-31 13:15:57 +02:00
|
|
|
this.model.set('closed', !api.settings.get('show_controlbox_by_default'));
|
2017-08-29 11:08:37 +02:00
|
|
|
}
|
2018-10-23 03:41:38 +02:00
|
|
|
}
|
2019-04-29 09:07:15 +02:00
|
|
|
this.el.innerHTML = tpl_controlbox(Object.assign(this.model.toJSON()));
|
2018-10-23 03:41:38 +02:00
|
|
|
|
|
|
|
if (!this.model.get('closed')) {
|
|
|
|
this.show();
|
|
|
|
} else {
|
|
|
|
this.hide();
|
|
|
|
}
|
2018-11-18 19:14:22 +01:00
|
|
|
|
2020-03-24 12:26:34 +01:00
|
|
|
const connection = _converse?.connection || {};
|
2018-11-18 19:14:22 +01:00
|
|
|
if (!connection.connected || !connection.authenticated || connection.disconnecting) {
|
2018-10-23 03:41:38 +02:00
|
|
|
this.renderLoginPanel();
|
2019-02-20 22:58:59 +01:00
|
|
|
} else if (this.model.get('connected')) {
|
2018-10-23 03:41:38 +02:00
|
|
|
this.renderControlBoxPane();
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
},
|
2018-02-15 15:50:55 +01:00
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
onConnected () {
|
|
|
|
if (this.model.get('connected')) {
|
2016-02-28 20:24:06 +01:00
|
|
|
this.render();
|
2018-10-23 03:41:38 +02:00
|
|
|
}
|
|
|
|
},
|
2016-02-28 20:24:06 +01:00
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
createBrandHeadingHTML () {
|
|
|
|
return tpl_brand_heading({
|
2020-03-31 13:15:57 +02:00
|
|
|
'sticky_controlbox': api.settings.get('sticky_controlbox')
|
2018-10-23 03:41:38 +02:00
|
|
|
});
|
|
|
|
},
|
2016-02-28 20:24:06 +01:00
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
insertBrandHeading () {
|
|
|
|
const heading_el = this.el.querySelector('.brand-heading-container');
|
2019-08-05 01:39:57 +02:00
|
|
|
if (heading_el === null) {
|
2018-10-23 03:41:38 +02:00
|
|
|
const el = this.el.querySelector('.controlbox-head');
|
|
|
|
el.insertAdjacentHTML('beforeend', this.createBrandHeadingHTML());
|
|
|
|
} else {
|
|
|
|
heading_el.outerHTML = this.createBrandHeadingHTML();
|
|
|
|
}
|
|
|
|
},
|
2017-09-17 01:30:52 +02:00
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
renderLoginPanel () {
|
|
|
|
this.el.classList.add("logged-out");
|
2019-08-05 01:39:57 +02:00
|
|
|
if (this.loginpanel) {
|
|
|
|
this.loginpanel.render();
|
|
|
|
} else {
|
2018-10-23 03:41:38 +02:00
|
|
|
this.loginpanel = new _converse.LoginPanel({
|
|
|
|
'model': new _converse.LoginPanelModel()
|
|
|
|
});
|
|
|
|
const panes = this.el.querySelector('.controlbox-panes');
|
|
|
|
panes.innerHTML = '';
|
|
|
|
panes.appendChild(this.loginpanel.render().el);
|
|
|
|
this.insertBrandHeading();
|
|
|
|
}
|
|
|
|
this.loginpanel.initPopovers();
|
2019-12-29 14:47:25 +01:00
|
|
|
if (_converse.auto_focus) {
|
|
|
|
this.loginpanel.el.querySelector('#converse-login-jid').focus();
|
|
|
|
}
|
2018-10-23 03:41:38 +02:00
|
|
|
return this;
|
|
|
|
},
|
2016-02-28 20:24:06 +01:00
|
|
|
|
2019-10-09 16:01:38 +02:00
|
|
|
/**
|
|
|
|
* Renders the "Contacts" panel of the controlbox.
|
|
|
|
* This will only be called after the user has already been logged in.
|
|
|
|
* @private
|
|
|
|
* @method _converse.ControlBoxView.renderControlBoxPane
|
|
|
|
*/
|
2018-10-23 03:41:38 +02:00
|
|
|
renderControlBoxPane () {
|
|
|
|
if (this.loginpanel) {
|
|
|
|
this.loginpanel.remove();
|
|
|
|
delete this.loginpanel;
|
|
|
|
}
|
2019-02-20 22:58:59 +01:00
|
|
|
if (this.controlbox_pane && u.isVisible(this.controlbox_pane.el)) {
|
|
|
|
return;
|
|
|
|
}
|
2018-10-23 03:41:38 +02:00
|
|
|
this.el.classList.remove("logged-out");
|
|
|
|
this.controlbox_pane = new _converse.ControlBoxPane();
|
|
|
|
this.el.querySelector('.controlbox-panes').insertAdjacentElement(
|
|
|
|
'afterBegin',
|
|
|
|
this.controlbox_pane.el
|
|
|
|
)
|
|
|
|
},
|
2016-02-28 20:24:06 +01:00
|
|
|
|
2019-10-24 14:29:15 +02:00
|
|
|
async close (ev) {
|
2018-10-23 03:41:38 +02:00
|
|
|
if (ev && ev.preventDefault) { ev.preventDefault(); }
|
2020-03-24 12:26:34 +01:00
|
|
|
if (ev?.name === 'closeAllChatBoxes' &&
|
2019-10-24 14:29:15 +02:00
|
|
|
(_converse.disconnection_cause !== _converse.LOGOUT ||
|
2020-03-31 13:15:57 +02:00
|
|
|
api.settings.get('show_controlbox_by_default'))) {
|
2019-10-24 14:29:15 +02:00
|
|
|
return;
|
|
|
|
}
|
2020-03-31 13:15:57 +02:00
|
|
|
if (api.settings.get('sticky_controlbox')) {
|
2016-02-28 20:24:06 +01:00
|
|
|
return;
|
|
|
|
}
|
2020-03-24 12:26:34 +01:00
|
|
|
const connection = _converse?.connection || {};
|
2018-11-18 19:14:22 +01:00
|
|
|
if (connection.connected && !connection.disconnecting) {
|
2019-10-24 14:29:15 +02:00
|
|
|
await new Promise((resolve, reject) => {
|
|
|
|
return this.model.save(
|
|
|
|
{'closed': true},
|
2020-03-02 16:39:47 +01:00
|
|
|
{'success': resolve, 'error': reject, 'wait': true}
|
2019-10-24 14:29:15 +02:00
|
|
|
);
|
|
|
|
});
|
2018-10-23 03:41:38 +02:00
|
|
|
} else {
|
|
|
|
this.model.trigger('hide');
|
|
|
|
}
|
2020-03-31 13:15:57 +02:00
|
|
|
api.trigger('controlBoxClosed', this);
|
2018-10-23 03:41:38 +02:00
|
|
|
return this;
|
|
|
|
},
|
2016-02-28 20:24:06 +01:00
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
ensureClosedState () {
|
|
|
|
if (this.model.get('closed')) {
|
|
|
|
this.hide();
|
|
|
|
} else {
|
|
|
|
this.show();
|
2017-09-22 18:55:02 +02:00
|
|
|
}
|
2018-10-23 03:41:38 +02:00
|
|
|
},
|
2016-02-28 20:24:06 +01:00
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
hide (callback) {
|
2020-03-31 13:15:57 +02:00
|
|
|
if (api.settings.get('sticky_controlbox')) {
|
2018-10-23 03:41:38 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
u.addClass('hidden', this.el);
|
2020-03-31 13:15:57 +02:00
|
|
|
api.trigger('chatBoxClosed', this);
|
2018-11-18 19:14:22 +01:00
|
|
|
|
2020-03-31 13:15:57 +02:00
|
|
|
if (!api.connection.connected()) {
|
2018-10-23 03:41:38 +02:00
|
|
|
_converse.controlboxtoggle.render();
|
|
|
|
}
|
|
|
|
_converse.controlboxtoggle.show(callback);
|
|
|
|
return this;
|
|
|
|
},
|
2017-09-08 14:24:38 +02:00
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
onControlBoxToggleHidden () {
|
|
|
|
this.model.set('closed', false);
|
|
|
|
this.el.classList.remove('hidden');
|
2019-07-12 12:41:21 +02:00
|
|
|
/**
|
|
|
|
* Triggered once the controlbox has been opened
|
|
|
|
* @event _converse#controlBoxOpened
|
|
|
|
* @type {_converse.ControlBox}
|
|
|
|
*/
|
2020-03-31 13:15:57 +02:00
|
|
|
api.trigger('controlBoxOpened', this);
|
2018-10-23 03:41:38 +02:00
|
|
|
},
|
2018-05-17 18:25:12 +02:00
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
show () {
|
2020-03-02 16:39:47 +01:00
|
|
|
_converse.controlboxtoggle.hide(() => this.onControlBoxToggleHidden());
|
2018-10-23 03:41:38 +02:00
|
|
|
return this;
|
|
|
|
},
|
2018-05-17 18:25:12 +02:00
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
showHelpMessages () {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-09-19 16:54:55 +02:00
|
|
|
_converse.LoginPanelModel = Model.extend({
|
2018-10-23 03:41:38 +02:00
|
|
|
defaults: {
|
|
|
|
// Passed-by-reference. Fine in this case because there's
|
|
|
|
// only one such model.
|
|
|
|
'errors': [],
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-01-30 12:57:11 +01:00
|
|
|
_converse.LoginPanel = View.extend({
|
2018-10-23 03:41:38 +02:00
|
|
|
tagName: 'div',
|
|
|
|
id: "converse-login-panel",
|
2020-01-30 01:45:01 +01:00
|
|
|
className: 'controlbox-pane fade-in row no-gutters',
|
2018-10-23 03:41:38 +02:00
|
|
|
events: {
|
|
|
|
'submit form#converse-login': 'authenticate',
|
|
|
|
'change input': 'validate'
|
|
|
|
},
|
|
|
|
|
2019-10-09 16:01:38 +02:00
|
|
|
initialize () {
|
2019-09-06 14:34:59 +02:00
|
|
|
this.listenTo(this.model, 'change', this.render)
|
2018-10-23 03:41:38 +02:00
|
|
|
this.listenTo(_converse.connfeedback, 'change', this.render);
|
|
|
|
this.render();
|
|
|
|
},
|
|
|
|
|
|
|
|
toHTML () {
|
|
|
|
const connection_status = _converse.connfeedback.get('connection_status');
|
|
|
|
let feedback_class, pretty_status;
|
2019-10-04 14:53:20 +02:00
|
|
|
if (REPORTABLE_STATUSES.includes(connection_status)) {
|
2018-10-23 03:41:38 +02:00
|
|
|
pretty_status = PRETTY_CONNECTION_STATUS[connection_status];
|
|
|
|
feedback_class = CONNECTION_STATUS_CSS_CLASS[pretty_status];
|
2016-02-28 20:24:06 +01:00
|
|
|
}
|
2018-10-23 03:41:38 +02:00
|
|
|
return tpl_login_panel(
|
2019-04-29 09:07:15 +02:00
|
|
|
Object.assign(this.model.toJSON(), {
|
2018-10-23 03:41:38 +02:00
|
|
|
'_converse': _converse,
|
|
|
|
'ANONYMOUS': _converse.ANONYMOUS,
|
|
|
|
'EXTERNAL': _converse.EXTERNAL,
|
|
|
|
'LOGIN': _converse.LOGIN,
|
|
|
|
'PREBIND': _converse.PREBIND,
|
|
|
|
'auto_login': _converse.auto_login,
|
2020-03-31 13:15:57 +02:00
|
|
|
'authentication': api.settings.get("authentication"),
|
2018-10-23 03:41:38 +02:00
|
|
|
'connection_status': connection_status,
|
|
|
|
'conn_feedback_class': feedback_class,
|
|
|
|
'conn_feedback_subject': pretty_status,
|
|
|
|
'conn_feedback_message': _converse.connfeedback.get('message'),
|
2020-03-31 13:15:57 +02:00
|
|
|
'placeholder_username': (api.settings.get('locked_domain') || api.settings.get('default_domain')) &&
|
2018-10-23 03:41:38 +02:00
|
|
|
__('Username') || __('user@domain'),
|
2018-11-10 08:46:17 +01:00
|
|
|
'show_trust_checkbox': _converse.trusted !== 'on' && _converse.trusted !== 'off'
|
2018-10-23 03:41:38 +02:00
|
|
|
})
|
|
|
|
);
|
|
|
|
},
|
2016-02-28 20:24:06 +01:00
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
initPopovers () {
|
2019-10-04 14:53:20 +02:00
|
|
|
Array.from(this.el.querySelectorAll('[data-title]')).forEach(el => {
|
2019-10-09 16:01:38 +02:00
|
|
|
new bootstrap.Popover(el, {
|
2020-03-31 13:15:57 +02:00
|
|
|
'trigger': api.settings.get("view_mode") === 'mobile' && 'click' || 'hover',
|
|
|
|
'dismissible': api.settings.get("view_mode") === 'mobile' && true || false,
|
2018-10-23 03:41:38 +02:00
|
|
|
'container': this.el.parentElement.parentElement.parentElement
|
|
|
|
})
|
|
|
|
});
|
|
|
|
},
|
2016-02-28 20:24:06 +01:00
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
validate () {
|
|
|
|
const form = this.el.querySelector('form');
|
|
|
|
const jid_element = form.querySelector('input[name=jid]');
|
|
|
|
if (jid_element.value &&
|
2020-03-31 13:15:57 +02:00
|
|
|
!api.settings.get('locked_domain') &&
|
|
|
|
!api.settings.get('default_domain') &&
|
2018-10-23 03:41:38 +02:00
|
|
|
!u.isValidJID(jid_element.value)) {
|
|
|
|
jid_element.setCustomValidity(__('Please enter a valid XMPP address'));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
jid_element.setCustomValidity('');
|
|
|
|
return true;
|
|
|
|
},
|
2016-02-28 20:24:06 +01:00
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
authenticate (ev) {
|
|
|
|
/* Authenticate the user based on a form submission event.
|
|
|
|
*/
|
|
|
|
if (ev && ev.preventDefault) { ev.preventDefault(); }
|
2020-03-31 13:15:57 +02:00
|
|
|
if (api.settings.get("authentication") === _converse.ANONYMOUS) {
|
2018-05-29 12:00:23 +02:00
|
|
|
return this.connect(_converse.jid, null);
|
2016-02-28 20:24:06 +01:00
|
|
|
}
|
2018-10-23 03:41:38 +02:00
|
|
|
if (!this.validate()) { return; }
|
2016-02-28 20:24:06 +01:00
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
const form_data = new FormData(ev.target);
|
2018-11-10 08:46:17 +01:00
|
|
|
|
|
|
|
if (_converse.trusted === 'on' || _converse.trusted === 'off') {
|
|
|
|
_converse.config.save({
|
|
|
|
'trusted': _converse.trusted === 'on',
|
2019-12-17 13:39:32 +01:00
|
|
|
'storage': _converse.trusted === 'on' ? 'persistent' : 'session'
|
2018-11-10 08:46:17 +01:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
_converse.config.save({
|
|
|
|
'trusted': form_data.get('trusted') && true || false,
|
2019-12-17 13:39:32 +01:00
|
|
|
'storage': form_data.get('trusted') ? 'persistent' : 'session'
|
2018-11-10 08:46:17 +01:00
|
|
|
});
|
|
|
|
}
|
2016-02-28 20:24:06 +01:00
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
let jid = form_data.get('jid');
|
2020-03-31 13:15:57 +02:00
|
|
|
if (api.settings.get('locked_domain')) {
|
|
|
|
const last_part = '@' + api.settings.get('locked_domain');
|
2018-10-23 03:41:38 +02:00
|
|
|
if (jid.endsWith(last_part)) {
|
|
|
|
jid = jid.substr(0, jid.length - last_part.length);
|
2016-02-28 20:24:06 +01:00
|
|
|
}
|
2018-10-23 03:41:38 +02:00
|
|
|
jid = Strophe.escapeNode(jid) + last_part;
|
2020-03-31 13:15:57 +02:00
|
|
|
} else if (api.settings.get('default_domain') && !jid.includes('@')) {
|
|
|
|
jid = jid + '@' + api.settings.get('default_domain');
|
2018-10-23 03:41:38 +02:00
|
|
|
}
|
2018-05-29 12:00:23 +02:00
|
|
|
this.connect(jid, form_data.get('password'));
|
2018-10-23 03:41:38 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
connect (jid, password) {
|
2019-09-19 16:54:55 +02:00
|
|
|
if (["converse/login", "converse/register"].includes(_converse.router.history.getFragment())) {
|
2018-10-23 03:41:38 +02:00
|
|
|
_converse.router.navigate('', {'replace': true});
|
|
|
|
}
|
2018-11-18 19:14:22 +01:00
|
|
|
_converse.connection && _converse.connection.reset();
|
2020-03-31 13:15:57 +02:00
|
|
|
api.user.login(jid, password);
|
2018-10-23 03:41:38 +02:00
|
|
|
}
|
|
|
|
});
|
2016-12-04 16:03:06 +01:00
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
|
2019-09-19 16:54:55 +02:00
|
|
|
_converse.ControlBoxPane = View.extend({
|
2018-10-23 03:41:38 +02:00
|
|
|
tagName: 'div',
|
|
|
|
className: 'controlbox-pane',
|
|
|
|
|
|
|
|
initialize () {
|
2019-08-05 11:07:38 +02:00
|
|
|
/**
|
|
|
|
* Triggered once the {@link _converse.ControlBoxPane} has been initialized
|
|
|
|
* @event _converse#controlBoxPaneInitialized
|
|
|
|
* @type { _converse.ControlBoxPane }
|
|
|
|
* @example _converse.api.listen.on('controlBoxPaneInitialized', view => { ... });
|
|
|
|
*/
|
2020-03-31 13:15:57 +02:00
|
|
|
api.trigger('controlBoxPaneInitialized', this);
|
2018-10-23 03:41:38 +02:00
|
|
|
}
|
|
|
|
});
|
2018-08-28 14:58:33 +02:00
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
|
2019-09-19 16:54:55 +02:00
|
|
|
_converse.ControlBoxToggle = View.extend({
|
2018-10-23 03:41:38 +02:00
|
|
|
tagName: 'a',
|
|
|
|
className: 'toggle-controlbox hidden',
|
|
|
|
id: 'toggle-controlbox',
|
|
|
|
events: {
|
|
|
|
'click': 'onClick'
|
|
|
|
},
|
|
|
|
attributes: {
|
|
|
|
'href': "#"
|
|
|
|
},
|
|
|
|
|
|
|
|
initialize () {
|
|
|
|
_converse.chatboxviews.insertRowColumn(this.render().el);
|
2020-03-31 13:15:57 +02:00
|
|
|
api.waitUntil('initialized')
|
2018-10-23 03:41:38 +02:00
|
|
|
.then(this.render.bind(this))
|
2019-11-06 11:01:34 +01:00
|
|
|
.catch(e => log.fatal(e));
|
2018-10-23 03:41:38 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
render () {
|
|
|
|
// We let the render method of ControlBoxView decide whether
|
|
|
|
// the ControlBox or the Toggle must be shown. This prevents
|
|
|
|
// artifacts (i.e. on page load the toggle is shown only to then
|
2018-11-29 12:20:34 +01:00
|
|
|
// seconds later be hidden in favor of the controlbox).
|
2018-10-23 03:41:38 +02:00
|
|
|
this.el.innerHTML = tpl_controlbox_toggle({
|
2020-03-31 13:15:57 +02:00
|
|
|
'label_toggle': api.connection.connected() ? __('Chat Contacts') : __('Toggle chat')
|
2018-10-23 03:41:38 +02:00
|
|
|
})
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
hide (callback) {
|
2020-03-02 16:38:57 +01:00
|
|
|
if (u.isVisible(this.el)) {
|
|
|
|
u.hideElement(this.el);
|
|
|
|
callback();
|
|
|
|
}
|
2018-10-23 03:41:38 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
show (callback) {
|
2020-03-02 16:38:57 +01:00
|
|
|
if (!u.isVisible(this.el)) {
|
|
|
|
u.fadeIn(this.el, callback);
|
|
|
|
}
|
2018-10-23 03:41:38 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
showControlBox () {
|
|
|
|
let controlbox = _converse.chatboxes.get('controlbox');
|
|
|
|
if (!controlbox) {
|
2018-12-12 14:08:00 +01:00
|
|
|
controlbox = addControlBox();
|
2018-05-18 12:44:27 +02:00
|
|
|
}
|
2020-03-31 13:15:57 +02:00
|
|
|
if (api.connection.connected()) {
|
2019-04-16 16:36:58 +02:00
|
|
|
controlbox.save({'closed': false});
|
2018-10-23 03:41:38 +02:00
|
|
|
} else {
|
|
|
|
controlbox.trigger('show');
|
|
|
|
}
|
|
|
|
},
|
2018-05-18 12:44:27 +02:00
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
onClick (e) {
|
|
|
|
e.preventDefault();
|
|
|
|
if (u.isVisible(_converse.root.querySelector("#controlbox"))) {
|
|
|
|
const controlbox = _converse.chatboxes.get('controlbox');
|
2020-03-31 13:15:57 +02:00
|
|
|
if (api.connection.connected) {
|
2018-10-23 03:41:38 +02:00
|
|
|
controlbox.save({closed: true});
|
|
|
|
} else {
|
|
|
|
controlbox.trigger('hide');
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.showControlBox();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2018-02-22 11:06:30 +01:00
|
|
|
|
2019-08-01 08:55:44 +02:00
|
|
|
|
|
|
|
/******************** Event Handlers ********************/
|
|
|
|
|
2020-03-31 13:15:57 +02:00
|
|
|
api.listen.on('chatBoxViewsInitialized', () => {
|
2018-10-23 03:41:38 +02:00
|
|
|
_converse.chatboxes.on('add', item => {
|
|
|
|
if (item.get('type') === _converse.CONTROLBOX_TYPE) {
|
2019-05-30 11:15:29 +02:00
|
|
|
const views = _converse.chatboxviews;
|
|
|
|
const view = views.get(item.get('id'));
|
2018-10-23 03:41:38 +02:00
|
|
|
if (view) {
|
|
|
|
view.model = item;
|
|
|
|
view.initialize();
|
|
|
|
} else {
|
2019-05-30 11:15:29 +02:00
|
|
|
views.add(item.get('id'), new _converse.ControlBoxView({model: item}));
|
2018-10-23 03:41:38 +02:00
|
|
|
}
|
|
|
|
}
|
2018-02-22 11:06:30 +01:00
|
|
|
});
|
2018-10-23 03:41:38 +02:00
|
|
|
});
|
|
|
|
|
2020-03-31 13:15:57 +02:00
|
|
|
api.listen.on('clearSession', () => {
|
2020-03-24 12:26:34 +01:00
|
|
|
const chatboxviews = _converse?.chatboxviews;
|
2019-08-01 10:26:35 +02:00
|
|
|
const view = chatboxviews && chatboxviews.get('controlbox');
|
|
|
|
if (view) {
|
|
|
|
u.safeSave(view.model, {'connected': false});
|
2020-03-24 12:26:34 +01:00
|
|
|
if (view?.controlbox_pane) {
|
2019-08-01 10:26:35 +02:00
|
|
|
view.controlbox_pane.remove();
|
|
|
|
delete view.controlbox_pane;
|
|
|
|
}
|
2018-10-23 03:41:38 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-08-01 10:26:35 +02:00
|
|
|
|
2020-03-31 13:15:57 +02:00
|
|
|
api.waitUntil('chatBoxViewsInitialized')
|
2018-11-18 19:14:22 +01:00
|
|
|
.then(addControlBox)
|
2019-11-06 11:01:34 +01:00
|
|
|
.catch(e => log.fatal(e));
|
2018-10-23 03:41:38 +02:00
|
|
|
|
2020-03-31 13:15:57 +02:00
|
|
|
api.listen.on('chatBoxesFetched', () => {
|
2018-12-12 14:08:00 +01:00
|
|
|
const controlbox = _converse.chatboxes.get('controlbox') || addControlBox();
|
2019-10-24 14:29:15 +02:00
|
|
|
controlbox.save({'connected': true});
|
2018-10-23 03:41:38 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
const disconnect = function () {
|
|
|
|
/* Upon disconnection, set connected to `false`, so that if
|
|
|
|
* we reconnect, "onConnected" will be called,
|
|
|
|
* to fetch the roster again and to send out a presence stanza.
|
|
|
|
*/
|
|
|
|
const view = _converse.chatboxviews.get('controlbox');
|
|
|
|
view.model.set({'connected': false});
|
|
|
|
return view;
|
|
|
|
};
|
2020-03-31 13:15:57 +02:00
|
|
|
api.listen.on('disconnected', () => disconnect().renderLoginPanel());
|
|
|
|
api.listen.on('will-reconnect', disconnect);
|
2019-02-20 22:58:59 +01:00
|
|
|
|
2019-08-01 08:55:44 +02:00
|
|
|
/************************ API ************************/
|
|
|
|
|
2020-03-31 13:15:57 +02:00
|
|
|
Object.assign(api, {
|
2018-11-29 12:20:34 +01:00
|
|
|
/**
|
|
|
|
* The "controlbox" namespace groups methods pertaining to the
|
|
|
|
* controlbox view
|
|
|
|
*
|
|
|
|
* @namespace _converse.api.controlbox
|
|
|
|
* @memberOf _converse.api
|
|
|
|
*/
|
2019-11-01 16:04:55 +01:00
|
|
|
controlbox: {
|
2018-11-29 12:20:34 +01:00
|
|
|
/**
|
2019-11-01 16:04:55 +01:00
|
|
|
* Opens the controlbox
|
|
|
|
* @method _converse.api.controlbox.open
|
|
|
|
* @returns { Promise<_converse.ControlBox> }
|
|
|
|
*/
|
|
|
|
async open () {
|
2020-03-31 13:15:57 +02:00
|
|
|
await api.waitUntil('chatBoxesFetched');
|
|
|
|
const model = await api.chatboxes.get('controlbox') ||
|
|
|
|
api.chatboxes.create('controlbox', {}, _converse.Controlbox);
|
2019-11-01 16:04:55 +01:00
|
|
|
model.trigger('show');
|
|
|
|
return model;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the controlbox view.
|
2019-03-21 20:55:07 +01:00
|
|
|
* @method _converse.api.controlbox.get
|
2020-01-30 01:45:01 +01:00
|
|
|
* @returns { View } View representing the controlbox
|
2019-11-01 16:04:55 +01:00
|
|
|
* @example const view = _converse.api.controlbox.get();
|
2018-11-29 12:20:34 +01:00
|
|
|
*/
|
|
|
|
get () {
|
|
|
|
return _converse.chatboxviews.get('controlbox');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2018-10-23 03:41:38 +02:00
|
|
|
}
|
|
|
|
});
|