2016-10-02 22:37:38 +08:00
|
|
|
/* global $ */
|
2016-09-23 00:16:15 +08:00
|
|
|
|
|
|
|
import Backbone from 'backbone'
|
2016-10-17 10:39:08 -04:00
|
|
|
import { Howl } from 'howler'
|
2016-09-23 10:37:59 +08:00
|
|
|
import Autolinker from 'autolinker'
|
2016-12-11 16:09:12 -05:00
|
|
|
import { clone, template as lodashTemplate } from 'lodash'
|
2016-09-29 09:33:13 +08:00
|
|
|
import outdent from 'outdent'
|
2016-09-23 00:16:15 +08:00
|
|
|
// TODO is this line good or bad
|
|
|
|
// Backbone.$ = window.$
|
|
|
|
|
2016-12-11 16:09:12 -05:00
|
|
|
const linker = new Autolinker({ newWindow: true, truncate: 50, email: false, phone: false })
|
2016-09-23 00:07:30 +08:00
|
|
|
|
|
|
|
var Private = {
|
2016-09-29 09:33:13 +08:00
|
|
|
messageHTML: outdent`
|
|
|
|
<div class='chat-message'>
|
|
|
|
<div class='chat-message-user'><img src='{{ user_image }}' title='{{user_name }}'/></div>
|
|
|
|
<div class='chat-message-text'>{{ message }}</div>
|
|
|
|
<div class='chat-message-time'>{{ timestamp }}</div>
|
|
|
|
<div class='clearfloat'></div>
|
|
|
|
</div>`,
|
|
|
|
participantHTML: outdent`
|
|
|
|
<div class='participant participant-{{ id }} {{ selfClass }}'>
|
|
|
|
<div class='chat-participant-image'>
|
|
|
|
<img src='{{ image }}' style='border: 2px solid {{ color }};' />
|
|
|
|
</div>
|
|
|
|
<div class='chat-participant-name'>
|
|
|
|
{{ username }} {{ selfName }}
|
|
|
|
</div>
|
|
|
|
<button type='button'
|
|
|
|
class='button chat-participant-invite-call'
|
|
|
|
onclick='Metamaps.Realtime.inviteACall({{ id}});'
|
|
|
|
></button>
|
|
|
|
<button type='button'
|
|
|
|
class='button chat-participant-invite-join'
|
|
|
|
onclick='Metamaps.Realtime.inviteToJoin({{ id}});'
|
|
|
|
></button>
|
|
|
|
<span class='chat-participant-participating'>
|
|
|
|
<div class='green-dot'></div>
|
|
|
|
</span>
|
|
|
|
<div class='clearfloat'></div>
|
|
|
|
</div>`,
|
2016-11-07 15:25:08 -05:00
|
|
|
templates: function() {
|
2016-12-11 16:09:12 -05:00
|
|
|
const templateSettings = {
|
2016-09-29 09:24:17 +08:00
|
|
|
interpolate: /\{\{(.+?)\}\}/g
|
2016-09-23 00:07:30 +08:00
|
|
|
}
|
2016-09-29 09:24:17 +08:00
|
|
|
|
2016-12-11 16:09:12 -05:00
|
|
|
this.messageTemplate = lodashTemplate(Private.messageHTML, templateSettings)
|
|
|
|
|
|
|
|
this.participantTemplate = lodashTemplate(Private.participantHTML, templateSettings)
|
2016-09-29 09:24:17 +08:00
|
|
|
},
|
2016-11-07 15:25:08 -05:00
|
|
|
createElements: function() {
|
2016-09-29 09:24:17 +08:00
|
|
|
this.$unread = $('<div class="chat-unread"></div>')
|
|
|
|
this.$button = $('<div class="chat-button"><div class="tooltips">Chat</div></div>')
|
|
|
|
this.$messageInput = $('<textarea placeholder="Send a message..." class="chat-input"></textarea>')
|
|
|
|
this.$juntoHeader = $('<div class="junto-header">PARTICIPANTS</div>')
|
|
|
|
this.$videoToggle = $('<div class="video-toggle"></div>')
|
|
|
|
this.$cursorToggle = $('<div class="cursor-toggle"></div>')
|
|
|
|
this.$participants = $('<div class="participants"></div>')
|
2016-09-29 09:33:13 +08:00
|
|
|
this.$conversationInProgress = $(outdent`
|
|
|
|
<div class="conversation-live">
|
|
|
|
LIVE
|
|
|
|
<span class="call-action leave" onclick="Metamaps.Realtime.leaveCall();">
|
|
|
|
LEAVE
|
|
|
|
</span>
|
|
|
|
<span class="call-action join" onclick="Metamaps.Realtime.joinCall();">
|
|
|
|
JOIN
|
|
|
|
</span>
|
|
|
|
</div>`)
|
2016-09-29 09:24:17 +08:00
|
|
|
this.$chatHeader = $('<div class="chat-header">CHAT</div>')
|
|
|
|
this.$soundToggle = $('<div class="sound-toggle"></div>')
|
|
|
|
this.$messages = $('<div class="chat-messages"></div>')
|
|
|
|
this.$container = $('<div class="chat-box"></div>')
|
|
|
|
},
|
2016-11-07 15:25:08 -05:00
|
|
|
attachElements: function() {
|
2016-09-29 09:24:17 +08:00
|
|
|
this.$button.append(this.$unread)
|
|
|
|
|
|
|
|
this.$juntoHeader.append(this.$videoToggle)
|
|
|
|
this.$juntoHeader.append(this.$cursorToggle)
|
|
|
|
|
|
|
|
this.$chatHeader.append(this.$soundToggle)
|
|
|
|
|
|
|
|
this.$participants.append(this.$conversationInProgress)
|
|
|
|
|
|
|
|
this.$container.append(this.$juntoHeader)
|
|
|
|
this.$container.append(this.$participants)
|
|
|
|
this.$container.append(this.$chatHeader)
|
|
|
|
this.$container.append(this.$button)
|
|
|
|
this.$container.append(this.$messages)
|
|
|
|
this.$container.append(this.$messageInput)
|
|
|
|
},
|
2016-11-07 15:25:08 -05:00
|
|
|
addEventListeners: function() {
|
2016-09-29 09:24:17 +08:00
|
|
|
var self = this
|
|
|
|
|
2016-11-07 15:25:08 -05:00
|
|
|
this.participants.on('add', function(participant) {
|
2016-09-29 09:24:17 +08:00
|
|
|
Private.addParticipant.call(self, participant)
|
|
|
|
})
|
|
|
|
|
2016-11-07 15:25:08 -05:00
|
|
|
this.participants.on('remove', function(participant) {
|
2016-09-29 09:24:17 +08:00
|
|
|
Private.removeParticipant.call(self, participant)
|
|
|
|
})
|
|
|
|
|
2016-11-07 15:25:08 -05:00
|
|
|
this.$button.on('click', function() {
|
2016-09-29 09:24:17 +08:00
|
|
|
Handlers.buttonClick.call(self)
|
|
|
|
})
|
2016-11-07 15:25:08 -05:00
|
|
|
this.$videoToggle.on('click', function() {
|
2016-09-29 09:24:17 +08:00
|
|
|
Handlers.videoToggleClick.call(self)
|
|
|
|
})
|
2016-11-07 15:25:08 -05:00
|
|
|
this.$cursorToggle.on('click', function() {
|
2016-09-29 09:24:17 +08:00
|
|
|
Handlers.cursorToggleClick.call(self)
|
|
|
|
})
|
2016-11-07 15:25:08 -05:00
|
|
|
this.$soundToggle.on('click', function() {
|
2016-09-29 09:24:17 +08:00
|
|
|
Handlers.soundToggleClick.call(self)
|
|
|
|
})
|
2016-11-07 15:25:08 -05:00
|
|
|
this.$messageInput.on('keyup', function(event) {
|
2016-09-29 09:24:17 +08:00
|
|
|
Handlers.keyUp.call(self, event)
|
|
|
|
})
|
2016-11-07 15:25:08 -05:00
|
|
|
this.$messageInput.on('focus', function() {
|
2016-09-29 09:24:17 +08:00
|
|
|
Handlers.inputFocus.call(self)
|
|
|
|
})
|
2016-11-07 15:25:08 -05:00
|
|
|
this.$messageInput.on('blur', function() {
|
2016-09-29 09:24:17 +08:00
|
|
|
Handlers.inputBlur.call(self)
|
|
|
|
})
|
|
|
|
},
|
2016-11-07 15:25:08 -05:00
|
|
|
initializeSounds: function(soundUrls) {
|
2016-09-29 09:24:17 +08:00
|
|
|
this.sound = new Howl({
|
2016-10-03 08:32:37 +08:00
|
|
|
src: soundUrls,
|
2016-09-29 09:24:17 +08:00
|
|
|
sprite: {
|
|
|
|
joinmap: [0, 561],
|
|
|
|
leavemap: [1000, 592],
|
|
|
|
receivechat: [2000, 318],
|
|
|
|
sendchat: [3000, 296],
|
|
|
|
sessioninvite: [4000, 5393, true]
|
|
|
|
}
|
|
|
|
})
|
|
|
|
},
|
2016-11-07 15:25:08 -05:00
|
|
|
incrementUnread: function() {
|
2016-09-29 09:24:17 +08:00
|
|
|
this.unreadMessages++
|
|
|
|
this.$unread.html(this.unreadMessages)
|
|
|
|
this.$unread.show()
|
|
|
|
},
|
2016-11-07 15:25:08 -05:00
|
|
|
addMessage: function(message, isInitial, wasMe) {
|
2016-09-29 09:24:17 +08:00
|
|
|
if (!this.isOpen && !isInitial) Private.incrementUnread.call(this)
|
|
|
|
|
2016-11-07 15:25:08 -05:00
|
|
|
function addZero(i) {
|
2016-09-29 09:24:17 +08:00
|
|
|
if (i < 10) {
|
|
|
|
i = '0' + i
|
|
|
|
}
|
|
|
|
return i
|
|
|
|
}
|
2016-12-11 16:09:12 -05:00
|
|
|
var m = clone(message.attributes)
|
2016-09-29 09:24:17 +08:00
|
|
|
|
|
|
|
m.timestamp = new Date(m.created_at)
|
|
|
|
|
|
|
|
var date = (m.timestamp.getMonth() + 1) + '/' + m.timestamp.getDate()
|
|
|
|
date += ' ' + addZero(m.timestamp.getHours()) + ':' + addZero(m.timestamp.getMinutes())
|
|
|
|
m.timestamp = date
|
2016-10-21 09:29:04 -04:00
|
|
|
m.image = m.user_image
|
2016-09-29 09:24:17 +08:00
|
|
|
m.message = linker.link(m.message)
|
|
|
|
var $html = $(this.messageTemplate(m))
|
|
|
|
this.$messages.append($html)
|
|
|
|
if (!isInitial) this.scrollMessages(200)
|
|
|
|
|
|
|
|
if (!wasMe && !isInitial && this.alertSound) this.sound.play('receivechat')
|
|
|
|
},
|
2016-11-07 15:25:08 -05:00
|
|
|
initialMessages: function() {
|
2016-09-29 09:24:17 +08:00
|
|
|
var messages = this.messages.models
|
|
|
|
for (var i = 0; i < messages.length; i++) {
|
|
|
|
Private.addMessage.call(this, messages[i], true)
|
|
|
|
}
|
|
|
|
},
|
2016-11-07 15:25:08 -05:00
|
|
|
handleInputMessage: function() {
|
2016-09-29 09:24:17 +08:00
|
|
|
var message = {
|
|
|
|
message: this.$messageInput.val()
|
|
|
|
}
|
|
|
|
this.$messageInput.val('')
|
|
|
|
$(document).trigger(ChatView.events.message + '-' + this.room, [message])
|
|
|
|
},
|
2016-11-07 15:25:08 -05:00
|
|
|
addParticipant: function(participant) {
|
2016-12-11 16:09:12 -05:00
|
|
|
var p = clone(participant.attributes)
|
2016-09-29 09:24:17 +08:00
|
|
|
if (p.self) {
|
|
|
|
p.selfClass = 'is-self'
|
|
|
|
p.selfName = '(me)'
|
|
|
|
} else {
|
|
|
|
p.selfClass = ''
|
|
|
|
p.selfName = ''
|
|
|
|
}
|
|
|
|
var html = this.participantTemplate(p)
|
|
|
|
this.$participants.append(html)
|
|
|
|
},
|
2016-11-07 15:25:08 -05:00
|
|
|
removeParticipant: function(participant) {
|
2016-09-29 09:24:17 +08:00
|
|
|
this.$container.find('.participant-' + participant.get('id')).remove()
|
|
|
|
}
|
|
|
|
}
|
2016-09-23 00:07:30 +08:00
|
|
|
|
|
|
|
var Handlers = {
|
2016-11-07 15:25:08 -05:00
|
|
|
buttonClick: function() {
|
2016-09-29 09:24:17 +08:00
|
|
|
if (this.isOpen) this.close()
|
|
|
|
else if (!this.isOpen) this.open()
|
|
|
|
},
|
2016-11-07 15:25:08 -05:00
|
|
|
videoToggleClick: function() {
|
2016-09-29 09:24:17 +08:00
|
|
|
this.$videoToggle.toggleClass('active')
|
|
|
|
this.videosShowing = !this.videosShowing
|
|
|
|
$(document).trigger(this.videosShowing ? ChatView.events.videosOn : ChatView.events.videosOff)
|
|
|
|
},
|
2016-11-07 15:25:08 -05:00
|
|
|
cursorToggleClick: function() {
|
2016-09-29 09:24:17 +08:00
|
|
|
this.$cursorToggle.toggleClass('active')
|
|
|
|
this.cursorsShowing = !this.cursorsShowing
|
|
|
|
$(document).trigger(this.cursorsShowing ? ChatView.events.cursorsOn : ChatView.events.cursorsOff)
|
|
|
|
},
|
2016-11-07 15:25:08 -05:00
|
|
|
soundToggleClick: function() {
|
2016-09-29 09:24:17 +08:00
|
|
|
this.alertSound = !this.alertSound
|
|
|
|
this.$soundToggle.toggleClass('active')
|
|
|
|
},
|
2016-11-07 15:25:08 -05:00
|
|
|
keyUp: function(event) {
|
2016-09-29 09:24:17 +08:00
|
|
|
switch (event.which) {
|
|
|
|
case 13: // enter
|
|
|
|
Private.handleInputMessage.call(this)
|
|
|
|
break
|
2016-09-23 00:07:30 +08:00
|
|
|
}
|
2016-09-29 09:24:17 +08:00
|
|
|
},
|
2016-11-07 15:25:08 -05:00
|
|
|
inputFocus: function() {
|
2016-09-29 09:24:17 +08:00
|
|
|
$(document).trigger(ChatView.events.inputFocus)
|
|
|
|
},
|
2016-11-07 15:25:08 -05:00
|
|
|
inputBlur: function() {
|
2016-09-29 09:24:17 +08:00
|
|
|
$(document).trigger(ChatView.events.inputBlur)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-08 15:51:39 -05:00
|
|
|
const ChatView = function(messages, mapper, room, opts = {}) {
|
2016-09-29 09:24:17 +08:00
|
|
|
this.room = room
|
|
|
|
this.mapper = mapper
|
|
|
|
this.messages = messages // backbone collection
|
|
|
|
|
|
|
|
this.isOpen = false
|
|
|
|
this.alertSound = true // whether to play sounds on arrival of new messages or not
|
|
|
|
this.cursorsShowing = true
|
|
|
|
this.videosShowing = true
|
|
|
|
this.unreadMessages = 0
|
|
|
|
this.participants = new Backbone.Collection()
|
|
|
|
|
|
|
|
Private.templates.call(this)
|
|
|
|
Private.createElements.call(this)
|
|
|
|
Private.attachElements.call(this)
|
|
|
|
Private.addEventListeners.call(this)
|
|
|
|
Private.initialMessages.call(this)
|
2016-11-08 15:51:39 -05:00
|
|
|
Private.initializeSounds.call(this, opts.soundUrls)
|
2016-09-29 09:24:17 +08:00
|
|
|
this.$container.css({
|
|
|
|
right: '-300px'
|
|
|
|
})
|
|
|
|
}
|
2016-09-23 00:07:30 +08:00
|
|
|
|
2016-11-07 15:25:08 -05:00
|
|
|
ChatView.prototype.conversationInProgress = function(participating) {
|
2016-09-29 09:24:17 +08:00
|
|
|
this.$conversationInProgress.show()
|
|
|
|
this.$participants.addClass('is-live')
|
|
|
|
if (participating) this.$participants.addClass('is-participating')
|
|
|
|
this.$button.addClass('active')
|
2016-09-23 00:07:30 +08:00
|
|
|
|
2016-09-29 09:24:17 +08:00
|
|
|
// hide invite to call buttons
|
2016-09-23 00:07:30 +08:00
|
|
|
}
|
|
|
|
|
2016-11-07 15:25:08 -05:00
|
|
|
ChatView.prototype.conversationEnded = function() {
|
2016-09-29 09:24:17 +08:00
|
|
|
this.$conversationInProgress.hide()
|
|
|
|
this.$participants.removeClass('is-live')
|
|
|
|
this.$participants.removeClass('is-participating')
|
|
|
|
this.$button.removeClass('active')
|
|
|
|
this.$participants.find('.participant').removeClass('active')
|
|
|
|
this.$participants.find('.participant').removeClass('pending')
|
2016-09-23 00:07:30 +08:00
|
|
|
}
|
|
|
|
|
2016-11-07 15:25:08 -05:00
|
|
|
ChatView.prototype.leaveConversation = function() {
|
2016-09-29 09:24:17 +08:00
|
|
|
this.$participants.removeClass('is-participating')
|
2016-09-23 00:07:30 +08:00
|
|
|
}
|
|
|
|
|
2016-11-07 15:25:08 -05:00
|
|
|
ChatView.prototype.mapperJoinedCall = function(id) {
|
2016-09-29 09:24:17 +08:00
|
|
|
this.$participants.find('.participant-' + id).addClass('active')
|
2016-09-23 00:07:30 +08:00
|
|
|
}
|
|
|
|
|
2016-11-07 15:25:08 -05:00
|
|
|
ChatView.prototype.mapperLeftCall = function(id) {
|
2016-09-29 09:24:17 +08:00
|
|
|
this.$participants.find('.participant-' + id).removeClass('active')
|
2016-09-23 00:07:30 +08:00
|
|
|
}
|
|
|
|
|
2016-11-07 15:25:08 -05:00
|
|
|
ChatView.prototype.invitationPending = function(id) {
|
2016-09-29 09:24:17 +08:00
|
|
|
this.$participants.find('.participant-' + id).addClass('pending')
|
2016-09-23 00:07:30 +08:00
|
|
|
}
|
|
|
|
|
2016-11-07 15:25:08 -05:00
|
|
|
ChatView.prototype.invitationAnswered = function(id) {
|
2016-09-29 09:24:17 +08:00
|
|
|
this.$participants.find('.participant-' + id).removeClass('pending')
|
2016-09-23 00:07:30 +08:00
|
|
|
}
|
|
|
|
|
2016-11-07 15:25:08 -05:00
|
|
|
ChatView.prototype.addParticipant = function(participant) {
|
2016-09-29 09:24:17 +08:00
|
|
|
this.participants.add(participant)
|
2016-09-23 00:07:30 +08:00
|
|
|
}
|
|
|
|
|
2016-11-07 15:25:08 -05:00
|
|
|
ChatView.prototype.removeParticipant = function(username) {
|
2016-09-29 09:24:17 +08:00
|
|
|
var p = this.participants.find(p => p.get('username') === username)
|
|
|
|
if (p) {
|
|
|
|
this.participants.remove(p)
|
|
|
|
}
|
2016-09-23 00:07:30 +08:00
|
|
|
}
|
|
|
|
|
2016-11-07 15:25:08 -05:00
|
|
|
ChatView.prototype.removeParticipants = function() {
|
2016-09-29 09:24:17 +08:00
|
|
|
this.participants.remove(this.participants.models)
|
2016-09-23 00:07:30 +08:00
|
|
|
}
|
|
|
|
|
2016-11-07 15:25:08 -05:00
|
|
|
ChatView.prototype.open = function() {
|
2016-09-29 09:24:17 +08:00
|
|
|
this.$container.css({
|
|
|
|
right: '0'
|
|
|
|
})
|
|
|
|
this.$messageInput.focus()
|
|
|
|
this.isOpen = true
|
|
|
|
this.unreadMessages = 0
|
|
|
|
this.$unread.hide()
|
|
|
|
this.scrollMessages(0)
|
|
|
|
$(document).trigger(ChatView.events.openTray)
|
2016-09-23 00:07:30 +08:00
|
|
|
}
|
|
|
|
|
2016-11-07 15:25:08 -05:00
|
|
|
ChatView.prototype.addMessage = function(message, isInitial, wasMe) {
|
2016-09-29 09:24:17 +08:00
|
|
|
this.messages.add(message)
|
|
|
|
Private.addMessage.call(this, message, isInitial, wasMe)
|
2016-09-23 00:07:30 +08:00
|
|
|
}
|
|
|
|
|
2016-11-07 15:25:08 -05:00
|
|
|
ChatView.prototype.scrollMessages = function(duration) {
|
2016-09-29 09:24:17 +08:00
|
|
|
duration = duration || 0
|
2016-09-23 00:07:30 +08:00
|
|
|
|
2016-09-29 09:24:17 +08:00
|
|
|
this.$messages.animate({
|
|
|
|
scrollTop: this.$messages[0].scrollHeight
|
|
|
|
}, duration)
|
2016-09-23 00:07:30 +08:00
|
|
|
}
|
|
|
|
|
2016-11-07 15:25:08 -05:00
|
|
|
ChatView.prototype.clearMessages = function() {
|
2016-09-29 09:24:17 +08:00
|
|
|
this.unreadMessages = 0
|
|
|
|
this.$unread.hide()
|
|
|
|
this.$messages.empty()
|
2016-09-23 00:07:30 +08:00
|
|
|
}
|
|
|
|
|
2016-11-07 15:25:08 -05:00
|
|
|
ChatView.prototype.close = function() {
|
2016-09-29 09:24:17 +08:00
|
|
|
this.$container.css({
|
|
|
|
right: '-300px'
|
|
|
|
})
|
|
|
|
this.$messageInput.blur()
|
|
|
|
this.isOpen = false
|
|
|
|
$(document).trigger(ChatView.events.closeTray)
|
2016-09-23 00:07:30 +08:00
|
|
|
}
|
|
|
|
|
2016-11-07 15:25:08 -05:00
|
|
|
ChatView.prototype.remove = function() {
|
2016-09-29 09:24:17 +08:00
|
|
|
this.$button.off()
|
|
|
|
this.$container.remove()
|
2016-09-23 00:07:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @class
|
|
|
|
* @static
|
|
|
|
*/
|
|
|
|
ChatView.events = {
|
2016-09-29 09:24:17 +08:00
|
|
|
message: 'ChatView:message',
|
|
|
|
openTray: 'ChatView:openTray',
|
|
|
|
closeTray: 'ChatView:closeTray',
|
|
|
|
inputFocus: 'ChatView:inputFocus',
|
|
|
|
inputBlur: 'ChatView:inputBlur',
|
|
|
|
cursorsOff: 'ChatView:cursorsOff',
|
|
|
|
cursorsOn: 'ChatView:cursorsOn',
|
|
|
|
videosOff: 'ChatView:videosOff',
|
|
|
|
videosOn: 'ChatView:videosOn'
|
|
|
|
}
|
2016-09-23 00:07:30 +08:00
|
|
|
|
|
|
|
export default ChatView
|