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-01-25 13:26:42 +01:00
|
|
|
var typeahead = Backbone.TypeaheadCollection.extend({
|
2015-01-26 02:51:22 +01:00
|
|
|
typeaheadAttributes: ['name'],
|
2015-01-25 13:26:42 +01:00
|
|
|
database: Whisper.Database,
|
|
|
|
storeName: 'conversations',
|
|
|
|
model: Whisper.Conversation,
|
|
|
|
|
|
|
|
comparator: function(m) {
|
|
|
|
return m.get('name');
|
|
|
|
},
|
|
|
|
|
|
|
|
_tokenize: function(s) {
|
|
|
|
s = $.trim(s);
|
|
|
|
if (s.length === 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.toLowerCase().split(/[\s\-_+]+/)
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
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-25 13:26:42 +01:00
|
|
|
this.template = $('#new-conversation').html();
|
2014-09-01 02:58:36 +02:00
|
|
|
Mustache.parse(this.template);
|
2014-11-17 01:01:28 +01:00
|
|
|
this.$el.html($(Mustache.render(this.template)));
|
2015-01-25 13:26:42 +01:00
|
|
|
this.input = new Whisper.PhoneInputView({
|
|
|
|
el: this.$el.find('div.phone-number-input')
|
|
|
|
});
|
2014-09-01 02:58:36 +02:00
|
|
|
|
2015-01-25 13:26:42 +01:00
|
|
|
this.typeahead_collection = new typeahead();
|
|
|
|
this.typeahead_view = new Whisper.ConversationListView({
|
|
|
|
collection : new Whisper.ConversationCollection(),
|
|
|
|
className: 'typeahead'
|
|
|
|
});
|
|
|
|
|
|
|
|
this.typeahead_view.$el.appendTo(this.$el.find('.contacts'));
|
|
|
|
this.typeahead_collection.fetch();
|
2015-01-25 13:53:07 +01:00
|
|
|
|
|
|
|
this.new_contact = new Whisper.ConversationListItemView({
|
|
|
|
model: new Whisper.Conversation({
|
|
|
|
active_at: null
|
|
|
|
})
|
2015-01-26 02:53:12 +01:00
|
|
|
});
|
|
|
|
this.new_contact.render().$el.hide();
|
2015-01-25 13:53:07 +01:00
|
|
|
this.$el.find('.new-contact').append(this.new_contact.el);
|
2014-09-01 02:58:36 +02:00
|
|
|
},
|
|
|
|
|
2015-01-25 13:26:42 +01:00
|
|
|
filterContacts: function(query) {
|
2015-01-26 02:53:12 +01:00
|
|
|
if (this.maybeNumber(query)) {
|
|
|
|
this.new_contact.model.set('name', query);
|
|
|
|
this.new_contact.$el.show();
|
|
|
|
} else {
|
|
|
|
this.new_contact.$el.hide();
|
|
|
|
}
|
2015-01-25 13:26:42 +01:00
|
|
|
if (query.length) {
|
|
|
|
this.typeahead_view.collection.reset(
|
|
|
|
this.typeahead_collection.typeahead(query)
|
|
|
|
);
|
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
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
})();
|