2018-03-02 20:54:15 +00:00
|
|
|
/* global ConversationController: false */
|
|
|
|
/* global i18n: false */
|
|
|
|
/* global Whisper: false */
|
|
|
|
|
|
|
|
// eslint-disable-next-line func-names
|
2018-04-27 21:25:04 +00:00
|
|
|
(function() {
|
2018-03-02 20:54:15 +00:00
|
|
|
'use strict';
|
2015-10-15 19:10:03 +00:00
|
|
|
|
2018-03-02 20:54:15 +00:00
|
|
|
window.Whisper = window.Whisper || {};
|
2015-12-06 03:21:15 +00:00
|
|
|
|
2018-04-27 21:25:04 +00:00
|
|
|
const isSearchable = conversation => conversation.isSearchable();
|
2015-10-15 19:10:03 +00:00
|
|
|
|
2018-03-02 20:54:15 +00:00
|
|
|
Whisper.NewContactView = Whisper.View.extend({
|
|
|
|
templateName: 'new-contact',
|
|
|
|
className: 'conversation-list-item contact',
|
|
|
|
events: {
|
|
|
|
click: 'validate',
|
|
|
|
},
|
|
|
|
initialize() {
|
|
|
|
this.listenTo(this.model, 'change', this.render); // auto update
|
|
|
|
},
|
|
|
|
render_attributes() {
|
|
|
|
return {
|
2018-03-02 20:59:39 +00:00
|
|
|
number: i18n('startConversation'),
|
2018-03-02 20:54:15 +00:00
|
|
|
title: this.model.getNumber(),
|
|
|
|
avatar: this.model.getAvatar(),
|
|
|
|
};
|
|
|
|
},
|
|
|
|
validate() {
|
|
|
|
if (this.model.isValid()) {
|
|
|
|
this.$el.addClass('valid');
|
|
|
|
} else {
|
|
|
|
this.$el.removeClass('valid');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
2015-10-15 19:10:03 +00:00
|
|
|
|
2018-03-02 20:54:15 +00:00
|
|
|
Whisper.ConversationSearchView = Whisper.View.extend({
|
|
|
|
className: 'conversation-search',
|
|
|
|
initialize(options) {
|
|
|
|
this.$input = options.input;
|
|
|
|
this.$new_contact = this.$('.new-contact');
|
2015-10-15 19:10:03 +00:00
|
|
|
|
2018-03-02 20:54:15 +00:00
|
|
|
this.typeahead = new Whisper.ConversationCollection();
|
|
|
|
// View to display the matched contacts from typeahead
|
|
|
|
this.typeahead_view = new Whisper.ConversationListView({
|
|
|
|
collection: new Whisper.ConversationCollection([], {
|
2018-04-27 21:25:04 +00:00
|
|
|
comparator(m) {
|
|
|
|
return m.getTitle().toLowerCase();
|
|
|
|
},
|
2018-03-02 20:54:15 +00:00
|
|
|
}),
|
|
|
|
});
|
|
|
|
this.$el.append(this.typeahead_view.el);
|
|
|
|
this.initNewContact();
|
|
|
|
// this.listenTo(this.collection, 'reset', this.filterContacts);
|
|
|
|
this.pending = Promise.resolve();
|
|
|
|
},
|
2015-12-06 03:21:15 +00:00
|
|
|
|
2018-03-02 20:54:15 +00:00
|
|
|
events: {
|
|
|
|
'click .new-contact': 'createConversation',
|
|
|
|
},
|
2015-10-15 19:10:03 +00:00
|
|
|
|
2018-03-02 20:54:15 +00:00
|
|
|
filterContacts() {
|
|
|
|
const query = this.$input.val().trim();
|
|
|
|
if (query.length) {
|
|
|
|
if (this.maybeNumber(query)) {
|
|
|
|
this.new_contact_view.model.set('id', query);
|
|
|
|
this.new_contact_view.render().$el.show();
|
|
|
|
this.new_contact_view.validate();
|
|
|
|
this.hideHints();
|
|
|
|
} else {
|
|
|
|
this.new_contact_view.$el.hide();
|
|
|
|
}
|
|
|
|
// NOTE: Temporarily allow `then` until we convert the entire file
|
|
|
|
// to `async` / `await`:
|
|
|
|
/* eslint-disable more/no-then */
|
|
|
|
this.pending = this.pending.then(() =>
|
|
|
|
this.typeahead.search(query).then(() => {
|
2018-04-27 21:25:04 +00:00
|
|
|
this.typeahead_view.collection.reset(
|
|
|
|
this.typeahead.filter(isSearchable)
|
|
|
|
);
|
|
|
|
})
|
|
|
|
);
|
2018-03-02 20:54:15 +00:00
|
|
|
/* eslint-enable more/no-then */
|
|
|
|
this.trigger('show');
|
|
|
|
} else {
|
|
|
|
this.resetTypeahead();
|
|
|
|
}
|
|
|
|
},
|
2015-10-15 19:10:03 +00:00
|
|
|
|
2018-03-02 20:54:15 +00:00
|
|
|
initNewContact() {
|
|
|
|
if (this.new_contact_view) {
|
|
|
|
this.new_contact_view.undelegateEvents();
|
|
|
|
this.new_contact_view.$el.hide();
|
|
|
|
}
|
2018-03-27 23:04:55 +00:00
|
|
|
const model = new Whisper.Conversation({ type: 'private' });
|
2018-03-02 20:54:15 +00:00
|
|
|
this.new_contact_view = new Whisper.NewContactView({
|
|
|
|
el: this.$new_contact,
|
2018-03-27 23:04:55 +00:00
|
|
|
model,
|
2018-03-02 20:54:15 +00:00
|
|
|
}).render();
|
|
|
|
},
|
2015-10-15 19:10:03 +00:00
|
|
|
|
2018-03-27 23:04:55 +00:00
|
|
|
async createConversation() {
|
|
|
|
const isValidNumber = this.new_contact_view.model.isValid();
|
|
|
|
if (!isValidNumber) {
|
2018-03-02 20:54:15 +00:00
|
|
|
this.new_contact_view.$('.number').text(i18n('invalidNumberError'));
|
|
|
|
this.$input.focus();
|
2018-03-27 23:04:55 +00:00
|
|
|
return;
|
2018-03-02 20:54:15 +00:00
|
|
|
}
|
2018-03-27 23:04:55 +00:00
|
|
|
|
|
|
|
const newConversationId = this.new_contact_view.model.id;
|
2018-04-27 21:25:04 +00:00
|
|
|
const conversation = await ConversationController.getOrCreateAndWait(
|
|
|
|
newConversationId,
|
|
|
|
'private'
|
|
|
|
);
|
2018-03-27 23:04:55 +00:00
|
|
|
this.trigger('open', conversation);
|
|
|
|
this.initNewContact();
|
|
|
|
this.resetTypeahead();
|
2018-03-02 20:54:15 +00:00
|
|
|
},
|
2015-10-15 19:10:03 +00:00
|
|
|
|
2018-03-02 20:54:15 +00:00
|
|
|
reset() {
|
|
|
|
this.delegateEvents();
|
|
|
|
this.typeahead_view.delegateEvents();
|
|
|
|
this.new_contact_view.delegateEvents();
|
|
|
|
this.resetTypeahead();
|
|
|
|
},
|
2015-10-15 19:10:03 +00:00
|
|
|
|
2018-03-02 20:54:15 +00:00
|
|
|
resetTypeahead() {
|
|
|
|
this.hideHints();
|
|
|
|
this.new_contact_view.$el.hide();
|
|
|
|
this.$input.val('').focus();
|
|
|
|
if (this.showAllContacts) {
|
|
|
|
// NOTE: Temporarily allow `then` until we convert the entire file
|
|
|
|
// to `async` / `await`:
|
|
|
|
// eslint-disable-next-line more/no-then
|
|
|
|
this.typeahead.fetchAlphabetical().then(() => {
|
|
|
|
if (this.typeahead.length > 0) {
|
2018-04-27 21:25:04 +00:00
|
|
|
this.typeahead_view.collection.reset(
|
|
|
|
this.typeahead.filter(isSearchable)
|
|
|
|
);
|
2018-03-02 20:54:15 +00:00
|
|
|
} else {
|
|
|
|
this.showHints();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
this.trigger('show');
|
|
|
|
} else {
|
|
|
|
this.typeahead_view.collection.reset([]);
|
|
|
|
this.trigger('hide');
|
|
|
|
}
|
|
|
|
},
|
2015-11-28 01:34:02 +00:00
|
|
|
|
2018-03-02 20:54:15 +00:00
|
|
|
showHints() {
|
|
|
|
if (!this.hintView) {
|
|
|
|
this.hintView = new Whisper.HintView({
|
|
|
|
className: 'contact placeholder',
|
|
|
|
content: i18n('newPhoneNumber'),
|
|
|
|
}).render();
|
|
|
|
this.hintView.$el.insertAfter(this.$input);
|
|
|
|
}
|
|
|
|
this.hintView.$el.show();
|
|
|
|
},
|
2015-11-28 01:34:02 +00:00
|
|
|
|
2018-03-02 20:54:15 +00:00
|
|
|
hideHints() {
|
|
|
|
if (this.hintView) {
|
|
|
|
this.hintView.remove();
|
|
|
|
this.hintView = null;
|
|
|
|
}
|
|
|
|
},
|
2015-10-15 19:10:03 +00:00
|
|
|
|
2018-03-02 20:54:15 +00:00
|
|
|
maybeNumber(number) {
|
|
|
|
return number.replace(/[\s-.()]*/g, '').match(/^\+?[0-9]*$/);
|
|
|
|
},
|
|
|
|
});
|
2018-04-27 21:25:04 +00:00
|
|
|
})();
|