2018-02-27 14:16:23 +00:00
|
|
|
// Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved.
|
|
|
|
//
|
|
|
|
// This software (Documize Community Edition) is licensed under
|
|
|
|
// GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html
|
|
|
|
//
|
|
|
|
// You can operate outside the AGPL restrictions by purchasing
|
|
|
|
// Documize Enterprise Edition and obtaining a commercial license
|
|
|
|
// by contacting <sales@documize.com>.
|
|
|
|
//
|
|
|
|
// https://documize.com
|
|
|
|
|
|
|
|
import $ from 'jquery';
|
|
|
|
import { inject as service } from '@ember/service';
|
2018-02-28 14:55:36 +00:00
|
|
|
import { debounce } from '@ember/runloop';
|
2018-02-27 14:16:23 +00:00
|
|
|
import Component from '@ember/component';
|
|
|
|
import AuthProvider from '../../mixins/auth';
|
|
|
|
import ModalMixin from '../../mixins/modal';
|
|
|
|
|
|
|
|
export default Component.extend(AuthProvider, ModalMixin, {
|
|
|
|
groupSvc: service('group'),
|
2018-02-28 14:55:36 +00:00
|
|
|
userSvc: service('user'),
|
2018-02-27 14:16:23 +00:00
|
|
|
newGroup: null,
|
2018-02-28 14:55:36 +00:00
|
|
|
searchText: '',
|
|
|
|
showUsers: false,
|
|
|
|
showMembers: true,
|
|
|
|
users: null,
|
|
|
|
members: null,
|
2018-02-27 14:16:23 +00:00
|
|
|
|
|
|
|
didReceiveAttrs() {
|
|
|
|
this._super(...arguments);
|
2018-02-28 15:39:46 +00:00
|
|
|
this.loadGroups();
|
2018-02-27 14:16:23 +00:00
|
|
|
this.setDefaults();
|
|
|
|
},
|
|
|
|
|
2018-02-28 15:39:46 +00:00
|
|
|
loadGroups() {
|
2018-02-27 14:16:23 +00:00
|
|
|
this.get('groupSvc').getAll().then((groups) => {
|
|
|
|
this.set('groups', groups);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
setDefaults() {
|
|
|
|
this.set('newGroup', { name: '', purpose: '' });
|
|
|
|
},
|
|
|
|
|
2018-02-28 15:39:46 +00:00
|
|
|
loadGroupInfo() {
|
|
|
|
let groupId = this.get('membersGroup.id');
|
|
|
|
let searchText = this.get('searchText');
|
|
|
|
|
|
|
|
this.get('groupSvc').getGroupMembers(groupId).then((members) => {
|
|
|
|
this.set('members', members);
|
|
|
|
|
|
|
|
this.get('userSvc').matchUsers(searchText).then((users) => {
|
2018-02-28 14:55:36 +00:00
|
|
|
users.forEach((user) => {
|
|
|
|
let m = members.findBy('userId', user.get('id'));
|
|
|
|
user.set('isMember', is.not.undefined(m));
|
|
|
|
})
|
|
|
|
|
2018-02-28 15:39:46 +00:00
|
|
|
if (this.get('showMembers') && members.length === 0) {
|
|
|
|
this.set('showMembers', false);
|
|
|
|
this.set('showUsers', true);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.set('users', users);
|
|
|
|
});
|
2018-02-28 14:55:36 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2018-02-27 14:16:23 +00:00
|
|
|
actions: {
|
|
|
|
onOpenGroupModal() {
|
|
|
|
this.modalOpen("#add-group-modal", {"show": true}, '#new-group-name');
|
|
|
|
},
|
|
|
|
|
|
|
|
onAddGroup(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
let newGroup = this.get('newGroup');
|
2018-02-28 14:55:36 +00:00
|
|
|
|
2018-02-27 14:16:23 +00:00
|
|
|
if (is.empty(newGroup.name)) {
|
|
|
|
$("#new-group-name").addClass("is-invalid").focus();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.get('groupSvc').add(newGroup).then(() => {
|
|
|
|
this.load();
|
|
|
|
});
|
|
|
|
|
|
|
|
this.modalClose("#add-group-modal");
|
|
|
|
this.setDefaults();
|
|
|
|
},
|
|
|
|
|
|
|
|
onShowDeleteModal(groupId) {
|
|
|
|
this.set('deleteGroup', { name: '', id: groupId });
|
|
|
|
this.modalOpen("#delete-group-modal", {"show": true}, '#delete-group-name');
|
|
|
|
},
|
|
|
|
|
|
|
|
onDeleteGroup(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
let deleteGroup = this.get('deleteGroup');
|
|
|
|
let group = this.get('groups').findBy('id', deleteGroup.id);
|
|
|
|
|
|
|
|
if (is.empty(deleteGroup.name) || group.get('name') !== deleteGroup.name) {
|
|
|
|
$("#delete-group-name").addClass("is-invalid").focus();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.get('groupSvc').delete(deleteGroup.id).then(() => {
|
|
|
|
this.load();
|
|
|
|
});
|
|
|
|
|
|
|
|
this.modalClose("#delete-group-modal");
|
|
|
|
this.set('deleteGroup', { name: '', id: '' });
|
|
|
|
},
|
|
|
|
|
|
|
|
onShowEditModal(groupId) {
|
2018-02-28 14:55:36 +00:00
|
|
|
this.set('editGroup', this.get('groups').findBy('id', groupId));
|
2018-02-27 14:16:23 +00:00
|
|
|
this.modalOpen("#edit-group-modal", {"show": true}, '#edit-group-name');
|
|
|
|
},
|
|
|
|
|
|
|
|
onEditGroup(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
let group = this.get('editGroup');
|
|
|
|
|
|
|
|
if (is.empty(group.get('name'))) {
|
|
|
|
$("#edit-group-name").addClass("is-invalid").focus();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.get('groupSvc').update(group).then(() => {
|
|
|
|
this.load();
|
|
|
|
});
|
|
|
|
|
|
|
|
this.modalClose("#edit-group-modal");
|
|
|
|
this.set('editGroup', null);
|
2018-02-28 14:55:36 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
onShowMembersModal(groupId) {
|
|
|
|
this.set('membersGroup', this.get('groups').findBy('id', groupId));
|
|
|
|
this.modalOpen("#group-members-modal", {"show": true}, '#group-members-search');
|
|
|
|
this.set('members', null);
|
|
|
|
this.set('users', null);
|
2018-02-28 15:39:46 +00:00
|
|
|
this.set('showMembers', true);
|
|
|
|
this.set('showUsers', false);
|
|
|
|
this.loadGroupInfo();
|
2018-02-28 14:55:36 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
onSearch() {
|
|
|
|
debounce(this, function() {
|
|
|
|
let searchText = this.get('searchText');
|
2018-02-28 15:39:46 +00:00
|
|
|
this.loadGroupInfo();
|
2018-02-28 14:55:36 +00:00
|
|
|
|
|
|
|
if (is.not.empty(searchText)) {
|
|
|
|
this.set('showMembers', false);
|
|
|
|
this.set('showUsers', true);
|
|
|
|
} else {
|
|
|
|
this.set('showMembers', true);
|
|
|
|
this.set('showUsers', false);
|
|
|
|
}
|
|
|
|
}, 250);
|
|
|
|
},
|
|
|
|
|
|
|
|
onLeaveGroup(userId) {
|
2018-02-28 15:39:46 +00:00
|
|
|
let groupId = this.get('membersGroup.id');
|
|
|
|
|
|
|
|
this.get('groupSvc').leave(groupId, userId).then(() => {
|
|
|
|
this.loadGroupInfo();
|
|
|
|
this.loadGroups();
|
2018-02-28 14:55:36 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
onJoinGroup(userId) {
|
2018-02-28 15:39:46 +00:00
|
|
|
let groupId = this.get('membersGroup.id');
|
|
|
|
|
|
|
|
this.get('groupSvc').join(groupId, userId).then(() => {
|
|
|
|
this.loadGroupInfo();
|
|
|
|
this.loadGroups();
|
2018-02-28 14:55:36 +00:00
|
|
|
});
|
2018-02-27 14:16:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|