2018-07-07 00:48:14 +00:00
|
|
|
/* global Whisper, Backbone, ConversationController */
|
|
|
|
|
|
|
|
// eslint-disable-next-line func-names
|
2018-04-27 21:25:04 +00:00
|
|
|
(function() {
|
|
|
|
'use strict';
|
2018-07-07 00:48:14 +00:00
|
|
|
|
2018-04-27 21:25:04 +00:00
|
|
|
window.Whisper = window.Whisper || {};
|
|
|
|
|
2018-07-07 00:48:14 +00:00
|
|
|
const ContactsTypeahead = Backbone.TypeaheadCollection.extend({
|
2018-04-27 21:25:04 +00:00
|
|
|
typeaheadAttributes: [
|
|
|
|
'name',
|
|
|
|
'e164_number',
|
|
|
|
'national_number',
|
|
|
|
'international_number',
|
|
|
|
],
|
|
|
|
model: Whisper.Conversation,
|
2018-09-21 01:47:19 +00:00
|
|
|
async fetchContacts() {
|
|
|
|
const models = window.Signal.Data.getAllPrivateConversations({
|
|
|
|
ConversationCollection: Whisper.ConversationCollection,
|
|
|
|
});
|
|
|
|
|
|
|
|
this.reset(models);
|
2018-04-27 21:25:04 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Whisper.ContactPillView = Whisper.View.extend({
|
|
|
|
tagName: 'span',
|
|
|
|
className: 'recipient',
|
|
|
|
events: {
|
|
|
|
'click .remove': 'removeModel',
|
|
|
|
},
|
|
|
|
templateName: 'contact_pill',
|
2018-07-07 00:48:14 +00:00
|
|
|
initialize() {
|
|
|
|
const error = this.model.validate(this.model.attributes);
|
2018-04-27 21:25:04 +00:00
|
|
|
if (error) {
|
|
|
|
this.$el.addClass('error');
|
|
|
|
}
|
|
|
|
},
|
2018-07-07 00:48:14 +00:00
|
|
|
removeModel() {
|
2018-04-27 21:25:04 +00:00
|
|
|
this.$el.trigger('remove', { modelId: this.model.id });
|
|
|
|
this.remove();
|
|
|
|
},
|
2018-07-07 00:48:14 +00:00
|
|
|
render_attributes() {
|
2018-04-27 21:25:04 +00:00
|
|
|
return { name: this.model.getTitle() };
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Whisper.RecipientListView = Whisper.ListView.extend({
|
|
|
|
itemView: Whisper.ContactPillView,
|
|
|
|
});
|
|
|
|
|
|
|
|
Whisper.SuggestionView = Whisper.ConversationListItemView.extend({
|
|
|
|
className: 'contact-details contact',
|
|
|
|
templateName: 'contact_name_and_number',
|
|
|
|
});
|
|
|
|
|
|
|
|
Whisper.SuggestionListView = Whisper.ConversationListView.extend({
|
|
|
|
itemView: Whisper.SuggestionView,
|
|
|
|
});
|
|
|
|
|
|
|
|
Whisper.RecipientsInputView = Whisper.View.extend({
|
|
|
|
className: 'recipients-input',
|
|
|
|
templateName: 'recipients-input',
|
2018-07-07 00:48:14 +00:00
|
|
|
initialize(options) {
|
2018-04-27 21:25:04 +00:00
|
|
|
if (options) {
|
|
|
|
this.placeholder = options.placeholder;
|
|
|
|
}
|
|
|
|
this.render();
|
|
|
|
this.$input = this.$('input.search');
|
|
|
|
this.$new_contact = this.$('.new-contact');
|
|
|
|
|
|
|
|
// Collection of recipients selected for the new message
|
|
|
|
this.recipients = new Whisper.ConversationCollection([], {
|
|
|
|
comparator: false,
|
|
|
|
});
|
|
|
|
|
|
|
|
// View to display the selected recipients
|
|
|
|
this.recipients_view = new Whisper.RecipientListView({
|
|
|
|
collection: this.recipients,
|
|
|
|
el: this.$('.recipients'),
|
|
|
|
});
|
|
|
|
|
|
|
|
// Collection of contacts to match user input against
|
|
|
|
this.typeahead = new ContactsTypeahead();
|
|
|
|
this.typeahead.fetchContacts();
|
|
|
|
|
|
|
|
// View to display the matched contacts from typeahead
|
|
|
|
this.typeahead_view = new Whisper.SuggestionListView({
|
|
|
|
collection: new Whisper.ConversationCollection([], {
|
2018-07-07 00:48:14 +00:00
|
|
|
comparator(m) {
|
2018-04-27 21:25:04 +00:00
|
|
|
return m.getTitle().toLowerCase();
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
});
|
|
|
|
this.$('.contacts').append(this.typeahead_view.el);
|
|
|
|
this.initNewContact();
|
|
|
|
this.listenTo(this.typeahead, 'reset', this.filterContacts);
|
|
|
|
},
|
|
|
|
|
2018-07-07 00:48:14 +00:00
|
|
|
render_attributes() {
|
2018-04-27 21:25:04 +00:00
|
|
|
return { placeholder: this.placeholder || 'name or phone number' };
|
|
|
|
},
|
|
|
|
|
|
|
|
events: {
|
|
|
|
'input input.search': 'filterContacts',
|
|
|
|
'select .new-contact': 'addNewRecipient',
|
|
|
|
'select .contacts': 'addRecipient',
|
|
|
|
'remove .recipient': 'removeRecipient',
|
|
|
|
},
|
|
|
|
|
2018-07-07 00:48:14 +00:00
|
|
|
filterContacts() {
|
|
|
|
const query = this.$input.val();
|
2018-04-27 21:25:04 +00:00
|
|
|
if (query.length) {
|
|
|
|
if (this.maybeNumber(query)) {
|
|
|
|
this.new_contact_view.model.set('id', query);
|
|
|
|
this.new_contact_view.render().$el.show();
|
|
|
|
} else {
|
|
|
|
this.new_contact_view.$el.hide();
|
2015-07-06 19:07:47 +00:00
|
|
|
}
|
2018-04-27 21:25:04 +00:00
|
|
|
this.typeahead_view.collection.reset(this.typeahead.typeahead(query));
|
|
|
|
} else {
|
|
|
|
this.resetTypeahead();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-07-07 00:48:14 +00:00
|
|
|
initNewContact() {
|
2018-04-27 21:25:04 +00:00
|
|
|
if (this.new_contact_view) {
|
|
|
|
this.new_contact_view.undelegateEvents();
|
|
|
|
this.new_contact_view.$el.hide();
|
|
|
|
}
|
|
|
|
// Creates a view to display a new contact
|
|
|
|
this.new_contact_view = new Whisper.ConversationListItemView({
|
|
|
|
el: this.$new_contact,
|
|
|
|
model: ConversationController.create({
|
|
|
|
type: 'private',
|
|
|
|
newContact: true,
|
|
|
|
}),
|
|
|
|
}).render();
|
|
|
|
},
|
|
|
|
|
2018-07-07 00:48:14 +00:00
|
|
|
addNewRecipient() {
|
2018-04-27 21:25:04 +00:00
|
|
|
this.recipients.add(this.new_contact_view.model);
|
|
|
|
this.initNewContact();
|
|
|
|
this.resetTypeahead();
|
|
|
|
},
|
|
|
|
|
2018-07-07 00:48:14 +00:00
|
|
|
addRecipient(e, conversation) {
|
2018-04-27 21:25:04 +00:00
|
|
|
this.recipients.add(this.typeahead.remove(conversation.id));
|
|
|
|
this.resetTypeahead();
|
|
|
|
},
|
|
|
|
|
2018-07-07 00:48:14 +00:00
|
|
|
removeRecipient(e, data) {
|
|
|
|
const model = this.recipients.remove(data.modelId);
|
2018-04-27 21:25:04 +00:00
|
|
|
if (!model.get('newContact')) {
|
|
|
|
this.typeahead.add(model);
|
|
|
|
}
|
|
|
|
this.filterContacts();
|
|
|
|
},
|
|
|
|
|
2018-07-07 00:48:14 +00:00
|
|
|
reset() {
|
2018-04-27 21:25:04 +00:00
|
|
|
this.delegateEvents();
|
|
|
|
this.typeahead_view.delegateEvents();
|
|
|
|
this.recipients_view.delegateEvents();
|
|
|
|
this.new_contact_view.delegateEvents();
|
|
|
|
this.typeahead.add(
|
2018-07-07 00:48:14 +00:00
|
|
|
this.recipients.filter(model => !model.get('newContact'))
|
2018-04-27 21:25:04 +00:00
|
|
|
);
|
|
|
|
this.recipients.reset([]);
|
|
|
|
this.resetTypeahead();
|
|
|
|
this.typeahead.fetchContacts();
|
|
|
|
},
|
|
|
|
|
2018-07-07 00:48:14 +00:00
|
|
|
resetTypeahead() {
|
2018-04-27 21:25:04 +00:00
|
|
|
this.new_contact_view.$el.hide();
|
|
|
|
this.$input.val('').focus();
|
|
|
|
this.typeahead_view.collection.reset([]);
|
|
|
|
},
|
|
|
|
|
2018-07-07 00:48:14 +00:00
|
|
|
maybeNumber(number) {
|
2018-04-27 21:25:04 +00:00
|
|
|
return number.match(/^\+?[0-9]*$/);
|
|
|
|
},
|
|
|
|
});
|
2015-03-12 23:14:21 +00:00
|
|
|
})();
|