2014-07-03 00:17:05 +08:00
|
|
|
|
Template.messageBar.helpers
|
2014-07-11 23:53:49 +08:00
|
|
|
|
getMessagesInChat: (beforeJoin=true) ->
|
2014-07-09 21:18:52 +08:00
|
|
|
|
friend = chattingWith = Session.get('inChatWith') # the recipient(s) of the messages
|
|
|
|
|
|
2014-07-11 23:53:49 +08:00
|
|
|
|
if chattingWith is 'PUBLIC_CHAT' # find all public messages
|
|
|
|
|
if beforeJoin
|
|
|
|
|
Meteor.Chat.find({'message.chat_type': chattingWith, 'message.from_time': {$lt: String(Session.get("joinedAt"))}})
|
|
|
|
|
else
|
|
|
|
|
Meteor.Chat.find({'message.chat_type': chattingWith, 'message.from_time': {$gt: String(Session.get("joinedAt"))}})
|
2014-07-09 21:18:52 +08:00
|
|
|
|
else
|
|
|
|
|
me = Session.get "userId"
|
|
|
|
|
Meteor.Chat.find({ # find all messages between current user and recipient
|
|
|
|
|
'message.chat_type': 'PRIVATE_CHAT',
|
|
|
|
|
$or: [{'message.from_userid': me, 'message.to_userid': friend},{'message.from_userid': friend, 'message.to_userid': me}]
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
isUserInPrivateChat: -> # true if user is in public chat
|
|
|
|
|
Session.get('inChatWith') isnt "PUBLIC_CHAT"
|
2014-07-03 00:17:05 +08:00
|
|
|
|
|
2014-07-02 23:41:12 +08:00
|
|
|
|
# Must be be called when template is finished rendering or will not work
|
|
|
|
|
Template.messageBar.rendered = -> # Scroll down the messages box the amount of its height, which places it at the bottom
|
2014-07-09 21:18:52 +08:00
|
|
|
|
height = $('#chatScrollWindow').height()
|
|
|
|
|
$('#chatScrollWindow').scrollTop(height)
|
2014-06-25 21:28:15 +08:00
|
|
|
|
|
2014-07-02 23:41:12 +08:00
|
|
|
|
Template.tabButtons.events
|
2014-07-10 21:11:38 +08:00
|
|
|
|
'click .tab': (event) ->
|
|
|
|
|
# $('.tab').removeClass('active')
|
|
|
|
|
|
|
|
|
|
|
2014-07-09 21:18:52 +08:00
|
|
|
|
'click .publicChatTab': (event) ->
|
|
|
|
|
Session.set 'display_chatPane', true
|
|
|
|
|
Session.set 'inChatWith', 'PUBLIC_CHAT'
|
2014-07-10 21:11:38 +08:00
|
|
|
|
Meteor.call 'invalidateAllTabs', Session.get('userId'), false
|
|
|
|
|
toUpdate = Meteor.ChatTabs.findOne({name:"Public"})
|
|
|
|
|
Meteor.ChatTabs.update({_id: toUpdate._id}, {$set: 'isActive':true})
|
2014-07-04 01:56:16 +08:00
|
|
|
|
|
2014-07-09 21:18:52 +08:00
|
|
|
|
'click .optionsChatTab': (event) ->
|
|
|
|
|
Session.set 'display_chatPane', false
|
2014-07-10 21:11:38 +08:00
|
|
|
|
Meteor.call 'invalidateAllTabs', Session.get('userId'), false
|
|
|
|
|
toUpdate = Meteor.ChatTabs.findOne({name:"Options"})
|
|
|
|
|
Meteor.ChatTabs.update({_id: toUpdate._id}, {$set: 'isActive':true})
|
2014-06-24 21:46:42 +08:00
|
|
|
|
|
2014-07-09 21:18:52 +08:00
|
|
|
|
'click .privateChatTab': (event) ->
|
|
|
|
|
Session.set 'display_chatPane', true
|
|
|
|
|
Session.set 'inChatWith', @userId
|
2014-07-10 21:11:38 +08:00
|
|
|
|
Meteor.call 'invalidateAllTabs', Session.get('userId'), false
|
|
|
|
|
console.log @name
|
|
|
|
|
toUpdate = Meteor.ChatTabs.findOne({name:@name})
|
2014-07-10 22:36:29 +08:00
|
|
|
|
if toUpdate? then Meteor.ChatTabs.update({_id: toUpdate._id}, {$set: 'isActive':true})
|
2014-07-07 23:30:17 +08:00
|
|
|
|
|
2014-07-09 21:18:52 +08:00
|
|
|
|
'click .close': (event) -> # user closes private chat
|
|
|
|
|
toRemove = Meteor.ChatTabs.findOne({name:@name})
|
2014-07-10 22:36:29 +08:00
|
|
|
|
if toRemove? then Meteor.ChatTabs.remove({_id: toRemove._id}) # should probably delete chat history here too?
|
|
|
|
|
|
|
|
|
|
Session.set 'display_chatPane', true
|
|
|
|
|
Session.set 'inChatWith', 'PUBLIC_CHAT'
|
|
|
|
|
Meteor.call 'invalidateAllTabs', Session.get('userId'), false
|
|
|
|
|
toUpdate = Meteor.ChatTabs.findOne({name:"Public"})
|
|
|
|
|
Meteor.ChatTabs.update({_id: toUpdate._id}, {$set: 'isActive':true})
|
|
|
|
|
event.stopPropogation()
|
2014-07-07 23:30:17 +08:00
|
|
|
|
|
2014-07-10 21:11:38 +08:00
|
|
|
|
|
2014-07-02 23:41:12 +08:00
|
|
|
|
Template.chatInput.events
|
2014-07-09 21:18:52 +08:00
|
|
|
|
'keypress #newMessageInput': (event) -> # user pressed a button inside the chatbox
|
2014-07-02 23:41:12 +08:00
|
|
|
|
if event.which is 13 # Check for pressing enter to submit message
|
2014-07-09 21:18:52 +08:00
|
|
|
|
chattingWith = Session.get 'inChatWith'
|
2014-06-27 01:13:19 +08:00
|
|
|
|
|
2014-07-02 23:41:12 +08:00
|
|
|
|
messageForServer = { # construct message for server
|
|
|
|
|
"message": $("#newMessageInput").val()
|
2014-07-09 21:18:52 +08:00
|
|
|
|
"chat_type": if chattingWith is "PUBLIC_CHAT" then "PUBLIC_CHAT" else "PRIVATE_CHAT"
|
2014-07-02 23:41:12 +08:00
|
|
|
|
"from_userid": Session.get "userId"
|
2014-07-09 21:18:52 +08:00
|
|
|
|
"from_username": getUsersName()
|
2014-07-02 23:41:12 +08:00
|
|
|
|
"from_tz_offset": "240"
|
2014-07-09 21:18:52 +08:00
|
|
|
|
"to_username": if chattingWith is "PUBLIC_CHAT" then "public_chat_username" else chattingWith
|
|
|
|
|
"to_userid": if chattingWith is "PUBLIC_CHAT" then "public_chat_userid" else chattingWith
|
2014-07-02 23:41:12 +08:00
|
|
|
|
"from_lang": "en"
|
2014-07-11 23:53:49 +08:00
|
|
|
|
"from_time": getTime()
|
2014-07-02 23:41:12 +08:00
|
|
|
|
"from_color": "0"
|
|
|
|
|
}
|
2014-07-11 23:53:49 +08:00
|
|
|
|
# console.log "time of join was " + Session.get("joinedAt")
|
2014-07-09 21:18:52 +08:00
|
|
|
|
# console.log 'Sending message to server:'
|
|
|
|
|
# console.log messageForServer
|
2014-07-02 23:41:12 +08:00
|
|
|
|
Meteor.call "sendChatMessagetoServer", Session.get("meetingId"), messageForServer
|
|
|
|
|
$('#newMessageInput').val '' # Clear message box
|
2014-07-04 01:56:16 +08:00
|
|
|
|
|
|
|
|
|
Template.optionsBar.events
|
|
|
|
|
'click .private-chat-user-entry': (event) -> # clicked a user's name to begin private chat
|
2014-07-09 21:18:52 +08:00
|
|
|
|
currUserId = Session.get "userId"
|
|
|
|
|
duplicate = Meteor.ChatTabs.findOne({'belongsTo':Session.get("userId"), 'userId': @userId})
|
2014-07-04 01:56:16 +08:00
|
|
|
|
|
2014-07-09 21:18:52 +08:00
|
|
|
|
if not duplicate and @userId isnt currUserId
|
|
|
|
|
messageForServer = {
|
|
|
|
|
"message": "Hey #{@user.name}, its #{getUsersName()} lets start a private chat."
|
|
|
|
|
"chat_type": "PRIVATE_CHAT"
|
|
|
|
|
"from_userid": Session.get "userId"
|
|
|
|
|
"from_username": getUsersName()
|
|
|
|
|
"from_tz_offset": "240"
|
|
|
|
|
"to_username": @user.name
|
|
|
|
|
"to_userid": @userId
|
|
|
|
|
"from_lang": "en"
|
2014-07-11 23:53:49 +08:00
|
|
|
|
"from_time": getTime()
|
2014-07-09 21:18:52 +08:00
|
|
|
|
"from_color": "0"
|
|
|
|
|
}
|
2014-07-04 01:56:16 +08:00
|
|
|
|
|
2014-07-09 21:18:52 +08:00
|
|
|
|
# console.log 'Sending private message to server:'
|
|
|
|
|
# console.log messageForServer
|
|
|
|
|
Meteor.call "sendChatMessagetoServer", Session.get("meetingId"), messageForServer
|
|
|
|
|
Meteor.call "invalidateAllTabs", currUserId, false
|
|
|
|
|
# Give tab to us
|
|
|
|
|
Meteor.ChatTabs.insert({belongsTo: currUserId, name: @user.name, isActive: true, class: "privateChatTab", 'userId': @userId})
|
|
|
|
|
# Give tab to recipient to notify them
|
|
|
|
|
Meteor.ChatTabs.insert({belongsTo: @userId, name: getUsersName(), isActive: false, class: "privateChatTab", 'userId': currUserId})
|
2014-07-10 22:36:29 +08:00
|
|
|
|
Session.set 'display_chatPane', true
|
2014-07-09 21:18:52 +08:00
|
|
|
|
Session.set "inChatWith", @userId
|
2014-07-10 21:11:38 +08:00
|
|
|
|
# $('.tab').removeClass('active')
|
|
|
|
|
# Todo:
|
|
|
|
|
# just need a way to make the newly created tab active
|
|
|
|
|
# don't know how to do that right here since it doesn't get created here
|
|
|
|
|
# once that's handled, private chat is essentially done
|
|
|
|
|
# $("#tabButtonContainer").append("<li>fsdfdsf| </li>")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-07-04 01:56:16 +08:00
|
|
|
|
|
2014-07-09 21:18:52 +08:00
|
|
|
|
Template.tabButtons.helpers
|
|
|
|
|
getChatbarTabs: ->
|
2014-07-10 21:11:38 +08:00
|
|
|
|
console.log "displaying tabs"
|
|
|
|
|
t = Meteor.ChatTabs.find({}).fetch()
|
|
|
|
|
# console.log JSON.stringify t
|
|
|
|
|
t
|
2014-07-04 01:56:16 +08:00
|
|
|
|
|
2014-07-09 21:18:52 +08:00
|
|
|
|
makeTabButton: -> # create tab button for private chat or other such as options
|
2014-07-10 21:11:38 +08:00
|
|
|
|
console.log "#{@name} is " + if @isActive then "active" else "not active"
|
|
|
|
|
button = '<li '
|
|
|
|
|
button += 'class="'
|
2014-07-09 21:18:52 +08:00
|
|
|
|
button += 'active ' if @isActive
|
2014-07-10 21:11:38 +08:00
|
|
|
|
button += "#{@class} tab\"><a href=\"#\" data-toggle=\"tab\">#{@name}"
|
2014-07-09 21:18:52 +08:00
|
|
|
|
button += ' <button class="close closeTab" type="button" >×</button>' if @name isnt 'Public' and @name isnt 'Options'
|
|
|
|
|
button += '</a></li>'
|
2014-07-10 21:11:38 +08:00
|
|
|
|
console.log "and here it is the button"
|
|
|
|
|
console.log button
|
2014-07-09 21:18:52 +08:00
|
|
|
|
button
|
2014-07-07 23:30:17 +08:00
|
|
|
|
|