signal-desktop/js/views/new_group_update_view.js

100 lines
2.7 KiB
JavaScript
Raw Normal View History

/* global Whisper, _ */
/* eslint-disable more/no-then */
// eslint-disable-next-line func-names
2018-04-27 21:25:04 +00:00
(function() {
'use strict';
2018-04-27 21:25:04 +00:00
window.Whisper = window.Whisper || {};
2018-04-27 21:25:04 +00:00
Whisper.NewGroupUpdateView = Whisper.View.extend({
tagName: 'div',
className: 'new-group-update',
templateName: 'new-group-update',
initialize(options) {
2018-04-27 21:25:04 +00:00
this.render();
this.avatarInput = new Whisper.FileInputView({
el: this.$('.group-avatar'),
window: options.window,
});
2018-04-27 21:25:04 +00:00
this.recipients_view = new Whisper.RecipientsInputView();
this.listenTo(this.recipients_view.typeahead, 'sync', () =>
this.model.contactCollection.models.forEach(model => {
if (this.recipients_view.typeahead.get(model)) {
this.recipients_view.typeahead.remove(model);
}
})
);
2018-04-27 21:25:04 +00:00
this.recipients_view.$el.insertBefore(this.$('.container'));
2018-04-27 21:25:04 +00:00
this.member_list_view = new Whisper.ContactListView({
collection: this.model.contactCollection,
className: 'members',
});
this.member_list_view.render();
this.$('.scrollable').append(this.member_list_view.el);
},
events: {
'click .back': 'goBack',
'click .send': 'send',
'focusin input.search': 'showResults',
'focusout input.search': 'hideResults',
},
hideResults() {
2018-04-27 21:25:04 +00:00
this.$('.results').hide();
},
showResults() {
2018-04-27 21:25:04 +00:00
this.$('.results').show();
},
goBack() {
2018-04-27 21:25:04 +00:00
this.trigger('back');
},
render_attributes() {
2018-04-27 21:25:04 +00:00
return {
name: this.model.getTitle(),
avatar: this.model.getAvatar(),
};
},
2018-09-21 01:47:19 +00:00
async send() {
// When we turn this view on again, need to handle avatars in the new way
2015-07-14 20:39:24 +00:00
2018-09-21 01:47:19 +00:00
// const avatarFile = await this.avatarInput.getThumbnail();
const now = Date.now();
const attrs = {
timestamp: now,
active_at: now,
name: this.$('.name').val(),
members: _.union(
this.model.get('members'),
this.recipients_view.recipients.pluck('id')
),
};
2015-07-14 20:39:24 +00:00
2018-09-21 01:47:19 +00:00
// if (avatarFile) {
// attrs.avatar = avatarFile;
// }
// Because we're no longer using Backbone-integrated saves, we need to manually
// clear the changed fields here so model.changed is accurate.
this.model.changed = {};
this.model.set(attrs);
const groupUpdate = this.model.changed;
await window.Signal.Data.updateConversation(
this.model.id,
this.model.attributes,
{ Conversation: Whisper.Conversation }
);
if (groupUpdate.avatar) {
this.model.trigger('change:avatar');
}
this.model.updateGroup(groupUpdate);
this.goBack();
2018-04-27 21:25:04 +00:00
},
});
})();