1
0
Fork 0
mirror of https://github.com/documize/community.git synced 2025-07-20 05:39:42 +02:00
documize/gui/app/services/section.js

150 lines
4 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 { inject as service } from '@ember/service';
2016-07-07 18:54:16 -07:00
import BaseService from '../services/base';
2016-08-16 13:34:09 +02:00
2016-07-07 18:54:16 -07:00
export default BaseService.extend({
2016-08-16 13:34:09 +02:00
sessionService: service('session'),
ajax: service(),
store: service(),
2016-07-07 18:54:16 -07:00
// Returns all available sections.
getAll() {
return this.get('ajax').request(`sections`, {
method: 'GET'
}).then((response) => {
let data = [];
if (is.not.array(response)) response = [];
2016-08-12 14:02:57 +02:00
data = response.map((obj) => {
let data = this.get('store').normalize('section', obj);
return this.get('store').push(data);
});
2016-07-07 18:54:16 -07:00
return data;
});
},
2016-07-07 18:54:16 -07:00
// Requests data from the specified section handler, passing the method and document ID
// and POST payload.
fetch(page, method, data) {
let documentId = page.get('documentId');
let section = page.get('contentType');
let url = `sections?documentID=${documentId}&section=${section}&method=${method}`;
2016-07-07 18:54:16 -07:00
return this.get('ajax').post(url, {
data: JSON.stringify(data),
contentType: "application/json"
});
},
2016-07-07 18:54:16 -07:00
2018-08-07 19:15:25 +01:00
// Requests data from the specified section handler, passing the method and document ID
// and POST payload.
fetchText(page, method, data) {
let documentId = page.get('documentId');
let section = page.get('contentType');
let url = `sections?documentID=${documentId}&section=${section}&method=${method}`;
return this.get('ajax').post(url, {
data: JSON.stringify(data),
contentType: "application/json",
dataType: "html"
});
},
// Did any dynamic sections change? Fetch and send up for rendering?
refresh(documentId) {
let url = `sections/refresh?documentID=${documentId}`;
2016-07-07 18:54:16 -07:00
return this.get('ajax').request(url, {
method: 'GET'
}).then((response) => {
let pages = [];
if (is.not.array(response)) response = [];
2016-07-07 18:54:16 -07:00
if (is.not.null(response) && is.array(response) && response.length > 0) {
2016-08-12 14:02:57 +02:00
pages = response.map((page) => {
let data = this.get('store').normalize('page', page);
return this.get('store').push(data);
});
}
2016-07-07 18:54:16 -07:00
return pages;
2017-03-10 11:16:31 +00:00
}).catch((/*error*/) => {
// we ignore any error to cater for anon users who don't
// have permissions to perform refresh
});
},
2017-01-21 14:29:36 -08:00
/**************************************************
* Reusable Content Blocks
**************************************************/
2017-01-21 14:29:36 -08:00
// Save new reusable content block.
addBlock(payload) {
let url = `sections/blocks`;
return this.get('ajax').post(url, {
data: JSON.stringify(payload),
contentType: 'json'
}).then((response) => {
let data = this.get('store').normalize('block', response);
return this.get('store').push(data);
});
},
// Returns reusable content block.
getBlock(blockId) {
return this.get('ajax').request(`sections/blocks/${blockId}`, {
method: 'GET'
}).then((response) => {
let data = this.get('store').normalize('block', response);
return this.get('store').push(data);
});
},
2017-01-21 14:29:36 -08:00
// Returns all available reusable content block for section.
getSpaceBlocks(folderId) {
return this.get('ajax').request(`sections/blocks/space/${folderId}`, {
method: 'GET'
}).then((response) => {
let data = [];
if (is.not.array(response)) response = [];
data = response.map((obj) => {
2017-01-21 14:29:36 -08:00
let data = this.get('store').normalize('block', obj);
return this.get('store').push(data);
});
return data;
});
},
// Returns reusable content block.
updateBlock(block) {
return this.get('ajax').request(`sections/blocks/${block.id}`, {
method: 'PUT',
data: JSON.stringify(block)
});
},
// Removes specified reusable content block.
deleteBlock: function (blockId) {
let url = `sections/blocks/${blockId}`;
return this.get('ajax').request(url, {
method: 'DELETE'
});
}
2016-07-07 18:54:16 -07:00
});