2014-10-09 04:09:28 +08:00
|
|
|
# --------------------------------------------------------------------------------------------------------------------
|
|
|
|
# If a function's last line is the statement false that represents the function returning false
|
|
|
|
# A function such as a click handler will continue along with the propogation and default behaivour if not stopped
|
|
|
|
# Returning false stops propogation/prevents default. You cannot always use the event object to call these methods
|
2014-11-06 12:51:01 +08:00
|
|
|
# Because most Meteor event handlers set the event object to the exact context of the event which does not
|
2014-10-09 04:09:28 +08:00
|
|
|
# allow you to simply call these methods.
|
|
|
|
# --------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
2014-10-28 22:29:11 +08:00
|
|
|
@activateBreakLines = (str) ->
|
|
|
|
if typeof str is 'string'
|
|
|
|
res = str.replace /\\n/gim, '<br/>'
|
|
|
|
res = res.replace /\r/gim, '<br/>'
|
2014-09-23 03:35:53 +08:00
|
|
|
|
2014-10-28 22:29:11 +08:00
|
|
|
@detectUnreadChat = ->
|
|
|
|
#if the current tab is not the same as the tab we just published in
|
|
|
|
Meteor.Chat.find({}).observe({
|
|
|
|
added: (chatMessage) =>
|
|
|
|
findDestinationTab = ->
|
|
|
|
if chatMessage.message?.chat_type is "PUBLIC_CHAT"
|
|
|
|
"PUBLIC_CHAT"
|
|
|
|
else
|
|
|
|
chatMessage.message?.from_userid
|
2014-12-01 10:39:33 +08:00
|
|
|
Tracker.autorun (comp) ->
|
2014-12-20 01:23:24 +08:00
|
|
|
tabsTime = getInSession('tabsRenderedTime')
|
|
|
|
if tabsTime? and chatMessage.message.from_userid isnt "SYSTEM_MESSAGE" and chatMessage.message.from_time - tabsTime > 0
|
|
|
|
populateChatTabs(chatMessage) # check if we need to open a new tab
|
|
|
|
destinationTab = findDestinationTab()
|
|
|
|
if destinationTab isnt getInSession "inChatWith"
|
|
|
|
setInSession 'chatTabs', getInSession('chatTabs').map((tab) ->
|
|
|
|
tab.gotMail = true if tab.userId is destinationTab
|
|
|
|
tab
|
|
|
|
)
|
|
|
|
comp.stop()
|
2014-10-28 22:29:11 +08:00
|
|
|
})
|
2014-08-09 00:04:04 +08:00
|
|
|
|
2014-10-16 06:26:27 +08:00
|
|
|
# This method returns all messages for the user. It looks at the session to determine whether the user is in
|
2014-10-28 22:29:11 +08:00
|
|
|
# private or public chat. If true is passed, messages returned are from before the user joined. Else, the messages are from after the user joined
|
2014-10-16 06:26:27 +08:00
|
|
|
@getFormattedMessagesForChat = ->
|
2014-12-17 08:20:13 +08:00
|
|
|
chattingWith = getInSession('inChatWith')
|
|
|
|
if chattingWith is 'PUBLIC_CHAT' # find all public and system messages
|
2014-12-17 08:29:58 +08:00
|
|
|
return Meteor.Chat.find({'message.chat_type': $in: ["SYSTEM_MESSAGE","PUBLIC_CHAT"]},{sort: {'message.from_time': 1}}).fetch()
|
2014-10-16 06:26:27 +08:00
|
|
|
else
|
2014-12-17 08:20:13 +08:00
|
|
|
unless chattingWith is 'OPTIONS'
|
|
|
|
return Meteor.Chat.find({'message.chat_type': 'PRIVATE_CHAT', $or: [{'message.to_userid': chattingWith},{'message.from_userid': chattingWith}]}).fetch()
|
2014-10-16 06:26:27 +08:00
|
|
|
|
2014-10-28 22:29:11 +08:00
|
|
|
# Scrolls the message container to the bottom. The number of pixels to scroll down is the height of the container
|
|
|
|
Handlebars.registerHelper "autoscroll", ->
|
|
|
|
$('#chatbody').scrollTop($('#chatbody')[0]?.scrollHeight)
|
|
|
|
false
|
|
|
|
|
|
|
|
Handlebars.registerHelper "grabChatTabs", ->
|
2014-11-22 11:26:02 +08:00
|
|
|
if getInSession('chatTabs') is undefined
|
|
|
|
initTabs = [
|
|
|
|
userId: "PUBLIC_CHAT"
|
|
|
|
name: "Public"
|
|
|
|
gotMail: false
|
|
|
|
class: "publicChatTab"
|
|
|
|
,
|
|
|
|
userId: "OPTIONS"
|
|
|
|
name: "Options"
|
|
|
|
gotMail: false
|
|
|
|
class: "optionsChatTab"
|
|
|
|
]
|
|
|
|
setInSession 'chatTabs', initTabs
|
|
|
|
getInSession('chatTabs')[0..3]
|
2014-10-28 22:29:11 +08:00
|
|
|
|
2015-01-16 00:10:44 +08:00
|
|
|
@resizeChatbar = ->
|
|
|
|
if window.matchMedia('(orientation: landscape)').matches
|
|
|
|
chat = $('#chat')
|
|
|
|
navbarHeight = $('#navbar').height()
|
|
|
|
footerHeight = $('#footer').height()
|
|
|
|
bodyHeight = $('body').height()
|
|
|
|
margins = parseInt(chat.css('margin-top'))*2 # *2 for top & bottom
|
|
|
|
paddingSpace = 10
|
|
|
|
windowHeight = ( bodyHeight - ( navbarHeight + footerHeight + margins + paddingSpace ) )
|
|
|
|
|
|
|
|
chat.height( windowHeight + 'px')
|
|
|
|
$("#chatbody").height( (windowHeight- ($("#chatInput").outerHeight()*2)) + 'px')
|
|
|
|
|
2014-10-28 22:29:11 +08:00
|
|
|
@sendMessage = ->
|
|
|
|
message = linkify $('#newMessageInput').val() # get the message from the input box
|
|
|
|
unless (message?.length > 0 and (/\S/.test(message))) # check the message has content and it is not whitespace
|
|
|
|
return # do nothing if invalid message
|
|
|
|
|
|
|
|
chattingWith = getInSession('inChatWith')
|
|
|
|
|
|
|
|
if chattingWith isnt "PUBLIC_CHAT"
|
2014-11-29 00:40:14 +08:00
|
|
|
toUsername = Meteor.Users.findOne(userId: chattingWith)?.user.name
|
2014-10-28 22:29:11 +08:00
|
|
|
|
|
|
|
messageForServer = { # construct message for server
|
|
|
|
"message": message
|
|
|
|
"chat_type": if chattingWith is "PUBLIC_CHAT" then "PUBLIC_CHAT" else "PRIVATE_CHAT"
|
2014-11-27 06:49:21 +08:00
|
|
|
"from_userid": getInSession("userId")
|
2014-11-12 21:30:34 +08:00
|
|
|
"from_username": BBB.getMyUserName()
|
2014-10-28 22:29:11 +08:00
|
|
|
"from_tz_offset": "240"
|
2014-11-29 00:40:14 +08:00
|
|
|
"to_username": if chattingWith is "PUBLIC_CHAT" then "public_chat_username" else toUsername
|
2014-10-28 22:29:11 +08:00
|
|
|
"to_userid": if chattingWith is "PUBLIC_CHAT" then "public_chat_userid" else chattingWith
|
|
|
|
"from_lang": "en"
|
|
|
|
"from_time": getTime()
|
|
|
|
"from_color": "0x000000"
|
|
|
|
# "from_color": "0x#{getInSession("messageColor")}"
|
|
|
|
}
|
|
|
|
|
2014-11-27 06:49:21 +08:00
|
|
|
Meteor.call "sendChatMessagetoServer", getInSession("meetingId"), messageForServer, getInSession("userId"), getInSession("authToken")
|
|
|
|
|
2014-10-28 22:29:11 +08:00
|
|
|
$('#newMessageInput').val '' # Clear message box
|
|
|
|
|
2014-10-16 06:26:27 +08:00
|
|
|
Template.chatbar.helpers
|
2014-09-23 03:35:53 +08:00
|
|
|
getCombinedMessagesForChat: ->
|
2014-10-16 06:26:27 +08:00
|
|
|
msgs = getFormattedMessagesForChat()
|
2014-12-17 08:20:13 +08:00
|
|
|
len = msgs?.length # get length of messages
|
2014-09-23 03:35:53 +08:00
|
|
|
i = 0
|
|
|
|
while i < len # Must be a do while, for loop compiles and stores the length of array which can change inside the loop!
|
|
|
|
if msgs[i].message.from_userid isnt 'System' # skip system messages
|
|
|
|
j = i+1 # Start looking at messages right after the current one
|
|
|
|
|
|
|
|
while j < len
|
|
|
|
deleted = false
|
|
|
|
if msgs[j].message.from_userid isnt 'System' # Ignore system messages
|
|
|
|
# Check if the time discrepancy between the two messages exceeds window for grouping
|
|
|
|
if (parseFloat(msgs[j].message.from_time)-parseFloat(msgs[i].message.from_time)) >= 60000 # 60 seconds/1 minute
|
|
|
|
break # Messages are too far between, so them seperated and stop joining here
|
|
|
|
|
|
|
|
if msgs[i].message.from_userid is msgs[j].message.from_userid # Both messages are from the same user
|
2014-10-11 01:49:17 +08:00
|
|
|
msgs[i].message.message += "\r#{msgs[j].message.message}" # Combine the messages
|
2014-09-23 03:35:53 +08:00
|
|
|
msgs.splice(j,1) # Delete the message from the collection
|
2014-10-08 07:09:33 +08:00
|
|
|
deleted = true
|
2014-09-23 03:35:53 +08:00
|
|
|
else break # Messages are from different people, move on
|
|
|
|
#
|
|
|
|
else break # This is the break point in the chat, don't merge
|
|
|
|
#
|
|
|
|
len = msgs.length
|
|
|
|
++j if not deleted
|
|
|
|
#
|
|
|
|
++i
|
|
|
|
len = msgs.length
|
|
|
|
|
|
|
|
msgs
|
|
|
|
|
2014-12-18 05:09:12 +08:00
|
|
|
userExists: ->
|
|
|
|
if getInSession('inChatWith') in ["PUBLIC_CHAT", "OPTIONS"]
|
|
|
|
return true
|
|
|
|
else
|
|
|
|
return Meteor.Users.findOne({userId: getInSession('inChatWith')})?
|
|
|
|
|
2014-12-20 01:23:24 +08:00
|
|
|
# When chatbar gets rendered, launch the auto-check for unread chat
|
2014-09-12 00:52:33 +08:00
|
|
|
Template.chatbar.rendered = ->
|
2014-10-16 22:38:50 +08:00
|
|
|
detectUnreadChat()
|
2015-01-16 00:10:44 +08:00
|
|
|
resizeChatbar()
|
2014-12-20 01:23:24 +08:00
|
|
|
|
|
|
|
# When message gets rendered, scroll to the bottom
|
|
|
|
Template.message.rendered = ->
|
2014-09-23 03:35:53 +08:00
|
|
|
$('#chatbody').scrollTop($('#chatbody')[0]?.scrollHeight)
|
|
|
|
false
|
2014-10-28 22:29:11 +08:00
|
|
|
|
|
|
|
Template.chatInput.events
|
|
|
|
'click #sendMessageButton': (event) ->
|
2014-12-20 07:51:31 +08:00
|
|
|
$('#sendMessageButton').blur()
|
2014-10-28 22:29:11 +08:00
|
|
|
sendMessage()
|
|
|
|
|
|
|
|
'keypress #newMessageInput': (event) -> # user pressed a button inside the chatbox
|
2014-12-16 23:11:23 +08:00
|
|
|
key = (if event.charCode then event.charCode else (if event.keyCode then event.keyCode else 0))
|
2014-12-16 22:43:03 +08:00
|
|
|
|
|
|
|
if event.shiftKey and (key is 13)
|
2014-12-16 23:11:23 +08:00
|
|
|
event.preventDefault()
|
2014-10-28 22:29:11 +08:00
|
|
|
$("#newMessageInput").append("\r") # Change newline character
|
|
|
|
return
|
2014-11-06 12:51:01 +08:00
|
|
|
|
2014-12-16 22:43:03 +08:00
|
|
|
if key is 13 # Check for pressing enter to submit message
|
2014-12-16 23:11:23 +08:00
|
|
|
event.preventDefault()
|
2014-10-28 22:29:11 +08:00
|
|
|
sendMessage()
|
|
|
|
$('#newMessageInput').val("")
|
|
|
|
return false
|
|
|
|
|
|
|
|
Template.chatInput.rendered = ->
|
2015-01-12 10:47:27 +08:00
|
|
|
$('input[rel=tooltip]').tooltip()
|
|
|
|
$('button[rel=tooltip]').tooltip()
|
|
|
|
$("#newMessageInput").focus()
|
2014-10-28 22:29:11 +08:00
|
|
|
|
|
|
|
Template.extraConversations.events
|
2014-11-04 21:45:23 +08:00
|
|
|
"click .extraConversation": (event) ->
|
|
|
|
console.log "extra conversation"
|
|
|
|
user = @
|
|
|
|
console.log user
|
|
|
|
console.log "#{user.name} #{user.userId}"
|
|
|
|
# put this conversation in the 3rd position in the chat tabs collection (after public and options)
|
|
|
|
# Take all the tabs and turn into an array
|
2014-11-22 11:26:02 +08:00
|
|
|
tabArray = getInSession('chatTabs')
|
2014-11-04 21:45:23 +08:00
|
|
|
|
|
|
|
# find the index of the selected tab
|
|
|
|
index = do ->
|
|
|
|
for value, idx in tabArray
|
|
|
|
if value.userId is user.userId
|
|
|
|
selected = value
|
|
|
|
return idx
|
|
|
|
null
|
|
|
|
|
|
|
|
if index?
|
|
|
|
# take object
|
|
|
|
selected = tabArray[index]
|
|
|
|
|
|
|
|
if selected?
|
|
|
|
# remove it
|
|
|
|
tabArray.splice(index, 1)
|
|
|
|
# insert it at the 3rd index
|
|
|
|
tabArray.splice(2, 0, selected)
|
2014-11-22 11:26:02 +08:00
|
|
|
# update collection
|
|
|
|
setInSession 'chatTabs', tabArray
|
2014-10-28 22:29:11 +08:00
|
|
|
|
|
|
|
Template.extraConversations.helpers
|
|
|
|
getExtraConversations: ->
|
2014-11-22 11:26:02 +08:00
|
|
|
getInSession('chatTabs')[4..]
|
2014-10-28 22:29:11 +08:00
|
|
|
|
|
|
|
tooManyConversations: ->
|
2014-11-22 11:26:02 +08:00
|
|
|
return false if getInSession('chatTabs') is undefined
|
|
|
|
getInSession('chatTabs').length > 4
|
2014-10-28 22:29:11 +08:00
|
|
|
|
|
|
|
Template.message.helpers
|
|
|
|
sanitizeAndFormat: (str) ->
|
|
|
|
if typeof str is 'string'
|
|
|
|
# First, replace replace all tags with the ascii equivalent (excluding those involved in anchor tags)
|
|
|
|
res = str.replace(/&/g, '&').replace(/<(?![au\/])/g, '<').replace(/\/([^au])>/g, '$1>').replace(/([^=])"(?!>)/g, '$1"');
|
|
|
|
res = toClickable res
|
|
|
|
res = activateBreakLines res
|
|
|
|
|
|
|
|
toClockTime: (epochTime) ->
|
|
|
|
if epochTime is null
|
|
|
|
return ""
|
|
|
|
local = new Date()
|
|
|
|
offset = local.getTimezoneOffset()
|
|
|
|
epochTime = epochTime - offset * 60000 # 1 min = 60 s = 60,000 ms
|
|
|
|
dateObj = new Date(epochTime)
|
|
|
|
hours = dateObj.getUTCHours()
|
|
|
|
minutes = dateObj.getUTCMinutes()
|
|
|
|
if minutes < 10
|
|
|
|
minutes = "0" + minutes
|
|
|
|
hours + ":" + minutes
|
2014-07-04 01:56:16 +08:00
|
|
|
|
|
|
|
Template.optionsBar.events
|
2014-09-23 03:35:53 +08:00
|
|
|
'click .private-chat-user-entry': (event) -> # clicked a user's name to begin private chat
|
2014-12-12 07:21:51 +08:00
|
|
|
tabs = getInSession('chatTabs')
|
|
|
|
_this = @
|
|
|
|
|
|
|
|
# if you are starting a private chat
|
|
|
|
if tabs.filter((tab) -> tab.userId is _this.userId).length is 0
|
|
|
|
userName = Meteor.Users.findOne({userId: _this.userId})?.user?.name
|
|
|
|
tabs.push {userId: _this.userId, name: userName, gotMail: false, class: 'privateChatTab'}
|
|
|
|
setInSession 'chatTabs', tabs
|
|
|
|
|
2014-09-23 03:35:53 +08:00
|
|
|
setInSession 'display_chatPane', true
|
2014-12-12 07:21:51 +08:00
|
|
|
setInSession "inChatWith", _this.userId
|
2014-07-10 21:11:38 +08:00
|
|
|
|
2014-10-09 21:45:49 +08:00
|
|
|
Template.optionsBar.helpers
|
|
|
|
thereArePeopletoChatWith: -> # Subtract 1 for the current user. Returns whether there are other people in the chat
|
|
|
|
# TODO: Add a check for the count to only include users who are allowed to private chat
|
|
|
|
(Meteor.Users.find({'meetingId': getInSession("meetingId")}).count()-1) >= 1
|
|
|
|
|
2014-08-16 00:26:17 +08:00
|
|
|
Template.optionsBar.rendered = ->
|
2014-09-23 03:35:53 +08:00
|
|
|
$('div[rel=tooltip]').tooltip()
|
2014-08-16 00:26:17 +08:00
|
|
|
|
2014-08-16 01:17:51 +08:00
|
|
|
Template.optionsFontSize.events
|
2014-09-23 03:35:53 +08:00
|
|
|
"click .fontSizeSelector": (event) ->
|
|
|
|
selectedFontSize = parseInt(event.target.id)
|
|
|
|
if selectedFontSize
|
|
|
|
setInSession "messageFontSize", selectedFontSize
|
|
|
|
else setInSession "messageFontSize", 12
|
2014-08-16 01:17:51 +08:00
|
|
|
|
2014-08-09 00:04:04 +08:00
|
|
|
Template.tabButtons.events
|
|
|
|
'click .close': (event) -> # user closes private chat
|
|
|
|
setInSession 'inChatWith', 'PUBLIC_CHAT'
|
2014-08-14 02:30:55 +08:00
|
|
|
setInSession 'display_chatPane', true
|
2014-10-21 20:22:54 +08:00
|
|
|
console.log "userId: #{@userId}"
|
2014-11-22 11:26:02 +08:00
|
|
|
_this = @
|
|
|
|
tabs = getInSession('chatTabs')
|
|
|
|
if tabs.filter((tab) -> tab.userId is _this.userId).length > 0
|
|
|
|
tabs = $.grep(tabs, (t) -> t.userId isnt _this.userId)
|
|
|
|
setInSession 'chatTabs', tabs
|
2014-09-26 01:57:58 +08:00
|
|
|
|
2014-08-14 02:30:55 +08:00
|
|
|
return false # stops propogation/prevents default
|
2014-08-09 00:04:04 +08:00
|
|
|
|
2014-10-28 22:29:11 +08:00
|
|
|
'click .gotUnreadMail': (event) ->
|
2014-11-22 11:26:02 +08:00
|
|
|
#chatTabs.update({userId: @userId}, {$set: {gotMail: false}})
|
|
|
|
_this = @
|
|
|
|
setInSession 'chatTabs', getInSession('chatTabs').map((tab) ->
|
|
|
|
tab.gotMail = false if tab.userId is _this.userId
|
|
|
|
tab
|
|
|
|
)
|
2014-10-28 22:29:11 +08:00
|
|
|
|
2014-08-09 00:04:04 +08:00
|
|
|
'click .optionsChatTab': (event) ->
|
2014-10-22 02:37:31 +08:00
|
|
|
console.log "options"
|
|
|
|
setInSession "inChatWith", "OPTIONS"
|
2014-08-09 00:04:04 +08:00
|
|
|
setInSession 'display_chatPane', false
|
|
|
|
|
|
|
|
'click .privateChatTab': (event) ->
|
2014-12-12 07:21:51 +08:00
|
|
|
console.log "private:"
|
2014-10-22 02:37:31 +08:00
|
|
|
console.log @
|
2014-11-04 23:38:18 +08:00
|
|
|
setInSession "inChatWith", @userId
|
2014-08-09 00:04:04 +08:00
|
|
|
setInSession 'display_chatPane', true
|
|
|
|
|
|
|
|
'click .publicChatTab': (event) ->
|
2014-10-22 02:37:31 +08:00
|
|
|
console.log "public"
|
|
|
|
setInSession "inChatWith", "PUBLIC_CHAT"
|
2014-08-09 00:04:04 +08:00
|
|
|
setInSession 'display_chatPane', true
|
|
|
|
|
2014-11-06 12:51:01 +08:00
|
|
|
'click .tab': (event) ->
|
2014-12-13 00:20:06 +08:00
|
|
|
unless getInSession "inChatWith" is "OPTIONS"
|
|
|
|
$("#newMessageInput").focus()
|
|
|
|
console.log "tab"
|
2014-09-26 01:24:42 +08:00
|
|
|
|
2014-07-09 21:18:52 +08:00
|
|
|
Template.tabButtons.helpers
|
2014-11-06 12:51:01 +08:00
|
|
|
hasGotUnreadMailClass: (gotMail) ->
|
2014-12-20 05:07:36 +08:00
|
|
|
if gotMail
|
2014-11-06 12:51:01 +08:00
|
|
|
return "gotUnreadMail"
|
|
|
|
else
|
|
|
|
return ""
|
2014-07-07 23:30:17 +08:00
|
|
|
|
2014-11-06 23:27:43 +08:00
|
|
|
isTabActive: (userId) ->
|
2014-12-12 07:21:51 +08:00
|
|
|
if getInSession("inChatWith") is userId
|
|
|
|
return "active"
|
2014-11-06 23:27:43 +08:00
|
|
|
|
|
|
|
makeSafe: (string) ->
|
|
|
|
safeString(string)
|
|
|
|
|
2014-12-01 10:39:33 +08:00
|
|
|
Template.tabButtons.rendered = ->
|
|
|
|
Tracker.autorun (comp) ->
|
|
|
|
setInSession 'tabsRenderedTime', TimeSync.serverTime()
|
|
|
|
if getInSession('tabsRenderedTime') isnt undefined
|
|
|
|
comp.stop()
|
|
|
|
|
2014-10-16 22:38:50 +08:00
|
|
|
# make links received from Flash client clickable in HTML
|
|
|
|
@toClickable = (str) ->
|
2014-10-16 23:11:09 +08:00
|
|
|
if typeof str is 'string'
|
|
|
|
res = str.replace /<a href='event:/gim, "<a target='_blank' href='"
|
|
|
|
res = res.replace /<a href="event:/gim, '<a target="_blank" href="'
|
2014-10-16 22:38:50 +08:00
|
|
|
|
|
|
|
Template.message.helpers
|
2014-09-23 03:35:53 +08:00
|
|
|
toClockTime: (epochTime) ->
|
|
|
|
if epochTime is null
|
|
|
|
return ""
|
|
|
|
local = new Date()
|
|
|
|
offset = local.getTimezoneOffset()
|
|
|
|
epochTime = epochTime - offset * 60000 # 1 min = 60 s = 60,000 ms
|
|
|
|
dateObj = new Date(epochTime)
|
|
|
|
hours = dateObj.getUTCHours()
|
|
|
|
minutes = dateObj.getUTCMinutes()
|
|
|
|
if minutes < 10
|
|
|
|
minutes = "0" + minutes
|
|
|
|
hours + ":" + minutes
|
|
|
|
|
|
|
|
sanitizeAndFormat: (str) ->
|
2014-10-16 23:11:09 +08:00
|
|
|
if typeof str is 'string'
|
2014-10-16 22:38:50 +08:00
|
|
|
# First, replace replace all tags with the ascii equivalent (excluding those involved in anchor tags)
|
2014-10-16 23:11:09 +08:00
|
|
|
res = str.replace(/&/g, '&').replace(/<(?![au\/])/g, '<').replace(/\/([^au])>/g, '$1>').replace(/([^=])"(?!>)/g, '$1"');
|
2014-10-16 22:38:50 +08:00
|
|
|
res = toClickable res
|
|
|
|
res = activateBreakLines res
|