2012-12-11 00:34:06 +08:00
|
|
|
define [
|
|
|
|
'jquery',
|
|
|
|
'underscore',
|
|
|
|
'backbone',
|
2012-12-11 02:28:30 +08:00
|
|
|
'globals',
|
2012-12-15 08:58:14 +08:00
|
|
|
'text!templates/session_chat.html',
|
2012-12-11 02:28:30 +08:00
|
|
|
'text!templates/chat_message.html',
|
2012-12-15 08:58:14 +08:00
|
|
|
], ($, _, Backbone, globals, sessionChatTemplate, chatMessageTemplate) ->
|
2012-12-11 00:34:06 +08:00
|
|
|
|
|
|
|
# The chat panel in a session
|
|
|
|
# The contents are rendered by SessionView, this class is Used to
|
|
|
|
# manage the events in the chat.
|
|
|
|
SessionChatView = Backbone.View.extend
|
|
|
|
events:
|
2012-12-15 08:41:30 +08:00
|
|
|
"click button#chat-send": "_sendMessage"
|
|
|
|
"keyup #chat-input-box": "_inputKeyPressed"
|
2012-12-11 02:28:30 +08:00
|
|
|
|
|
|
|
initialize: ->
|
|
|
|
@chatInputID = "#chat-input-box"
|
|
|
|
@msgBoxID = "#chat-messages"
|
|
|
|
|
|
|
|
# Bind to the event triggered when the client connects to the server
|
|
|
|
globals.connection.bind "connection:connected",
|
2012-12-15 08:41:30 +08:00
|
|
|
@_registerConnectionEvents, @
|
|
|
|
|
|
|
|
render: ->
|
2012-12-15 08:58:14 +08:00
|
|
|
data = { auth: globals.currentAuth }
|
|
|
|
compiledTemplate = _.template(sessionChatTemplate, data)
|
|
|
|
@$el.html compiledTemplate
|
2012-12-11 02:28:30 +08:00
|
|
|
|
|
|
|
# Registers listeners for events in the application socket.
|
2012-12-15 08:41:30 +08:00
|
|
|
_registerConnectionEvents: ->
|
2012-12-11 02:28:30 +08:00
|
|
|
socket = globals.connection.socket
|
|
|
|
|
|
|
|
# Received event for a new public chat message
|
|
|
|
# @param {string} name name of user
|
|
|
|
# @param {string} msg message to be displayed
|
|
|
|
socket.on "msg", (name, msg) =>
|
2012-12-15 08:41:30 +08:00
|
|
|
@_addChatMessage(name, msg)
|
|
|
|
@_scrollToBottom()
|
2012-12-11 02:28:30 +08:00
|
|
|
|
|
|
|
# Received event to update all the messages in the chat box
|
|
|
|
# @param {Array} messages Array of messages in public chat box
|
|
|
|
socket.on "all_messages", (messages) =>
|
|
|
|
for msgBlock in messages
|
2012-12-15 08:41:30 +08:00
|
|
|
@_addChatMessage(msgBlock.username, msgBlock.message)
|
|
|
|
@_scrollToBottom()
|
2012-12-11 00:34:06 +08:00
|
|
|
|
2012-12-12 10:15:14 +08:00
|
|
|
# TODO: for now these messages are only being shown in the chat, maybe
|
|
|
|
# they should have their own view and do more stuff
|
|
|
|
# (e.g. disable the interface when disconnected)
|
|
|
|
socket.on "connect", =>
|
2012-12-15 08:41:30 +08:00
|
|
|
@_addChatMessage("system", "Connected to the server.")
|
2012-12-12 10:15:14 +08:00
|
|
|
socket.on "disconnect", =>
|
2012-12-15 08:41:30 +08:00
|
|
|
@_addChatMessage("system", "Disconnected form the server.")
|
2012-12-12 10:15:14 +08:00
|
|
|
socket.on "reconnect", =>
|
2012-12-15 08:41:30 +08:00
|
|
|
@_addChatMessage("system", "Reconnected!")
|
2012-12-12 10:15:14 +08:00
|
|
|
socket.on "reconnecting", =>
|
2012-12-15 08:41:30 +08:00
|
|
|
@_addChatMessage("system", "Reconnecting...")
|
2012-12-12 10:15:14 +08:00
|
|
|
socket.on "reconnect_failed", =>
|
2012-12-15 08:41:30 +08:00
|
|
|
@_addChatMessage("system", "Reconnect failed!")
|
2012-12-11 00:34:06 +08:00
|
|
|
|
2012-12-11 02:28:30 +08:00
|
|
|
# Send a chat message
|
2012-12-15 08:41:30 +08:00
|
|
|
_sendMessage: ->
|
2012-12-11 09:29:56 +08:00
|
|
|
$chatInput = @$(@chatInputID)
|
2012-12-11 02:28:30 +08:00
|
|
|
msg = $chatInput.val()
|
|
|
|
if msg? and msg.trim() isnt ""
|
|
|
|
globals.connection.emitMsg msg
|
|
|
|
$chatInput.val("")
|
|
|
|
$chatInput.focus()
|
|
|
|
|
2012-12-11 09:29:56 +08:00
|
|
|
# Add a message to the screen and scroll the chat area to bottom
|
2012-12-15 08:41:30 +08:00
|
|
|
_addChatMessage: (username, message) ->
|
2012-12-11 02:28:30 +08:00
|
|
|
data =
|
|
|
|
username: username
|
|
|
|
message: message
|
|
|
|
compiledTemplate = _.template(chatMessageTemplate, data)
|
2012-12-11 09:29:56 +08:00
|
|
|
@$(@msgBoxID).append compiledTemplate
|
2012-12-11 02:28:30 +08:00
|
|
|
|
|
|
|
# Scrolls the chat area to bottom to show the last messages
|
2012-12-15 08:41:30 +08:00
|
|
|
_scrollToBottom: ->
|
2012-12-11 09:29:56 +08:00
|
|
|
$msgBox = @$(@msgBoxID)
|
2012-12-11 02:28:30 +08:00
|
|
|
$msgBox.prop({ scrollTop: $msgBox.prop("scrollHeight") })
|
|
|
|
|
|
|
|
# A key was pressed in the input box
|
2012-12-15 08:41:30 +08:00
|
|
|
_inputKeyPressed: (e) ->
|
2012-12-11 02:28:30 +08:00
|
|
|
# Send message when the enter key is pressed
|
2012-12-15 08:41:30 +08:00
|
|
|
@_sendMessage() if e.keyCode is 13
|
2012-12-11 00:34:06 +08:00
|
|
|
|
2012-12-15 08:41:30 +08:00
|
|
|
# TODO: limit/show length of text in chatbox
|
2012-12-11 02:28:30 +08:00
|
|
|
# $("#chat-input-box").on "keyup", (e) ->
|
2012-12-11 00:34:06 +08:00
|
|
|
# count = $(this).attr("maxlength")
|
|
|
|
# chcount.innerHTML = max - chatbox.value.length
|
|
|
|
|
|
|
|
SessionChatView
|