app/assets/javascripts/backbone/views/contacts/contacts_list.js.coffee
b27f285a
 Xmpp.Views.Contacts ||= {}
 
 class Xmpp.Views.Contacts.ListView extends Backbone.View
   template: JST["backbone/templates/contacts/contact_list"]
 
   initialize: () ->
     _.bindAll(this)
 
     @el = '#' + @attributes['id']
619ceb7d
     @title = I18n.t(@attributes['title'])
b27f285a
 
4eed5808
     @titleClass = @listClass = ''
     @activeGroup = false
     @contactViews = []
 
635339a3
     Backbone.Events.on('removeContact', @detachContact)
 
52ef7688
   createListContainer: ->
e2e90aca
     $(@el).html(@template(title: @title, isActiveGroup: @activeGroup))
b27f285a
 
52ef7688
   appendContact: (contact) ->
e2e90aca
     if @hasContact(contact)
       return false
 
     if @contactViews.length == 0
       @createListContainer()
 
52ef7688
     view =  new Xmpp.Views.Contacts.ContactView(model: contact, attributes: {listView: this})
     @contactViews.push view
     @render(view.render().el)
 
e2e90aca
   detachContact: (contact) ->
     matchingView = @hasContact(contact)
     if matchingView
       @contactViews = _.without(@contactViews, matchingView)
       matchingView.remove()
 
       if @contactViews.length == 0
         $(@el).html('')
 
     return !!matchingView
 
   hasContact: (contact) ->
     _.find(@contactViews, (view) ->
       view.model == contact
     )
 
   setAsActiveChatGroup: ->
     @activeGroup = true
 
52ef7688
   render: (contactHtml) ->
     if (contactHtml?)
       $(@el).find('ul').append(contactHtml)
     else
c4a05fed
       _.each(@contactViews, (contact) =>
52ef7688
         @render(contact.render().el)
       )
 
43899b3a
     return this
 
   filter: (searchTerm) ->
     regex = new RegExp(searchTerm.trim(), 'i')
     _.each(@contactViews, (view) ->
       searchInAttributes = view.model.pick('jid', 'name', 'message')
       searchString = _.toArray(searchInAttributes).join(' ')
       found = (searchString.search(regex) != -1)
 
       if found then view.unhide() else view.hide()
c4a05fed
     )
 
   reOrder: ->
     groupedByStatus = _.groupBy(@contactViews, (view) ->
       view.model.get('status')
     )
 
     sortedGroupNames = {
       online: groupedByStatus.online
       away: groupedByStatus.away
       offline: groupedByStatus.offline
     }
 
     sorted = _.map(sortedGroupNames, (group) ->
       _.sortBy(group, (view) ->
         view.model.get('name') || view.model.get('jid')
       )
     )
 
     @contactViews = _.flatten(sorted)
     @render()