2014-11-13 23:36:09 +01: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 02:58:36 +02:00
|
|
|
var Whisper = Whisper || {};
|
|
|
|
|
|
|
|
(function () {
|
|
|
|
'use strict';
|
|
|
|
|
2015-02-06 07:42:16 +01:00
|
|
|
var ContactsTypeahead = Backbone.TypeaheadCollection.extend({
|
2015-01-29 06:28:49 +01:00
|
|
|
typeaheadAttributes: [
|
|
|
|
'name',
|
|
|
|
'e164_number',
|
|
|
|
'national_number',
|
|
|
|
'international_number'
|
|
|
|
],
|
2015-01-25 13:26:42 +01:00
|
|
|
database: Whisper.Database,
|
|
|
|
storeName: 'conversations',
|
2015-01-29 06:28:49 +01:00
|
|
|
model: Whisper.Conversation
|
2015-01-25 13:26:42 +01:00
|
|
|
});
|
|
|
|
|
2015-02-06 07:42:16 +01:00
|
|
|
Whisper.ContactPillView = Backbone.View.extend({
|
|
|
|
tagName: 'span',
|
|
|
|
className: 'recipient',
|
|
|
|
events: {
|
|
|
|
'click .remove': 'removeModel'
|
|
|
|
},
|
|
|
|
initialize: function() {
|
|
|
|
this.template = $('#contact_pill').html();
|
|
|
|
Mustache.parse(this.template);
|
|
|
|
|
|
|
|
var error = this.model.validate(this.model.attributes);
|
|
|
|
if (error) {
|
|
|
|
this.$el.addClass('error');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
removeModel: function() {
|
|
|
|
this.$el.trigger('remove', {modelId: this.model.id});
|
|
|
|
this.remove();
|
|
|
|
},
|
|
|
|
render: function() {
|
|
|
|
this.$el.html(
|
|
|
|
Mustache.render(this.template, { name: this.model.getTitle() })
|
|
|
|
);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Whisper.RecipientListView = Whisper.ListView.extend({
|
|
|
|
itemView: Whisper.ContactPillView
|
|
|
|
});
|
|
|
|
|
2014-09-01 02:58:36 +02:00
|
|
|
Whisper.NewConversationView = Backbone.View.extend({
|
2015-01-25 13:26:42 +01:00
|
|
|
className: 'new-conversation',
|
2014-09-01 02:58:36 +02:00
|
|
|
initialize: function() {
|
2015-01-26 08:44:53 +01:00
|
|
|
this.template = $('#new-conversation').html();
|
|
|
|
Mustache.parse(this.template);
|
|
|
|
this.$el.html($(Mustache.render(this.template)));
|
2015-01-29 05:22:41 +01:00
|
|
|
this.$group_update = this.$el.find('.new-group-update-form');
|
2015-02-06 07:42:16 +01:00
|
|
|
this.$buttons = this.$el.find('.buttons');
|
|
|
|
this.$input = this.$el.find('input.new-message');
|
2015-02-08 05:53:29 +01:00
|
|
|
this.$new_contact = this.$el.find('.new-contact');
|
2015-02-06 07:42:16 +01:00
|
|
|
|
|
|
|
// Collection of contacts to match user input against
|
|
|
|
this.typeahead = new ContactsTypeahead();
|
|
|
|
this.typeahead.fetch({ conditions: { type: 'private' } });
|
2014-09-01 02:58:36 +02:00
|
|
|
|
2015-02-06 07:42:16 +01:00
|
|
|
// View to display the matched contacts from typeahead
|
2015-01-26 08:44:53 +01:00
|
|
|
this.typeahead_view = new Whisper.ConversationListView({
|
2015-02-06 07:42:16 +01:00
|
|
|
collection : new Whisper.ConversationCollection([], {
|
2015-01-29 05:48:21 +01:00
|
|
|
comparator: function(m) { return m.getTitle(); }
|
2015-02-06 07:42:16 +01:00
|
|
|
})
|
2015-01-28 13:24:18 +01:00
|
|
|
});
|
2015-02-06 07:42:16 +01:00
|
|
|
this.$el.find('.contacts').append(this.typeahead_view.el);
|
2015-01-25 13:53:07 +01:00
|
|
|
|
2015-02-08 05:53:29 +01:00
|
|
|
this.initNewContact();
|
2015-01-29 05:22:41 +01:00
|
|
|
|
2015-02-06 07:42:16 +01:00
|
|
|
// Group avatar file input
|
|
|
|
this.avatarInput = new Whisper.FileInputView({
|
|
|
|
el: this.$el.find('.group-avatar')
|
|
|
|
});
|
|
|
|
|
|
|
|
// Collection of recipients selected for the new message
|
|
|
|
this.recipients = new Whisper.ConversationCollection([], {
|
|
|
|
comparator: false
|
|
|
|
});
|
|
|
|
// View to display the selected recipients
|
2015-02-08 05:51:52 +01:00
|
|
|
this.recipients_view = new Whisper.RecipientListView({
|
2015-02-06 07:42:16 +01:00
|
|
|
collection: this.recipients,
|
|
|
|
el: this.$el.find('.recipients')
|
2015-01-29 05:22:41 +01:00
|
|
|
});
|
2014-09-01 02:58:36 +02:00
|
|
|
},
|
|
|
|
|
2015-01-26 08:44:53 +01:00
|
|
|
events: {
|
|
|
|
'change input.new-message': 'filterContacts',
|
2015-01-29 05:22:41 +01:00
|
|
|
'keyup input.new-message': 'filterContacts',
|
2015-02-08 19:44:31 +01:00
|
|
|
'select .new-contact': 'addNewRecipient',
|
|
|
|
'select .contacts': 'addRecipient',
|
2015-02-06 07:42:16 +01:00
|
|
|
'remove .recipient': 'removeRecipient',
|
|
|
|
'click .create': 'create'
|
2015-01-29 05:22:41 +01:00
|
|
|
},
|
|
|
|
|
2015-02-08 05:53:29 +01:00
|
|
|
initNewContact: function() {
|
|
|
|
// Creates a view to display a new contact
|
|
|
|
this.new_contact = new Whisper.ConversationListItemView({
|
|
|
|
el: this.$new_contact,
|
|
|
|
model: new Whisper.Conversation({
|
|
|
|
active_at: null,
|
|
|
|
type: 'private',
|
|
|
|
newContact: true
|
|
|
|
})
|
|
|
|
}).render();
|
|
|
|
},
|
|
|
|
|
2015-02-06 07:42:16 +01:00
|
|
|
addNewRecipient: function(e, data) {
|
|
|
|
this.recipients.add(this.new_contact.model);
|
2015-02-08 05:53:29 +01:00
|
|
|
this.initNewContact();
|
2015-02-06 07:42:16 +01:00
|
|
|
this.resetTypeahead();
|
|
|
|
this.updateControls();
|
|
|
|
},
|
|
|
|
|
|
|
|
addRecipient: function(e, data) {
|
|
|
|
this.recipients.add(this.typeahead.remove(data.modelId));
|
|
|
|
this.filterContacts();
|
|
|
|
this.updateControls();
|
|
|
|
},
|
|
|
|
|
|
|
|
removeRecipient: function(e, data) {
|
|
|
|
var model = this.recipients.remove(data.modelId);
|
2015-02-08 05:53:29 +01:00
|
|
|
if (!model.get('newContact')) {
|
2015-02-06 07:42:16 +01:00
|
|
|
this.typeahead.add(model);
|
|
|
|
}
|
|
|
|
this.filterContacts();
|
|
|
|
this.updateControls();
|
|
|
|
},
|
|
|
|
|
|
|
|
updateControls: function() {
|
|
|
|
if (this.recipients.length > 0) {
|
|
|
|
this.$buttons.slideDown();
|
2015-01-29 05:22:41 +01:00
|
|
|
} else {
|
2015-02-06 07:42:16 +01:00
|
|
|
this.$buttons.slideUp();
|
2015-01-29 05:22:41 +01:00
|
|
|
}
|
2015-02-06 07:42:16 +01:00
|
|
|
if (this.recipients.length > 1) {
|
|
|
|
this.$group_update.slideDown();
|
|
|
|
} else {
|
|
|
|
this.$group_update.slideUp();
|
|
|
|
}
|
|
|
|
this.$input.focus();
|
|
|
|
},
|
|
|
|
|
|
|
|
create: function() {
|
2015-02-08 05:51:52 +01:00
|
|
|
var errors = this.recipients_view.$el.find('.error');
|
|
|
|
if (errors.length) {
|
|
|
|
|
|
|
|
// TODO: css animation or error notification
|
|
|
|
errors.removeClass('error');
|
|
|
|
setTimeout(function(){ errors.addClass('error'); }, 300);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2015-02-06 07:42:16 +01:00
|
|
|
if (this.recipients.length > 1) {
|
|
|
|
this.createGroup();
|
2015-01-29 05:22:41 +01:00
|
|
|
} else {
|
2015-02-06 07:42:16 +01:00
|
|
|
this.createConversation();
|
2015-01-29 05:22:41 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-02-06 07:42:16 +01:00
|
|
|
createConversation: function() {
|
|
|
|
var conversation = new Whisper.Conversation({
|
2015-02-08 05:54:01 +01:00
|
|
|
active_at: null,
|
2015-02-06 07:42:16 +01:00
|
|
|
id: this.recipients.at(0).id,
|
|
|
|
type: 'private'
|
|
|
|
});
|
|
|
|
conversation.fetch().fail(function() {
|
|
|
|
if (conversation.save()) {
|
|
|
|
this.$el.trigger('open', { modelId: conversation.id });
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2015-01-29 05:22:41 +01:00
|
|
|
createGroup: function() {
|
2015-02-06 07:42:16 +01:00
|
|
|
return this.avatarInput.getFiles().then(function(avatarFiles) {
|
2015-01-29 05:22:41 +01:00
|
|
|
var attributes = {
|
|
|
|
type: 'group',
|
|
|
|
name: this.$el.find('.new-group-update-form .name').val(),
|
|
|
|
avatar: avatarFiles[0],
|
2015-02-06 07:42:16 +01:00
|
|
|
members: this.recipients.pluck('id')
|
2015-01-29 05:22:41 +01: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-02-08 19:44:31 +01:00
|
|
|
this.$el.trigger('open', {modelId: id});
|
2015-01-29 05:22:41 +01:00
|
|
|
}.bind(this));
|
|
|
|
}.bind(this));
|
|
|
|
}.bind(this));
|
2015-01-26 08:44:53 +01:00
|
|
|
},
|
|
|
|
|
2015-02-06 07:42:16 +01:00
|
|
|
resetTypeahead: function() {
|
2015-01-26 08:44:53 +01:00
|
|
|
this.new_contact.$el.hide();
|
|
|
|
this.$input.val('').focus();
|
2015-02-06 07:42:16 +01:00
|
|
|
this.typeahead_view.collection.reset(this.typeahead.models);
|
|
|
|
},
|
|
|
|
|
|
|
|
reset: function() {
|
|
|
|
this.$buttons.hide();
|
|
|
|
this.$group_update.hide();
|
2015-02-08 05:53:29 +01:00
|
|
|
this.typeahead.add(
|
|
|
|
this.recipients.filter(function(model) {
|
|
|
|
return !model.get('newContact');
|
|
|
|
})
|
|
|
|
);
|
2015-02-06 07:42:16 +01:00
|
|
|
this.recipients.reset([]);
|
|
|
|
this.resetTypeahead();
|
2015-01-26 08:44:53 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
filterContacts: function() {
|
|
|
|
var query = this.$input.val();
|
2015-01-25 13:26:42 +01:00
|
|
|
if (query.length) {
|
2015-01-26 08:44:53 +01:00
|
|
|
if (this.maybeNumber(query)) {
|
2015-01-26 22:44:03 +01:00
|
|
|
this.new_contact.model.set('id', query);
|
2015-02-06 07:42:16 +01:00
|
|
|
this.new_contact.render().$el.show();
|
2015-01-26 08:44:53 +01:00
|
|
|
} else {
|
|
|
|
this.new_contact.$el.hide();
|
|
|
|
}
|
2015-01-25 13:26:42 +01:00
|
|
|
this.typeahead_view.collection.reset(
|
2015-02-06 07:42:16 +01:00
|
|
|
this.typeahead.typeahead(query)
|
2015-01-25 13:26:42 +01:00
|
|
|
);
|
2015-01-26 08:44:53 +01:00
|
|
|
} else {
|
2015-02-06 07:42:16 +01:00
|
|
|
this.resetTypeahead();
|
2014-11-04 02:51:49 +01:00
|
|
|
}
|
2015-01-26 02:53:12 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
maybeNumber: function(number) {
|
|
|
|
return number.match(/^\+?[0-9]*$/);
|
2014-09-01 02:58:36 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
})();
|