1
0
Fork 0
mirror of https://github.com/documize/community.git synced 2025-07-19 21:29:42 +02:00
documize/gui/app/services/folder.js

189 lines
4.2 KiB
JavaScript
Raw Normal View History

2016-07-07 18:54:16 -07: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 Ember from 'ember';
import BaseService from '../services/base';
const {
2016-08-17 15:37:46 +02:00
RSVP,
inject: { service }
2016-07-07 18:54:16 -07:00
} = Ember;
export default BaseService.extend({
sessionService: service('session'),
ajax: service(),
localStorage: service(),
store: service(),
// selected folder
currentFolder: null,
2017-09-14 12:54:57 +01:00
permissions: {},
// Add a new folder.
2017-08-21 17:51:06 +01:00
add(payload) {
2017-09-13 19:22:38 +01:00
return this.get('ajax').post(`space`, {
contentType: 'json',
2017-08-21 17:51:06 +01:00
data: JSON.stringify(payload)
}).then((folder) => {
let data = this.get('store').normalize('folder', folder);
return this.get('store').push(data);
});
},
// Returns folder model for specified folder id.
getFolder(id) {
2017-09-13 19:22:38 +01:00
return this.get('ajax').request(`space/${id}`, {
method: 'GET'
}).then((folder) => {
let data = this.get('store').normalize('folder', folder);
return this.get('store').push(data);
2017-05-03 11:37:39 +01:00
}).catch((error) => {
this.get('router').transitionTo('/not-found');
return error;
});
},
// Returns all folders that user can see.
getAll() {
2017-09-13 19:22:38 +01:00
let folders = this.get('space');
2016-08-17 15:37:46 +02:00
if (folders != null) {
return new RSVP.resolve(folders);
}
2016-08-17 15:37:46 +02:00
return this.reload();
},
// Updates an existing folder record.
save(folder) {
let id = folder.get('id');
2017-09-13 19:22:38 +01:00
return this.get('ajax').request(`space/${id}`, {
method: 'PUT',
contentType: 'json',
data: JSON.stringify(folder)
});
},
remove(folderId, moveToId) {
2017-09-13 19:22:38 +01:00
let url = `space/${folderId}/move/${moveToId}`;
return this.get('ajax').request(url, {
method: 'DELETE'
});
},
2017-03-24 13:10:32 +00:00
delete(folderId) {
2017-09-13 19:22:38 +01:00
return this.get('ajax').request(`space/${folderId}`, {
2017-03-24 13:10:32 +00:00
method: 'DELETE'
});
},
onboard(folderId, payload) {
let url = `public/share/${folderId}`;
return this.get('ajax').post(url, {
contentType: "application/json",
data: payload
});
},
// reloads and caches folders.
reload() {
2017-09-13 19:22:38 +01:00
return this.get('ajax').request(`space`, {
method: "GET"
}).then((response) => {
let data = [];
2016-08-12 14:02:57 +02:00
data = response.map((obj) => {
let data = this.get('store').normalize('folder', obj);
return this.get('store').push(data);
});
return data;
});
},
// so who can see/edit this folder?
getPermissions(folderId) {
2017-09-13 19:22:38 +01:00
return this.get('ajax').request(`space/${folderId}/permissions`, {
method: "GET"
}).then((response) => {
let data = [];
2016-08-12 14:02:57 +02:00
data = response.map((obj) => {
2017-09-14 12:54:57 +01:00
let data = this.get('store').normalize('space-permission', obj);
return this.get('store').push(data);
});
return data;
});
},
// persist folder permissions
savePermissions(folderId, payload) {
2017-09-13 19:22:38 +01:00
return this.get('ajax').request(`space/${folderId}/permissions`, {
method: 'PUT',
contentType: 'json',
data: JSON.stringify(payload)
});
},
// share this folder with new users!
share(folderId, invitation) {
return this.get('ajax').post(`folders/${folderId}/invitation`, {
contentType: 'json',
data: JSON.stringify(invitation)
});
},
// Current folder caching
setCurrentFolder(folder) {
if (is.undefined(folder) || is.null(folder)) {
return;
}
2017-09-13 19:22:38 +01:00
let folderId = folder.get('id');
this.set('currentFolder', folder);
2017-09-13 19:22:38 +01:00
this.get('localStorage').storeSessionItem("folder", folderId);
let userId = this.get('sessionService.user.id');
if (userId === "") {
userId = "0";
}
2017-09-13 19:22:38 +01:00
let url = `space/${folderId}/permissions/user`;
2017-09-14 12:54:57 +01:00
return this.get('ajax').request(url).then((response) => {
let data = this.get('store').normalize('space-permission', response);
let data2 = this.get('store').push(data);
this.set('permissions', data2);
return data2;
});
},
2017-09-26 20:13:44 +01:00
// returns all spaces -- for use by documize admin user
adminList() {
return this.get('ajax').request(`space/manage`, {
method: "GET"
}).then((response) => {
let data = [];
data = response.map((obj) => {
let data = this.get('store').normalize('folder', obj);
return this.get('store').push(data);
});
return data;
});
}
});