app/assets/javascripts/_main.js.coffee
43b171a9
 this.App =
52ef7688
   debug: (msg) ->
     console.log msg
 
a7decc88
   stripJid: (jid) ->
     r = new RegExp('(^.*)\/.*$')
     match = r.exec(jid)
     if match and (match.length > 0)
       return match[1]
     else
       return jid
 
43b171a9
   UI:
1ec65f29
     setAutoHeight: ->
3a34499e
       App.UI.resizeRoster()
       App.UI.resizeMessages()
 
     resizeRoster: ->
d5d0ee30
       if (windowHeight < $('.roster').css('min-height').replace(/[^-\d\.]/g, ''))
         return
 
3a34499e
       windowHeight = $(window).height()
d5d0ee30
       toolboxHeight = $("#height-setter-1 .toolbox").height()
3a34499e
       $(".friends").css({height: windowHeight - toolboxHeight - $('#js-me').height() - 40})
       $("#height-setter-1").css({height: windowHeight - toolboxHeight - $('#js-active-friends').height() - 120})
 
     resizeMessages: ->
       windowHeight = $(window).height()
       $("#height-setter-2").css({height: windowHeight - $('#tabbar').outerHeight() - $('#msg-writer').outerHeight() - 62})
       messagesContainer = $('.messages')
       messagesContainer.animate(scrollTop: messagesContainer.height(), 'fast')
ff94bdbf
 
43899b3a
     filterContacts: (searchTerm) ->
       App.Collections.contacts.filter(searchTerm)
 
ff94bdbf
   Com:
b27f285a
     connect: (callback) ->
       App._dispatcher = new WebSocketRails('www.xmpp.dev:3000/websocket')
       App._dispatcher.on_open = =>
         @_setupBackboneComponents()
9234acc2
         @_bindListeners(callback)
ff94bdbf
 
191a317f
     trigger: (options) ->
b27f285a
       _.defaults(options, {data: {} })
ff94bdbf
       App._dispatcher.trigger(options.event, options.data, options.success, options.error)
b27f285a
 
5011e360
     initRoster: (callback) ->
191a317f
       App.Com.trigger(
52ef7688
         event: 'app.roster.initRoster'
         success: (data) ->
6a6ccd01
           _.each(data.contacts, (contact) ->
             newContact = new Xmpp.Models.Contact(
               id: contact.jid
               jid: contact.jid
635339a3
               belongsTo: [contact.belongsTo]
6a6ccd01
             )
635339a3
             App.Collections.contacts.add(newContact, merge: true)
b27f285a
           )
9fc0e3f3
 
           callback?()
52ef7688
       )
 
     startFetchingVcards: ->
191a317f
       App.Com.trigger(event: 'app.roster.startFetchingVcards')
52ef7688
 
     startPollingRoster: ->
191a317f
       App.Com.trigger(event: 'app.roster.startPolling')
b27f285a
 
1f685c13
     startPollingMessages: ->
       App.Com.trigger(event: 'app.chat.startPollingMessages')
 
43ad79fa
     setPresence: ->
191a317f
       App.Com.trigger(event: 'app.roster.setPresence')
43ad79fa
 
d4872cda
     getMe: ->
       App.Com.trigger(event: 'app.roster.myself', success: (response) ->
4b5bb9c5
         App.Models.me = new Xmpp.Models.Me(
           jid: response.jid
           name: response.vcard.name
           status: response.status
           avatar: response.vcard.avatar
         )
d4872cda
       )
 
31111b63
     sendMessage: (message, to, from, callbackOk, callbackFail) ->
       App.Com.trigger(event: 'app.chat.sendMessage', data: {message: message, to: to, from: from}, success: callbackOk, error: callbackFail)
 
24b129a9
     sendMultiMessage: (message, chatId, from, callbackOk, callbackFail) ->
00f00dbe
       App.Com.trigger(event: 'app.chat.sendMessage', data: {message: message, chatId: chatId, from: from}, success: callbackOk, error: callbackFail)
2a61cdcc
 
94bd8368
     openNewMultiChat: (chatOwner, attendant, chat) ->
       @trigger(event: 'app.chat.newMultiChat', data: {chatOwner: chatOwner.get('jid')}, success: (response) =>
2a61cdcc
         chat.setChatId(response.id)
296dd1c6
         chat.appendWithWhom(attendant)
94bd8368
         @trigger(event: 'app.chat.addToMultiChat', data: {chatOwner: chatOwner.get('jid'), chatId: response.id, jid: attendant.get('jid')}, success: ->
           Backbone.Events.trigger('openChat', chat)
         )
424fd46f
       )
 
296dd1c6
     inviteToChat: (chat, toAdd, me) ->
       @trigger(event: 'app.chat.addToMultiChat', data: {chatOwner: me, chatId: chat.get('chatId'), jid: toAdd.get('jid')}, success: ->
         chat.appendWithWhom(toAdd)
         Backbone.Events.trigger('openChat', chat)
       )
 
aa2c1028
     iClosedMultichat: (chatId, jid) ->
       @trigger(event: 'app.chat.iClosedMultichat', data: {chatId: chatId, me: jid})
 
     kickFromMultichat: (chatId, me, jidToKick) ->
       @trigger(event: 'app.chat.kickFromMultichat', data: {chatId: chatId, me: me, kick: jidToKick})
 
37ab3198
     switchMultiChatOwner: (me, newOwner, chatId) ->
       @trigger(event: 'app.chat.switchOwnership', data: {chatId: chatId, me: me, newOwner: newOwner})
 
24b129a9
     syncMultiChatContacts: (me, chatId) ->
a0c73ec7
       @trigger(event: 'app.chat.syncMultiChatContacts', data: {me: me, chatId: chatId})
 
     syncedContacts: () ->
 
24b129a9
 
c4a1e746
     updateMyStatus: (message, state)->
       App.Com.trigger(event: 'app.roster.updateMyStatus', data: {message: message, state: state})
 
6fb9497e
     updateMyVcardName: (name) ->
6347ea28
       App.Com.trigger(event: 'app.roster.updateMyVcard', data: {name: name})
 
6fb9497e
     updateMyVcardAvatar: (imageBase64) ->
       App.Com.trigger(event: 'app.roster.updateMyVcard', data: {avatar: imageBase64})
 
635339a3
     removeContactRemote: (contact, client) ->
       App.Com.trigger(event: 'app.roster.removeContact', data: {jid: contact, client: client})
 
910e0f23
     addContact: (jid) ->
       App.Com.trigger(event: 'app.roster.addContact', data: {jid: jid})
 
     answerFriendRequest: (jid, answer) ->
       App.Com.trigger(event: 'app.roster.answerFriendRequest', data: { jid: jid, answer: answer})
 
b27f285a
     _setupBackboneComponents: ->
6a6ccd01
       App.Collections.contacts = new Xmpp.Collections.ContactsCollection()
       App.Collections.chats = new Xmpp.Collections.ChatsCollection()
 
       App.Views.tabbar = new Xmpp.Views.Tabbar.TabbarView()
b27f285a
 
9234acc2
     _bindListeners: (callback) ->
3719af96
       App._dispatcher.bind('connection_closed', ->
         window.location.reload()
       )
 
9234acc2
       App._dispatcher.bind('app.client.connected', (jid) ->
         callback?(jid)
       )
 
       App._dispatcher.bind('app.client.cannot_connect', (jid) ->
         alert('cannot connect to ' + jid)
       )
 
52ef7688
       App._dispatcher.bind('app.roster.statusChanged', (result) ->
         App.debug 'change contact state'
6a6ccd01
         App.Collections.contacts.updateStatus(result)
52ef7688
       )
 
       App._dispatcher.bind('app.roster.vcard', (result) ->
         App.debug 'got vcard'
6a6ccd01
         App.Collections.contacts.udpateVcard(result)
52ef7688
       )
 
635339a3
       App._dispatcher.bind('app.roster.subscriptionChanged', (subscription) ->
         App.debug 'subscription changed'
         App.Collections.contacts.subscriptionChanged(subscription)
       )
 
7a817e66
       App._dispatcher.bind('app.roster.using_this_app', (person) ->
         App.debug ['is using this app', person.jid]
         contact = App.Collections.contacts.get(person.jid)
         if (contact)
           contact.setUsingMyApp()
       )
 
aa2c1028
       App._dispatcher.bind('app.chat.destroyMultichat', (data) ->
         chatId = data.chat_id
         chat = App.Collections.chats.findById(chatId)
         if chat
           App.debug 'destroying multicat from outside'
           Backbone.Events.trigger('closeChat', chat, true)
       )
 
37ab3198
       App._dispatcher.bind('app.chat.makeMeNewOwner', (chatData) ->
         App.debug 'Makeing me new chat owner'
         chat = App.Collections.chats.findById(chatData.chat_id)
         if chat
           attendants = chat.get('withWhom')
           attendants = _.without(attendants, App.Models.me) #TODO zamenit .me za model zo serveru
 
           chat.set('who', App.Models.me)
           chat.set('withWhom', attendants)
       )
 
910e0f23
       App._dispatcher.bind('app.roster.friendRequest', (request) ->
         App.debug ['receiving friend request', request]
         popup = new Xmpp.Views.Popup.FriendRequestView(jid: request.jid, name: request.name)
         popup.render()
       )
 
c6c526f1
       App._dispatcher.bind('app.chat.importChat', (chatData) ->
296dd1c6
         chatId = chatData.chat_id
         chat = App.Collections.chats.findById(chatId)
 
         if not chat
           chat = new Xmpp.Models.Chat(
             chatId: chatId,
             isMultiChat: true
           )
c6c526f1
 
296dd1c6
           App.Collections.chats.add(chat);
b1c6b359
 
296dd1c6
         chat.syncContacts(chatData.contacts, chatData.owner)
         Backbone.Events.trigger('openChat', chat)
c6c526f1
       )
 
a0c73ec7
       App._dispatcher.bind('app.chat.updateSyncedContacts', (syncData) ->
         App.debug ['new multichat contacts arrived', syncData.contacts]
 
         chat = App.Collections.chats.findById(syncData.chat_id)
         if chat
           chat.syncContacts(syncData.contacts, syncData.owner)
           Backbone.Events.trigger('openChat', chat)
       )
 
1f685c13
       App._dispatcher.bind('app.chat.messageReceived', (result) ->
b1c6b359
         App.debug ['message received', result]
1f685c13
 
b1c6b359
         if (result.chat_id)
           chat = App.Collections.chats.findById(result.chat_id)
1f685c13
 
b1c6b359
           Backbone.Events.trigger('openChat', chat)
 
           tab = _.find(App.Views.tabbar.tabs, (tab) ->
             tab.getChatId() == result.chat_id
1f685c13
           )
 
b1c6b359
           tab.showChat()
e513299a
           contact = App.Collections.contacts.findByJid(App.stripJid(result.from))
b1c6b359
           tab.chatWindow.appendMessage(contact, new Date(), result.message)
         else
           #TODO: prepisat tuto hrozu
 
a7decc88
           contact = App.Collections.contacts.findByJid(App.stripJid(result.from))
b1c6b359
 
           if contact
             contactView = App.Collections.contacts.friendsList.hasContact(contact) ||
               App.Collections.contacts.activeList.hasContact(contact)
           else
             contact = new Xmpp.Models.Contact(
7957c8ca
               id: App.stripJid(result.from)
               jid: App.stripJid(result.from)
b1c6b359
               belongsTo: [result.to]
             )
             App.Collections.contacts.add(contact, merge: true)
             contactView = App.Collections.contacts.friendsList.hasContact(contact)
 
7957c8ca
           contact.setPreferredResource(result.from)
b1c6b359
           contactView.startChat()
1f685c13
 
b1c6b359
           tab = _.find(App.Views.tabbar.tabs, (tab) ->
             tab.hasParticipants(App.Models.me, contact)
           )
1f685c13
 
b1c6b359
           tab.chatWindow.appendMessage(contact, new Date(), result.message)
1f685c13
       )
 
d4872cda
   Models:
     me: null
 
b27f285a
   Collections:
6a6ccd01
     contacts: null
     chats: null
 
   Views:
     tabbar: null
     chat: null