1
0
mirror of https://github.com/moparisthebest/kaiwa synced 2024-11-29 12:42:16 -05:00
kaiwa/clientapp/pages/chat.js

226 lines
7.0 KiB
JavaScript
Raw 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');
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');
2013-09-25 23:38:00 -04:00
var chatHelpers = require('../helpers/chatHelpers');
2013-10-15 03:02:45 -04:00
var attachMediaStream = require('attachmediastream');
2013-08-29 23:38:28 -04:00
2013-09-25 23:38:00 -04:00
module.exports = BasePage.extend(chatHelpers).extend({
2013-08-29 23:38:28 -04:00
template: templates.pages.chat,
initialize: function (spec) {
this.editMode = false;
this.model.fetchHistory();
2013-09-25 23:38:00 -04:00
this.listenTo(this, 'pageloaded', this.handlePageLoaded);
this.listenTo(this, 'pageunloaded', this.handlePageUnloaded);
this.listenTo(this.model.messages, 'change:body', this.refreshModel);
this.listenTo(this.model.messages, 'change:edited', this.refreshModel);
this.listenTo(this.model.messages, 'change:pending', this.refreshModel);
2013-10-15 03:02:45 -04:00
this.listenTo(this.model, 'change:stream', this.handleStream);
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',
'click .call': 'handleCallClick'
2013-08-29 23:38:28 -04:00
},
srcBindings: {
avatar: 'header .avatar'
},
textBindings: {
2013-09-03 18:25:14 -04:00
displayName: 'header .name',
2013-09-16 05:19:07 -04:00
formattedTZO: 'header .tzo'
},
2013-10-15 04:01:49 -04:00
classBindings: {
2013-10-15 23:06:18 -04: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]);
client.sendMessage({
to: this.model.lockedResource || this.model.jid,
chatState: 'active'
});
},
hide: function () {
BasePage.prototype.hide.apply(this);
client.sendMessage({
to: this.model.lockedResource || this.model.jid,
chatState: 'inactive'
});
2013-08-29 23:38:28 -04:00
},
render: function () {
2013-09-25 23:38:00 -04:00
if (this.rendered) return this;
this.rendered = true;
2013-08-29 23:38:28 -04:00
this.renderAndBind();
2013-09-25 23:38:00 -04:00
this.typingTimer = null;
2013-09-16 05:19:07 -04:00
this.$chatInput = this.$('.chatBox textarea');
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
this.$scrollContainer = this.$messageList;
this.listenTo(this.model.messages, 'add', this.handleChatAdded);
this.listenToAndRun(this.model, 'change:jingleResources', this.handleJingleResourcesChanged);
2013-09-25 23:38:00 -04:00
this.renderCollection();
$(window).on('resize', _.bind(this.handleWindowResize, this));
this.initializeScroll();
2013-08-29 23:38:28 -04:00
return this;
},
2013-09-25 23:38:00 -04:00
handlePageLoaded: function () {
this.scrollPageLoad();
this.handleWindowResize();
},
handlePageUnloaded: function () {
this.scrollPageUnload();
},
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;
var previous;
var bottom = this.isBottom() || this.$messageList.is(':empty');
this.model.messages.each(function (model, i) {
self.appendModel(model);
});
this.scrollIfPinned();
},
handleWindowResize: function () {
this.scrollIfPinned();
this.resizeInput();
2013-09-25 23:38:00 -04:00
},
2013-08-29 23:38:28 -04:00
handleKeyDown: function (e) {
clearTimeout(this.typingTimer);
2013-08-29 23:38:28 -04:00
if (e.which === 13 && !e.shiftKey) {
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) {
if (!this.typing) {
this.typing = true;
client.sendMessage({
to: this.model.lockedResource || this.model.jid,
chatState: 'composing'
});
}
}
},
handleKeyUp: function (e) {
2013-09-15 18:06:37 -04:00
this.resizeInput();
clearTimeout(this.typingTimer);
2013-09-17 14:59:33 -04:00
this.typingTimer = setTimeout(this.pausedTyping.bind(this), 5000);
2013-09-15 18:06:37 -04:00
if (this.typing && this.$chatInput.val().length === 0) {
this.typing = false;
client.sendMessage({
to: this.model.lockedResource || this.model.jid,
chatState: 'active'
});
}
},
pausedTyping: function () {
if (this.typing) {
this.typing = false;
client.sendMessage({
to: this.model.lockedResource || this.model.jid,
chatState: 'paused'
});
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
this.scrollToBottom(true);
2013-08-29 23:38:28 -04:00
if (val) {
message = {
2013-09-03 18:25:14 -04:00
to: this.model.lockedResource || this.model.jid,
2013-08-29 23:38:28 -04:00
type: 'chat',
body: val,
chatState: 'active'
};
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
}
var id = client.sendMessage(message);
message.id = id;
message.from = me.jid;
if (this.editMode) {
this.model.lastSentMessage.correct(message);
} else {
var msgModel = new MessageModel(message);
this.model.messages.add(msgModel);
this.model.lastSentMessage = msgModel;
}
}
this.editMode = false;
this.typing = false;
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);
existing.replaceWith(model.partialTemplateHtml);
},
handleJingleResourcesChanged: function (model, val) {
var resources = val || this.model.jingleResources;
this.$('button.call').prop('disabled', !resources.length);
},
2013-10-15 03:02:45 -04:00
handleStream: function () {
2013-10-15 04:01:49 -04:00
this.attach = attachMediaStream;
if (!!this.model.stream) {
attachMediaStream(this.model.stream, this.$('.remoteVideo')[0]);
2013-10-15 03:02:45 -04:00
}
},
2013-09-25 23:38:00 -04:00
appendModel: function (model, preload) {
var self = this;
var isGrouped = model.shouldGroupWith(this.lastModel);
var newEl, first, last;
if (isGrouped) {
newEl = $(model.partialTemplateHtml);
last = this.$messageList.find('li').last();
last.find('.messageWrapper').append(newEl);
last.addClass('chatGroup');
} else {
newEl = $(model.templateHtml);
this.$messageList.append(newEl);
}
this.lastModel = model;
this.scrollIfPinned();
2013-08-29 23:38:28 -04:00
}
});