2013-09-16 19:12:00 -04:00
|
|
|
/*global $, app, me, client*/
|
|
|
|
"use strict";
|
|
|
|
|
2013-12-13 19:16:40 -05:00
|
|
|
var _ = require('underscore');
|
2013-09-16 19:12:00 -04:00
|
|
|
var BasePage = require('./base');
|
|
|
|
var templates = require('../templates');
|
2013-09-24 16:24:35 -04:00
|
|
|
var Message = require('../views/mucMessage');
|
2013-09-16 19:12:00 -04:00
|
|
|
var MessageModel = require('../models/message');
|
2013-12-13 19:16:40 -05:00
|
|
|
var chatHelpers = require('../helpers/chatHelpers');
|
2013-09-16 19:12:00 -04:00
|
|
|
|
|
|
|
|
2013-12-13 19:16:40 -05:00
|
|
|
module.exports = BasePage.extend(chatHelpers).extend({
|
2013-09-16 19:12:00 -04:00
|
|
|
template: templates.pages.groupchat,
|
|
|
|
initialize: function (spec) {
|
|
|
|
this.editMode = false;
|
2013-12-13 19:16:40 -05: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);
|
|
|
|
this.listenTo(this.model.messages, 'change:mine', this.refreshModel);
|
|
|
|
|
2013-09-16 19:12:00 -04:00
|
|
|
this.render();
|
|
|
|
},
|
|
|
|
events: {
|
|
|
|
'keydown textarea': 'handleKeyDown',
|
|
|
|
'keyup textarea': 'handleKeyUp',
|
|
|
|
'click .joinRoom': 'handleJoin',
|
|
|
|
'click .leaveRoom': 'handleLeave'
|
|
|
|
},
|
2013-12-16 13:06:03 -05:00
|
|
|
classBindings: {
|
|
|
|
joined: '.controls'
|
|
|
|
},
|
2013-09-16 19:12:00 -04:00
|
|
|
srcBindings: {
|
|
|
|
avatar: 'header .avatar'
|
|
|
|
},
|
|
|
|
textBindings: {
|
2013-09-24 16:24:35 -04:00
|
|
|
displayName: 'header .name'
|
2013-09-16 19:12:00 -04:00
|
|
|
},
|
|
|
|
show: function (animation) {
|
|
|
|
BasePage.prototype.show.apply(this, [animation]);
|
|
|
|
client.sendMessage({
|
2013-10-08 11:14:23 -04:00
|
|
|
type: 'groupchat',
|
2013-10-08 11:03:58 -04:00
|
|
|
to: this.model.jid,
|
2013-09-16 19:12:00 -04:00
|
|
|
chatState: 'active'
|
|
|
|
});
|
|
|
|
},
|
|
|
|
hide: function () {
|
|
|
|
BasePage.prototype.hide.apply(this);
|
|
|
|
client.sendMessage({
|
2013-10-08 11:14:23 -04:00
|
|
|
type: 'groupchat',
|
2013-10-08 11:03:58 -04:00
|
|
|
to: this.model.jid,
|
2013-09-16 19:12:00 -04:00
|
|
|
chatState: 'inactive'
|
|
|
|
});
|
|
|
|
},
|
|
|
|
render: function () {
|
2013-12-13 19:16:40 -05:00
|
|
|
if (this.rendered) return this;
|
|
|
|
this.rendered = true;
|
|
|
|
|
2013-09-16 19:12:00 -04:00
|
|
|
this.renderAndBind();
|
|
|
|
this.typingTimer = null;
|
|
|
|
this.$chatInput = this.$('.chatBox textarea');
|
2013-12-13 19:16:40 -05:00
|
|
|
this.$chatBox = this.$('.chatBox');
|
2013-09-16 19:12:00 -04:00
|
|
|
this.$messageList = this.$('.messages');
|
2013-12-13 19:16:40 -05:00
|
|
|
this.$scrollContainer = this.$messageList;
|
|
|
|
|
|
|
|
this.listenTo(this.model.messages, 'add', this.handleChatAdded);
|
|
|
|
|
|
|
|
this.renderCollection();
|
|
|
|
|
|
|
|
$(window).on('resize', _.bind(this.handleWindowResize, this));
|
|
|
|
|
|
|
|
this.initializeScroll();
|
|
|
|
|
2013-09-16 19:12:00 -04:00
|
|
|
this.registerBindings();
|
2013-12-13 19:16:40 -05:00
|
|
|
|
2013-09-16 19:12:00 -04:00
|
|
|
return this;
|
|
|
|
},
|
2013-12-13 19:16:40 -05: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();
|
|
|
|
},
|
|
|
|
handleChatAdded: function (model) {
|
|
|
|
this.appendModel(model, true);
|
|
|
|
},
|
|
|
|
handlePageLoaded: function () {
|
|
|
|
this.scrollPageLoad();
|
|
|
|
this.handleWindowResize();
|
|
|
|
},
|
|
|
|
handlePageUnloaded: function () {
|
|
|
|
this.scrollPageUnload();
|
|
|
|
},
|
|
|
|
handleWindowResize: function () {
|
|
|
|
this.scrollIfPinned();
|
|
|
|
this.resizeInput();
|
|
|
|
},
|
2013-09-16 19:12:00 -04:00
|
|
|
handleKeyDown: function (e) {
|
|
|
|
clearTimeout(this.typingTimer);
|
|
|
|
if (e.which === 13 && !e.shiftKey) {
|
|
|
|
this.sendChat();
|
|
|
|
e.preventDefault();
|
|
|
|
return false;
|
|
|
|
} else if (e.which === 38 && this.$chatInput.val() === '' && this.model.lastSentMessage) {
|
|
|
|
this.editMode = true;
|
|
|
|
this.$chatInput.addClass('editing');
|
|
|
|
this.$chatInput.val(this.model.lastSentMessage.body);
|
|
|
|
e.preventDefault();
|
|
|
|
return false;
|
|
|
|
} else if (e.which === 40 && this.editMode) {
|
|
|
|
this.editMode = false;
|
|
|
|
this.$chatInput.removeClass('editing');
|
|
|
|
e.preventDefault();
|
|
|
|
return false;
|
2013-10-12 00:20:18 -04:00
|
|
|
} else if (!e.ctrlKey && !e.metaKey) {
|
2013-09-16 19:12:00 -04:00
|
|
|
if (!this.typing) {
|
|
|
|
this.typing = true;
|
|
|
|
client.sendMessage({
|
2013-10-08 11:14:23 -04:00
|
|
|
type: 'groupchat',
|
2013-10-08 11:03:58 -04:00
|
|
|
to: this.model.jid,
|
2013-09-16 19:12:00 -04:00
|
|
|
chatState: 'composing'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
handleKeyUp: function (e) {
|
|
|
|
this.resizeInput();
|
|
|
|
this.typingTimer = setTimeout(this.pausedTyping.bind(this), 3000);
|
|
|
|
if (this.typing && this.$chatInput.val().length === 0) {
|
|
|
|
this.typing = false;
|
|
|
|
client.sendMessage({
|
2013-10-08 11:14:23 -04:00
|
|
|
type: 'groupchat',
|
2013-10-08 11:03:58 -04:00
|
|
|
to: this.model.jid,
|
2013-09-16 19:12:00 -04:00
|
|
|
chatState: 'active'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
resizeInput: function () {
|
|
|
|
var height;
|
|
|
|
var scrollHeight;
|
|
|
|
var newHeight;
|
|
|
|
var newPadding;
|
|
|
|
var paddingDelta;
|
|
|
|
var maxHeight = 102;
|
|
|
|
|
|
|
|
this.$chatInput.removeAttr('style');
|
|
|
|
height = this.$chatInput.height() + 10,
|
|
|
|
scrollHeight = this.$chatInput.get(0).scrollHeight,
|
|
|
|
newHeight = scrollHeight + 2;
|
|
|
|
|
|
|
|
if (newHeight > maxHeight) newHeight = maxHeight;
|
|
|
|
if (newHeight > height) {
|
|
|
|
this.$chatInput.css('height', newHeight);
|
|
|
|
newPadding = newHeight + 21;
|
|
|
|
paddingDelta = newPadding - parseInt(this.$messageList.css('paddingBottom'), 10);
|
|
|
|
if (!!paddingDelta) {
|
|
|
|
this.$messageList.css('paddingBottom', newPadding);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
pausedTyping: function () {
|
|
|
|
if (this.typing) {
|
|
|
|
this.typing = false;
|
|
|
|
client.sendMessage({
|
2013-10-08 11:14:23 -04:00
|
|
|
type: 'groupchat',
|
2013-10-08 11:03:58 -04:00
|
|
|
to: this.model.jid,
|
2013-09-16 19:12:00 -04:00
|
|
|
chatState: 'paused'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
sendChat: function () {
|
|
|
|
var message;
|
|
|
|
var val = this.$chatInput.val();
|
|
|
|
|
|
|
|
if (val) {
|
|
|
|
message = {
|
2013-09-24 16:24:35 -04:00
|
|
|
to: this.model.jid,
|
2013-09-16 19:12:00 -04:00
|
|
|
type: 'groupchat',
|
|
|
|
body: val,
|
|
|
|
chatState: 'active'
|
|
|
|
};
|
|
|
|
if (this.editMode) {
|
|
|
|
message.replace = this.model.lastSentMessage.id || this.model.lastSentMessage.cid;
|
|
|
|
}
|
|
|
|
|
|
|
|
var id = client.sendMessage(message);
|
|
|
|
message.id = id;
|
2013-12-13 19:16:40 -05:00
|
|
|
message.from = client.JID(this.model.jid.bare + '/' + this.model.nick);
|
2013-09-16 19:12:00 -04:00
|
|
|
|
|
|
|
if (this.editMode) {
|
|
|
|
this.model.lastSentMessage.correct(message);
|
|
|
|
} else {
|
|
|
|
var msgModel = new MessageModel(message);
|
|
|
|
msgModel.cid = id;
|
|
|
|
this.model.messages.add(msgModel);
|
|
|
|
this.model.lastSentMessage = msgModel;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.editMode = false;
|
|
|
|
this.typing = false;
|
|
|
|
this.$chatInput.removeClass('editing');
|
|
|
|
this.$chatInput.val('');
|
|
|
|
},
|
|
|
|
handleJoin: function () {
|
|
|
|
this.model.join();
|
|
|
|
},
|
|
|
|
handleLeave: function () {
|
|
|
|
this.model.leave();
|
2013-12-13 19:16:40 -05: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();
|
|
|
|
},
|
|
|
|
refreshModel: function (model) {
|
|
|
|
var existing = this.$('#chat' + model.cid);
|
|
|
|
existing.replaceWith(model.partialTemplateHtml);
|
2013-09-16 19:12:00 -04:00
|
|
|
}
|
|
|
|
});
|