2014-11-13 22:36:09 +00: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 00:58:36 +00:00
|
|
|
var Whisper = Whisper || {};
|
|
|
|
|
|
|
|
(function () {
|
|
|
|
'use strict';
|
|
|
|
|
2015-01-25 12:26:42 +00:00
|
|
|
var typeahead = Backbone.TypeaheadCollection.extend({
|
2015-01-26 01:51:22 +00:00
|
|
|
typeaheadAttributes: ['name'],
|
2015-01-25 12:26:42 +00:00
|
|
|
database: Whisper.Database,
|
|
|
|
storeName: 'conversations',
|
|
|
|
model: Whisper.Conversation,
|
|
|
|
|
|
|
|
_tokenize: function(s) {
|
|
|
|
s = $.trim(s);
|
|
|
|
if (s.length === 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2015-01-29 04:22:41 +00:00
|
|
|
return s.toLowerCase().split(/[\s\-_+]+/);
|
2015-01-25 12:26:42 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-09-01 00:58:36 +00:00
|
|
|
Whisper.NewConversationView = Backbone.View.extend({
|
2015-01-25 12:26:42 +00:00
|
|
|
className: 'new-conversation',
|
2014-09-01 00:58:36 +00:00
|
|
|
initialize: function() {
|
2015-01-26 07:44:53 +00:00
|
|
|
this.template = $('#new-conversation').html();
|
|
|
|
Mustache.parse(this.template);
|
|
|
|
this.$el.html($(Mustache.render(this.template)));
|
|
|
|
this.$input = this.$el.find('input.new-message');
|
2015-01-29 04:22:41 +00:00
|
|
|
this.$group_update = this.$el.find('.new-group-update-form');
|
2014-09-01 00:58:36 +00:00
|
|
|
|
2015-01-26 07:44:53 +00:00
|
|
|
this.typeahead_collection = new typeahead();
|
|
|
|
this.typeahead_view = new Whisper.ConversationListView({
|
2015-01-29 04:48:21 +00:00
|
|
|
collection : new Whisper.ConversationCollection({
|
|
|
|
comparator: function(m) { return m.getTitle(); }
|
|
|
|
}),
|
2015-01-26 07:44:53 +00:00
|
|
|
className: 'typeahead'
|
|
|
|
});
|
2015-01-25 12:26:42 +00:00
|
|
|
|
2015-01-26 07:44:53 +00:00
|
|
|
this.typeahead_view.$el.appendTo(this.$el.find('.contacts'));
|
2015-01-28 12:24:18 +00:00
|
|
|
this.typeahead_collection.fetch({
|
|
|
|
conditions: { type: 'private' }
|
|
|
|
});
|
2015-01-25 12:53:07 +00:00
|
|
|
|
2015-01-26 07:44:53 +00:00
|
|
|
this.new_contact = new Whisper.ConversationListItemView({
|
|
|
|
model: new Whisper.Conversation({
|
2015-01-26 23:55:22 +00:00
|
|
|
active_at: null,
|
|
|
|
type: 'private'
|
2015-01-26 07:44:53 +00:00
|
|
|
})
|
|
|
|
}).render();
|
2015-01-29 04:22:41 +00:00
|
|
|
|
|
|
|
this.newGroupUpdateView = new Whisper.NewGroupUpdateView({
|
|
|
|
model: new Whisper.Conversation({ type: 'group' }),
|
|
|
|
el: this.$group_update
|
|
|
|
});
|
|
|
|
this.group_members = new Whisper.ConversationCollection();
|
2015-01-26 07:44:53 +00:00
|
|
|
this.$el.find('.new-contact').append(this.new_contact.el);
|
2014-09-01 00:58:36 +00:00
|
|
|
},
|
|
|
|
|
2015-01-26 07:44:53 +00:00
|
|
|
events: {
|
|
|
|
'change input.new-message': 'filterContacts',
|
2015-01-29 04:22:41 +00:00
|
|
|
'keyup input.new-message': 'filterContacts',
|
|
|
|
'checkbox .contact': 'updateGroup',
|
|
|
|
'click .create-group': 'createGroup'
|
|
|
|
},
|
|
|
|
|
|
|
|
updateGroup: function(e, data) {
|
|
|
|
this.$input.focus();
|
|
|
|
if (data.checked) {
|
|
|
|
this.group_members.add({id: data.modelId});
|
|
|
|
} else {
|
|
|
|
this.group_members.remove({id: data.modelId});
|
|
|
|
}
|
|
|
|
this.group_members
|
|
|
|
if (this.group_members.length) {
|
|
|
|
this.$group_update.show();
|
|
|
|
} else {
|
|
|
|
this.$group_update.hide();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
createGroup: function() {
|
|
|
|
return this.newGroupUpdateView.avatarInput.getFiles().then(function(avatarFiles) {
|
|
|
|
var attributes = {
|
|
|
|
type: 'group',
|
|
|
|
name: this.$el.find('.new-group-update-form .name').val(),
|
|
|
|
avatar: avatarFiles[0],
|
|
|
|
members: this.group_members.pluck('id')
|
|
|
|
};
|
|
|
|
return textsecure.messaging.createGroup(
|
|
|
|
attributes.members, attributes.name, attributes.avatar
|
|
|
|
).then(function(groupId) {
|
|
|
|
var id = getString(groupId);
|
|
|
|
var group = new Whisper.Conversation(attributes);
|
|
|
|
group.save({ id: id, groupId: id }).then(function() {
|
|
|
|
this.$el.trigger('open', {modelId: id});
|
|
|
|
}.bind(this));
|
|
|
|
}.bind(this));
|
|
|
|
}.bind(this));
|
2015-01-26 07:44:53 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
reset: function() {
|
|
|
|
this.new_contact.$el.hide();
|
|
|
|
this.$input.val('').focus();
|
|
|
|
this.typeahead_view.collection.reset(this.typeahead_collection.models);
|
2015-01-29 04:22:41 +00:00
|
|
|
this.group_members.reset([]);
|
2015-01-26 07:44:53 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
filterContacts: function() {
|
|
|
|
var query = this.$input.val();
|
2015-01-25 12:26:42 +00:00
|
|
|
if (query.length) {
|
2015-01-26 07:44:53 +00:00
|
|
|
if (this.maybeNumber(query)) {
|
2015-01-26 21:44:03 +00:00
|
|
|
this.new_contact.model.set('id', query);
|
2015-01-26 07:44:53 +00:00
|
|
|
this.new_contact.$el.show();
|
|
|
|
} else {
|
|
|
|
this.new_contact.$el.hide();
|
|
|
|
}
|
2015-01-25 12:26:42 +00:00
|
|
|
this.typeahead_view.collection.reset(
|
|
|
|
this.typeahead_collection.typeahead(query)
|
|
|
|
);
|
2015-01-26 07:44:53 +00:00
|
|
|
} else {
|
|
|
|
this.reset();
|
2014-11-04 01:51:49 +00:00
|
|
|
}
|
2015-01-26 01:53:12 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
maybeNumber: function(number) {
|
|
|
|
return number.match(/^\+?[0-9]*$/);
|
2014-09-01 00:58:36 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
})();
|