Browse code

Automaticke zoradovanie ludi podla stavu a abecedy

Cinan Rakosnik authored on 07/04/2013 at 18:43:56
Showing 2 changed files
... ...
@@ -15,6 +15,7 @@ class Xmpp.Views.Contacts.ContactView extends Backbone.View
15 15
     @parentList = @attributes['listView']
16 16
 
17 17
     @model.on('change', @updateContact)
18
+    @model.on('change:status', => @parentList.reOrder())
18 19
 
19 20
   updateContact: (contact) ->
20 21
     @model = contact
... ...
@@ -2,6 +2,11 @@ Xmpp.Views.Contacts ||= {}
2 2
 
3 3
 class Xmpp.Views.Contacts.ListView extends Backbone.View
4 4
   template: JST["backbone/templates/contacts/contact_list"]
5
+  contactViews: []
6
+
7
+  titleClass = ''
8
+  listClass = ''
9
+  activeGroup = false
5 10
 
6 11
   initialize: () ->
7 12
     _.bindAll(this)
... ...
@@ -9,10 +14,6 @@ class Xmpp.Views.Contacts.ListView extends Backbone.View
9 9
     @el = '#' + @attributes['id']
10 10
     @title = I18n.t(@attributes['title'])
11 11
 
12
-    @titleClass = @listClass = ''
13
-    @activeGroup = false
14
-    @contactViews = []
15
-
16 12
     Backbone.Events.on('removeContact', @detachContact)
17 13
 
18 14
   createListContainer: ->
... ...
@@ -52,7 +53,7 @@ class Xmpp.Views.Contacts.ListView extends Backbone.View
52 52
     if (contactHtml?)
53 53
       $(@el).find('ul').append(contactHtml)
54 54
     else
55
-      _.each(@contactViews, (i, contact) =>
55
+      _.each(@contactViews, (contact) =>
56 56
         @render(contact.render().el)
57 57
       )
58 58
 
... ...
@@ -66,4 +67,24 @@ class Xmpp.Views.Contacts.ListView extends Backbone.View
66 66
       found = (searchString.search(regex) != -1)
67 67
 
68 68
       if found then view.unhide() else view.hide()
69
-    )
70 69
\ No newline at end of file
70
+    )
71
+
72
+  reOrder: ->
73
+    groupedByStatus = _.groupBy(@contactViews, (view) ->
74
+      view.model.get('status')
75
+    )
76
+
77
+    sortedGroupNames = {
78
+      online: groupedByStatus.online
79
+      away: groupedByStatus.away
80
+      offline: groupedByStatus.offline
81
+    }
82
+
83
+    sorted = _.map(sortedGroupNames, (group) ->
84
+      _.sortBy(group, (view) ->
85
+        view.model.get('name') || view.model.get('jid')
86
+      )
87
+    )
88
+
89
+    @contactViews = _.flatten(sorted)
90
+    @render()
71 91
\ No newline at end of file