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

119 lines
3.7 KiB
JavaScript
Raw Normal View History

2013-08-29 23:38:28 -04:00
/*global $, app, me, client*/
"use strict";
var BasePage = require('./base');
var templates = require('../templates');
var ContactListItem = require('../views/contactListItem');
var Message = require('../views/message');
var MessageModel = require('../models/message');
module.exports = BasePage.extend({
template: templates.pages.chat,
initialize: function (spec) {
this.editMode = false;
2013-09-03 18:25:14 -04:00
this.model.fetchTimezone();
2013-08-29 23:38:28 -04:00
this.model.fetchHistory();
this.render();
},
events: {
'keydown #chatBuffer': 'handleKeyDown',
'keyup #chatBuffer': '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',
formattedTZO: 'header #tzo'
2013-08-29 23:38:28 -04:00
},
render: function () {
this.renderAndBind();
this.typingTimer = null;
2013-08-29 23:38:28 -04:00
this.$chatBuffer = this.$('#chatBuffer');
this.renderCollection(this.model.messages, Message, this.$('#conversation'));
this.registerBindings();
return this;
},
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;
} else if (e.which === 38 && this.$chatBuffer.val() === '' && this.model.lastSentMessage) {
this.editMode = true;
this.$chatBuffer.addClass('editing');
this.$chatBuffer.val(this.model.lastSentMessage.body);
e.preventDefault();
return false;
} else if (e.which === 40 && this.editMode) {
this.editMode = false;
this.$chatBuffer.removeClass('editing');
e.preventDefault();
return false;
2013-09-10 03:57:01 -04:00
} else if (!e.ctrlKey) {
if (!this.typing) {
this.typing = true;
client.sendMessage({
to: this.model.lockedResource || this.model.jid,
chatState: 'composing'
});
}
}
},
handleKeyUp: function (e) {
this.typingTimer = setTimeout(this.pausedTyping.bind(this), 5000);
if (this.typing && this.$chatBuffer.val().length === 0) {
this.typing = false;
client.sendMessage({
to: this.model.lockedResource || this.model.jid,
chatState: 'active'
});
}
},
pausedTyping: function () {
console.log('paused?', this.typing);
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;
var val = this.$chatBuffer.val();
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) {
message.replace = this.model.lastSentMessage.id;
}
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);
msgModel.cid = id;
2013-08-29 23:38:28 -04:00
this.model.messages.add(msgModel);
this.model.lastSentMessage = msgModel;
}
}
this.editMode = false;
this.typing = false;
2013-08-29 23:38:28 -04:00
this.$chatBuffer.removeClass('editing');
this.$chatBuffer.val('');
}
});