app/assets/javascripts/backbone/views/chat/window.js.coffee
b933b434
 Xmpp.Views.Chat ||= {}
 
 class Xmpp.Views.Chat.WindowView extends Backbone.View
   template: JST["backbone/templates/chat/window"]
   el: $('#conversation-js')
f7a4c553
   inputSelector: 'input[type=text]'
b933b434
 
10f4c7d2
   maxHistoryLength: 20
 
424fd46f
   events:
     'submit #msg-writer': (e) -> @sendMessage(e)
 
b933b434
   initialize: () ->
     _.bindAll(this)
     @tab = @attributes['tab']
00f00dbe
     @historyStack = []
b933b434
 
   render: ->
00f00dbe
     historyStackHtml = _.map(@historyStack, (view) ->
       view.render().el.outerHTML
     )
     $(@el).html(@template(showWriter: true, history: historyStackHtml))
b933b434
     @show()
     return this
 
   hide: ->
     $(@el).addClass('hidden')
00f00dbe
     @undelegateEvents()
b933b434
     this
 
   show: ->
     $(@el).removeClass('hidden')
00f00dbe
     @delegateEvents()
f7a4c553
     $(@el).find(@inputSelector).focus()
b933b434
     this
 
   remove: ->
     @undelegateEvents()
     $(@el).empty()
00f00dbe
     @stopListening()
     _.each(@historyStack, (item)->
       item.remove()
     )
     return this
424fd46f
 
   sendMessage: (e) ->
     e.preventDefault()
     $this = $(e.currentTarget)
f7a4c553
     input = $this.find(@inputSelector)
b1c6b359
     message = input.val()
f7a4c553
     input.val('').focus()
424fd46f
 
b1c6b359
     if (message.trim())
       chatId = @tab.getChatId()
a7decc88
       attendant = if not chatId then @tab.getAttendant().getPreferredResource() else null
b1c6b359
 
       App.Models.me.sendMessage(message, chatId, attendant
00f00dbe
       , (message) =>
         @sendSuccess(@tab.getOwner(), message)
b1c6b359
       , (message) =>
00f00dbe
         @sendFail(message))
 
   sendSuccess: (me, msg) ->
     @appendMessage(me, new Date(), msg)
 
   sendFail: ->
     @appendEvent(I18n.t('chat.window.sendFailed'), false)
 
   appendEvent: (msg, logMe) ->
     App.debug ['append event', msg]
 #    @log(eventView) if logMe == true
 
   appendMessage: (user, date, msg) ->
     messageView = new Xmpp.Views.Chat.MessageView(user: user, date: date, message: msg)
     $(@el).find('#msg-writer').before(messageView.render().el)
     @log(messageView)
424fd46f
 
00f00dbe
   log: (view) ->
10f4c7d2
     if (@historyStack.length + 1 >= @maxHistoryLength)
       @historyStack = @historyStack.slice(@historyStack.length - @maxHistoryLength + 1)
 
00f00dbe
     @historyStack.push(view)
 #    App.debug @historyStack