ControlInput updating nick view properly
[KiwiIRC.git] / client / assets / dev / model_memberlist.js
CommitLineData
eaaf73b0
D
1_kiwi.model.MemberList = Backbone.Collection.extend({\r
2 model: _kiwi.model.Member,\r
9df70c63
D
3 comparator: function (a, b) {\r
4 var i, a_modes, b_modes, a_idx, b_idx, a_nick, b_nick;\r
eaaf73b0 5 var user_prefixes = _kiwi.gateway.get('user_prefixes');\r
9df70c63
D
6 a_modes = a.get("modes");\r
7 b_modes = b.get("modes");\r
8 // Try to sort by modes first\r
9 if (a_modes.length > 0) {\r
10 // a has modes, but b doesn't so a should appear first\r
11 if (b_modes.length === 0) {\r
12 return -1;\r
13 }\r
14 a_idx = b_idx = -1;\r
15 // Compare the first (highest) mode\r
16 for (i = 0; i < user_prefixes.length; i++) {\r
17 if (user_prefixes[i].mode === a_modes[0]) {\r
18 a_idx = i;\r
19 }\r
20 }\r
21 for (i = 0; i < user_prefixes.length; i++) {\r
22 if (user_prefixes[i].mode === b_modes[0]) {\r
23 b_idx = i;\r
24 }\r
25 }\r
26 if (a_idx < b_idx) {\r
27 return -1;\r
28 } else if (a_idx > b_idx) {\r
29 return 1;\r
30 }\r
31 // If we get to here both a and b have the same highest mode so have to resort to lexicographical sorting\r
32\r
33 } else if (b_modes.length > 0) {\r
34 // b has modes but a doesn't so b should appear first\r
35 return 1;\r
36 }\r
37 a_nick = a.get("nick").toLocaleUpperCase();\r
38 b_nick = b.get("nick").toLocaleUpperCase();\r
39 // Lexicographical sorting\r
40 if (a_nick < b_nick) {\r
41 return -1;\r
42 } else if (a_nick > b_nick) {\r
43 return 1;\r
44 } else {\r
9df70c63
D
45 return 0;\r
46 }\r
47 },\r
48 initialize: function (options) {\r
eaaf73b0 49 this.view = new _kiwi.view.MemberList({"model": this});\r
9df70c63
D
50 },\r
51 getByNick: function (nick) {\r
52 if (typeof nick !== 'string') return;\r
53 return this.find(function (m) {\r
54 return nick.toLowerCase() === m.get('nick').toLowerCase();\r
55 });\r
56 }\r
57});