From d435ff003b7abe9203fa055cda04448183181115 Mon Sep 17 00:00:00 2001 From: lilia Date: Wed, 28 Jan 2015 19:28:49 -1000 Subject: [PATCH] Improve typeahead contact selector experience Store and match on various phone number formats. Still not perfect, as occasionally all the models are returned for a non-matching query. --- js/libphonenumber-util.js | 8 ++++++++ js/models/conversations.js | 10 ++++++---- js/views/new_conversation_view.js | 18 +++++++----------- 3 files changed, 21 insertions(+), 15 deletions(-) diff --git a/js/libphonenumber-util.js b/js/libphonenumber-util.js index 22687f3a..3bf13e29 100644 --- a/js/libphonenumber-util.js +++ b/js/libphonenumber-util.js @@ -33,6 +33,14 @@ } }, + splitCountryCode: function(number) { + var parsedNumber = libphonenumber.parse(number); + return { + country_code: parsedNumber.values_[1], + national_number: parsedNumber.values_[2] + }; + }, + getCountryCode: function(regionCode) { var cc = libphonenumber.getCountryCodeForRegion(regionCode); return (cc != 0) ? cc : ""; diff --git a/js/models/conversations.js b/js/models/conversations.js index c5cf3b7c..9682d6ba 100644 --- a/js/models/conversations.js +++ b/js/models/conversations.js @@ -39,13 +39,15 @@ var missing = _.filter(required, function(attr) { return !attributes[attr]; }); if (missing.length) { return "Conversation must have " + missing; } + // hack if (this.get('type') === 'private') { - var number = libphonenumber.util.verifyNumber(id); + this.id = libphonenumber.util.verifyNumber(this.id); + var number = libphonenumber.util.splitCountryCode(this.id); this.set({ - id: number, - international_number: libphonenumber.format(number, libphonenumber.PhoneNumberFormat.INTERNATIONAL), - national_number: libphonenumber.format(number, libphonenumber.PhoneNumberFormat.NATIONAL) + e164_number: this.id, + national_number: '' + number.national_number, + international_number: '' + number.country_code + number.national_number }); } diff --git a/js/views/new_conversation_view.js b/js/views/new_conversation_view.js index 1dab6ba9..0ad05d79 100644 --- a/js/views/new_conversation_view.js +++ b/js/views/new_conversation_view.js @@ -19,19 +19,15 @@ var Whisper = Whisper || {}; 'use strict'; var typeahead = Backbone.TypeaheadCollection.extend({ - typeaheadAttributes: ['name'], + typeaheadAttributes: [ + 'name', + 'e164_number', + 'national_number', + 'international_number' + ], database: Whisper.Database, storeName: 'conversations', - model: Whisper.Conversation, - - _tokenize: function(s) { - s = $.trim(s); - if (s.length === 0) { - return null; - } - - return s.toLowerCase().split(/[\s\-_+]+/); - } + model: Whisper.Conversation }); Whisper.NewConversationView = Backbone.View.extend({