app/assets/javascripts/backbone/views/tabbar/tabbar.js.coffee
6a6ccd01
 Xmpp.Views.Tabbar ||= {}
 
 class Xmpp.Views.Tabbar.TabbarView extends Backbone.View
   template: JST["backbone/templates/tabbar/tabbar"]
   el: $('#js-tabbar')
 
   initialize: () ->
     _.bindAll(this)
 
b933b434
     Backbone.Events.on('openChat', (chat) =>
       @hideCurrentChatWindow()
       @addOrSelect(chat)
     )
aa2c1028
 
     Backbone.Events.on('closeChat', (chat) =>
       tab = _.find(@tabs, (_tab) =>
         _tab.model == chat
       )
 
       @closeTab(tab) if tab
bdf5dc64
     )
bcce524b
 
af637cc8
     Backbone.Events.on('removeContact', (contact) =>
       tab = _.find(@tabs, (_tab) =>
         _tab.model.get('withWhom') == contact
       )
 
04884cc1
       Backbone.Events.trigger('closeChat', tab.model) if tab
af637cc8
     )
 
6a6ccd01
     @tabs = []
b933b434
     @activeTab = null
6a6ccd01
     @render()
 
   addOrSelect: (chat) ->
     tab = _.find(@tabs, (tab) ->
       tab.hasParticipants(chat.get('who'), chat.get('withWhom'))
     )
 
     if (!! tab)
       App.debug 'selecting existing tab'
     else
       App.debug 'adding new tab'
       tab = new Xmpp.Views.Tabbar.TabView(model: chat)
       @tabs.push tab
       $(@el).append(tab.render().el)
 
     _.each(@tabs, (tab) ->
       tab.setInactive()
     )
 
b933b434
     @activeTab = tab
     @activeTab.setActive()
     @activeTab.showChat()
 
   hideCurrentChatWindow: ->
     if @activeTab
       App.debug 'zakryvam chat window'
       @activeTab.hideChat()
6a6ccd01
 
aa2c1028
   closeTab: (tab) ->
     @removeTab(tab)
 
     if tab == @activeTab
       firstTab = @selectFirstTab()
       if firstTab
         App.debug 'fire openChat'
         Backbone.Events.trigger('openChat', firstTab.model)
     #TODO: vybrat naposledy pouzity tab
 
6a6ccd01
   removeTab: (tab) ->
     @tabs = _.without(@tabs, tab)
 
   render: ->
b933b434
     $(@el).html(@template())
6a6ccd01
     return this
bdf5dc64
 
   selectFirstTab: ->
     _.first(@tabs)