Commit 3984aadb authored by JC Brand's avatar JC Brand

More work (with tests) on showing roster items under groups.

updates #83
parent be761562
...@@ -280,6 +280,9 @@ ...@@ -280,6 +280,9 @@
'xa': __('This contact is away for an extended period'), 'xa': __('This contact is away for an extended period'),
'away': __('This contact is away') 'away': __('This contact is away')
}; };
var DESC_GROUP_TOGGLE = __('Click to hide these contacts');
var HEADER_CURRENT_CONTACTS = __('My contacts');
var HEADER_UNGROUPED = __('Ungrouped');
// Module-level variables // Module-level variables
// ---------------------- // ----------------------
...@@ -2806,6 +2809,7 @@ ...@@ -2806,6 +2809,7 @@
'id': jid, 'id': jid,
'user_id': Strophe.getNodeFromJid(jid), 'user_id': Strophe.getNodeFromJid(jid),
'resources': [], 'resources': [],
'groups': [],
'status': '' 'status': ''
}, attributes); }, attributes);
attrs.sorted = false; attrs.sorted = false;
...@@ -3252,22 +3256,22 @@ ...@@ -3252,22 +3256,22 @@
}, },
render: function () { render: function () {
var desc_group_toggle = __('Click to hide these contacts'), var toggle_state = 'opened', // TODO: remember state...
toggle_state = 'opened',
roster_markup = converse.templates.group_header({ roster_markup = converse.templates.group_header({
label_group: this.roster_groups ? __('Ungrouped') : __('My contacts'), label_group: converse.roster_groups ? HEADER_UNGROUPED : HEADER_CURRENT_CONTACTS,
desc_group_toggle: desc_group_toggle, desc_group_toggle: DESC_GROUP_TOGGLE,
toggle_state: toggle_state toggle_state: toggle_state
}); });
if (converse.allow_contact_requests) { if (converse.allow_contact_requests) {
roster_markup += converse.templates.requesting_contacts({ roster_markup += converse.templates.requesting_contacts({
label_contact_requests: __('Contact requests'), label_contact_requests: __('Contact requests'),
desc_group_toggle: desc_group_toggle, desc_group_toggle: DESC_GROUP_TOGGLE,
toggle_state: toggle_state toggle_state: toggle_state
}) + }) +
converse.templates.pending_contacts({ converse.templates.pending_contacts({
label_pending_contacts: __('Pending contacts'), label_pending_contacts: __('Pending contacts'),
desc_group_toggle: desc_group_toggle, desc_group_toggle: DESC_GROUP_TOGGLE,
toggle_state: toggle_state toggle_state: toggle_state
}); });
} }
...@@ -3275,10 +3279,18 @@ ...@@ -3275,10 +3279,18 @@
this.$fragment.append($(roster_markup)); this.$fragment.append($(roster_markup));
}, },
insertRosterFragment: function () {
if (this.$fragment) {
this.$el.html(this.$fragment)
delete this.$fragment;
}
return this;
},
onAdd: function (item) { onAdd: function (item) {
this.addRosterItem(item).updateRoster(); this.addRosterItem(item);
if (item.get('is_last')) { if (item.get('is_last')) {
this.sortRoster().showRoster(); this.toggleHeaders().sortRoster().insertRosterFragment().updateCount();
} }
if (!item.get('vcard_updated')) { if (!item.get('vcard_updated')) {
// This will update the vcard, which triggers a change // This will update the vcard, which triggers a change
...@@ -3287,19 +3299,11 @@ ...@@ -3287,19 +3299,11 @@
} }
}, },
showRoster: function () {
if (this.$fragment) {
this.$el.html(this.$fragment)
delete this.$fragment;
}
return this;
},
onChange: function (item) { onChange: function (item) {
if ((_.size(item.changed) === 1) && _.contains(_.keys(item.changed), 'sorted')) { if ((_.size(item.changed) === 1) && _.contains(_.keys(item.changed), 'sorted')) {
return; return;
} }
this.updateChatBox(item).renderRosterItem(item).updateRoster(); this.updateChatBox(item).renderRosterItem(item).toggleHeaders().updateCount();
if (item.changed.chat_status) { // A changed chat status implies a new sort order if (item.changed.chat_status) { // A changed chat status implies a new sort order
this.sortRoster(); this.sortRoster();
} }
...@@ -3323,14 +3327,14 @@ ...@@ -3323,14 +3327,14 @@
removeAllRosterItemViews: function () { removeAllRosterItemViews: function () {
this.removeAll(); this.removeAll();
this.updateRoster(); this.updateCount().toggleHeaders();
return this; return this;
}, },
removeRosterItemView: function (item) { removeRosterItemView: function (item) {
if (this.get(item.id)) { if (this.get(item.id)) {
this.get(item.id).remove(); this.get(item.id).remove();
this.updateRoster(); this.updateCount().toggleHeaders();
} }
return this; return this;
}, },
...@@ -3361,6 +3365,32 @@ ...@@ -3361,6 +3365,32 @@
} }
}, },
addCurrentContact: function (view) {
var $el = this.getRosterElement(),
item = view.model;
if (converse.roster_groups) {
if (item.get('groups').length === 0) {
$el.find('.roster-group[data-group="'+HEADER_UNGROUPED+'"]').after(view.el);
} else {
_.each(item.get('groups'), $.proxy(function (group) {
var $group = $el.find('.roster-group[data-group="'+group+'"]');
if ($group.length > 0) {
$group.after(view.el);
} else {
$group = $(converse.templates.group_header({
label_group: group,
desc_group_toggle: DESC_GROUP_TOGGLE,
toggle_state: 'opened' // TODO: remember state...
})).after(view.el);
this.getRosterElement().append($group);
}
},this));
}
} else {
$el.find('.roster-group[data-group="'+HEADER_CURRENT_CONTACTS+'"]').after(view.el);
}
},
addRosterItem: function (item) { addRosterItem: function (item) {
var view = new converse.RosterItemView({model: item}); var view = new converse.RosterItemView({model: item});
this.add(item.id, view); this.add(item.id, view);
...@@ -3368,24 +3398,16 @@ ...@@ -3368,24 +3398,16 @@
return this; return this;
} }
view.render() view.render()
var $el = this.getRosterElement();
if (view.$el.hasClass('current-xmpp-contact')) { if (view.$el.hasClass('current-xmpp-contact')) {
// TODO: need to add group support this.addCurrentContact(view);
$el.find('.roster-group').after(view.el);
} else if (view.$el.hasClass('pending-xmpp-contact')) { } else if (view.$el.hasClass('pending-xmpp-contact')) {
$el.find('#pending-xmpp-contacts').after(view.el); this.getRosterElement().find('#pending-xmpp-contacts').after(view.el);
} else if (view.$el.hasClass('requesting-xmpp-contact')) { } else if (view.$el.hasClass('requesting-xmpp-contact')) {
$el.find('#xmpp-contact-requests').after(view.render().el); this.getRosterElement().find('#xmpp-contact-requests').after(view.render().el);
} }
return this; return this;
}, },
updateRoster: function (item) {
this.updateCount().toggleHeaders();
converse.emit('rosterViewUpdated');
return this;
},
updateCount: function () { updateCount: function () {
var $count = $('#online-count'); var $count = $('#online-count');
$count.text('('+this.model.getNumOnlineContacts()+')'); $count.text('('+this.model.getNumOnlineContacts()+')');
......
...@@ -784,8 +784,6 @@ Here are the different events that are emitted: ...@@ -784,8 +784,6 @@ Here are the different events that are emitted:
+----------------------------------+---------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------+ +----------------------------------+---------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------+
| **roster** | When the roster is updated. | ``converse.on('roster', function (items) { ... });`` | | **roster** | When the roster is updated. | ``converse.on('roster', function (items) { ... });`` |
+----------------------------------+---------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------+ +----------------------------------+---------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------+
| **rosterViewUpdated** | Whenever the roster view (i.e. the rendered HTML) has changed. | ``converse.on('rosterViewUpdated', function (items) { ... });`` |
+----------------------------------+---------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------+
| **callButtonClicked** | When a call button (i.e. with class .toggle-call) on a chat box has been clicked. | ``converse.on('callButtonClicked', function (connection, model) { ... });`` | | **callButtonClicked** | When a call button (i.e. with class .toggle-call) on a chat box has been clicked. | ``converse.on('callButtonClicked', function (connection, model) { ... });`` |
+----------------------------------+---------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------+ +----------------------------------+---------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------+
| **chatBoxOpened** | When a chat box has been opened. | ``converse.on('chatBoxOpened', function (chatbox) { ... });`` | | **chatBoxOpened** | When a chat box has been opened. | ``converse.on('chatBoxOpened', function (chatbox) { ... });`` |
......
...@@ -116,7 +116,7 @@ ...@@ -116,7 +116,7 @@
</dl> </dl>
<dl id="converse-roster" style="display: block;"> <dl id="converse-roster" style="display: block;">
<dt class="roster-group" style="display: block;"> <dt class="roster-group" style="display: block;">
<a href="#" class="group-toggle icon-opened" title="Click to hide these contacts">Colleagues</a> <a href="#" data-group="Colleagues" class="group-toggle icon-opened" title="Click to hide these contacts">Colleagues</a>
</dt> </dt>
<dd class="online current-xmpp-contact"> <dd class="online current-xmpp-contact">
<a class="open-chat" title="Click to chat with this contact" href="#"> <a class="open-chat" title="Click to chat with this contact" href="#">
...@@ -141,7 +141,7 @@ ...@@ -141,7 +141,7 @@
</dd> </dd>
<dt class="roster-group" style="display: block;"> <dt class="roster-group" style="display: block;">
<a href="#" class="group-toggle icon-opened" title="Click to hide these contacts">Family</a> <a href="#" data-group="Family" class="group-toggle icon-opened" title="Click to hide these contacts">Family</a>
</dt> </dt>
<dd class="away current-xmpp-contact"> <dd class="away current-xmpp-contact">
<a class="open-chat" title="Click to chat with this contact" href="#"> <a class="open-chat" title="Click to chat with this contact" href="#">
...@@ -159,7 +159,7 @@ ...@@ -159,7 +159,7 @@
</dd> </dd>
<dt class="roster-group" style="display: block;"> <dt class="roster-group" style="display: block;">
<a href="#" class="group-toggle icon-opened" title="Click to hide these contacts">Friends</a> <a href="#" data-group="Friends" class="group-toggle icon-opened" title="Click to hide these contacts">Friends</a>
</dt> </dt>
<dd class="online current-xmpp-contact"> <dd class="online current-xmpp-contact">
<a class="open-chat" title="Click to chat with this contact" href="#"> <a class="open-chat" title="Click to chat with this contact" href="#">
......
This diff is collapsed.
<dt class="roster-group"><a href="#" class="group-toggle icon-{{toggle_state}}" title="{{desc_group_toggle}}">{{label_group}}</a></dt> <dt class="roster-group" data-group="{{label_group}}">
<a href="#" class="group-toggle icon-{{toggle_state}}" title="{{desc_group_toggle}}">{{label_group}}</a>
</dt>
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment