app/assets/javascripts/backbone/models/contact.js.coffee
ff94bdbf
 class Xmpp.Models.Contact extends Xmpp.Models.Model
   namespace: 'app.roster'
 
   defaults:
52ef7688
     jid: ''
     name: ''
     status: 'offline'
     message: ''
4b5bb9c5
     avatar: ''
635339a3
     belongsTo: []
ff94bdbf
 
   initialize: ->
b27f285a
     _.bindAll(this)
ff94bdbf
 
52ef7688
     if ! @get('name')
       @set(name: @get('jid'))
 
6a6ccd01
   firstname: ->
     @get('name').split(' ')[0]
52ef7688
 
ff94bdbf
 class Xmpp.Collections.ContactsCollection extends Backbone.Collection
   model: Xmpp.Models.Contact
b27f285a
 
   initialize: ->
     _.bindAll(this)
e2e90aca
 
b27f285a
     @friendsList = new Xmpp.Views.Contacts.ListView(collection: this, attributes: {title: 'chat.roster.friends', id: 'js-inactive-friends'})
e2e90aca
     @friendsList.createListContainer()
 
     @activeList  = new Xmpp.Views.Contacts.ListView(collection: this, attributes: {title: 'chat.roster.chat-group', id: 'js-active-friends'})
     @activeList.setAsActiveChatGroup()
b27f285a
 
bcce524b
     @on('add', @appendContact)
635339a3
     @on('change:belongsTo', @mergeContacts)
bcce524b
 
     Backbone.Events.on('openChat', (chat) =>
       @moveToInactiveList('all')
       @moveToActiveList(chat.get('withWhom'))
     )
 
     Backbone.Events.on('closeChat', (tab, chat) =>
       @moveToInactiveList(chat.get('withWhom'))
     )
b27f285a
 
   appendContact: (contact) ->
635339a3
     App.debug ['apppend', contact]
52ef7688
     @friendsList.appendContact(contact)
 
635339a3
   mergeContacts: (contact) ->
     App.debug ['merging contacts', contact]
     #TODO: ak ten isty clovek je vo viacerych rosteroch, treba nastavit spravne belongsTo
 
     existingContact = @get(contact.get('id'))
     if existingContact?
       alreadyBelongsTo = existingContact.get('belongsTo')
       willBelongTo = contact.get('belongsTo')
       existingContact.set(belongsTo: _.union(alreadyBelongsTo, willBelongTo))
     else
       @add(contact)
 
e2e90aca
   moveToActiveList: (contact) ->
624379a8
     @_switchContactBelongingList(@get(contact), @friendsList, @activeList) && @activeList.reOrder()
e2e90aca
 
   moveToInactiveList: (contact) ->
     if contact == 'all'
       _.each(@activeList.contactViews, (view) =>
         @_switchContactBelongingList(view.model, @activeList, @friendsList)
       )
624379a8
       @friendsList.reOrder()
e2e90aca
     else
624379a8
       @_switchContactBelongingList(@get(contact), @activeList, @friendsList) && @friendsList.reOrder()
e2e90aca
 
52ef7688
   updateStatus: (response) ->
     @get(response.jid).set(message: response.status.message, status: response.status.status)
 
   udpateVcard: (response) ->
e2e90aca
     @get(response.jid).set(name: response.vcard.name, avatar: response.vcard.avatar)
 
635339a3
   subscriptionChanged: (newSubscription) ->
     if (newSubscription.action == 'subscribed')
       App.debug ['subscribed', newSubscription]
 
       newContact = new Xmpp.Models.Contact(
         id: newSubscription.jid
         jid: newSubscription.jid
         belongsTo: [newSubscription.belongsTo]
       )
       @mergeContacts(newContact)
     else if (newSubscription.action == 'unsubscribed')
       # TODO: show info and ask if user want to remove him from roster
       App.debug ['unsubscribed', newSubscription]
 
       contact = @get(newSubscription.jid)
       App.Com.removeContactRemote(contact.get('jid'), newSubscription.belongsTo)
 
       nowBelongsTo = _.without(contact.get('belongsTo'), newSubscription.belongsTo)
       if (nowBelongsTo.length == 0)
         Backbone.Events.trigger('removeContact', contact)
         @remove(contact)
 
     # ignore 'unsubscribe' type of action
 
43899b3a
   filter: (searchTerm) ->
     @friendsList.filter(searchTerm)
     @activeList.filter(searchTerm)
 
e2e90aca
   _switchContactBelongingList: (contact, fromList, toList) ->
     if !contact?
       false
 
     fromList.detachContact(contact) && toList.appendContact(contact)
 
     true