2016-03-13 17:40:52 +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-13 17:40:52 +01:00
|
|
|
// Licensed under the Mozilla Public License (MPLv2)
|
|
|
|
//
|
2017-04-21 15:52:16 +02:00
|
|
|
/*global define */
|
2016-03-13 17:40:52 +01:00
|
|
|
|
|
|
|
(function (root, factory) {
|
2017-07-10 21:14:48 +02:00
|
|
|
define(["jquery.noconflict",
|
|
|
|
"converse-core",
|
2016-09-23 10:54:55 +02:00
|
|
|
"tpl!group_header",
|
|
|
|
"tpl!pending_contact",
|
|
|
|
"tpl!requesting_contact",
|
|
|
|
"tpl!roster",
|
2017-03-30 16:45:52 +02:00
|
|
|
"tpl!roster_filter",
|
2017-08-16 11:16:22 +02:00
|
|
|
"tpl!roster_item",
|
|
|
|
"converse-chatboxes"
|
2016-09-23 10:54:55 +02:00
|
|
|
], factory);
|
|
|
|
}(this, function (
|
2017-07-10 21:14:48 +02:00
|
|
|
$,
|
2016-12-20 11:42:20 +01:00
|
|
|
converse,
|
2016-09-23 10:54:55 +02:00
|
|
|
tpl_group_header,
|
|
|
|
tpl_pending_contact,
|
|
|
|
tpl_requesting_contact,
|
|
|
|
tpl_roster,
|
2017-03-30 16:45:52 +02:00
|
|
|
tpl_roster_filter,
|
2016-09-23 10:54:55 +02:00
|
|
|
tpl_roster_item) {
|
2016-03-13 17:40:52 +01:00
|
|
|
"use strict";
|
2017-07-10 21:14:48 +02:00
|
|
|
const { Backbone, utils, Strophe, $iq, b64_sha1, sizzle, _ } = converse.env;
|
2016-09-23 10:54:55 +02:00
|
|
|
|
2017-04-21 17:52:54 +02:00
|
|
|
|
2017-02-03 19:12:19 +01:00
|
|
|
converse.plugins.add('converse-rosterview', {
|
2016-03-13 17:40:52 +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-07-10 17:46:22 +02:00
|
|
|
afterReconnected () {
|
2016-08-31 12:06:17 +02:00
|
|
|
this.__super__.afterReconnected.apply(this, arguments);
|
2016-06-21 17:06:51 +02:00
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
_tearDown () {
|
2016-12-06 12:06:21 +01:00
|
|
|
/* Remove the rosterview when tearing down. It gets created
|
|
|
|
* anew when reconnecting or logging in.
|
|
|
|
*/
|
|
|
|
this.__super__._tearDown.apply(this, arguments);
|
|
|
|
if (!_.isUndefined(this.rosterview)) {
|
|
|
|
this.rosterview.remove();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-06-21 17:06:51 +02:00
|
|
|
RosterGroups: {
|
2017-07-10 17:46:22 +02:00
|
|
|
comparator () {
|
2016-07-18 13:34:31 +02:00
|
|
|
// RosterGroupsComparator only gets set later (once i18n is
|
|
|
|
// set up), so we need to wrap it in this nameless function.
|
2017-07-10 17:46:22 +02:00
|
|
|
const { _converse } = this.__super__;
|
2016-12-20 10:30:20 +01:00
|
|
|
return _converse.RosterGroupsComparator.apply(this, arguments);
|
2016-06-21 17:06:51 +02:00
|
|
|
}
|
2016-03-13 17:40:52 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
initialize () {
|
2016-03-13 17:40:52 +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,
|
2017-09-24 20:36:39 +02:00
|
|
|
{ __ } = _converse;
|
2016-12-20 11:42:20 +01:00
|
|
|
|
2017-07-05 11:03:13 +02:00
|
|
|
_converse.api.settings.update({
|
2017-02-02 14:57:17 +01:00
|
|
|
allow_chat_pending_contacts: true,
|
2016-03-17 13:24:26 +01:00
|
|
|
allow_contact_removal: true,
|
2016-03-13 17:40:52 +01:00
|
|
|
show_toolbar: true,
|
|
|
|
});
|
2017-08-16 12:31:17 +02:00
|
|
|
_converse.api.promises.add('rosterViewInitialized');
|
2016-03-13 17:40:52 +01:00
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
const STATUSES = {
|
2016-07-18 13:34:31 +02:00
|
|
|
'dnd': __('This contact is busy'),
|
|
|
|
'online': __('This contact is online'),
|
|
|
|
'offline': __('This contact is offline'),
|
|
|
|
'unavailable': __('This contact is unavailable'),
|
|
|
|
'xa': __('This contact is away for an extended period'),
|
|
|
|
'away': __('This contact is away')
|
|
|
|
};
|
2017-07-10 17:46:22 +02:00
|
|
|
const LABEL_CONTACTS = __('Contacts');
|
|
|
|
const LABEL_GROUPS = __('Groups');
|
|
|
|
const HEADER_CURRENT_CONTACTS = __('My contacts');
|
|
|
|
const HEADER_PENDING_CONTACTS = __('Pending contacts');
|
|
|
|
const HEADER_REQUESTING_CONTACTS = __('Contact requests');
|
|
|
|
const HEADER_UNGROUPED = __('Ungrouped');
|
|
|
|
const HEADER_WEIGHTS = {};
|
2016-07-18 13:34:31 +02:00
|
|
|
HEADER_WEIGHTS[HEADER_REQUESTING_CONTACTS] = 0;
|
|
|
|
HEADER_WEIGHTS[HEADER_CURRENT_CONTACTS] = 1;
|
|
|
|
HEADER_WEIGHTS[HEADER_UNGROUPED] = 2;
|
|
|
|
HEADER_WEIGHTS[HEADER_PENDING_CONTACTS] = 3;
|
|
|
|
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.RosterGroupsComparator = function (a, b) {
|
2016-07-18 13:34:31 +02:00
|
|
|
/* Groups are sorted alphabetically, ignoring case.
|
|
|
|
* However, Ungrouped, Requesting Contacts and Pending Contacts
|
|
|
|
* appear last and in that order.
|
|
|
|
*/
|
|
|
|
a = a.get('name');
|
|
|
|
b = b.get('name');
|
2017-07-10 17:46:22 +02:00
|
|
|
const special_groups = _.keys(HEADER_WEIGHTS);
|
|
|
|
const a_is_special = _.includes(special_groups, a);
|
|
|
|
const b_is_special = _.includes(special_groups, b);
|
2016-07-18 13:34:31 +02:00
|
|
|
if (!a_is_special && !b_is_special ) {
|
|
|
|
return a.toLowerCase() < b.toLowerCase() ? -1 : (a.toLowerCase() > b.toLowerCase() ? 1 : 0);
|
|
|
|
} else if (a_is_special && b_is_special) {
|
|
|
|
return HEADER_WEIGHTS[a] < HEADER_WEIGHTS[b] ? -1 : (HEADER_WEIGHTS[a] > HEADER_WEIGHTS[b] ? 1 : 0);
|
|
|
|
} else if (!a_is_special && b_is_special) {
|
|
|
|
return (b === HEADER_REQUESTING_CONTACTS) ? 1 : -1;
|
|
|
|
} else if (a_is_special && !b_is_special) {
|
|
|
|
return (a === HEADER_REQUESTING_CONTACTS) ? -1 : 1;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.RosterFilter = Backbone.Model.extend({
|
2017-07-10 17:46:22 +02:00
|
|
|
initialize () {
|
2016-04-02 13:30:54 +02:00
|
|
|
this.set({
|
|
|
|
'filter_text': '',
|
|
|
|
'filter_type': 'contacts',
|
|
|
|
'chat_state': ''
|
|
|
|
});
|
|
|
|
},
|
|
|
|
});
|
2016-03-13 17:40:52 +01:00
|
|
|
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.RosterFilterView = Backbone.View.extend({
|
2016-04-02 13:30:54 +02:00
|
|
|
tagName: 'span',
|
2016-03-13 17:40:52 +01:00
|
|
|
events: {
|
|
|
|
"keydown .roster-filter": "liveFilter",
|
2017-02-13 16:05:43 +01:00
|
|
|
"submit form.roster-filter-form": "submitFilter",
|
2016-03-13 17:40:52 +01:00
|
|
|
"click .onX": "clearFilter",
|
2016-04-02 13:30:54 +02:00
|
|
|
"mousemove .x": "toggleX",
|
|
|
|
"change .filter-type": "changeTypeFilter",
|
|
|
|
"change .state-type": "changeChatStateFilter"
|
2016-03-13 17:40:52 +01:00
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
initialize () {
|
2017-02-13 16:05:43 +01:00
|
|
|
this.model.on('change:filter_type', this.render, this);
|
2017-04-21 19:37:11 +02:00
|
|
|
this.model.on('change:filter_text', this.renderClearButton, this);
|
2016-04-02 13:30:54 +02:00
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
render () {
|
2017-03-30 16:45:52 +02:00
|
|
|
this.el.innerHTML = tpl_roster_filter(
|
2016-04-02 13:30:54 +02:00
|
|
|
_.extend(this.model.toJSON(), {
|
2016-04-07 12:35:07 +02:00
|
|
|
placeholder: __('Filter'),
|
2016-04-02 13:30:54 +02:00
|
|
|
label_contacts: LABEL_CONTACTS,
|
|
|
|
label_groups: LABEL_GROUPS,
|
|
|
|
label_state: __('State'),
|
|
|
|
label_any: __('Any'),
|
2017-04-20 21:25:58 +02:00
|
|
|
label_unread_messages: __('Unread'),
|
2016-04-02 13:30:54 +02:00
|
|
|
label_online: __('Online'),
|
2016-08-31 12:06:17 +02:00
|
|
|
label_chatty: __('Chatty'),
|
2016-04-02 13:30:54 +02:00
|
|
|
label_busy: __('Busy'),
|
|
|
|
label_away: __('Away'),
|
|
|
|
label_xa: __('Extended Away'),
|
|
|
|
label_offline: __('Offline')
|
2017-03-30 16:45:52 +02:00
|
|
|
}));
|
2017-02-13 16:05:43 +01:00
|
|
|
this.renderClearButton();
|
|
|
|
return this.$el;
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
renderClearButton () {
|
|
|
|
const roster_filter = this.el.querySelector('.roster-filter');
|
2017-04-21 19:37:11 +02:00
|
|
|
if (_.isNull(roster_filter)) {
|
|
|
|
return;
|
|
|
|
}
|
2017-04-06 23:21:52 +02:00
|
|
|
roster_filter.classList[this.tog(roster_filter.value)]('x');
|
2016-04-02 13:30:54 +02:00
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
tog (v) {
|
2017-04-06 23:21:52 +02:00
|
|
|
return v?'add':'remove';
|
2016-04-02 13:30:54 +02:00
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
toggleX (ev) {
|
2016-04-02 13:30:54 +02:00
|
|
|
if (ev && ev.preventDefault) { ev.preventDefault(); }
|
2017-07-10 17:46:22 +02:00
|
|
|
const el = ev.target;
|
2017-04-13 16:10:52 +02:00
|
|
|
el.classList[this.tog(el.offsetWidth-18 < ev.clientX-el.getBoundingClientRect().left)]('onX');
|
2016-04-02 13:30:54 +02:00
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
changeChatStateFilter (ev) {
|
2016-04-02 13:30:54 +02:00
|
|
|
if (ev && ev.preventDefault) { ev.preventDefault(); }
|
|
|
|
this.model.save({
|
2017-03-30 16:45:52 +02:00
|
|
|
'chat_state': this.el.querySelector('.state-type').value
|
2016-04-02 13:30:54 +02:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
changeTypeFilter (ev) {
|
2016-04-02 13:30:54 +02:00
|
|
|
if (ev && ev.preventDefault) { ev.preventDefault(); }
|
2017-07-10 17:46:22 +02:00
|
|
|
const type = ev.target.value;
|
2016-04-02 13:30:54 +02:00
|
|
|
if (type === 'state') {
|
|
|
|
this.model.save({
|
|
|
|
'filter_type': type,
|
2017-03-30 16:45:52 +02:00
|
|
|
'chat_state': this.el.querySelector('.state-type').value
|
2016-04-02 13:30:54 +02:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this.model.save({
|
|
|
|
'filter_type': type,
|
2017-03-30 16:45:52 +02:00
|
|
|
'filter_text': this.el.querySelector('.roster-filter').value
|
2016-04-02 13:30:54 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
liveFilter: _.debounce(function (ev) {
|
|
|
|
this.model.save({
|
2017-03-30 16:45:52 +02:00
|
|
|
'filter_type': this.el.querySelector('.filter-type').value,
|
|
|
|
'filter_text': this.el.querySelector('.roster-filter').value
|
2016-04-02 13:30:54 +02:00
|
|
|
});
|
|
|
|
}, 250),
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
submitFilter (ev) {
|
2017-02-13 16:05:43 +01:00
|
|
|
if (ev && ev.preventDefault) { ev.preventDefault(); }
|
|
|
|
this.liveFilter();
|
|
|
|
this.render();
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
isActive () {
|
2016-05-24 09:58:54 +02:00
|
|
|
/* Returns true if the filter is enabled (i.e. if the user
|
|
|
|
* has added values to the filter).
|
|
|
|
*/
|
|
|
|
if (this.model.get('filter_type') === 'state' ||
|
|
|
|
this.model.get('filter_text')) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
show () {
|
2016-04-02 13:30:54 +02:00
|
|
|
if (this.$el.is(':visible')) { return this; }
|
|
|
|
this.$el.show();
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
hide () {
|
2016-04-02 13:30:54 +02:00
|
|
|
if (!this.$el.is(':visible')) { return this; }
|
2017-03-30 16:45:52 +02:00
|
|
|
if (this.el.querySelector('.roster-filter').value.length > 0) {
|
2016-04-02 13:30:54 +02:00
|
|
|
// Don't hide if user is currently filtering.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.model.save({
|
|
|
|
'filter_text': '',
|
|
|
|
'chat_state': ''
|
|
|
|
});
|
|
|
|
this.$el.hide();
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
clearFilter (ev) {
|
2016-04-02 13:30:54 +02:00
|
|
|
if (ev && ev.preventDefault) {
|
|
|
|
ev.preventDefault();
|
|
|
|
$(ev.target).removeClass('x onX').val('');
|
|
|
|
}
|
|
|
|
this.model.save({
|
|
|
|
'filter_text': ''
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.RosterView = Backbone.Overview.extend({
|
2016-04-02 13:30:54 +02:00
|
|
|
tagName: 'div',
|
|
|
|
id: 'converse-roster',
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
initialize () {
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.roster.on("add", this.onContactAdd, this);
|
|
|
|
_converse.roster.on('change', this.onContactChange, this);
|
|
|
|
_converse.roster.on("destroy", this.update, this);
|
|
|
|
_converse.roster.on("remove", this.update, this);
|
2016-03-13 17:40:52 +01:00
|
|
|
this.model.on("add", this.onGroupAdd, this);
|
|
|
|
this.model.on("reset", this.reset, this);
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.on('rosterGroupsFetched', this.positionFetchedGroups, this);
|
|
|
|
_converse.on('rosterContactsFetched', this.update, this);
|
2016-09-21 15:00:09 +02:00
|
|
|
this.createRosterFilter();
|
2016-04-02 13:30:54 +02:00
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
render () {
|
2017-03-30 16:45:52 +02:00
|
|
|
this.renderRoster();
|
2016-04-02 13:30:54 +02:00
|
|
|
this.$el.html(this.filter_view.render());
|
2016-12-20 10:30:20 +01:00
|
|
|
if (!_converse.allow_contact_requests) {
|
2016-04-02 13:30:54 +02:00
|
|
|
// XXX: if we ever support live editing of config then
|
|
|
|
// we'll need to be able to remove this class on the fly.
|
2017-03-30 16:45:52 +02:00
|
|
|
this.el.classList.add('no-contact-requests');
|
2016-04-02 13:30:54 +02:00
|
|
|
}
|
|
|
|
return this;
|
2016-03-13 17:40:52 +01:00
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
renderRoster () {
|
2017-03-30 16:45:52 +02:00
|
|
|
this.$roster = $(tpl_roster());
|
|
|
|
this.roster = this.$roster[0];
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
createRosterFilter () {
|
2016-09-21 15:00:09 +02:00
|
|
|
// Create a model on which we can store filter properties
|
2017-07-10 17:46:22 +02:00
|
|
|
const model = new _converse.RosterFilter();
|
|
|
|
model.id = b64_sha1(`_converse.rosterfilter${_converse.bare_jid}`);
|
2016-09-21 15:00:09 +02:00
|
|
|
model.browserStorage = new Backbone.BrowserStorage.local(this.filter.id);
|
2016-12-20 10:30:20 +01:00
|
|
|
this.filter_view = new _converse.RosterFilterView({'model': model});
|
2016-09-21 15:00:09 +02:00
|
|
|
this.filter_view.model.on('change', this.updateFilter, this);
|
|
|
|
this.filter_view.model.fetch();
|
|
|
|
},
|
|
|
|
|
2016-04-02 13:30:54 +02:00
|
|
|
updateFilter: _.debounce(function () {
|
|
|
|
/* Filter the roster again.
|
|
|
|
* Called whenever the filter settings have been changed or
|
|
|
|
* when contacts have been added, removed or changed.
|
|
|
|
*
|
|
|
|
* Debounced so that it doesn't get called for every
|
|
|
|
* contact fetched from browser storage.
|
|
|
|
*/
|
2017-07-10 17:46:22 +02:00
|
|
|
const type = this.filter_view.model.get('filter_type');
|
2016-04-02 13:30:54 +02:00
|
|
|
if (type === 'state') {
|
|
|
|
this.filter(this.filter_view.model.get('chat_state'), type);
|
|
|
|
} else {
|
|
|
|
this.filter(this.filter_view.model.get('filter_text'), type);
|
|
|
|
}
|
|
|
|
}, 100),
|
|
|
|
|
|
|
|
update: _.debounce(function () {
|
2017-03-30 16:45:52 +02:00
|
|
|
if (_.isNull(this.roster.parentElement)) {
|
2016-03-13 17:40:52 +01:00
|
|
|
this.$el.append(this.$roster.show());
|
|
|
|
}
|
|
|
|
return this.showHideFilter();
|
2016-12-20 10:30:20 +01:00
|
|
|
}, _converse.animate ? 100 : 0),
|
2016-03-13 17:40:52 +01:00
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
showHideFilter () {
|
2016-04-02 13:30:54 +02:00
|
|
|
if (!this.$el.is(':visible')) {
|
|
|
|
return;
|
|
|
|
}
|
2017-07-21 18:47:43 +02:00
|
|
|
if (_converse.roster.length >= 10) {
|
2016-04-02 13:30:54 +02:00
|
|
|
this.filter_view.show();
|
2016-05-24 09:58:54 +02:00
|
|
|
} else if (!this.filter_view.isActive()) {
|
2016-04-02 13:30:54 +02:00
|
|
|
this.filter_view.hide();
|
2016-03-13 17:40:52 +01:00
|
|
|
}
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
filter (query, type) {
|
2016-04-02 13:30:54 +02:00
|
|
|
// First we make sure the filter is restored to its
|
|
|
|
// original state
|
|
|
|
_.each(this.getAll(), function (view) {
|
|
|
|
if (view.model.contacts.length > 0) {
|
|
|
|
view.show().filter('');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
// Now we can filter
|
2016-03-13 17:40:52 +01:00
|
|
|
query = query.toLowerCase();
|
|
|
|
if (type === 'groups') {
|
|
|
|
_.each(this.getAll(), function (view, idx) {
|
2017-01-26 15:49:02 +01:00
|
|
|
if (!_.includes(view.model.get('name').toLowerCase(), query.toLowerCase())) {
|
2016-03-13 17:40:52 +01:00
|
|
|
view.hide();
|
|
|
|
} else if (view.model.contacts.length > 0) {
|
|
|
|
view.show();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
_.each(this.getAll(), function (view) {
|
|
|
|
view.filter(query, type);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
reset () {
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.roster.reset();
|
2016-03-13 17:40:52 +01:00
|
|
|
this.removeAll();
|
2017-03-30 16:45:52 +02:00
|
|
|
this.renderRoster();
|
2016-03-13 17:40:52 +01:00
|
|
|
this.render().update();
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
onGroupAdd (group) {
|
|
|
|
const view = new _converse.RosterGroupView({model: group});
|
2016-03-13 17:40:52 +01:00
|
|
|
this.add(group.get('name'), view.render());
|
|
|
|
this.positionGroup(view);
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
onContactAdd (contact) {
|
2016-03-13 17:40:52 +01:00
|
|
|
this.addRosterContact(contact).update();
|
2016-04-02 13:30:54 +02:00
|
|
|
this.updateFilter();
|
2016-03-13 17:40:52 +01:00
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
onContactChange (contact) {
|
2016-03-13 17:40:52 +01:00
|
|
|
this.updateChatBox(contact).update();
|
|
|
|
if (_.has(contact.changed, 'subscription')) {
|
|
|
|
if (contact.changed.subscription === 'from') {
|
|
|
|
this.addContactToGroup(contact, HEADER_PENDING_CONTACTS);
|
2017-01-26 15:49:02 +01:00
|
|
|
} else if (_.includes(['both', 'to'], contact.get('subscription'))) {
|
2016-03-13 17:40:52 +01:00
|
|
|
this.addExistingContact(contact);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (_.has(contact.changed, 'ask') && contact.changed.ask === 'subscribe') {
|
|
|
|
this.addContactToGroup(contact, HEADER_PENDING_CONTACTS);
|
|
|
|
}
|
|
|
|
if (_.has(contact.changed, 'subscription') && contact.changed.requesting === 'true') {
|
|
|
|
this.addContactToGroup(contact, HEADER_REQUESTING_CONTACTS);
|
|
|
|
}
|
2016-04-02 13:30:54 +02:00
|
|
|
this.updateFilter();
|
2016-03-13 17:40:52 +01:00
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
updateChatBox (contact) {
|
|
|
|
const chatbox = _converse.chatboxes.get(contact.get('jid')),
|
2016-03-13 17:40:52 +01:00
|
|
|
changes = {};
|
|
|
|
if (!chatbox) {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
if (_.has(contact.changed, 'chat_status')) {
|
|
|
|
changes.chat_status = contact.get('chat_status');
|
|
|
|
}
|
|
|
|
if (_.has(contact.changed, 'status')) {
|
|
|
|
changes.status = contact.get('status');
|
|
|
|
}
|
|
|
|
chatbox.save(changes);
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
positionFetchedGroups () {
|
2016-03-13 17:40:52 +01:00
|
|
|
/* Instead of throwing an add event for each group
|
2016-03-19 23:16:00 +01:00
|
|
|
* fetched, we wait until they're all fetched and then
|
|
|
|
* we position them.
|
|
|
|
* Works around the problem of positionGroup not
|
|
|
|
* working when all groups besides the one being
|
|
|
|
* positioned aren't already in inserted into the
|
|
|
|
* roster DOM element.
|
|
|
|
*/
|
2017-07-10 17:46:22 +02:00
|
|
|
const that = this;
|
2016-09-21 15:00:09 +02:00
|
|
|
this.model.sort();
|
|
|
|
this.model.each(function (group, idx) {
|
2017-07-10 17:46:22 +02:00
|
|
|
let view = that.get(group.get('name'));
|
2016-03-13 17:40:52 +01:00
|
|
|
if (!view) {
|
2016-12-20 10:30:20 +01:00
|
|
|
view = new _converse.RosterGroupView({model: group});
|
2016-12-19 08:09:31 +01:00
|
|
|
that.add(group.get('name'), view.render());
|
2016-03-13 17:40:52 +01:00
|
|
|
}
|
|
|
|
if (idx === 0) {
|
2016-12-19 08:09:31 +01:00
|
|
|
that.$roster.append(view.$el);
|
2016-03-13 17:40:52 +01:00
|
|
|
} else {
|
2016-12-19 08:09:31 +01:00
|
|
|
that.appendGroup(view);
|
2016-03-13 17:40:52 +01:00
|
|
|
}
|
2016-12-19 08:09:31 +01:00
|
|
|
});
|
2016-03-13 17:40:52 +01:00
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
positionGroup (view) {
|
2016-03-13 17:40:52 +01:00
|
|
|
/* Place the group's DOM element in the correct alphabetical
|
2016-03-19 23:16:00 +01:00
|
|
|
* position amongst the other groups in the roster.
|
|
|
|
*/
|
2017-07-10 17:46:22 +02:00
|
|
|
const $groups = this.$roster.find('.roster-group'),
|
2016-03-13 17:40:52 +01:00
|
|
|
index = $groups.length ? this.model.indexOf(view.model) : 0;
|
|
|
|
if (index === 0) {
|
|
|
|
this.$roster.prepend(view.$el);
|
|
|
|
} else if (index === (this.model.length-1)) {
|
|
|
|
this.appendGroup(view);
|
|
|
|
} else {
|
|
|
|
$($groups.eq(index)).before(view.$el);
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
appendGroup (view) {
|
2016-03-13 17:40:52 +01:00
|
|
|
/* Add the group at the bottom of the roster
|
2016-03-19 23:16:00 +01:00
|
|
|
*/
|
2017-07-10 17:46:22 +02:00
|
|
|
const $last = this.$roster.find('.roster-group').last();
|
|
|
|
const $siblings = $last.siblings('dd');
|
2016-03-13 17:40:52 +01:00
|
|
|
if ($siblings.length > 0) {
|
|
|
|
$siblings.last().after(view.$el);
|
|
|
|
} else {
|
|
|
|
$last.after(view.$el);
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
getGroup (name) {
|
2016-03-13 17:40:52 +01:00
|
|
|
/* Returns the group as specified by name.
|
2016-03-19 23:16:00 +01:00
|
|
|
* Creates the group if it doesn't exist.
|
|
|
|
*/
|
2017-07-10 17:46:22 +02:00
|
|
|
const view = this.get(name);
|
2016-03-13 17:40:52 +01:00
|
|
|
if (view) {
|
|
|
|
return view.model;
|
|
|
|
}
|
2017-07-10 17:46:22 +02:00
|
|
|
return this.model.create({name, id: b64_sha1(name)});
|
2016-03-13 17:40:52 +01:00
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
addContactToGroup (contact, name) {
|
2016-03-13 17:40:52 +01:00
|
|
|
this.getGroup(name).contacts.add(contact);
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
addExistingContact (contact) {
|
|
|
|
let groups;
|
2016-12-20 10:30:20 +01:00
|
|
|
if (_converse.roster_groups) {
|
2016-03-13 17:40:52 +01:00
|
|
|
groups = contact.get('groups');
|
|
|
|
if (groups.length === 0) {
|
|
|
|
groups = [HEADER_UNGROUPED];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
groups = [HEADER_CURRENT_CONTACTS];
|
|
|
|
}
|
|
|
|
_.each(groups, _.bind(this.addContactToGroup, this, contact));
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
addRosterContact (contact) {
|
2016-03-13 17:40:52 +01:00
|
|
|
if (contact.get('subscription') === 'both' || contact.get('subscription') === 'to') {
|
|
|
|
this.addExistingContact(contact);
|
|
|
|
} else {
|
|
|
|
if ((contact.get('ask') === 'subscribe') || (contact.get('subscription') === 'from')) {
|
|
|
|
this.addContactToGroup(contact, HEADER_PENDING_CONTACTS);
|
|
|
|
} else if (contact.get('requesting') === true) {
|
|
|
|
this.addContactToGroup(contact, HEADER_REQUESTING_CONTACTS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.RosterContactView = Backbone.View.extend({
|
2016-03-13 17:40:52 +01:00
|
|
|
tagName: 'dd',
|
|
|
|
|
|
|
|
events: {
|
|
|
|
"click .accept-xmpp-request": "acceptRequest",
|
|
|
|
"click .decline-xmpp-request": "declineRequest",
|
|
|
|
"click .open-chat": "openChat",
|
|
|
|
"click .remove-xmpp-contact": "removeContact"
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
initialize () {
|
2016-03-13 17:40:52 +01:00
|
|
|
this.model.on("change", this.render, this);
|
|
|
|
this.model.on("remove", this.remove, this);
|
|
|
|
this.model.on("destroy", this.remove, this);
|
|
|
|
this.model.on("open", this.openChat, this);
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
render () {
|
|
|
|
const that = this;
|
2016-04-02 13:30:54 +02:00
|
|
|
if (!this.mayBeShown()) {
|
2016-03-13 17:40:52 +01:00
|
|
|
this.$el.hide();
|
|
|
|
return this;
|
|
|
|
}
|
2017-07-10 17:46:22 +02:00
|
|
|
const item = this.model,
|
2016-03-13 17:40:52 +01:00
|
|
|
ask = item.get('ask'),
|
|
|
|
chat_status = item.get('chat_status'),
|
|
|
|
requesting = item.get('requesting'),
|
|
|
|
subscription = item.get('subscription');
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
const classes_to_remove = [
|
2016-03-13 17:40:52 +01:00
|
|
|
'current-xmpp-contact',
|
|
|
|
'pending-xmpp-contact',
|
|
|
|
'requesting-xmpp-contact'
|
|
|
|
].concat(_.keys(STATUSES));
|
|
|
|
|
|
|
|
_.each(classes_to_remove,
|
|
|
|
function (cls) {
|
2017-01-26 15:49:02 +01:00
|
|
|
if (_.includes(that.el.className, cls)) {
|
2017-03-30 16:45:52 +02:00
|
|
|
that.el.classList.remove(cls);
|
2016-03-13 17:40:52 +01:00
|
|
|
}
|
2017-01-26 15:49:02 +01:00
|
|
|
});
|
2016-03-13 17:40:52 +01:00
|
|
|
this.$el.addClass(chat_status).data('status', chat_status);
|
|
|
|
|
|
|
|
if ((ask === 'subscribe') || (subscription === 'from')) {
|
|
|
|
/* ask === 'subscribe'
|
2016-03-19 23:16:00 +01:00
|
|
|
* Means we have asked to subscribe to them.
|
|
|
|
*
|
|
|
|
* subscription === 'from'
|
|
|
|
* They are subscribed to use, but not vice versa.
|
|
|
|
* We assume that there is a pending subscription
|
|
|
|
* from us to them (otherwise we're in a state not
|
|
|
|
* supported by converse.js).
|
|
|
|
*
|
|
|
|
* So in both cases the user is a "pending" contact.
|
|
|
|
*/
|
2017-03-30 16:45:52 +02:00
|
|
|
this.el.classList.add('pending-xmpp-contact');
|
2017-02-19 10:58:30 +01:00
|
|
|
this.$el.html(tpl_pending_contact(
|
2016-03-13 17:40:52 +01:00
|
|
|
_.extend(item.toJSON(), {
|
2017-09-24 20:36:39 +02:00
|
|
|
'desc_remove': __('Click to remove %1$s as a contact', item.get('fullname')),
|
2016-12-20 10:30:20 +01:00
|
|
|
'allow_chat_pending_contacts': _converse.allow_chat_pending_contacts
|
2016-03-13 17:40:52 +01:00
|
|
|
})
|
|
|
|
));
|
|
|
|
} else if (requesting === true) {
|
2017-03-30 16:45:52 +02:00
|
|
|
this.el.classList.add('requesting-xmpp-contact');
|
2017-02-19 10:58:30 +01:00
|
|
|
this.$el.html(tpl_requesting_contact(
|
2016-03-13 17:40:52 +01:00
|
|
|
_.extend(item.toJSON(), {
|
2017-09-24 20:36:39 +02:00
|
|
|
'desc_accept': __("Click to accept the contact request from %1$s", item.get('fullname')),
|
|
|
|
'desc_decline': __("Click to decline the contact request from %1$s", item.get('fullname')),
|
2016-12-20 10:30:20 +01:00
|
|
|
'allow_chat_pending_contacts': _converse.allow_chat_pending_contacts
|
2016-03-13 17:40:52 +01:00
|
|
|
})
|
|
|
|
));
|
|
|
|
} else if (subscription === 'both' || subscription === 'to') {
|
2017-03-30 16:45:52 +02:00
|
|
|
this.el.classList.add('current-xmpp-contact');
|
2017-06-12 20:30:58 +02:00
|
|
|
this.el.classList.remove(_.without(['both', 'to'], subscription)[0]);
|
2017-04-20 21:25:58 +02:00
|
|
|
this.el.classList.add(subscription);
|
|
|
|
this.renderRosterItem(item);
|
2016-03-13 17:40:52 +01:00
|
|
|
}
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
renderRosterItem (item) {
|
|
|
|
const chat_status = item.get('chat_status');
|
2017-04-20 21:25:58 +02:00
|
|
|
this.$el.html(tpl_roster_item(
|
|
|
|
_.extend(item.toJSON(), {
|
|
|
|
'desc_status': STATUSES[chat_status||'offline'],
|
|
|
|
'desc_chat': __('Click to chat with this contact'),
|
2017-09-24 20:36:39 +02:00
|
|
|
'desc_remove': __('Click to remove %1$s as a contact', item.get('fullname')),
|
2017-04-20 21:25:58 +02:00
|
|
|
'title_fullname': __('Name'),
|
|
|
|
'allow_contact_removal': _converse.allow_contact_removal,
|
|
|
|
'num_unread': item.get('num_unread') || 0
|
|
|
|
})
|
|
|
|
));
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
isGroupCollapsed () {
|
2016-04-02 13:30:54 +02:00
|
|
|
/* Check whether the group in which this contact appears is
|
|
|
|
* collapsed.
|
|
|
|
*/
|
|
|
|
// XXX: this sucks and is fragile.
|
|
|
|
// It's because I tried to do the "right thing"
|
|
|
|
// and use definition lists to represent roster groups.
|
|
|
|
// If roster group items were inside the group elements, we
|
|
|
|
// would simplify things by not having to check whether the
|
|
|
|
// group is collapsed or not.
|
2017-07-10 17:46:22 +02:00
|
|
|
const name = this.$el.prevAll('dt:first').data('group');
|
|
|
|
const group = _.head(_converse.rosterview.model.where({'name': name.toString()}));
|
2016-12-20 10:30:20 +01:00
|
|
|
if (group.get('state') === _converse.CLOSED) {
|
2016-04-02 13:30:54 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
mayBeShown () {
|
2016-04-02 13:30:54 +02:00
|
|
|
/* Return a boolean indicating whether this contact should
|
|
|
|
* generally be visible in the roster.
|
|
|
|
*
|
|
|
|
* It doesn't check for the more specific case of whether
|
|
|
|
* the group it's in is collapsed (see isGroupCollapsed).
|
|
|
|
*/
|
2017-07-10 17:46:22 +02:00
|
|
|
const chatStatus = this.model.get('chat_status');
|
2016-12-20 10:30:20 +01:00
|
|
|
if ((_converse.show_only_online_users && chatStatus !== 'online') ||
|
|
|
|
(_converse.hide_offline_users && chatStatus === 'offline')) {
|
2016-04-02 13:30:54 +02:00
|
|
|
// If pending or requesting, show
|
|
|
|
if ((this.model.get('ask') === 'subscribe') ||
|
|
|
|
(this.model.get('subscription') === 'from') ||
|
|
|
|
(this.model.get('requesting') === true)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
openChat (ev) {
|
2016-03-13 17:40:52 +01:00
|
|
|
if (ev && ev.preventDefault) { ev.preventDefault(); }
|
2017-04-21 19:52:27 +02:00
|
|
|
return _converse.chatboxviews.showChat(this.model.attributes, true);
|
2016-03-13 17:40:52 +01:00
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
removeContact (ev) {
|
2016-03-13 17:40:52 +01:00
|
|
|
if (ev && ev.preventDefault) { ev.preventDefault(); }
|
2016-12-20 10:30:20 +01:00
|
|
|
if (!_converse.allow_contact_removal) { return; }
|
2017-07-10 17:46:22 +02:00
|
|
|
const result = confirm(__("Are you sure you want to remove this contact?"));
|
2016-03-13 17:40:52 +01:00
|
|
|
if (result === true) {
|
2017-07-10 17:46:22 +02:00
|
|
|
const iq = $iq({type: 'set'})
|
2016-03-13 17:40:52 +01:00
|
|
|
.c('query', {xmlns: Strophe.NS.ROSTER})
|
|
|
|
.c('item', {jid: this.model.get('jid'), subscription: "remove"});
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.connection.sendIQ(iq,
|
2017-07-10 17:46:22 +02:00
|
|
|
(iq) => {
|
2016-03-13 17:40:52 +01:00
|
|
|
this.model.destroy();
|
|
|
|
this.remove();
|
2017-07-10 17:46:22 +02:00
|
|
|
},
|
2016-03-13 17:40:52 +01:00
|
|
|
function (err) {
|
2017-08-23 11:57:17 +02:00
|
|
|
alert(__('Sorry, there was an error while trying to remove %1$s as a contact.', name));
|
2017-07-05 11:33:55 +02:00
|
|
|
_converse.log(err, Strophe.LogLevel.ERROR);
|
2016-03-13 17:40:52 +01:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
acceptRequest (ev) {
|
2016-03-13 17:40:52 +01:00
|
|
|
if (ev && ev.preventDefault) { ev.preventDefault(); }
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.roster.sendContactAddIQ(
|
2016-03-13 17:40:52 +01:00
|
|
|
this.model.get('jid'),
|
|
|
|
this.model.get('fullname'),
|
|
|
|
[],
|
2017-07-10 17:46:22 +02:00
|
|
|
() => { this.model.authorize().subscribe(); }
|
2016-03-13 17:40:52 +01:00
|
|
|
);
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
declineRequest (ev) {
|
2016-03-13 17:40:52 +01:00
|
|
|
if (ev && ev.preventDefault) { ev.preventDefault(); }
|
2017-07-10 17:46:22 +02:00
|
|
|
const result = confirm(__("Are you sure you want to decline this contact request?"));
|
2016-03-13 17:40:52 +01:00
|
|
|
if (result === true) {
|
|
|
|
this.model.unauthorize().destroy();
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.RosterGroupView = Backbone.Overview.extend({
|
2016-03-13 17:40:52 +01:00
|
|
|
tagName: 'dt',
|
|
|
|
className: 'roster-group',
|
|
|
|
events: {
|
|
|
|
"click a.group-toggle": "toggle"
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
initialize () {
|
2016-03-13 17:40:52 +01:00
|
|
|
this.model.contacts.on("add", this.addContact, this);
|
|
|
|
this.model.contacts.on("change:subscription", this.onContactSubscriptionChange, this);
|
|
|
|
this.model.contacts.on("change:requesting", this.onContactRequestChange, this);
|
|
|
|
this.model.contacts.on("change:chat_status", function (contact) {
|
|
|
|
// This might be optimized by instead of first sorting,
|
|
|
|
// finding the correct position in positionContact
|
|
|
|
this.model.contacts.sort();
|
|
|
|
this.positionContact(contact).render();
|
|
|
|
}, this);
|
|
|
|
this.model.contacts.on("destroy", this.onRemove, this);
|
|
|
|
this.model.contacts.on("remove", this.onRemove, this);
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.roster.on('change:groups', this.onContactGroupChange, this);
|
2016-03-13 17:40:52 +01:00
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
render () {
|
2017-03-30 16:45:52 +02:00
|
|
|
this.el.setAttribute('data-group', this.model.get('name'));
|
2017-07-10 17:46:22 +02:00
|
|
|
const html = tpl_group_header({
|
2017-04-21 17:52:54 +02:00
|
|
|
label_group: this.model.get('name'),
|
|
|
|
desc_group_toggle: this.model.get('description'),
|
|
|
|
toggle_state: this.model.get('state')
|
|
|
|
});
|
|
|
|
this.el.innerHTML = html;
|
2016-03-13 17:40:52 +01:00
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
addContact (contact) {
|
|
|
|
let view = new _converse.RosterContactView({model: contact});
|
2016-03-13 17:40:52 +01:00
|
|
|
this.add(contact.get('id'), view);
|
|
|
|
view = this.positionContact(contact).render();
|
2016-04-02 13:30:54 +02:00
|
|
|
if (view.mayBeShown()) {
|
2016-12-20 10:30:20 +01:00
|
|
|
if (this.model.get('state') === _converse.CLOSED) {
|
2016-03-13 17:40:52 +01:00
|
|
|
if (view.$el[0].style.display !== "none") { view.$el.hide(); }
|
|
|
|
if (!this.$el.is(':visible')) { this.$el.show(); }
|
|
|
|
} else {
|
|
|
|
if (this.$el[0].style.display !== "block") { this.show(); }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
positionContact (contact) {
|
2016-03-13 17:40:52 +01:00
|
|
|
/* Place the contact's DOM element in the correct alphabetical
|
2016-03-19 23:16:00 +01:00
|
|
|
* position amongst the other contacts in this group.
|
|
|
|
*/
|
2017-07-10 17:46:22 +02:00
|
|
|
const view = this.get(contact.get('id'));
|
|
|
|
const index = this.model.contacts.indexOf(contact);
|
2016-03-13 17:40:52 +01:00
|
|
|
view.$el.detach();
|
|
|
|
if (index === 0) {
|
|
|
|
this.$el.after(view.$el);
|
|
|
|
} else if (index === (this.model.contacts.length-1)) {
|
|
|
|
this.$el.nextUntil('dt').last().after(view.$el);
|
|
|
|
} else {
|
|
|
|
this.$el.nextUntil('dt').eq(index).before(view.$el);
|
|
|
|
}
|
|
|
|
return view;
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
show () {
|
2016-03-13 17:40:52 +01:00
|
|
|
this.$el.show();
|
2016-04-02 13:30:54 +02:00
|
|
|
_.each(this.getAll(), function (view) {
|
|
|
|
if (view.mayBeShown() && !view.isGroupCollapsed()) {
|
|
|
|
view.$el.show();
|
2016-03-13 17:40:52 +01:00
|
|
|
}
|
|
|
|
});
|
2016-04-02 13:30:54 +02:00
|
|
|
return this;
|
2016-03-13 17:40:52 +01:00
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
hide () {
|
2016-03-13 17:40:52 +01:00
|
|
|
this.$el.nextUntil('dt').addBack().hide();
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
filter (q, type) {
|
2016-03-13 17:40:52 +01:00
|
|
|
/* Filter the group's contacts based on the query "q".
|
2016-03-19 23:16:00 +01:00
|
|
|
* The query is matched against the contact's full name.
|
|
|
|
* If all contacts are filtered out (i.e. hidden), then the
|
|
|
|
* group must be filtered out as well.
|
|
|
|
*/
|
2017-07-10 17:46:22 +02:00
|
|
|
let matches;
|
2016-03-13 17:40:52 +01:00
|
|
|
if (q.length === 0) {
|
2016-12-20 10:30:20 +01:00
|
|
|
if (this.model.get('state') === _converse.OPENED) {
|
2017-07-10 17:46:22 +02:00
|
|
|
this.model.contacts.each(
|
|
|
|
(item) => {
|
|
|
|
const view = this.get(item.get('id'));
|
|
|
|
if (view.mayBeShown() && !view.isGroupCollapsed()) {
|
|
|
|
view.$el.show();
|
|
|
|
}
|
2016-03-13 17:40:52 +01:00
|
|
|
}
|
2017-07-10 17:46:22 +02:00
|
|
|
);
|
2016-03-13 17:40:52 +01:00
|
|
|
}
|
|
|
|
this.showIfNecessary();
|
|
|
|
} else {
|
|
|
|
q = q.toLowerCase();
|
2016-04-02 13:30:54 +02:00
|
|
|
if (type === 'state') {
|
2016-07-18 10:41:06 +02:00
|
|
|
if (this.model.get('name') === HEADER_REQUESTING_CONTACTS) {
|
|
|
|
// When filtering by chat state, we still want to
|
|
|
|
// show requesting contacts, even though they don't
|
|
|
|
// have the state in question.
|
|
|
|
matches = this.model.contacts.filter(
|
2017-07-10 17:46:22 +02:00
|
|
|
(contact) => utils.contains.not('chat_status', q)(contact) && !contact.get('requesting')
|
2016-07-18 10:41:06 +02:00
|
|
|
);
|
2017-04-20 21:25:58 +02:00
|
|
|
} else if (q === 'unread_messages') {
|
|
|
|
matches = this.model.contacts.filter({'num_unread': 0});
|
2016-07-18 10:41:06 +02:00
|
|
|
} else {
|
|
|
|
matches = this.model.contacts.filter(
|
|
|
|
utils.contains.not('chat_status', q)
|
|
|
|
);
|
|
|
|
}
|
2016-04-02 13:30:54 +02:00
|
|
|
} else {
|
|
|
|
matches = this.model.contacts.filter(
|
|
|
|
utils.contains.not('fullname', q)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (matches.length === this.model.contacts.length) {
|
|
|
|
// hide the whole group
|
2016-03-13 17:40:52 +01:00
|
|
|
this.hide();
|
|
|
|
} else {
|
2017-07-10 17:46:22 +02:00
|
|
|
_.each(matches, (item) => {
|
2016-03-13 17:40:52 +01:00
|
|
|
this.get(item.get('id')).$el.hide();
|
2017-07-10 17:46:22 +02:00
|
|
|
});
|
2017-07-21 18:48:55 +02:00
|
|
|
if (this.model.get('state') === _converse.OPENED) {
|
|
|
|
_.each(this.model.contacts.reject(
|
|
|
|
utils.contains.not('fullname', q)),
|
|
|
|
(item) => {
|
|
|
|
this.get(item.get('id')).$el.show();
|
|
|
|
});
|
|
|
|
}
|
2016-03-13 17:40:52 +01:00
|
|
|
this.showIfNecessary();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
showIfNecessary () {
|
2016-03-13 17:40:52 +01:00
|
|
|
if (!this.$el.is(':visible') && this.model.contacts.length > 0) {
|
|
|
|
this.$el.show();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
toggle (ev) {
|
2016-03-13 17:40:52 +01:00
|
|
|
if (ev && ev.preventDefault) { ev.preventDefault(); }
|
2017-07-10 17:46:22 +02:00
|
|
|
const $el = $(ev.target);
|
2016-03-13 17:40:52 +01:00
|
|
|
if ($el.hasClass("icon-opened")) {
|
|
|
|
this.$el.nextUntil('dt').slideUp();
|
2016-12-20 10:30:20 +01:00
|
|
|
this.model.save({state: _converse.CLOSED});
|
2016-03-13 17:40:52 +01:00
|
|
|
$el.removeClass("icon-opened").addClass("icon-closed");
|
|
|
|
} else {
|
|
|
|
$el.removeClass("icon-closed").addClass("icon-opened");
|
2016-12-20 10:30:20 +01:00
|
|
|
this.model.save({state: _converse.OPENED});
|
2016-03-13 17:40:52 +01:00
|
|
|
this.filter(
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.rosterview.$('.roster-filter').val() || '',
|
|
|
|
_converse.rosterview.$('.filter-type').val()
|
2016-03-13 17:40:52 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
onContactGroupChange (contact) {
|
|
|
|
const in_this_group = _.includes(contact.get('groups'), this.model.get('name'));
|
|
|
|
const cid = contact.get('id');
|
|
|
|
const in_this_overview = !this.get(cid);
|
2016-03-13 17:40:52 +01:00
|
|
|
if (in_this_group && !in_this_overview) {
|
|
|
|
this.model.contacts.remove(cid);
|
|
|
|
} else if (!in_this_group && in_this_overview) {
|
|
|
|
this.addContact(contact);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
onContactSubscriptionChange (contact) {
|
2016-03-13 17:40:52 +01:00
|
|
|
if ((this.model.get('name') === HEADER_PENDING_CONTACTS) && contact.get('subscription') !== 'from') {
|
|
|
|
this.model.contacts.remove(contact.get('id'));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
onContactRequestChange (contact) {
|
2016-03-13 17:40:52 +01:00
|
|
|
if ((this.model.get('name') === HEADER_REQUESTING_CONTACTS) && !contact.get('requesting')) {
|
2016-07-28 12:35:43 +02:00
|
|
|
/* We suppress events, otherwise the remove event will
|
|
|
|
* also cause the contact's view to be removed from the
|
|
|
|
* "Pending Contacts" group.
|
|
|
|
*/
|
|
|
|
this.model.contacts.remove(contact.get('id'), {'silent': true});
|
|
|
|
// Since we suppress events, we make sure the view and
|
|
|
|
// contact are removed from this group.
|
|
|
|
this.get(contact.get('id')).remove();
|
|
|
|
this.onRemove(contact);
|
2016-03-13 17:40:52 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
onRemove (contact) {
|
2016-03-13 17:40:52 +01:00
|
|
|
this.remove(contact.get('id'));
|
|
|
|
if (this.model.contacts.length === 0) {
|
|
|
|
this.$el.hide();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2016-11-02 14:13:49 +01:00
|
|
|
|
|
|
|
/* -------- Event Handlers ----------- */
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
const onChatBoxMaximized = function (chatboxview) {
|
2017-04-21 11:33:01 +02:00
|
|
|
/* When a chat box gets maximized, the num_unread counter needs
|
2017-05-03 09:06:28 +02:00
|
|
|
* to be cleared, but if chatbox is scrolled up, then num_unread should not be cleared.
|
2017-04-21 11:33:01 +02:00
|
|
|
*/
|
2017-07-10 17:46:22 +02:00
|
|
|
const chatbox = chatboxview.model;
|
2017-05-15 19:08:08 +02:00
|
|
|
if (chatbox.get('type') !== 'chatroom') {
|
2017-07-10 17:46:22 +02:00
|
|
|
const contact = _.head(_converse.roster.where({'jid': chatbox.get('jid')}));
|
2017-05-03 09:06:28 +02:00
|
|
|
if (!_.isUndefined(contact) && !chatbox.isScrolledUp()) {
|
2017-04-21 11:33:01 +02:00
|
|
|
contact.save({'num_unread': 0});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
const onMessageReceived = function (data) {
|
2017-04-20 21:25:58 +02:00
|
|
|
/* Given a newly received message, update the unread counter on
|
2017-05-16 09:21:51 +02:00
|
|
|
* the relevant roster contact.
|
2017-04-20 21:25:58 +02:00
|
|
|
*/
|
2017-07-10 17:46:22 +02:00
|
|
|
const { chatbox } = data;
|
2017-04-20 21:25:58 +02:00
|
|
|
if (_.isUndefined(chatbox)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (_.isNull(data.stanza.querySelector('body'))) {
|
|
|
|
return; // The message has no text
|
|
|
|
}
|
2017-05-16 09:21:51 +02:00
|
|
|
if (chatbox.get('type') !== 'chatroom' &&
|
2017-06-14 18:41:45 +02:00
|
|
|
utils.isNewMessage(data.stanza) &&
|
2017-05-16 09:21:51 +02:00
|
|
|
chatbox.newMessageWillBeHidden()) {
|
2017-04-20 21:25:58 +02:00
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
const contact = _.head(_converse.roster.where({'jid': chatbox.get('jid')}));
|
2017-05-16 09:21:51 +02:00
|
|
|
if (!_.isUndefined(contact)) {
|
|
|
|
contact.save({'num_unread': contact.get('num_unread') + 1});
|
2017-04-20 21:25:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
const onChatBoxScrolledDown = function (data) {
|
|
|
|
const { chatbox } = data;
|
2017-05-03 09:06:28 +02:00
|
|
|
if (_.isUndefined(chatbox)) {
|
|
|
|
return;
|
|
|
|
}
|
2017-07-10 17:46:22 +02:00
|
|
|
const contact = _.head(_converse.roster.where({'jid': chatbox.get('jid')}));
|
2017-05-03 09:06:28 +02:00
|
|
|
if (!_.isUndefined(contact)) {
|
|
|
|
contact.save({'num_unread': 0});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
const initRoster = function () {
|
2016-11-02 14:13:49 +01:00
|
|
|
/* Create an instance of RosterView once the RosterGroups
|
|
|
|
* collection has been created (in converse-core.js)
|
|
|
|
*/
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.rosterview = new _converse.RosterView({
|
|
|
|
'model': _converse.rostergroups
|
2016-11-02 14:13:49 +01:00
|
|
|
});
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.rosterview.render();
|
2017-08-16 12:31:17 +02:00
|
|
|
_converse.emit('rosterViewInitialized');
|
2016-11-02 14:13:49 +01:00
|
|
|
};
|
2017-04-20 21:25:58 +02:00
|
|
|
_converse.api.listen.on('rosterInitialized', initRoster);
|
|
|
|
_converse.api.listen.on('rosterReadyAfterReconnection', initRoster);
|
|
|
|
_converse.api.listen.on('message', onMessageReceived);
|
2017-04-21 11:33:01 +02:00
|
|
|
_converse.api.listen.on('chatBoxMaximized', onChatBoxMaximized);
|
2017-05-03 09:06:28 +02:00
|
|
|
_converse.api.listen.on('chatBoxScrolledDown', onChatBoxScrolledDown);
|
2016-03-13 17:40:52 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}));
|