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')
 
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()
b933b434
     this
 
   remove: ->
     @undelegateEvents()
     $(@el).empty()
00f00dbe
     @stopListening()
     _.each(@historyStack, (item)->
       item.remove()
     )
     return this
424fd46f
 
   sendMessage: (e) ->
     e.preventDefault()
     $this = $(e.currentTarget)
     input = $this.find('input[type=text]')
     message = input.val().trim()
00f00dbe
     input.val('')
424fd46f
 
     if (message)
00f00dbe
       @tab.getOwner().sendMessage(message, @tab.getChatId()
       , (message) =>
         @sendSuccess(@tab.getOwner(), message)
       , (message) ->
         @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