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
|
|
|
|
|
2017-11-16 13:28:05 +00:00
|
|
|
import { inject as service } from '@ember/service';
|
2016-07-07 18:54:16 -07:00
|
|
|
|
2017-11-16 13:28:05 +00: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
|
|
|
|
2016-08-11 15:18:39 +02:00
|
|
|
// Returns all available sections.
|
|
|
|
getAll() {
|
|
|
|
return this.get('ajax').request(`sections`, {
|
|
|
|
method: 'GET'
|
|
|
|
}).then((response) => {
|
|
|
|
let data = [];
|
2018-03-23 11:50:21 +00:00
|
|
|
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);
|
2016-08-26 13:13:26 +02:00
|
|
|
return this.get('store').push(data);
|
2016-08-11 15:18:39 +02:00
|
|
|
});
|
2016-07-07 18:54:16 -07:00
|
|
|
|
2016-08-11 15:18:39 +02:00
|
|
|
return data;
|
|
|
|
});
|
|
|
|
},
|
2016-07-07 18:54:16 -07:00
|
|
|
|
2016-08-11 15:18:39 +02: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}§ion=${section}&method=${method}`;
|
2016-07-07 18:54:16 -07:00
|
|
|
|
2016-08-11 15:18:39 +02: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}§ion=${section}&method=${method}`;
|
|
|
|
|
|
|
|
return this.get('ajax').post(url, {
|
|
|
|
data: JSON.stringify(data),
|
|
|
|
contentType: "application/json",
|
|
|
|
dataType: "html"
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2016-08-11 15:18:39 +02:00
|
|
|
// 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
|
|
|
|
2016-08-11 15:18:39 +02:00
|
|
|
return this.get('ajax').request(url, {
|
|
|
|
method: 'GET'
|
|
|
|
}).then((response) => {
|
|
|
|
let pages = [];
|
2018-03-23 11:50:21 +00:00
|
|
|
if (is.not.array(response)) response = [];
|
2016-07-07 18:54:16 -07:00
|
|
|
|
2016-08-11 15:18:39 +02:00
|
|
|
if (is.not.null(response) && is.array(response) && response.length > 0) {
|
2016-08-12 14:02:57 +02:00
|
|
|
pages = response.map((page) => {
|
2016-08-11 15:18:39 +02:00
|
|
|
let data = this.get('store').normalize('page', page);
|
2016-08-26 13:13:26 +02:00
|
|
|
return this.get('store').push(data);
|
2016-08-11 15:18:39 +02:00
|
|
|
});
|
|
|
|
}
|
2016-07-07 18:54:16 -07:00
|
|
|
|
2016-08-11 15:18:39 +02: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
|
2016-08-11 15:18:39 +02:00
|
|
|
});
|
2017-01-20 14:24:38 -08:00
|
|
|
},
|
|
|
|
|
2017-01-21 14:29:36 -08:00
|
|
|
/**************************************************
|
|
|
|
* Reusable Content Blocks
|
|
|
|
**************************************************/
|
2017-01-20 14:24:38 -08:00
|
|
|
|
2017-01-21 14:29:36 -08:00
|
|
|
// Save new reusable content block.
|
|
|
|
addBlock(payload) {
|
|
|
|
let url = `sections/blocks`;
|
2017-01-20 14:24:38 -08:00
|
|
|
|
|
|
|
return this.get('ajax').post(url, {
|
|
|
|
data: JSON.stringify(payload),
|
|
|
|
contentType: 'json'
|
2017-01-21 18:14:31 -08:00
|
|
|
}).then((response) => {
|
|
|
|
let data = this.get('store').normalize('block', response);
|
|
|
|
return this.get('store').push(data);
|
2017-01-20 14:24:38 -08:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2017-01-21 17:28:31 -08:00
|
|
|
// 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) {
|
2017-01-21 17:28:31 -08:00
|
|
|
return this.get('ajax').request(`sections/blocks/space/${folderId}`, {
|
2017-01-20 14:24:38 -08:00
|
|
|
method: 'GET'
|
|
|
|
}).then((response) => {
|
|
|
|
let data = [];
|
2018-03-23 11:50:21 +00:00
|
|
|
if (is.not.array(response)) response = [];
|
2017-01-20 14:24:38 -08:00
|
|
|
|
|
|
|
data = response.map((obj) => {
|
2017-01-21 14:29:36 -08:00
|
|
|
let data = this.get('store').normalize('block', obj);
|
2017-01-20 14:24:38 -08:00
|
|
|
return this.get('store').push(data);
|
|
|
|
});
|
|
|
|
|
|
|
|
return data;
|
|
|
|
});
|
2017-01-21 17:28:31 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
// Returns reusable content block.
|
|
|
|
updateBlock(block) {
|
|
|
|
return this.get('ajax').request(`sections/blocks/${block.id}`, {
|
|
|
|
method: 'PUT',
|
|
|
|
data: JSON.stringify(block)
|
|
|
|
});
|
2017-01-21 18:14:31 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
// Removes specified reusable content block.
|
|
|
|
deleteBlock: function (blockId) {
|
|
|
|
let url = `sections/blocks/${blockId}`;
|
|
|
|
|
|
|
|
return this.get('ajax').request(url, {
|
|
|
|
method: 'DELETE'
|
|
|
|
});
|
2016-08-11 15:18:39 +02:00
|
|
|
}
|
2016-07-07 18:54:16 -07:00
|
|
|
});
|