2013-08-29 23:38:28 -04:00
|
|
|
/*global $, app, me, client*/
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
var BasePage = require('./base');
|
|
|
|
var templates = require('../templates');
|
|
|
|
var Message = require('../views/message');
|
|
|
|
var MessageModel = require('../models/message');
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = BasePage.extend({
|
|
|
|
template: templates.pages.chat,
|
|
|
|
initialize: function (spec) {
|
|
|
|
this.editMode = false;
|
|
|
|
this.model.fetchHistory();
|
|
|
|
this.render();
|
|
|
|
},
|
|
|
|
events: {
|
2013-09-16 05:19:07 -04:00
|
|
|
'keydown textarea': 'handleKeyDown',
|
|
|
|
'keyup textarea': 'handleKeyUp'
|
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'
|
|
|
|
},
|
|
|
|
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 () {
|
|
|
|
this.renderAndBind();
|
2013-09-09 19:00:13 -04:00
|
|
|
this.typingTimer = null;
|
2013-09-16 05:19:07 -04:00
|
|
|
this.$chatInput = this.$('.chatBox textarea');
|
|
|
|
this.$messageList = this.$('.messages');
|
|
|
|
this.renderCollection(this.model.messages, Message, this.$('.messages'));
|
2013-08-29 23:38:28 -04:00
|
|
|
this.registerBindings();
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
handleKeyDown: function (e) {
|
2013-09-09 19:00:13 -04:00
|
|
|
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;
|
2013-09-10 03:57:01 -04:00
|
|
|
} else if (!e.ctrlKey) {
|
2013-09-09 19:00:13 -04:00
|
|
|
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();
|
2013-09-15 19:27:09 -04:00
|
|
|
this.typingTimer = setTimeout(this.pausedTyping.bind(this), 3000);
|
2013-09-15 18:06:37 -04:00
|
|
|
if (this.typing && this.$chatInput.val().length === 0) {
|
2013-09-09 19:00:13 -04:00
|
|
|
this.typing = false;
|
|
|
|
client.sendMessage({
|
|
|
|
to: this.model.lockedResource || this.model.jid,
|
|
|
|
chatState: 'active'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
2013-09-15 18:06:37 -04:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2013-09-09 19:00:13 -04:00
|
|
|
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
|
|
|
|
|
|
|
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-12 16:24:52 -04:00
|
|
|
message.replace = this.model.lastSentMessage.id || this.model.lastSentMessage.cid;
|
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);
|
2013-09-09 19:00:13 -04:00
|
|
|
msgModel.cid = id;
|
2013-08-29 23:38:28 -04:00
|
|
|
this.model.messages.add(msgModel);
|
|
|
|
this.model.lastSentMessage = msgModel;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.editMode = false;
|
2013-09-09 19:00:13 -04:00
|
|
|
this.typing = false;
|
2013-09-15 18:06:37 -04:00
|
|
|
this.$chatInput.removeClass('editing');
|
|
|
|
this.$chatInput.val('');
|
2013-08-29 23:38:28 -04:00
|
|
|
}
|
|
|
|
});
|