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 |
f6071db7 |
savingHistory: true |
7a706f0e |
historyStep: 0 |
10f4c7d2 |
|
424fd46f |
events:
'submit #msg-writer': (e) -> @sendMessage(e) |
f6071db7 |
'change .history-settings input': 'changeHistorySettings' |
7a706f0e |
'click .history-settings .load-history': 'loadHistory' |
424fd46f |
|
f6071db7 |
initialize: (options) -> |
b933b434 |
_.bindAll(this) |
00f00dbe |
@historyStack = [] |
f6071db7 |
@tab = options.tab
chatId = @tab.getChatId() |
7a706f0e |
attendant = if not chatId then @tab.getAttendant().get('jid') else null
|
f6071db7 |
App.Com.checkSavingHistory(App.Models.me.get('jid'), chatId, attendant, (canSave) =>
@savingHistory = canSave
@_changeHistoryVisibly(@savingHistory)
) |
b933b434 |
|
7a706f0e |
@loadHistory()
|
b933b434 |
render: -> |
bea5bc45 |
historyStackHtml = _.map(@historyStack, (view) ->
view.render().el.outerHTML
) |
f8de0937 |
$(@el).html(@template(hideHistoryLoading: @tab.getChatId() != null, history: historyStackHtml)) |
f6071db7 |
@_changeHistoryVisibly(@savingHistory)
|
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) => |
b7e1dd20 |
@sendSuccess(App.Models.me, 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) -> |
14a337d8 |
messageView = new Xmpp.Views.Chat.MessageView(user: user, date: date, message: msg, fromMe: user.get('jid') == App.Models.me.get('jid')) |
3a34499e |
$(@el).find('.messages').append(messageView.render().el) |
bea5bc45 |
@log(messageView) |
14a337d8 |
Backbone.Events.trigger('resizeWorkspace') |
bea5bc45 |
|
7a706f0e |
log: (view, isHistoryMessage) -> |
bea5bc45 |
if (@historyStack.length + 1 >= @maxHistoryLength)
@historyStack = @historyStack.slice(@historyStack.length - @maxHistoryLength + 1)
|
7a706f0e |
if not isHistoryMessage? || isHistoryMessage == false
@historyStack.push(view)
else
@historyStack.unshift(view) |
f6071db7 |
changeHistorySettings: (e) ->
$input = $(e.currentTarget)
state = $input.is(':checked')
chatId = @tab.getChatId()
attendant = if not chatId then @tab.getAttendant().getPreferredResource() else null
App.Com.setHistory(App.Models.me.get('jid'), chatId, attendant, state, =>
@savingHistory = state
@_changeHistoryVisibly(state)
)
_changeHistoryVisibly: (currentState) ->
if currentState
toEnable = '.enabled'
toDisable = '.disabled'
else
toEnable = '.disabled'
toDisable = '.enabled'
$(@el).find(".action#{toEnable}, .current#{toEnable}").show() |
7a706f0e |
$(@el).find(".action#{toDisable}, .current#{toDisable}").hide()
prependHistoryMessage: (user, date, msg) ->
messageView = new Xmpp.Views.Chat.MessageView(user: user, date: date, message: msg, fromMe: user.get('jid') == App.Models.me.get('jid'))
$(@el).find('.messages').prepend(messageView.render().el)
@log(messageView, true)
loadHistory: ->
chatId = @tab.getChatId()
attendant = if not chatId then @tab.getAttendant().get('jid') else null
App.Com.loadHistory(App.Models.me.get('jid'), chatId, attendant, @historyStep, (result) =>
App.debug result
_.each(result.history.reverse(), (message) =>
jid = App.stripJid(message.from)
if App.Models.me.get('jid') == jid
contact = App.Models.me
else
contact = App.Collections.contacts.findByJid(jid) || Xmpp.Collections.ChatsCollection.createTempContact(jid)
@prependHistoryMessage(contact, new Date(message.created_at), message.message)
)
@historyStep++ unless _.isEmpty(result.history)
) |