1
0
mirror of https://github.com/moparisthebest/kaiwa synced 2024-11-30 21:22:17 -05:00
kaiwa/clientapp/pages/chat.js

379 lines
13 KiB
JavaScript
Raw Permalink Normal View History

2013-08-29 23:38:28 -04:00
/*global $, app, me, client*/
"use strict";
2013-09-25 23:38:00 -04:00
var _ = require('underscore');
2014-01-01 15:34:38 -05:00
var StayDown = require('staydown');
2013-08-29 23:38:28 -04:00
var BasePage = require('./base');
var templates = require('../templates');
var Message = require('../views/message');
var MessageModel = require('../models/message');
2014-01-01 19:24:11 -05:00
var embedIt = require('../helpers/embedIt');
2014-01-02 03:52:26 -05:00
var htmlify = require('../helpers/htmlify');
2013-10-15 03:02:45 -04:00
var attachMediaStream = require('attachmediastream');
2013-08-29 23:38:28 -04:00
2014-01-01 15:34:38 -05:00
module.exports = BasePage.extend({
2013-08-29 23:38:28 -04:00
template: templates.pages.chat,
initialize: function (spec) {
this.editMode = false;
2013-09-25 23:38:00 -04:00
this.listenTo(this, 'pageloaded', this.handlePageLoaded);
this.listenTo(this, 'pageunloaded', this.handlePageUnloaded);
2013-12-18 16:31:22 -05:00
this.listenTo(this.model.messages, 'change', this.refreshModel);
2015-02-22 17:43:49 -05:00
this.listenTo(this.model.messages, 'reset', this.renderCollection);
this.listenTo(this.model, 'refresh', this.renderCollection);
app.state.bind('change:connected', this.connectionChange, this);
2015-03-20 15:36:40 -04:00
this.model.bind('change:avatar', this.handleAvatarChanged, this);
2013-09-25 23:38:00 -04:00
2013-08-29 23:38:28 -04:00
this.render();
},
events: {
2013-09-16 05:19:07 -04:00
'keydown textarea': 'handleKeyDown',
'keyup textarea': 'handleKeyUp',
2013-10-16 02:00:56 -04:00
'click .call': 'handleCallClick',
'click .accept': 'handleAcceptClick',
2013-10-16 02:00:56 -04:00
'click .end': 'handleEndClick',
'click .mute': 'handleMuteClick'
2013-08-29 23:38:28 -04:00
},
srcBindings: {
streamUrl: 'video.remote'
2013-08-29 23:38:28 -04:00
},
textBindings: {
2013-09-03 18:25:14 -04:00
displayName: 'header .name',
2013-12-20 02:04:14 -05:00
formattedTZO: 'header .tzo',
status: 'header .status',
chatStateText: '.chatBox .contactState'
2013-09-16 05:19:07 -04:00
},
2013-10-15 04:01:49 -04:00
classBindings: {
2013-12-20 02:04:14 -05:00
chatState: 'header',
2015-02-09 10:20:28 -05:00
idle: '.user_presence',
show: '.user_presence',
2013-12-20 02:04:14 -05:00
onCall: '.conversation'
2013-10-15 04:01:49 -04:00
},
2013-09-16 05:19:07 -04:00
show: function (animation) {
BasePage.prototype.show.apply(this, [animation]);
this.sendChatState('active');
2015-02-08 18:21:00 -05:00
this.firstChanged = true;
var self = this;
$('.messages').scroll(function() {
if (self.firstChanged && $(".messages li:first-child").offset().top > 0) {
self.firstChanged = false;
2015-02-22 17:43:49 -05:00
self.model.fetchHistory();
2015-02-08 18:21:00 -05:00
}
});
2015-02-09 09:27:16 -05:00
this.$chatInput.focus();
2013-09-16 05:19:07 -04:00
},
hide: function () {
BasePage.prototype.hide.apply(this);
this.sendChatState('inactive');
2013-08-29 23:38:28 -04:00
},
render: function () {
2013-09-25 23:38:00 -04:00
if (this.rendered) return this;
2014-01-01 15:34:38 -05:00
var self = this;
2013-09-25 23:38:00 -04:00
this.rendered = true;
2013-08-29 23:38:28 -04:00
this.renderAndBind();
2013-09-25 23:38:00 -04:00
2013-09-16 05:19:07 -04:00
this.$chatInput = this.$('.chatBox textarea');
2015-02-09 09:27:16 -05:00
this.$chatInput.val(app.composing[this.model.jid] || '');
2013-09-25 23:38:00 -04:00
this.$chatBox = this.$('.chatBox');
2013-09-16 05:19:07 -04:00
this.$messageList = this.$('.messages');
2013-09-25 23:38:00 -04:00
2014-01-01 15:34:38 -05:00
this.staydown = new StayDown(this.$messageList[0], 500);
2013-09-25 23:38:00 -04:00
this.renderCollection();
2014-01-01 15:34:38 -05:00
this.listenTo(this.model.messages, 'add', this.handleChatAdded);
this.listenToAndRun(this.model, 'change:jingleResources', this.handleJingleResourcesChanged);
2013-09-25 23:38:00 -04:00
2015-02-17 10:44:46 -05:00
$(window).on('resize', _.bind(this.resizeInput, this));
2013-09-25 23:38:00 -04:00
this.registerBindings(me, {
srcBindings: {
streamUrl: 'video.local'
}
});
2013-08-29 23:38:28 -04:00
return this;
},
2013-09-25 23:38:00 -04:00
handlePageLoaded: function () {
2014-01-01 15:34:38 -05:00
this.staydown.checkdown();
2015-02-17 10:44:46 -05:00
this.resizeInput();
},
2013-10-15 03:02:45 -04:00
handleCallClick: function (e) {
e.preventDefault();
this.model.call();
return false;
},
2013-09-25 23:38:00 -04:00
renderCollection: function () {
var self = this;
2015-02-22 17:43:49 -05:00
this.$messageList.empty();
2015-02-22 17:43:49 -05:00
delete this.firstModel;
delete this.firstDate;
delete this.lastModel;
delete this.lastDate;
2013-09-25 23:38:00 -04:00
this.model.messages.each(function (model, i) {
self.appendModel(model);
});
2014-01-01 15:34:38 -05:00
this.staydown.checkdown();
2013-09-25 23:38:00 -04:00
},
2013-08-29 23:38:28 -04:00
handleKeyDown: function (e) {
if (e.which === 13 && !e.shiftKey) {
2015-02-09 09:27:16 -05:00
app.composing[this.model.jid] = '';
2013-08-29 23:38:28 -04:00
this.sendChat();
e.preventDefault();
return false;
2013-09-15 18:06:37 -04:00
} else if (e.which === 38 && this.$chatInput.val() === '' && this.model.lastSentMessage) {
2013-08-29 23:38:28 -04:00
this.editMode = true;
2013-09-15 18:06:37 -04:00
this.$chatInput.addClass('editing');
this.$chatInput.val(this.model.lastSentMessage.body);
2013-08-29 23:38:28 -04:00
e.preventDefault();
return false;
} else if (e.which === 40 && this.editMode) {
this.editMode = false;
2013-09-15 18:06:37 -04:00
this.$chatInput.removeClass('editing');
2013-08-29 23:38:28 -04:00
e.preventDefault();
return false;
} else if (!e.ctrlKey && !e.metaKey) {
2013-12-18 17:01:32 -05:00
if (!this.typing || this.paused) {
this.typing = true;
2013-12-18 17:01:32 -05:00
this.paused = false;
2013-12-20 06:39:06 -05:00
this.$chatInput.addClass('typing');
this.sendChatState('composing');
}
}
},
handleKeyUp: function (e) {
2015-02-17 10:44:46 -05:00
this.resizeInput();
2015-02-09 09:27:16 -05:00
app.composing[this.model.jid] = this.$chatInput.val();
2013-09-15 18:06:37 -04:00
if (this.typing && this.$chatInput.val().length === 0) {
this.typing = false;
2013-12-20 06:39:06 -05:00
this.$chatInput.removeClass('typing');
this.sendChatState('active');
2013-12-18 17:01:32 -05:00
} else if (this.typing) {
this.pausedTyping();
}
},
2013-12-18 17:01:32 -05:00
pausedTyping: _.debounce(function () {
if (this.typing && !this.paused) {
this.paused = true;
this.sendChatState('paused');
2013-08-29 23:38:28 -04:00
}
2013-12-18 17:09:02 -05:00
}, 3000),
sendChatState: function (state) {
//if (!this.model.supportsChatStates) return;
client.sendMessage({
to: this.model.lockedResource || this.model.jid,
chatState: state
});
},
2013-08-29 23:38:28 -04:00
sendChat: function () {
var message;
2013-09-15 18:06:37 -04:00
var val = this.$chatInput.val();
2013-08-29 23:38:28 -04:00
if (val) {
2014-01-01 15:34:38 -05:00
this.staydown.intend_down = true;
2014-01-02 03:52:26 -05:00
var links = _.map(htmlify.collectLinks(val), function (link) {
return {url: link};
});
2013-08-29 23:38:28 -04:00
message = {
2014-01-08 01:43:59 -05:00
id: client.nextId(),
to: client.JID(this.model.lockedResource || this.model.jid),
2013-08-29 23:38:28 -04:00
type: 'chat',
body: val,
requestReceipt: true,
2014-01-02 03:52:26 -05:00
oobURIs: links
2013-08-29 23:38:28 -04:00
};
if (this.model.supportsChatStates) {
message.chatState = 'active';
}
2013-08-29 23:38:28 -04:00
if (this.editMode) {
2013-09-25 23:38:00 -04:00
message.replace = this.model.lastSentMessage.id;
2013-08-29 23:38:28 -04:00
}
2014-01-08 01:43:59 -05:00
client.sendMessage(message);
// Prep message to create a Message model
2013-08-29 23:38:28 -04:00
message.from = me.jid;
2014-01-08 01:43:59 -05:00
message.mid = message.id;
delete message.id;
2013-08-29 23:38:28 -04:00
if (this.editMode) {
this.model.lastSentMessage.correct(message);
} else {
var msgModel = new MessageModel(message);
2015-04-07 05:04:53 -04:00
this.model.addMessage(msgModel, false);
2013-08-29 23:38:28 -04:00
this.model.lastSentMessage = msgModel;
}
}
this.editMode = false;
this.typing = false;
2013-12-18 17:01:32 -05:00
this.paused = false;
2013-12-20 06:39:06 -05:00
this.$chatInput.removeClass('typing');
2013-09-15 18:06:37 -04:00
this.$chatInput.removeClass('editing');
this.$chatInput.val('');
2013-09-25 23:38:00 -04:00
},
handleChatAdded: function (model) {
this.appendModel(model, true);
},
refreshModel: function (model) {
var existing = this.$('#chat' + model.cid);
2015-04-07 07:12:20 -04:00
existing.replaceWith(model.bareMessageTemplate(existing.prev().hasClass('message_header')));
2015-02-08 18:21:00 -05:00
existing = this.$('#chat' + model.cid);
embedIt(existing);
2013-09-25 23:38:00 -04:00
},
handleJingleResourcesChanged: function (model, val) {
var resources = val || this.model.jingleResources;
this.$('button.call').prop('disabled', !resources.length);
},
2015-03-20 15:36:40 -04:00
handleAvatarChanged: function (contact, uri) {
if (!me.isMe(contact.jid)) {
$('.' + contact.jid.substr(0, contact.jid.indexOf('@')) + ' .messageAvatar img').attr('src', uri);
}
},
2013-09-25 23:38:00 -04:00
appendModel: function (model, preload) {
2015-02-08 18:21:00 -05:00
var newEl, first, last;
var msgDate = Date.create(model.timestamp);
var messageDay = msgDate.format('{month} {ord}, {yyyy}');
if (this.firstModel === undefined || msgDate > Date.create(this.firstModel.timestamp)) {
if (this.firstModel === undefined) {
this.firstModel = model;
this.firstDate = messageDay;
}
if (messageDay !== this.lastDate) {
var dayDivider = $(templates.includes.dayDivider({day_name: messageDay}));
this.staydown.append(dayDivider[0]);
this.lastDate = messageDay;
}
var isGrouped = model.shouldGroupWith(this.lastModel);
if (isGrouped) {
newEl = $(model.partialTemplateHtml);
last = this.$messageList.find('li').last();
last.find('.messageWrapper').append(newEl);
last.addClass('chatGroup');
this.staydown.checkdown();
} else {
newEl = $(model.templateHtml);
2015-03-20 15:36:40 -04:00
if (!me.isMe(model.sender.jid)) newEl.addClass(model.sender.jid.substr(0, model.sender.jid.indexOf('@')));
2015-02-08 18:21:00 -05:00
this.staydown.append(newEl[0]);
this.lastModel = model;
}
if (!model.pending) embedIt(newEl);
2015-02-09 10:20:28 -05:00
}
2015-02-08 18:21:00 -05:00
else {
var scrollDown = this.$messageList.prop('scrollHeight') - this.$messageList.scrollTop();
var firstEl = this.$messageList.find('li').first();
if (messageDay !== this.firstDate) {
var dayDivider = $(templates.includes.dayDivider({day_name: messageDay}));
firstEl.before(dayDivider[0]);
var firstEl = this.$messageList.find('li').first();
this.firstDate = messageDay;
}
var isGrouped = model.shouldGroupWith(this.firstModel);
if (isGrouped) {
newEl = $(model.partialTemplateHtml);
first = this.$messageList.find('li').first().next();
first.find('.messageWrapper div:first').after(newEl);
first.addClass('chatGroup');
} else {
newEl = $(model.templateHtml);
2015-03-20 15:36:40 -04:00
if (!me.isMe(model.sender.jid)) newEl.addClass(model.sender.jid.substr(0, model.sender.jid.indexOf('@')));
2015-02-08 18:21:00 -05:00
firstEl.after(newEl[0]);
this.firstModel = model;
}
if (!model.pending) embedIt(newEl);
2015-02-09 10:20:28 -05:00
2015-02-08 18:21:00 -05:00
this.$messageList.scrollTop(this.$messageList.prop('scrollHeight') - scrollDown);
this.firstChanged = true;
2013-09-25 23:38:00 -04:00
}
2013-10-16 02:00:56 -04:00
},
handleAcceptClick: function (e) {
e.preventDefault();
var self = this;
this.$('button.accept').prop('disabled', true);
if (this.model.jingleCall.jingleSession.state == 'pending') {
if (!client.jingle.localStream) {
client.jingle.startLocalMedia(null, function (err) {
if (err) {
self.model.jingleCall.end({
condition: 'decline'
});
} else {
client.sendPresence({to: client.JID(self.model.jingleCall.jingleSession.peer) });
self.model.jingleCall.jingleSession.accept();
}
});
} else {
client.sendPresence({to: client.JID(this.model.jingleCall.jingleSession.peer) });
this.model.jingleCall.jingleSession.accept();
}
}
return false;
},
2013-10-16 02:00:56 -04:00
handleEndClick: function (e) {
e.preventDefault();
var condition = 'success';
if (this.model.jingleCall) {
if (this.model.jingleCall.jingleSession && this.model.jingleCall.jingleSession.state == 'pending') {
condition = 'decline';
}
this.model.jingleCall.end({
condition: condition
});
}
2013-10-16 02:00:56 -04:00
return false;
},
handleMuteClick: function (e) {
return false;
2014-01-01 15:34:38 -05:00
},
resizeInput: _.throttle(function () {
var height;
var scrollHeight;
2015-02-17 10:44:46 -05:00
var heightDiff;
2014-01-01 15:34:38 -05:00
var newHeight;
2015-02-17 10:44:46 -05:00
var newMargin;
var marginDelta;
var maxHeight = parseInt(this.$chatInput.css('max-height'), 10);
2014-01-01 15:34:38 -05:00
this.$chatInput.removeAttr('style');
2015-02-17 10:44:46 -05:00
height = this.$chatInput.outerHeight(),
scrollHeight = this.$chatInput.get(0).scrollHeight,
newHeight = Math.max(height, scrollHeight);
heightDiff = height - this.$chatInput.innerHeight();
2014-01-01 15:34:38 -05:00
if (newHeight > maxHeight) newHeight = maxHeight;
if (newHeight > height) {
2015-02-17 10:44:46 -05:00
this.$chatInput.css('height', newHeight+heightDiff);
this.$chatInput.scrollTop(this.$chatInput[0].scrollHeight - this.$chatInput.height());
newMargin = newHeight - height + heightDiff;
marginDelta = newMargin - parseInt(this.$messageList.css('marginBottom'), 10);
if (!!marginDelta) {
this.$messageList.css('marginBottom', newMargin);
2014-01-01 15:34:38 -05:00
}
2015-02-17 10:44:46 -05:00
} else {
this.$messageList.css('marginBottom', 0);
2014-01-01 15:34:38 -05:00
}
2015-02-22 17:43:49 -05:00
}, 300),
connectionChange: function () {
if (app.state.connected) {
this.$chatInput.attr("disabled", false);
} else {
this.$chatInput.attr("disabled", "disabled");
}
}
2013-08-29 23:38:28 -04:00
});