Skip to content

Commit

Permalink
Initial support of resource and status messages in contact list
Browse files Browse the repository at this point in the history
  • Loading branch information
sebdeckers committed Oct 23, 2011
1 parent 9a29e59 commit f603f3a
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 38 deletions.
93 changes: 61 additions & 32 deletions modules/contactList.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,39 @@ function (events, dock, roster, template, settings) {
}
};

var updateContact = function (group, contact) {
var cachedContact = getContactFromCache(group, contact.jid);
while (cachedContact.hasChildNodes()) {
cachedContact.removeChild(cachedContact.firstChild);
}
var resources = Object.keys(contact.resources)
.map(function (resource) {
var res = contact.resources[resource];
return {
jid: contact.jid,
priority: res.priority,
resource: resource,
show: res.show,
status: res.status
};
})
.sort(function (a, b) {
return b.priority - a.priority;
});
template({
container: cachedContact,
source: "contactListItem",
data: {
jid: contact.jid,
name: contact.name,
ask: contact.ask,
subscription: contact.subscription,
resource: resources.length > 0 ? resources[0] : undefined,
resources: resources
}
});
};

var widget = new dock({
title: "Contacts",
sticky: true,
Expand All @@ -88,54 +121,50 @@ function (events, dock, roster, template, settings) {
}
}
});
template({css: "modules/contactList", source: "contactList", container: widget.content});
template({
css: "modules/contactList",
source: "contactList",
container: widget.content
});

events.subscribe("roster.change", function (changedContacts) {
widget.content.querySelector("p").classList.add("hide");
var groupsToUpdate = {};
Object.keys(changedContacts).forEach(function (jid) {
var resources = Object.keys(changedContacts[jid].resources).map(function (resource) {
var res = changedContacts[jid].resources[resource];
return {
jid: jid,
priority: res.priority,
resource: resource,
show: res.show,
status: res.status
};
}).sort(function (a, b) {
return b.priority - a.priority;
});
// Remove from old groups
Object.keys(htmlCache).filter(function (group) {
return Object.hasOwnProperty.call(htmlCache[group], jid) &&
(changedContacts[jid].groups.indexOf(group) === -1);
return Object.hasOwnProperty.call(htmlCache[group], jid)
&& changedContacts[jid].groups.indexOf(group) === -1;
}).forEach(function (group) {
removeContactFromCache(group, jid);
});
// Update new groups
((changedContacts[jid].groups.length > 0) ? changedContacts[jid].groups : ["Other Contacts"])
(changedContacts[jid].groups.length > 0
? changedContacts[jid].groups
: ["Other Contacts"])
.forEach(function (group) {
groupsToUpdate[group] = true;
var cachedContact = getContactFromCache(group, jid);
while (cachedContact.hasChildNodes()) {
cachedContact.removeChild(cachedContact.firstChild);
}
template({
container: cachedContact,
source: "contactListItem",
data: {
jid: jid,
name: changedContacts[jid].name,
ask: changedContacts[jid].ask,
subscription: changedContacts[jid].subscription,
resource: resources.length > 0 ? resources[0] : undefined,
resources: resources
}
});
updateContact(group, changedContacts[jid]);
});
});
Object.keys(groupsToUpdate).forEach(updateGroupHeader);
return true;
});

events.subscribe("roster.available", function (presence, jid, resource, rosterResource) {
return true;
var contact = roster[jid];
(contact.groups.length > 0
? contact.groups
: ["Other Contacts"])
.forEach(function (group) {
updateContact(group, contact);
});
return true;
});

events.subscribe("roster.unavailable", function (presence, jid, resource, status) {
return true;
});

return {};
Expand Down
12 changes: 6 additions & 6 deletions templates/contactListItem.html
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<p class="contact">
{{#resource}}
{{#resource}}
Online!
{{/resource}}
{{^resource}}
{{/resource}}
{{^resource}}
Offline!
{{/resource}}
{{/resource}}
<a href="/chat/{{jid}}">{{name}}</a>
</p>
<ul class="resources">
{{#resources}}
{{#resources}}
<li class="resource"><a href="/chat/{{jid}}/{{resource}}">{{resource}}</a></li>
{{/resources}}
{{/resources}}
</ul>

0 comments on commit f603f3a

Please sign in to comment.