kaiwa/clientapp/pages/groupchat.js

317 lines
10 KiB
JavaScript
Raw Normal View History

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');
2014-01-01 15:34:38 -05:00
var StayDown = require('staydown');
2013-09-16 19:12:00 -04:00
var BasePage = require('./base');
var templates = require('../templates');
2013-12-31 18:02:07 -05:00
var MUCRosterItem = require('../views/mucRosterItem');
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');
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');
2014-11-17 15:09:54 -05:00
var tempSubject = '';
2013-09-16 19:12:00 -04:00
2014-01-01 15:34:38 -05:00
module.exports = BasePage.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);
2013-12-18 16:31:22 -05:00
this.listenTo(this.model.messages, 'change', this.refreshModel);
2014-01-01 20:42:36 -05:00
this.listenTo(this.model.messages, 'reset', this.resetMessages);
2013-12-13 19:16:40 -05:00
2013-09-16 19:12:00 -04:00
this.render();
},
events: {
'keydown textarea': 'handleKeyDown',
'keyup textarea': 'handleKeyUp',
2014-11-17 15:09:54 -05:00
'click .status': 'clickStatusChange',
'blur .status': 'blurStatusChange',
2015-02-09 10:20:28 -05:00
'keydown .status': 'keyDownStatusChange',
'click #members_toggle': 'clickMembersToggle'
2013-09-16 19:12:00 -04:00
},
2013-12-16 13:06:03 -05:00
classBindings: {
joined: '.controls'
},
2013-09-16 19:12:00 -04:00
textBindings: {
2013-12-20 02:04:14 -05:00
displayName: 'header .name',
2015-02-09 10:20:28 -05:00
subject: 'header .status',
membersCount: '#members_toggle_count'
2013-09-16 19:12:00 -04:00
},
show: function (animation) {
BasePage.prototype.show.apply(this, [animation]);
client.sendMessage({
type: 'groupchat',
2013-10-08 11:03:58 -04:00
to: this.model.jid,
2013-09-16 19:12:00 -04:00
chatState: '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;
self.model.fetchHistory();
}
});
2015-02-09 09:27:16 -05:00
this.$chatInput.focus();
2013-09-16 19:12:00 -04:00
},
hide: function () {
BasePage.prototype.hide.apply(this);
client.sendMessage({
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.$chatInput = this.$('.chatBox textarea');
2015-02-09 09:27:16 -05:00
this.$chatInput.val(app.composing[this.model.jid] || '');
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
2014-01-01 15:34:38 -05:00
this.staydown = new StayDown(this.$messageList[0], 500);
2013-12-13 19:16:40 -05:00
2013-12-31 18:02:07 -05:00
this.renderMessages();
this.renderCollection(this.model.resources, MUCRosterItem, this.$('.groupRoster'));
2013-12-13 19:16:40 -05:00
2014-01-01 15:34:38 -05:00
this.listenTo(this.model.messages, 'add', this.handleChatAdded);
2013-12-13 19:16:40 -05:00
2015-02-09 10:20:28 -05:00
//$(window).on('resize', _.bind(this.resizeInput, this));
2013-12-13 19:16:40 -05:00
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-31 18:02:07 -05:00
renderMessages: function () {
2013-12-13 19:16:40 -05:00
var self = this;
2015-02-08 18:21:00 -05:00
this.firstDate = '';
2015-02-09 10:20:28 -05:00
this.lastDate = '';
2013-12-13 19:16:40 -05:00
this.model.messages.each(function (model, i) {
self.appendModel(model);
});
2014-01-01 15:34:38 -05:00
this.staydown.checkdown();
2013-12-13 19:16:40 -05:00
},
handleChatAdded: function (model) {
this.appendModel(model, true);
},
handlePageLoaded: function () {
2014-01-01 15:34:38 -05:00
this.staydown.checkdown();
2015-02-09 10:20:28 -05:00
//this.resizeInput();
2013-12-13 19:16:40 -05:00
},
2013-09-16 19:12:00 -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-09-16 19:12:00 -04:00
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;
} else if (!e.ctrlKey && !e.metaKey) {
2013-12-18 17:01:32 -05:00
if (!this.typing || this.paused) {
2013-09-16 19:12:00 -04:00
this.typing = true;
2013-12-18 17:01:32 -05:00
this.paused = false;
2013-09-16 19:12:00 -04:00
client.sendMessage({
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) {
2015-02-09 10:20:28 -05:00
//this.resizeInput();
2015-02-09 09:27:16 -05:00
app.composing[this.model.jid] = this.$chatInput.val();
2013-09-16 19:12:00 -04:00
if (this.typing && this.$chatInput.val().length === 0) {
this.typing = false;
2013-12-18 17:01:32 -05:00
this.paused = false;
2013-09-16 19:12:00 -04:00
client.sendMessage({
type: 'groupchat',
2013-10-08 11:03:58 -04:00
to: this.model.jid,
2013-09-16 19:12:00 -04:00
chatState: 'active'
});
2013-12-18 17:01:32 -05:00
} else if (this.typing) {
this.pausedTyping();
2013-09-16 19:12:00 -04:00
}
},
2014-01-01 15:34:38 -05:00
resizeInput: _.throttle(function () {
2013-09-16 19:12:00 -04:00
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);
}
}
2014-01-01 15:34:38 -05:00
}, 300),
2013-12-18 17:01:32 -05:00
pausedTyping: _.debounce(function () {
if (this.typing && !this.paused) {
this.paused = true;
2013-09-16 19:12:00 -04:00
client.sendMessage({
type: 'groupchat',
2013-10-08 11:03:58 -04:00
to: this.model.jid,
2013-09-16 19:12:00 -04:00
chatState: 'paused'
});
}
2013-12-18 17:09:02 -05:00
}, 3000),
2013-09-16 19:12:00 -04:00
sendChat: function () {
var message;
var val = this.$chatInput.val();
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-09-16 19:12:00 -04:00
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,
2014-01-02 03:52:26 -05:00
chatState: 'active',
oobURIs: links
2013-09-16 19:12:00 -04:00
};
if (this.editMode) {
message.replace = this.model.lastSentMessage.mid || this.model.lastSentMessage.cid;
2013-09-16 19:12:00 -04:00
}
var id = client.sendMessage(message);
message.mid = 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);
2013-12-18 16:31:22 -05:00
msgModel.save();
2013-09-16 19:12:00 -04:00
this.model.lastSentMessage = msgModel;
}
}
this.editMode = false;
this.typing = false;
2013-12-18 17:01:32 -05:00
this.paused = false;
2013-09-16 19:12:00 -04:00
this.$chatInput.removeClass('editing');
this.$chatInput.val('');
},
2014-11-17 15:09:54 -05:00
clickStatusChange: function (e) {
tempSubject = e.target.textContent;
},
blurStatusChange: function (e) {
var subject = e.target.textContent;
if (subject == '')
subject = true;
client.setSubject(this.model.jid, subject);
e.target.textContent = tempSubject;
},
keyDownStatusChange: function (e) {
if (e.which === 13 && !e.shiftKey) {
e.target.blur();
return false;
}
},
2015-02-09 10:20:28 -05:00
clickMembersToggle: function (e) {
var roster = $('.groupRoster');
if (roster.css('visibility') == 'hidden')
roster.css('visibility', 'visible');
else
roster.css('visibility', 'hidden');
},
2013-12-13 19:16:40 -05:00
appendModel: function (model, preload) {
var newEl, first, last;
2015-02-08 18:21:00 -05:00
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;
}
2013-12-13 19:16:40 -05:00
2015-02-08 18:21:00 -05:00
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);
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);
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-12-13 19:16:40 -05:00
}
},
refreshModel: function (model) {
var existing = this.$('#chat' + model.cid);
existing.replaceWith(model.partialTemplateHtml);
2014-01-01 20:42:36 -05:00
},
resetMessages: function () {
this.$messageList.empty();
2013-09-16 19:12:00 -04:00
}
});