-
Notifications
You must be signed in to change notification settings - Fork 3
/
user-list-view.js
46 lines (44 loc) · 1.15 KB
/
user-list-view.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
"use strict";
define([
"jquery"
], function($) {
function insertAlphabetically(el, user, nick) {
nick = nick.toLowerCase();
var users = el.find(".user");
for (var i = 0; i < users.length; i++) {
var otherNick = $(users[i]).find(".nick").text().toLowerCase();
if (nick < otherNick) {
user.insertBefore(users[i]);
return;
}
}
el.append(user);
}
return function(users, el) {
var nicks = {};
users.on('add', function(nick) {
var user = $('<div class="user"></div>');
$('<span class="nick"></span>').text(nick).appendTo(user);
insertAlphabetically(el, user, nick);
nicks[nick] = user;
});
users.on('rename', function(oldnick, newnick) {
var user = nicks[oldnick];
delete nicks[oldnick];
nicks[newnick] = user;
user.find(".nick").text(newnick);
insertAlphabetically(el, user.remove(), newnick);
});
users.on('remove', function(nick) {
nicks[nick].remove();
delete nicks[nick];
});
return {
users: users,
el: el,
getElementForNick: function(nick) {
return nicks[nick];
}
};
};
});