2014-11-13 22:36:09 +00:00
|
|
|
/* vim: ts=4:sw=4:expandtab
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2014-09-01 00:58:36 +00:00
|
|
|
(function () {
|
2015-03-05 23:38:39 +00:00
|
|
|
'use strict';
|
2015-03-05 23:44:53 +00:00
|
|
|
window.Whisper = window.Whisper || {};
|
2014-09-01 00:58:36 +00:00
|
|
|
|
2015-03-05 23:38:39 +00:00
|
|
|
Whisper.NewConversationView = Whisper.View.extend({
|
|
|
|
className: 'new-conversation',
|
|
|
|
template: $('#new-conversation').html(),
|
|
|
|
initialize: function() {
|
|
|
|
this.render();
|
|
|
|
this.$group_update = this.$el.find('.new-group-update-form');
|
2015-03-12 23:14:21 +00:00
|
|
|
this.$create = this.$el.find('.create');
|
|
|
|
this.$input = this.$el.find('input.search');
|
2015-01-29 04:22:41 +00:00
|
|
|
|
2015-03-05 23:38:39 +00:00
|
|
|
// Group avatar file input
|
|
|
|
this.avatarInput = new Whisper.FileInputView({
|
|
|
|
el: this.$el.find('.group-avatar')
|
|
|
|
});
|
2015-02-06 06:42:16 +00:00
|
|
|
|
2015-03-12 23:14:21 +00:00
|
|
|
this.recipients_view = new Whisper.RecipientsInputView();
|
|
|
|
this.$el.find('.scrollable').append(this.recipients_view.el);
|
|
|
|
this.listenTo(this.getRecipients(), 'add', this.updateControls);
|
|
|
|
this.listenTo(this.getRecipients(), 'remove', this.updateControls);
|
2015-03-05 23:38:39 +00:00
|
|
|
},
|
2014-09-01 00:58:36 +00:00
|
|
|
|
2015-03-05 23:38:39 +00:00
|
|
|
events: {
|
2015-03-09 21:21:06 +00:00
|
|
|
'click .create': 'create',
|
|
|
|
'click .back': 'goBack',
|
|
|
|
'keyup': 'keyup'
|
|
|
|
},
|
|
|
|
|
|
|
|
keyup: function(e) {
|
|
|
|
if (e.keyCode === 27) {
|
|
|
|
this.goBack();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
goBack: function() {
|
|
|
|
this.trigger('back');
|
2015-03-05 23:38:39 +00:00
|
|
|
},
|
2015-01-29 04:22:41 +00:00
|
|
|
|
2015-03-12 23:14:21 +00:00
|
|
|
getRecipients: function() {
|
|
|
|
return this.recipients_view.recipients;
|
2015-03-05 23:38:39 +00:00
|
|
|
},
|
2015-02-06 06:42:16 +00:00
|
|
|
|
2015-03-05 23:38:39 +00:00
|
|
|
updateControls: function() {
|
2015-03-12 23:14:21 +00:00
|
|
|
if (this.getRecipients().length > 0) {
|
|
|
|
this.$create.show();
|
2015-03-05 23:38:39 +00:00
|
|
|
} else {
|
2015-03-12 23:14:21 +00:00
|
|
|
this.$create.hide();
|
2015-03-05 23:38:39 +00:00
|
|
|
}
|
2015-03-12 23:14:21 +00:00
|
|
|
if (this.getRecipients().length > 1) {
|
2015-03-05 23:38:39 +00:00
|
|
|
this.$group_update.slideDown();
|
|
|
|
} else {
|
|
|
|
this.$group_update.slideUp();
|
|
|
|
}
|
|
|
|
this.$input.focus();
|
|
|
|
},
|
2015-02-06 06:42:16 +00:00
|
|
|
|
2015-03-05 23:38:39 +00:00
|
|
|
create: function() {
|
|
|
|
var errors = this.recipients_view.$el.find('.error');
|
|
|
|
if (errors.length) {
|
2015-02-08 04:51:52 +00:00
|
|
|
|
2015-03-05 23:38:39 +00:00
|
|
|
// TODO: css animation or error notification
|
|
|
|
errors.removeClass('error');
|
|
|
|
setTimeout(function(){ errors.addClass('error'); }, 300);
|
2015-02-08 04:51:52 +00:00
|
|
|
|
2015-03-05 23:38:39 +00:00
|
|
|
return;
|
2015-02-06 06:42:16 +00:00
|
|
|
}
|
2015-03-12 23:14:21 +00:00
|
|
|
if (this.getRecipients().length > 1) {
|
2015-03-05 23:38:39 +00:00
|
|
|
this.createGroup();
|
|
|
|
} else {
|
|
|
|
this.createConversation();
|
|
|
|
}
|
|
|
|
},
|
2015-02-06 06:42:16 +00:00
|
|
|
|
2015-03-05 23:38:39 +00:00
|
|
|
createConversation: function() {
|
|
|
|
var conversation = new Whisper.Conversation({
|
|
|
|
active_at: null,
|
2015-03-12 23:14:21 +00:00
|
|
|
id: this.getRecipients().at(0).id,
|
2015-03-05 23:38:39 +00:00
|
|
|
type: 'private'
|
|
|
|
});
|
|
|
|
conversation.fetch().then(function() {
|
2015-03-09 21:21:06 +00:00
|
|
|
this.trigger('open', { modelId: conversation.id });
|
2015-03-05 23:38:39 +00:00
|
|
|
}.bind(this)).fail(function() {
|
|
|
|
var saved = conversation.save(); // false or indexedDBRequest
|
|
|
|
if (saved) {
|
|
|
|
saved.then(function() {
|
2015-03-09 21:21:06 +00:00
|
|
|
this.trigger('open', { modelId: conversation.id });
|
2015-03-05 23:38:39 +00:00
|
|
|
}.bind(this));
|
|
|
|
}
|
|
|
|
}.bind(this));
|
|
|
|
},
|
|
|
|
|
|
|
|
createGroup: function() {
|
|
|
|
var name = this.$el.find('.new-group-update-form .name').val();
|
|
|
|
if (!name.trim().length) {
|
|
|
|
return;
|
|
|
|
}
|
2015-02-09 09:39:12 +00:00
|
|
|
|
2015-03-05 23:38:39 +00:00
|
|
|
return this.avatarInput.getFiles().then(function(avatarFiles) {
|
|
|
|
var attributes = {
|
|
|
|
type: 'group',
|
|
|
|
name: name,
|
|
|
|
avatar: avatarFiles[0],
|
2015-03-12 23:14:21 +00:00
|
|
|
members: this.getRecipients().pluck('id')
|
2015-03-05 23:38:39 +00:00
|
|
|
};
|
|
|
|
return textsecure.messaging.createGroup(
|
|
|
|
attributes.members, attributes.name, attributes.avatar
|
|
|
|
).then(function(groupId) {
|
|
|
|
var id = getString(groupId);
|
|
|
|
var group = new Whisper.Conversation(attributes);
|
|
|
|
group.save({ id: id, groupId: id }).then(function() {
|
2015-03-09 21:21:06 +00:00
|
|
|
this.trigger('open', {modelId: id});
|
2015-03-05 23:38:39 +00:00
|
|
|
}.bind(this));
|
2015-01-29 04:22:41 +00:00
|
|
|
}.bind(this));
|
|
|
|
}.bind(this));
|
2015-03-05 23:38:39 +00:00
|
|
|
},
|
2015-01-26 07:44:53 +00:00
|
|
|
|
2015-03-05 23:38:39 +00:00
|
|
|
reset: function() {
|
2015-03-12 23:14:21 +00:00
|
|
|
this.$create.hide();
|
2015-03-19 20:55:10 +00:00
|
|
|
this.$el.find('.new-group-update-form .name').val('');
|
2015-03-05 23:38:39 +00:00
|
|
|
this.$group_update.hide();
|
2015-03-12 23:14:21 +00:00
|
|
|
this.recipients_view.reset();
|
2015-03-05 23:38:39 +00:00
|
|
|
},
|
|
|
|
});
|
2014-09-01 00:58:36 +00:00
|
|
|
|
|
|
|
})();
|