Cable-Desktop/js/views/new_conversation_view.js

259 lines
8 KiB
JavaScript
Raw Normal View History

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-02-06 07:42:16 +01:00
var ContactsTypeahead = Backbone.TypeaheadCollection.extend({
typeaheadAttributes: [
'name',
'e164_number',
'national_number',
'international_number'
],
database: Whisper.Database,
storeName: 'conversations',
model: Whisper.Conversation
});
2015-02-06 07:42:16 +01:00
Whisper.ContactPillView = Backbone.View.extend({
tagName: 'span',
className: 'recipient',
events: {
'click .remove': 'removeModel'
},
initialize: function() {
this.template = $('#contact_pill').html();
Mustache.parse(this.template);
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: function() {
this.$el.html(
Mustache.render(this.template, { name: this.model.getTitle() })
);
return this;
}
});
Whisper.RecipientListView = Whisper.ListView.extend({
itemView: Whisper.ContactPillView
});
2014-09-01 02:58:36 +02:00
Whisper.NewConversationView = Backbone.View.extend({
className: 'new-conversation',
2014-09-01 02:58:36 +02:00
initialize: function() {
2015-01-26 08:44:53 +01:00
this.template = $('#new-conversation').html();
Mustache.parse(this.template);
this.$el.html($(Mustache.render(this.template)));
this.$group_update = this.$el.find('.new-group-update-form');
2015-02-06 07:42:16 +01:00
this.$buttons = this.$el.find('.buttons');
this.$input = this.$el.find('input.new-message');
2015-02-08 05:53:29 +01:00
this.$new_contact = this.$el.find('.new-contact');
2015-02-06 07:42:16 +01:00
// Collection of contacts to match user input against
this.typeahead = new ContactsTypeahead();
this.typeahead.fetch({ conditions: { type: 'private' } });
2014-09-01 02:58:36 +02:00
2015-02-06 07:42:16 +01:00
// View to display the matched contacts from typeahead
2015-01-26 08:44:53 +01:00
this.typeahead_view = new Whisper.ConversationListView({
2015-02-06 07:42:16 +01:00
collection : new Whisper.ConversationCollection([], {
2015-01-29 05:48:21 +01:00
comparator: function(m) { return m.getTitle(); }
2015-02-06 07:42:16 +01:00
})
});
2015-02-06 07:42:16 +01:00
this.$el.find('.contacts').append(this.typeahead_view.el);
2015-02-08 05:53:29 +01:00
this.initNewContact();
2015-02-06 07:42:16 +01:00
// Group avatar file input
this.avatarInput = new Whisper.FileInputView({
el: this.$el.find('.group-avatar')
});
// Collection of recipients selected for the new message
this.recipients = new Whisper.ConversationCollection([], {
comparator: false
});
// View to display the selected recipients
2015-02-08 05:51:52 +01:00
this.recipients_view = new Whisper.RecipientListView({
2015-02-06 07:42:16 +01:00
collection: this.recipients,
el: this.$el.find('.recipients')
});
2014-09-01 02:58:36 +02:00
},
2015-01-26 08:44:53 +01:00
events: {
'change input.new-message': 'filterContacts',
'keyup input.new-message': 'filterContacts',
2015-02-08 19:44:31 +01:00
'select .new-contact': 'addNewRecipient',
'select .contacts': 'addRecipient',
2015-02-06 07:42:16 +01:00
'remove .recipient': 'removeRecipient',
'click .create': 'create'
},
2015-02-08 05:53:29 +01:00
initNewContact: function() {
if (this.new_contact) {
this.new_contact.undelegateEvents();
this.new_contact.$el.hide();
}
2015-02-08 05:53:29 +01:00
// Creates a view to display a new contact
this.new_contact = new Whisper.ConversationListItemView({
el: this.$new_contact,
model: new Whisper.Conversation({
active_at: null,
type: 'private',
newContact: true
})
}).render();
},
2015-02-06 07:42:16 +01:00
addNewRecipient: function(e, data) {
this.recipients.add(this.new_contact.model);
2015-02-08 05:53:29 +01:00
this.initNewContact();
2015-02-06 07:42:16 +01:00
this.resetTypeahead();
this.updateControls();
},
addRecipient: function(e, data) {
this.recipients.add(this.typeahead.remove(data.modelId));
this.filterContacts();
this.updateControls();
},
removeRecipient: function(e, data) {
var model = this.recipients.remove(data.modelId);
2015-02-08 05:53:29 +01:00
if (!model.get('newContact')) {
2015-02-06 07:42:16 +01:00
this.typeahead.add(model);
}
this.filterContacts();
this.updateControls();
},
updateControls: function() {
if (this.recipients.length > 0) {
this.$buttons.slideDown();
} else {
2015-02-06 07:42:16 +01:00
this.$buttons.slideUp();
}
2015-02-06 07:42:16 +01:00
if (this.recipients.length > 1) {
this.$group_update.slideDown();
} else {
this.$group_update.slideUp();
}
this.$input.focus();
},
create: function() {
2015-02-08 05:51:52 +01:00
var errors = this.recipients_view.$el.find('.error');
if (errors.length) {
// TODO: css animation or error notification
errors.removeClass('error');
setTimeout(function(){ errors.addClass('error'); }, 300);
return;
}
2015-02-06 07:42:16 +01:00
if (this.recipients.length > 1) {
this.createGroup();
} else {
2015-02-06 07:42:16 +01:00
this.createConversation();
}
},
2015-02-06 07:42:16 +01:00
createConversation: function() {
var conversation = new Whisper.Conversation({
active_at: null,
2015-02-06 07:42:16 +01:00
id: this.recipients.at(0).id,
type: 'private'
});
conversation.fetch().then(function() {
this.$el.trigger('open', { modelId: conversation.id });
}.bind(this)).fail(function() {
2015-02-06 07:42:16 +01:00
if (conversation.save()) {
this.$el.trigger('open', { modelId: conversation.id });
}
}.bind(this));
2015-02-06 07:42:16 +01:00
},
createGroup: function() {
2015-02-06 07:42:16 +01:00
return this.avatarInput.getFiles().then(function(avatarFiles) {
var attributes = {
type: 'group',
name: this.$el.find('.new-group-update-form .name').val(),
avatar: avatarFiles[0],
2015-02-06 07:42:16 +01:00
members: this.recipients.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() {
2015-02-08 19:44:31 +01:00
this.$el.trigger('open', {modelId: id});
}.bind(this));
}.bind(this));
}.bind(this));
2015-01-26 08:44:53 +01:00
},
2015-02-06 07:42:16 +01:00
resetTypeahead: function() {
2015-01-26 08:44:53 +01:00
this.new_contact.$el.hide();
this.$input.val('').focus();
2015-02-06 07:42:16 +01:00
this.typeahead_view.collection.reset(this.typeahead.models);
},
reset: function() {
this.$buttons.hide();
this.$group_update.hide();
2015-02-08 05:53:29 +01:00
this.typeahead.add(
this.recipients.filter(function(model) {
return !model.get('newContact');
})
);
2015-02-06 07:42:16 +01:00
this.recipients.reset([]);
this.resetTypeahead();
2015-01-26 08:44:53 +01:00
},
filterContacts: function() {
var query = this.$input.val();
if (query.length) {
2015-01-26 08:44:53 +01:00
if (this.maybeNumber(query)) {
this.new_contact.model.set('id', query);
2015-02-06 07:42:16 +01:00
this.new_contact.render().$el.show();
2015-01-26 08:44:53 +01:00
} else {
this.new_contact.$el.hide();
}
this.typeahead_view.collection.reset(
2015-02-06 07:42:16 +01:00
this.typeahead.typeahead(query)
);
2015-01-26 08:44:53 +01:00
} else {
2015-02-06 07:42:16 +01:00
this.resetTypeahead();
}
},
maybeNumber: function(number) {
return number.match(/^\+?[0-9]*$/);
2014-09-01 02:58:36 +02:00
}
});
})();