2015-09-07 21:53:43 +00:00
|
|
|
/*
|
|
|
|
* vim: ts=4:sw=4:expandtab
|
2014-11-13 22:36:09 +00:00
|
|
|
*/
|
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(),
|
2015-06-04 20:29:01 +00:00
|
|
|
initialize: function(options) {
|
2015-03-05 23:38:39 +00:00
|
|
|
this.render();
|
2015-09-04 20:11:21 +00:00
|
|
|
this.$group_update = this.$('.group-info-input');
|
2015-03-28 01:47:58 +00:00
|
|
|
this.$create = this.$('.create');
|
2015-01-29 04:22:41 +00:00
|
|
|
|
2015-03-05 23:38:39 +00:00
|
|
|
// Group avatar file input
|
2015-06-04 20:29:01 +00:00
|
|
|
this.appWindow = options.appWindow;
|
2015-03-05 23:38:39 +00:00
|
|
|
this.avatarInput = new Whisper.FileInputView({
|
2015-06-04 20:29:01 +00:00
|
|
|
el: this.$('.group-avatar'),
|
|
|
|
window: this.appWindow.contentWindow
|
2015-03-05 23:38:39 +00:00
|
|
|
});
|
2015-02-06 06:42:16 +00:00
|
|
|
|
2015-03-12 23:14:21 +00:00
|
|
|
this.recipients_view = new Whisper.RecipientsInputView();
|
2015-09-04 20:11:21 +00:00
|
|
|
this.recipients_view.$el.insertAfter(this.$('.group-info-input'));
|
2015-07-08 22:53:58 +00:00
|
|
|
this.$input = this.$('input.search');
|
|
|
|
|
2015-03-12 23:14:21 +00:00
|
|
|
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-09-04 20:11:21 +00:00
|
|
|
render_attributes: function() {
|
|
|
|
return {
|
|
|
|
avatar: { url: '/images/group_default.png', color: 'gray' }
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
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() {
|
2015-03-28 01:47:58 +00:00
|
|
|
var errors = this.recipients_view.$('.error');
|
2015-03-05 23:38:39 +00:00
|
|
|
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');
|
2016-02-18 01:08:50 +00:00
|
|
|
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 {
|
2015-08-27 19:38:51 +00:00
|
|
|
var id = this.getRecipients().at(0).id;
|
|
|
|
ConversationController.findOrCreatePrivateById(id).then(function(conversation) {
|
|
|
|
conversation.save('active_at', Date.now());
|
2015-09-21 17:21:33 +00:00
|
|
|
this.trigger('open', conversation);
|
2015-08-27 19:38:51 +00:00
|
|
|
}.bind(this));
|
2015-03-05 23:38:39 +00:00
|
|
|
}
|
|
|
|
},
|
2015-02-06 06:42:16 +00:00
|
|
|
|
2015-03-05 23:38:39 +00:00
|
|
|
createGroup: function() {
|
2015-09-04 20:11:21 +00:00
|
|
|
var name = this.$('.group-info-input .name').val();
|
2015-03-05 23:38:39 +00:00
|
|
|
if (!name.trim().length) {
|
|
|
|
return;
|
|
|
|
}
|
2015-02-09 09:39:12 +00:00
|
|
|
|
2015-06-04 18:55:23 +00:00
|
|
|
return this.avatarInput.getThumbnail().then(function(avatarFile) {
|
2015-03-24 00:33:46 +00:00
|
|
|
var members = this.getRecipients().pluck('id');
|
2015-09-15 20:57:49 +00:00
|
|
|
members.push(textsecure.storage.user.getNumber());
|
2015-05-06 20:11:12 +00:00
|
|
|
textsecure.storage.groups.createNewGroup(members).then(function(group) {
|
|
|
|
return group.id;
|
|
|
|
}).then(function(groupId) {
|
2015-09-22 02:12:06 +00:00
|
|
|
var now = Date.now();
|
|
|
|
var group = ConversationController.create({
|
2015-05-06 20:11:12 +00:00
|
|
|
id: groupId,
|
|
|
|
groupId: groupId,
|
|
|
|
type: 'group',
|
|
|
|
name: name,
|
|
|
|
avatar: avatarFile,
|
2015-08-27 00:11:01 +00:00
|
|
|
members: members,
|
2015-09-22 02:12:06 +00:00
|
|
|
active_at: now,
|
|
|
|
});
|
2015-05-06 20:11:12 +00:00
|
|
|
group.save().then(function() {
|
2015-09-21 17:21:33 +00:00
|
|
|
this.trigger('open', group);
|
2015-05-06 20:11:12 +00:00
|
|
|
}.bind(this));
|
2015-09-22 02:12:06 +00:00
|
|
|
group.updateGroup();
|
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-08-26 22:13:14 +00:00
|
|
|
this.delegateEvents();
|
2015-08-27 00:11:01 +00:00
|
|
|
this.avatarInput.delegateEvents();
|
2015-03-12 23:14:21 +00:00
|
|
|
this.$create.hide();
|
2015-09-04 20:11:21 +00:00
|
|
|
this.$('.group-info-input .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
|
|
|
|
|
|
|
})();
|