2017-09-19 17:58:33 +01: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
|
|
|
|
|
2018-01-22 10:31:03 +00:00
|
|
|
import $ from 'jquery';
|
2018-05-03 12:49:49 +01:00
|
|
|
import { A } from '@ember/array';
|
2017-11-16 13:28:05 +00:00
|
|
|
import { inject as service } from '@ember/service';
|
2017-12-05 19:03:38 +00:00
|
|
|
import ModalMixin from '../../mixins/modal';
|
2018-06-21 12:38:13 +01:00
|
|
|
import Notifer from '../../mixins/notifier';
|
2018-03-06 10:39:47 +00:00
|
|
|
import Component from '@ember/component';
|
2017-09-19 17:58:33 +01:00
|
|
|
|
2018-12-06 14:10:00 +00:00
|
|
|
export default Component.extend(ModalMixin, Notifer, {
|
2018-03-06 10:39:47 +00:00
|
|
|
spaceSvc: service('folder'),
|
|
|
|
groupSvc: service('group'),
|
|
|
|
categorySvc: service('category'),
|
2017-09-19 17:58:33 +01:00
|
|
|
appMeta: service(),
|
|
|
|
store: service(),
|
2017-11-27 15:38:39 +00:00
|
|
|
deleteId: '',
|
2018-06-21 12:38:13 +01:00
|
|
|
newCategory: '',
|
2018-01-22 10:31:03 +00:00
|
|
|
|
|
|
|
init() {
|
|
|
|
this._super(...arguments);
|
|
|
|
this.users = [];
|
|
|
|
},
|
2017-09-19 17:58:33 +01:00
|
|
|
|
|
|
|
didReceiveAttrs() {
|
2017-10-04 13:27:48 -04:00
|
|
|
this._super(...arguments);
|
|
|
|
this.load();
|
2017-09-21 15:48:00 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
willDestroyElement() {
|
2017-10-04 13:27:48 -04:00
|
|
|
this._super(...arguments);
|
2017-09-21 15:48:00 +01:00
|
|
|
},
|
|
|
|
|
2017-09-19 17:58:33 +01:00
|
|
|
load() {
|
2017-09-21 15:48:00 +01:00
|
|
|
// get categories
|
2018-06-21 12:38:13 +01:00
|
|
|
this.get('categorySvc').getAll(this.get('space.id')).then((c) => {
|
2017-09-19 17:58:33 +01:00
|
|
|
this.set('category', c);
|
2017-09-21 15:48:00 +01:00
|
|
|
|
2017-09-21 18:59:43 +01:00
|
|
|
// get summary of documents and users for each category in space
|
2018-06-21 12:38:13 +01:00
|
|
|
this.get('categorySvc').getSummary(this.get('space.id')).then((s) => {
|
2017-09-21 18:59:43 +01:00
|
|
|
c.forEach((cat) => {
|
2018-03-06 10:39:47 +00:00
|
|
|
let docs = _.where(s, {categoryId: cat.get('id'), type: 'documents'});
|
|
|
|
let docCount = 0;
|
|
|
|
docs.forEach((d) => { docCount = docCount + d.count });
|
|
|
|
|
|
|
|
let users = _.where(s, {categoryId: cat.get('id'), type: 'users'});
|
|
|
|
let userCount = 0;
|
|
|
|
users.forEach((u) => { userCount = userCount + u.count });
|
2018-05-03 12:49:49 +01:00
|
|
|
|
2017-09-21 18:59:43 +01:00
|
|
|
cat.set('documents', docCount);
|
|
|
|
cat.set('users', userCount);
|
|
|
|
});
|
2018-11-16 19:18:10 +00:00
|
|
|
|
|
|
|
this.get('categorySvc').getUserVisible(this.get('space.id')).then((cm) => {
|
|
|
|
cm.forEach((cm) => {
|
|
|
|
let cat = _.findWhere(c, {id: cm.get('id') });
|
|
|
|
if (is.not.undefined(cat)) {
|
|
|
|
cat.set('access', is.not.undefined(cat));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2017-09-21 18:59:43 +01:00
|
|
|
});
|
2017-09-19 17:58:33 +01:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2018-03-06 10:39:47 +00:00
|
|
|
permissionRecord(who, whoId, name) {
|
|
|
|
let raw = {
|
|
|
|
id: whoId,
|
2018-06-21 12:38:13 +01:00
|
|
|
orgId: this.get('space.orgId'),
|
2018-03-06 10:39:47 +00:00
|
|
|
categoryId: this.get('currentCategory.id'),
|
|
|
|
whoId: whoId,
|
|
|
|
who: who,
|
|
|
|
name: name,
|
|
|
|
categoryView: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
let rec = this.get('store').normalize('category-permission', raw);
|
|
|
|
return this.get('store').push(rec);
|
|
|
|
},
|
|
|
|
|
2017-09-19 17:58:33 +01:00
|
|
|
setEdit(id, val) {
|
|
|
|
let cats = this.get('category');
|
|
|
|
let cat = cats.findBy('id', id);
|
|
|
|
|
|
|
|
if (is.not.undefined(cat)) {
|
|
|
|
cat.set('editMode', val);
|
|
|
|
}
|
|
|
|
|
|
|
|
return cat;
|
|
|
|
},
|
|
|
|
|
|
|
|
actions: {
|
2018-06-21 12:38:13 +01:00
|
|
|
onAdd(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
let cat = this.get('newCategory');
|
|
|
|
|
|
|
|
if (cat === '') {
|
|
|
|
$('#new-category-name').addClass('is-invalid').focus();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$('#new-category-name').removeClass('is-invalid').focus();
|
|
|
|
this.set('newCategory', '');
|
|
|
|
|
|
|
|
let c = {
|
|
|
|
category: cat,
|
2018-10-12 17:54:15 +01:00
|
|
|
spaceId: this.get('space.id')
|
2018-06-21 12:38:13 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
this.get('categorySvc').add(c).then(() => {
|
|
|
|
this.load();
|
2018-12-05 13:44:10 +00:00
|
|
|
this.notifySuccess('Category added');
|
2018-06-21 12:38:13 +01:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2017-11-27 15:38:39 +00:00
|
|
|
onShowDelete(id) {
|
|
|
|
let cat = this.get('category').findBy('id', id);
|
|
|
|
this.set('deleteId', cat.get('id'));
|
|
|
|
|
2017-12-05 19:03:38 +00:00
|
|
|
this.modalOpen('#category-delete-modal', {show: true});
|
2017-11-27 15:38:39 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
onDelete() {
|
2017-12-05 19:03:38 +00:00
|
|
|
this.modalClose('#category-delete-modal');
|
2017-11-27 15:38:39 +00:00
|
|
|
|
2018-03-06 10:39:47 +00:00
|
|
|
this.get('categorySvc').delete(this.get('deleteId')).then(() => {
|
2017-09-19 17:58:33 +01:00
|
|
|
this.load();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
onEdit(id) {
|
|
|
|
this.setEdit(id, true);
|
|
|
|
},
|
|
|
|
|
2017-09-21 15:48:00 +01:00
|
|
|
onEditCancel(id) {
|
2017-09-19 17:58:33 +01:00
|
|
|
this.setEdit(id, false);
|
2017-09-19 18:55:33 +01:00
|
|
|
this.load();
|
2017-09-19 17:58:33 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
onSave(id) {
|
2017-09-19 18:55:33 +01:00
|
|
|
let cat = this.setEdit(id, true);
|
|
|
|
if (cat.get('category') === '') {
|
2017-11-27 15:38:39 +00:00
|
|
|
$('#edit-category-' + cat.get('id')).addClass('is-invalid').focus();
|
|
|
|
return false;
|
2017-09-19 18:55:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
cat = this.setEdit(id, false);
|
2017-11-27 15:38:39 +00:00
|
|
|
$('#edit-category-' + cat.get('id')).removeClass('is-invalid');
|
2017-09-19 17:58:33 +01:00
|
|
|
|
2018-03-06 10:39:47 +00:00
|
|
|
this.get('categorySvc').save(cat).then(() => {
|
2017-09-19 17:58:33 +01:00
|
|
|
this.load();
|
|
|
|
});
|
2017-09-21 15:48:00 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
onShowAccessPicker(catId) {
|
2017-11-27 15:38:39 +00:00
|
|
|
this.set('showCategoryAccess', true);
|
|
|
|
|
2018-03-06 10:39:47 +00:00
|
|
|
let categoryPermissions = A([]);
|
2017-09-21 15:48:00 +01:00
|
|
|
let category = this.get('category').findBy('id', catId);
|
|
|
|
|
2018-03-06 10:39:47 +00:00
|
|
|
this.set('currentCategory', category);
|
|
|
|
this.set('categoryPermissions', categoryPermissions);
|
|
|
|
|
|
|
|
// get space permissions
|
2018-06-21 12:38:13 +01:00
|
|
|
this.get('spaceSvc').getPermissions(this.get('space.id')).then((spacePermissions) => {
|
2018-03-06 10:39:47 +00:00
|
|
|
spacePermissions.forEach((sp) => {
|
|
|
|
let cp = this.permissionRecord(sp.get('who'), sp.get('whoId'), sp.get('name'));
|
|
|
|
cp.set('selected', false);
|
|
|
|
categoryPermissions.pushObject(cp);
|
2017-09-21 15:48:00 +01:00
|
|
|
});
|
|
|
|
|
2018-03-06 10:39:47 +00:00
|
|
|
this.get('categorySvc').getPermissions(category.get('id')).then((perms) => {
|
|
|
|
// mark those users as selected that have permission to see the current category
|
|
|
|
perms.forEach((perm) => {
|
|
|
|
let c = categoryPermissions.findBy('whoId', perm.get('whoId'));
|
|
|
|
if (is.not.undefined(c)) {
|
|
|
|
c.set('selected', true);
|
2018-03-06 12:19:03 +00:00
|
|
|
}
|
2018-03-06 10:39:47 +00:00
|
|
|
});
|
2018-05-03 12:49:49 +01:00
|
|
|
|
2018-03-06 10:39:47 +00:00
|
|
|
this.set('categoryPermissions', categoryPermissions.sortBy('who', 'name'));
|
|
|
|
});
|
2017-09-21 15:48:00 +01:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2018-03-06 10:39:47 +00:00
|
|
|
onToggle(item) {
|
|
|
|
item.set('selected', !item.get('selected'));
|
|
|
|
},
|
|
|
|
|
2017-09-21 15:48:00 +01:00
|
|
|
onGrantAccess() {
|
2017-11-27 15:38:39 +00:00
|
|
|
this.set('showCategoryAccess', false);
|
|
|
|
|
2018-06-21 12:38:13 +01:00
|
|
|
let space = this.get('space');
|
2017-09-21 15:48:00 +01:00
|
|
|
let category = this.get('currentCategory');
|
2018-03-06 10:39:47 +00:00
|
|
|
let perms = this.get('categoryPermissions').filterBy('selected', true);
|
2017-09-21 15:48:00 +01:00
|
|
|
|
2018-06-21 12:38:13 +01:00
|
|
|
this.get('categorySvc').setViewers(space.get('id'), category.get('id'), perms).then(() => {
|
2017-09-21 18:59:43 +01:00
|
|
|
this.load();
|
|
|
|
});
|
2017-09-19 17:58:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|