2015-09-07 21:53:43 +00:00
|
|
|
/*
|
|
|
|
* vim: ts=4:sw=4:expandtab
|
2015-03-12 23:14:21 +00:00
|
|
|
*/
|
|
|
|
(function () {
|
|
|
|
'use strict';
|
|
|
|
window.Whisper = window.Whisper || {};
|
|
|
|
|
|
|
|
var ContactsTypeahead = Backbone.TypeaheadCollection.extend({
|
|
|
|
typeaheadAttributes: [
|
|
|
|
'name',
|
|
|
|
'e164_number',
|
|
|
|
'national_number',
|
|
|
|
'international_number'
|
|
|
|
],
|
|
|
|
database: Whisper.Database,
|
|
|
|
storeName: 'conversations',
|
2015-07-06 19:07:47 +00:00
|
|
|
model: Whisper.Conversation,
|
|
|
|
fetchContacts: function() {
|
2015-08-05 17:54:47 +00:00
|
|
|
return this.fetch({ reset: true, conditions: { type: 'private' } });
|
2015-07-06 19:07:47 +00:00
|
|
|
}
|
2015-03-12 23:14:21 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
Whisper.ContactPillView = Whisper.View.extend({
|
|
|
|
tagName: 'span',
|
|
|
|
className: 'recipient',
|
|
|
|
events: {
|
|
|
|
'click .remove': 'removeModel'
|
|
|
|
},
|
2015-09-04 20:06:17 +00:00
|
|
|
templateName: 'contact_pill',
|
2015-03-12 23:14:21 +00:00
|
|
|
initialize: function() {
|
|
|
|
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_attributes: function() {
|
|
|
|
return { name: this.model.getTitle() };
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Whisper.RecipientListView = Whisper.ListView.extend({
|
|
|
|
itemView: Whisper.ContactPillView
|
|
|
|
});
|
|
|
|
|
2015-09-04 20:11:21 +00:00
|
|
|
Whisper.SuggestionView = Whisper.ConversationListItemView.extend({
|
|
|
|
className: 'contact-details contact',
|
|
|
|
templateName: 'contact_name_and_number',
|
|
|
|
});
|
|
|
|
|
|
|
|
Whisper.SuggestionListView = Whisper.ConversationListView.extend({
|
|
|
|
itemView: Whisper.SuggestionView
|
|
|
|
});
|
|
|
|
|
2015-03-12 23:14:21 +00:00
|
|
|
Whisper.RecipientsInputView = Whisper.View.extend({
|
|
|
|
className: 'recipients-input',
|
2015-09-04 20:06:17 +00:00
|
|
|
templateName: 'recipients-input',
|
2015-06-09 19:04:03 +00:00
|
|
|
initialize: function(options) {
|
|
|
|
if (options) {
|
|
|
|
this.placeholder = options.placeholder;
|
|
|
|
}
|
2015-03-12 23:14:21 +00:00
|
|
|
this.render();
|
2015-03-28 01:47:58 +00:00
|
|
|
this.$input = this.$('input.search');
|
|
|
|
this.$new_contact = this.$('.new-contact');
|
2015-03-12 23:14:21 +00:00
|
|
|
|
|
|
|
// 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,
|
2015-03-28 01:47:58 +00:00
|
|
|
el: this.$('.recipients')
|
2015-03-12 23:14:21 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// Collection of contacts to match user input against
|
|
|
|
this.typeahead = new ContactsTypeahead();
|
2015-07-06 19:07:47 +00:00
|
|
|
this.typeahead.fetchContacts();
|
2015-03-12 23:14:21 +00:00
|
|
|
|
|
|
|
// View to display the matched contacts from typeahead
|
2015-09-04 20:11:21 +00:00
|
|
|
this.typeahead_view = new Whisper.SuggestionListView({
|
2015-03-12 23:14:21 +00:00
|
|
|
collection : new Whisper.ConversationCollection([], {
|
2015-06-19 22:48:00 +00:00
|
|
|
comparator: function(m) { return m.getTitle().toLowerCase(); }
|
2015-03-12 23:14:21 +00:00
|
|
|
})
|
|
|
|
});
|
2015-03-28 01:47:58 +00:00
|
|
|
this.$('.contacts').append(this.typeahead_view.el);
|
2015-08-27 19:38:51 +00:00
|
|
|
this.initNewContact();
|
2015-08-05 17:54:47 +00:00
|
|
|
this.listenTo(this.typeahead, 'reset', this.filterContacts);
|
2015-03-12 23:14:21 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
2015-06-09 19:04:03 +00:00
|
|
|
render_attributes: function() {
|
2015-08-31 22:11:00 +00:00
|
|
|
return { placeholder: this.placeholder || "name or phone number" };
|
2015-06-09 19:04:03 +00:00
|
|
|
},
|
|
|
|
|
2015-03-12 23:14:21 +00:00
|
|
|
events: {
|
2015-07-08 19:47:01 +00:00
|
|
|
'input input.search': 'filterContacts',
|
2015-03-12 23:14:21 +00:00
|
|
|
'select .new-contact': 'addNewRecipient',
|
|
|
|
'select .contacts': 'addRecipient',
|
|
|
|
'remove .recipient': 'removeRecipient',
|
|
|
|
},
|
|
|
|
|
|
|
|
filterContacts: function(e) {
|
|
|
|
var query = this.$input.val();
|
|
|
|
if (query.length) {
|
|
|
|
if (this.maybeNumber(query)) {
|
2015-08-27 00:52:44 +00:00
|
|
|
this.new_contact_view.model.set('id', query);
|
|
|
|
this.new_contact_view.render().$el.show();
|
2015-03-12 23:14:21 +00:00
|
|
|
} else {
|
2015-08-27 00:52:44 +00:00
|
|
|
this.new_contact_view.$el.hide();
|
2015-03-12 23:14:21 +00:00
|
|
|
}
|
|
|
|
this.typeahead_view.collection.reset(
|
|
|
|
this.typeahead.typeahead(query)
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
this.resetTypeahead();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
initNewContact: function() {
|
2015-08-27 00:52:44 +00:00
|
|
|
if (this.new_contact_view) {
|
|
|
|
this.new_contact_view.undelegateEvents();
|
|
|
|
this.new_contact_view.$el.hide();
|
2015-03-12 23:14:21 +00:00
|
|
|
}
|
|
|
|
// Creates a view to display a new contact
|
2015-08-27 00:52:44 +00:00
|
|
|
this.new_contact_view = new Whisper.ConversationListItemView({
|
2015-03-12 23:14:21 +00:00
|
|
|
el: this.$new_contact,
|
2015-08-27 19:38:51 +00:00
|
|
|
model: ConversationController.create({
|
2015-03-12 23:14:21 +00:00
|
|
|
type: 'private',
|
|
|
|
newContact: true
|
|
|
|
})
|
|
|
|
}).render();
|
|
|
|
},
|
|
|
|
|
2015-09-21 17:21:33 +00:00
|
|
|
addNewRecipient: function() {
|
2015-08-27 00:52:44 +00:00
|
|
|
this.recipients.add(this.new_contact_view.model);
|
2015-03-12 23:14:21 +00:00
|
|
|
this.initNewContact();
|
|
|
|
this.resetTypeahead();
|
|
|
|
},
|
|
|
|
|
2015-09-21 17:21:33 +00:00
|
|
|
addRecipient: function(e, conversation) {
|
|
|
|
this.recipients.add(this.typeahead.remove(conversation.id));
|
2015-06-17 19:27:54 +00:00
|
|
|
this.resetTypeahead();
|
2015-03-12 23:14:21 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
removeRecipient: function(e, data) {
|
|
|
|
var model = this.recipients.remove(data.modelId);
|
|
|
|
if (!model.get('newContact')) {
|
|
|
|
this.typeahead.add(model);
|
|
|
|
}
|
|
|
|
this.filterContacts();
|
|
|
|
},
|
|
|
|
|
|
|
|
reset: function() {
|
2015-08-27 00:11:01 +00:00
|
|
|
this.delegateEvents();
|
|
|
|
this.typeahead_view.delegateEvents();
|
|
|
|
this.recipients_view.delegateEvents();
|
2015-08-27 00:52:44 +00:00
|
|
|
this.new_contact_view.delegateEvents();
|
2015-03-12 23:14:21 +00:00
|
|
|
this.typeahead.add(
|
|
|
|
this.recipients.filter(function(model) {
|
|
|
|
return !model.get('newContact');
|
|
|
|
})
|
|
|
|
);
|
|
|
|
this.recipients.reset([]);
|
|
|
|
this.resetTypeahead();
|
2015-07-06 19:07:47 +00:00
|
|
|
this.typeahead.fetchContacts();
|
2015-03-12 23:14:21 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
resetTypeahead: function() {
|
2015-08-27 00:52:44 +00:00
|
|
|
this.new_contact_view.$el.hide();
|
2015-03-12 23:14:21 +00:00
|
|
|
this.$input.val('').focus();
|
2015-09-04 20:11:21 +00:00
|
|
|
this.typeahead_view.collection.reset([]);
|
2015-03-12 23:14:21 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
maybeNumber: function(number) {
|
|
|
|
return number.match(/^\+?[0-9]*$/);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
})();
|