1
0
Fork 0
mirror of https://github.com/documize/community.git synced 2025-07-19 05:09:42 +02:00
documize/gui/app/components/folder/settings-category.js

212 lines
5.2 KiB
JavaScript
Raw Normal View History

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
import $ from 'jquery';
2018-05-03 12:49:49 +01:00
import { A } from '@ember/array';
import { inject as service } from '@ember/service';
2017-12-05 19:03:38 +00:00
import ModalMixin from '../../mixins/modal';
import Notifer from '../../mixins/notifier';
import Component from '@ember/component';
2017-09-19 17:58:33 +01:00
export default Component.extend(ModalMixin, Notifer, {
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: '',
newCategory: '',
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();
},
willDestroyElement() {
2017-10-04 13:27:48 -04:00
this._super(...arguments);
},
2017-09-19 17:58:33 +01:00
load() {
// get categories
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 18:59:43 +01:00
// get summary of documents and users for each category in space
this.get('categorySvc').getSummary(this.get('space.id')).then((s) => {
2017-09-21 18:59:43 +01:00
c.forEach((cat) => {
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);
});
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
});
},
permissionRecord(who, whoId, name) {
let raw = {
id: whoId,
orgId: this.get('space.orgId'),
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: {
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,
spaceId: this.get('space.id')
};
this.get('categorySvc').add(c).then(() => {
this.load();
this.notifySuccess('Category added');
});
},
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
this.get('categorySvc').delete(this.get('deleteId')).then(() => {
2017-09-19 17:58:33 +01:00
this.load();
});
},
onEdit(id) {
this.setEdit(id, true);
},
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
this.get('categorySvc').save(cat).then(() => {
2017-09-19 17:58:33 +01:00
this.load();
});
},
onShowAccessPicker(catId) {
2017-11-27 15:38:39 +00:00
this.set('showCategoryAccess', true);
let categoryPermissions = A([]);
let category = this.get('category').findBy('id', catId);
this.set('currentCategory', category);
this.set('categoryPermissions', categoryPermissions);
// get space permissions
this.get('spaceSvc').getPermissions(this.get('space.id')).then((spacePermissions) => {
spacePermissions.forEach((sp) => {
let cp = this.permissionRecord(sp.get('who'), sp.get('whoId'), sp.get('name'));
cp.set('selected', false);
categoryPermissions.pushObject(cp);
});
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-05-03 12:49:49 +01:00
this.set('categoryPermissions', categoryPermissions.sortBy('who', 'name'));
});
});
},
onToggle(item) {
item.set('selected', !item.get('selected'));
},
onGrantAccess() {
2017-11-27 15:38:39 +00:00
this.set('showCategoryAccess', false);
let space = this.get('space');
let category = this.get('currentCategory');
let perms = this.get('categoryPermissions').filterBy('selected', true);
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
}
}
});