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 { set } from '@ember/object';
|
|
|
|
import { A } from '@ember/array';
|
2018-04-13 11:01:36 +01:00
|
|
|
import stringUtil from '../utils/string';
|
2017-11-16 13:28:05 +00:00
|
|
|
import ArrayProxy from '@ember/array/proxy';
|
|
|
|
import Service, { inject as service } from '@ember/service';
|
2016-07-07 18:54:16 -07:00
|
|
|
|
2017-11-16 13:28:05 +00:00
|
|
|
export default Service.extend({
|
2016-08-09 13:16:29 +02:00
|
|
|
sessionService: service('session'),
|
2018-04-13 11:01:36 +01:00
|
|
|
storageSvc: service('localStorage'),
|
2017-10-08 20:53:25 -04:00
|
|
|
folderService: service('folder'),
|
2016-08-09 13:16:29 +02:00
|
|
|
ajax: service(),
|
|
|
|
store: service(),
|
2016-07-07 18:54:16 -07:00
|
|
|
|
2018-01-10 16:07:17 +00:00
|
|
|
//**************************************************
|
|
|
|
// Document
|
|
|
|
//**************************************************
|
|
|
|
|
2016-07-07 18:54:16 -07:00
|
|
|
// Returns document model for specified document id.
|
|
|
|
getDocument(documentId) {
|
|
|
|
return this.get('ajax').request(`documents/${documentId}`, {
|
|
|
|
method: "GET"
|
|
|
|
}).then((response) => {
|
2016-08-12 14:02:57 +02:00
|
|
|
let data = this.get('store').normalize('document', response);
|
2016-08-26 13:13:26 +02:00
|
|
|
return this.get('store').push(data);
|
2016-11-20 13:41:43 -08:00
|
|
|
}).catch((error) => {
|
|
|
|
this.get('router').transitionTo('/not-found');
|
|
|
|
return error;
|
2016-07-07 18:54:16 -07:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2017-09-18 13:02:15 +01:00
|
|
|
// Returns all documents for specified space.
|
|
|
|
getAllBySpace(spaceId) {
|
|
|
|
return this.get('ajax').request(`documents?space=${spaceId}`, {
|
2016-07-07 18:54:16 -07:00
|
|
|
method: "GET"
|
|
|
|
}).then((response) => {
|
2017-11-16 13:28:05 +00:00
|
|
|
let documents = ArrayProxy.create({
|
|
|
|
content: A([])
|
2016-07-07 18:54:16 -07:00
|
|
|
});
|
2019-03-03 13:10:04 +00:00
|
|
|
if (!_.isArray(response)) response = [];
|
2016-07-07 18:54:16 -07:00
|
|
|
|
2016-08-12 14:02:57 +02:00
|
|
|
documents = response.map((doc) => {
|
2016-08-09 13:16:29 +02:00
|
|
|
let data = this.get('store').normalize('document', doc);
|
2016-08-26 13:13:26 +02:00
|
|
|
return this.get('store').push(data);
|
2016-07-07 18:54:16 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
return documents;
|
2018-01-22 10:31:03 +00:00
|
|
|
}).catch((error) => {
|
|
|
|
return error;
|
2016-07-07 18:54:16 -07:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
// saveDocument updates an existing document record.
|
|
|
|
save(doc) {
|
|
|
|
let id = doc.get('id');
|
|
|
|
|
|
|
|
return this.get('ajax').request(`documents/${id}`, {
|
|
|
|
method: 'PUT',
|
2016-08-29 16:06:13 +02:00
|
|
|
data: JSON.stringify(doc)
|
2016-07-07 18:54:16 -07:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2018-01-10 16:07:17 +00:00
|
|
|
deleteDocument(documentId) {
|
|
|
|
let url = `documents/${documentId}`;
|
2016-07-07 18:54:16 -07:00
|
|
|
|
2018-01-10 16:07:17 +00:00
|
|
|
return this.get('ajax').request(url, {
|
|
|
|
method: 'DELETE'
|
2016-07-07 18:54:16 -07:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2018-01-10 16:07:17 +00:00
|
|
|
//**************************************************
|
|
|
|
// Page
|
|
|
|
//**************************************************
|
|
|
|
|
|
|
|
// addPage inserts new page to an existing document.
|
|
|
|
addPage(documentId, payload) {
|
|
|
|
let url = `documents/${documentId}/pages`;
|
2016-07-07 18:54:16 -07:00
|
|
|
|
|
|
|
return this.get('ajax').post(url, {
|
|
|
|
data: JSON.stringify(payload),
|
|
|
|
contentType: 'json'
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2017-03-14 06:15:35 +00:00
|
|
|
updatePage(documentId, pageId, payload, skipRevision) {
|
2016-07-07 18:54:16 -07:00
|
|
|
var revision = skipRevision ? "?r=true" : "?r=false";
|
|
|
|
let url = `documents/${documentId}/pages/${pageId}${revision}`;
|
2016-11-10 11:19:26 -08:00
|
|
|
|
2017-11-16 13:28:05 +00:00
|
|
|
set(payload.meta, 'id', parseInt(payload.meta.id));
|
2016-07-07 18:54:16 -07:00
|
|
|
|
|
|
|
return this.get('ajax').request(url, {
|
|
|
|
method: 'PUT',
|
|
|
|
data: JSON.stringify(payload),
|
|
|
|
contentType: 'json'
|
2016-11-10 11:19:26 -08:00
|
|
|
}).then((response) => {
|
|
|
|
let data = this.get('store').normalize('page', response);
|
|
|
|
return this.get('store').push(data);
|
2018-01-22 10:31:03 +00:00
|
|
|
}).catch((error) => {
|
|
|
|
return error;
|
2016-07-07 18:54:16 -07:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2018-01-10 16:07:17 +00:00
|
|
|
// Returns all document pages with content
|
|
|
|
getPages(documentId) {
|
|
|
|
return this.get('ajax').request(`documents/${documentId}/pages`, {
|
|
|
|
method: 'GET'
|
|
|
|
}).then((response) => {
|
2019-03-03 13:10:04 +00:00
|
|
|
if (!_.isArray(response)) response = [];
|
2018-01-10 16:07:17 +00:00
|
|
|
let pages = [];
|
2016-07-07 18:54:16 -07:00
|
|
|
|
2018-01-10 16:07:17 +00:00
|
|
|
pages = response.map((page) => {
|
|
|
|
let data = this.get('store').normalize('page', page);
|
|
|
|
return this.get('store').push(data);
|
|
|
|
});
|
|
|
|
|
|
|
|
return pages;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
// Returns document page with content
|
|
|
|
getPage(documentId, pageId) {
|
|
|
|
return this.get('ajax').request(`documents/${documentId}/pages/${pageId}`, {
|
|
|
|
method: 'GET'
|
|
|
|
}).then((response) => {
|
|
|
|
let data = this.get('store').normalize('page', response);
|
|
|
|
return this.get('store').push(data);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
// Returns document page meta object
|
|
|
|
getPageMeta(documentId, pageId) {
|
|
|
|
return this.get('ajax').request(`documents/${documentId}/pages/${pageId}/meta`, {
|
|
|
|
method: 'GET'
|
|
|
|
}).then((response) => {
|
|
|
|
let data = this.get('store').normalize('page-meta', response);
|
|
|
|
return this.get('store').push(data);
|
|
|
|
}).catch(() => {
|
2016-07-07 18:54:16 -07:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
// Nukes multiple pages from the document.
|
2017-09-26 16:30:16 +01:00
|
|
|
deletePages(documentId, pageId, payload) {
|
2016-10-13 11:31:09 -07:00
|
|
|
let url = `documents/${documentId}/pages`;
|
2016-07-07 18:54:16 -07:00
|
|
|
|
2016-10-13 11:31:09 -07:00
|
|
|
return this.get('ajax').request(url, {
|
2016-07-07 18:54:16 -07:00
|
|
|
data: JSON.stringify(payload),
|
2016-10-13 11:31:09 -07:00
|
|
|
contentType: 'json',
|
|
|
|
method: 'DELETE'
|
2016-07-07 18:54:16 -07:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
// Nukes a single page from the document.
|
2017-09-26 16:30:16 +01:00
|
|
|
deletePage(documentId, pageId) {
|
2016-07-07 18:54:16 -07:00
|
|
|
let url = `documents/${documentId}/pages/${pageId}`;
|
|
|
|
|
|
|
|
return this.get('ajax').request(url, {
|
|
|
|
method: 'DELETE'
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2018-01-10 16:07:17 +00:00
|
|
|
//**************************************************
|
|
|
|
// Page Revisions
|
|
|
|
//**************************************************
|
|
|
|
|
2016-11-30 17:56:36 -08:00
|
|
|
getDocumentRevisions(documentId) {
|
|
|
|
let url = `documents/${documentId}/revisions`;
|
|
|
|
|
|
|
|
return this.get('ajax').request(url, {
|
|
|
|
method: "GET"
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2016-07-07 18:54:16 -07:00
|
|
|
getPageRevisions(documentId, pageId) {
|
|
|
|
let url = `documents/${documentId}/pages/${pageId}/revisions`;
|
|
|
|
|
|
|
|
return this.get('ajax').request(url, {
|
|
|
|
method: "GET"
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
getPageRevisionDiff(documentId, pageId, revisionId) {
|
|
|
|
let url = `documents/${documentId}/pages/${pageId}/revisions/${revisionId}`;
|
|
|
|
|
|
|
|
return this.get('ajax').request(url, {
|
|
|
|
method: "GET",
|
|
|
|
dataType: 'text'
|
2016-11-30 17:56:36 -08:00
|
|
|
}).then((response) => {
|
|
|
|
return response;
|
2016-11-30 18:10:54 -08:00
|
|
|
}).catch(() => {
|
2016-11-30 17:56:36 -08:00
|
|
|
return "";
|
2016-07-07 18:54:16 -07:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
rollbackPage(documentId, pageId, revisionId) {
|
|
|
|
let url = `documents/${documentId}/pages/${pageId}/revisions/${revisionId}`;
|
|
|
|
|
|
|
|
return this.get('ajax').request(url, {
|
|
|
|
method: "POST"
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2018-01-10 16:07:17 +00:00
|
|
|
//**************************************************
|
|
|
|
// Table of contents
|
|
|
|
//**************************************************
|
|
|
|
|
2016-07-07 18:54:16 -07:00
|
|
|
// Returns all pages without the content
|
|
|
|
getTableOfContents(documentId) {
|
|
|
|
return this.get('ajax').request(`documents/${documentId}/pages?content=0`, {
|
|
|
|
method: 'GET'
|
|
|
|
}).then((response) => {
|
2019-03-03 13:10:04 +00:00
|
|
|
if (!_.isArray(response)) response = [];
|
2018-03-23 11:50:21 +00:00
|
|
|
|
2016-07-07 18:54:16 -07:00
|
|
|
let data = [];
|
2016-08-12 14:02:57 +02:00
|
|
|
data = response.map((obj) => {
|
2016-08-09 13:16:29 +02:00
|
|
|
let data = this.get('store').normalize('page', obj);
|
2016-08-26 13:13:26 +02:00
|
|
|
return this.get('store').push(data);
|
2016-07-07 18:54:16 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
return data;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2018-01-10 16:07:17 +00:00
|
|
|
changePageSequence(documentId, payload) {
|
|
|
|
let url = `documents/${documentId}/pages/sequence`;
|
2016-07-07 18:54:16 -07:00
|
|
|
|
2018-01-10 16:07:17 +00:00
|
|
|
return this.get('ajax').post(url, {
|
|
|
|
data: JSON.stringify(payload),
|
|
|
|
contentType: 'json'
|
2016-07-07 18:54:16 -07:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2018-01-10 16:07:17 +00:00
|
|
|
changePageLevel(documentId, payload) {
|
|
|
|
let url = `documents/${documentId}/pages/level`;
|
2016-07-07 18:54:16 -07:00
|
|
|
|
2018-01-10 16:07:17 +00:00
|
|
|
return this.get('ajax').post(url, {
|
|
|
|
data: JSON.stringify(payload),
|
|
|
|
contentType: 'json'
|
2016-07-07 18:54:16 -07:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2018-01-10 16:07:17 +00:00
|
|
|
//**************************************************
|
|
|
|
// Attachments
|
|
|
|
//**************************************************
|
2016-07-07 18:54:16 -07:00
|
|
|
|
|
|
|
// document attachments without the actual content
|
|
|
|
getAttachments(documentId) {
|
|
|
|
return this.get('ajax').request(`documents/${documentId}/attachments`, {
|
|
|
|
method: 'GET'
|
|
|
|
}).then((response) => {
|
|
|
|
let data = [];
|
2016-08-12 14:02:57 +02:00
|
|
|
|
2016-08-16 13:34:09 +02:00
|
|
|
if (isObject(response)) {
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2016-08-12 14:02:57 +02:00
|
|
|
data = response.map((obj) => {
|
|
|
|
let data = this.get('store').normalize('attachment', obj);
|
2016-08-26 13:13:26 +02:00
|
|
|
return this.get('store').push(data);
|
2016-07-07 18:54:16 -07:00
|
|
|
});
|
2016-08-12 14:02:57 +02:00
|
|
|
|
2016-07-07 18:54:16 -07:00
|
|
|
return data;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
// nuke an attachment
|
|
|
|
deleteAttachment(documentId, attachmentId) {
|
|
|
|
return this.get('ajax').request(`documents/${documentId}/attachments/${attachmentId}`, {
|
|
|
|
method: 'DELETE'
|
|
|
|
});
|
2017-01-22 14:12:10 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
//**************************************************
|
|
|
|
// Page Move Copy
|
|
|
|
//**************************************************
|
|
|
|
|
|
|
|
// Return list of documents that can accept a page.
|
|
|
|
getPageMoveCopyTargets() {
|
|
|
|
return this.get('ajax').request(`sections/targets`, {
|
|
|
|
method: 'GET'
|
|
|
|
}).then((response) => {
|
2019-03-03 13:10:04 +00:00
|
|
|
if (!_.isArray(response)) response = [];
|
2017-01-22 14:12:10 -08:00
|
|
|
let data = [];
|
|
|
|
|
|
|
|
data = response.map((obj) => {
|
|
|
|
let data = this.get('store').normalize('document', obj);
|
|
|
|
return this.get('store').push(data);
|
|
|
|
});
|
|
|
|
|
|
|
|
return data;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
// Copy existing page to same or different document.
|
|
|
|
copyPage(documentId, pageId, targetDocumentId) {
|
|
|
|
return this.get('ajax').request(`documents/${documentId}/pages/${pageId}/copy/${targetDocumentId}`, {
|
|
|
|
method: 'POST'
|
|
|
|
}).then((response) => {
|
|
|
|
let data = this.get('store').normalize('page', response);
|
|
|
|
return this.get('store').push(data);
|
|
|
|
});
|
2017-01-22 15:22:14 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
// Move existing page to different document.
|
|
|
|
movePage(documentId, pageId, targetDocumentId) {
|
|
|
|
return this.get('ajax').request(`documents/${documentId}/pages/${pageId}/move/${targetDocumentId}`, {
|
|
|
|
method: 'POST'
|
|
|
|
}).then((response) => {
|
|
|
|
let data = this.get('store').normalize('page', response);
|
|
|
|
return this.get('store').push(data);
|
|
|
|
});
|
2017-10-08 20:53:25 -04:00
|
|
|
},
|
|
|
|
|
2018-04-13 11:01:36 +01:00
|
|
|
//**************************************************
|
|
|
|
// Voting / Liking
|
|
|
|
//**************************************************
|
|
|
|
|
|
|
|
// Vote records content vote from user.
|
|
|
|
// Anonymous users can vote to and are assigned temp id that is stored
|
|
|
|
// client-side in browser local storage.
|
|
|
|
vote(documentId, vote) {
|
|
|
|
let userId = '';
|
|
|
|
|
|
|
|
if (this.get('sessionService.authenticated')) {
|
|
|
|
userId = this.get('sessionService.user.id');
|
|
|
|
} else {
|
|
|
|
let id = this.get('storageSvc').getSessionItem('anonId');
|
|
|
|
|
2019-03-03 13:10:04 +00:00
|
|
|
if (!_.isNull(id) && !_.isUndefined(id) && id.length >= 16) {
|
2018-04-13 11:01:36 +01:00
|
|
|
userId = id;
|
|
|
|
} else {
|
|
|
|
userId = stringUtil.anonUserId();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.get('storageSvc').storeSessionItem('anonId', userId);
|
|
|
|
}
|
|
|
|
|
|
|
|
let payload = {
|
|
|
|
userId: userId,
|
|
|
|
vote: vote
|
|
|
|
};
|
|
|
|
|
|
|
|
return this.get('ajax').post(`public/document/${documentId}/vote`, {
|
|
|
|
data: JSON.stringify(payload),
|
|
|
|
contentType: 'json'
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2018-07-28 11:43:45 -04:00
|
|
|
//**************************************************
|
2018-12-19 12:41:36 +00:00
|
|
|
// Export content to HTML
|
2018-07-28 11:43:45 -04:00
|
|
|
//**************************************************
|
|
|
|
|
|
|
|
export(spec) {
|
|
|
|
return this.get('ajax').post('export', {
|
|
|
|
data: JSON.stringify(spec),
|
|
|
|
contentType: 'json',
|
2018-07-28 15:30:33 -04:00
|
|
|
dataType: 'html'
|
2018-07-28 11:43:45 -04:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2018-12-19 12:41:36 +00:00
|
|
|
//**************************************************
|
|
|
|
// Secure document attachment download
|
|
|
|
//**************************************************
|
|
|
|
|
|
|
|
downloadAttachment(fileId) {
|
|
|
|
return this.get('ajax').get(`attachment/${fileId}`, {});
|
|
|
|
},
|
|
|
|
|
2017-10-08 20:53:25 -04:00
|
|
|
//**************************************************
|
|
|
|
// Fetch bulk data
|
|
|
|
//**************************************************
|
|
|
|
|
|
|
|
// fetchXXX represents UI specific bulk data loading designed to
|
|
|
|
// reduce network traffic and boost app performance.
|
|
|
|
// This method that returns:
|
|
|
|
// 1. getUserVisible()
|
|
|
|
// 2. getSummary()
|
|
|
|
// 3. getSpaceCategoryMembership()
|
|
|
|
fetchDocumentData(documentId) {
|
|
|
|
return this.get('ajax').request(`fetch/document/${documentId}`, {
|
|
|
|
method: 'GET'
|
|
|
|
}).then((response) => {
|
|
|
|
let data = {
|
|
|
|
document: {},
|
|
|
|
permissions: {},
|
2017-12-26 13:25:10 +00:00
|
|
|
roles: {},
|
2017-10-08 20:53:25 -04:00
|
|
|
folders: [],
|
|
|
|
folder: {},
|
|
|
|
links: [],
|
2018-03-19 12:24:58 +00:00
|
|
|
versions: [],
|
2017-10-08 20:53:25 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
let doc = this.get('store').normalize('document', response.document);
|
|
|
|
doc = this.get('store').push(doc);
|
|
|
|
|
2017-10-09 10:56:59 -04:00
|
|
|
let perms = this.get('store').normalize('space-permission', response.permissions);
|
2017-12-26 13:25:10 +00:00
|
|
|
perms = this.get('store').push(perms);
|
2017-10-08 20:53:25 -04:00
|
|
|
this.get('folderService').set('permissions', perms);
|
|
|
|
|
2018-03-06 13:36:11 +00:00
|
|
|
let roles = this.get('store').normalize('document-permission', response.roles);
|
2017-12-26 13:25:10 +00:00
|
|
|
roles = this.get('store').push(roles);
|
|
|
|
|
2017-10-08 20:53:25 -04:00
|
|
|
let folders = response.folders.map((obj) => {
|
|
|
|
let data = this.get('store').normalize('folder', obj);
|
|
|
|
return this.get('store').push(data);
|
|
|
|
});
|
|
|
|
|
|
|
|
data.document = doc;
|
|
|
|
data.permissions = perms;
|
2017-12-26 13:25:10 +00:00
|
|
|
data.roles = roles;
|
2017-10-08 20:53:25 -04:00
|
|
|
data.folders = folders;
|
2018-10-12 17:54:15 +01:00
|
|
|
data.folder = folders.findBy('id', doc.get('spaceId'));
|
2017-10-08 20:53:25 -04:00
|
|
|
data.links = response.links;
|
2018-03-19 12:24:58 +00:00
|
|
|
data.versions = response.versions;
|
2017-10-08 20:53:25 -04:00
|
|
|
|
|
|
|
return data;
|
2018-01-22 10:31:03 +00:00
|
|
|
}).catch((error) => {
|
|
|
|
return error;
|
2017-10-08 20:53:25 -04:00
|
|
|
});
|
2018-01-22 10:31:03 +00:00
|
|
|
},
|
2017-10-08 20:53:25 -04:00
|
|
|
|
2018-01-22 10:31:03 +00:00
|
|
|
// fetchPages returns all pages, page meta and pending changes for document.
|
|
|
|
// This method bulk fetches data to reduce network chatter.
|
2018-03-19 12:24:58 +00:00
|
|
|
// We produce a bunch of calculated boolean's for UI display purposes
|
2018-01-22 10:31:03 +00:00
|
|
|
// that can tell us quickly about pending changes for UI display.
|
2018-04-05 14:23:39 +01:00
|
|
|
|
|
|
|
// Source - optional identifier of (document) referrer.
|
|
|
|
fetchPages(documentId, currentUserId, source) {
|
2018-01-22 10:31:03 +00:00
|
|
|
let constants = this.get('constants');
|
|
|
|
let changePending = false;
|
|
|
|
let changeAwaitingReview = false;
|
|
|
|
let changeRejected = false;
|
|
|
|
let userHasChangePending = false;
|
|
|
|
let userHasChangeAwaitingReview = false;
|
|
|
|
let userHasChangeRejected = false;
|
|
|
|
|
2019-03-03 13:10:04 +00:00
|
|
|
if (_.isNull(source) || _.isUndefined(source)) source = "";
|
2018-04-05 19:59:57 +01:00
|
|
|
|
2018-04-05 14:23:39 +01:00
|
|
|
return this.get('ajax').request(`fetch/page/${documentId}?source=${source}`, {
|
2018-01-22 10:31:03 +00:00
|
|
|
method: 'GET'
|
|
|
|
}).then((response) => {
|
|
|
|
let data = A([]);
|
|
|
|
|
|
|
|
response.forEach((page) => {
|
|
|
|
changePending = false;
|
|
|
|
changeAwaitingReview = false;
|
|
|
|
changeRejected = false;
|
|
|
|
userHasChangePending = false;
|
|
|
|
userHasChangeAwaitingReview = false;
|
|
|
|
userHasChangeRejected = false;
|
|
|
|
|
|
|
|
let p = this.get('store').normalize('page', page.page);
|
|
|
|
p = this.get('store').push(p);
|
|
|
|
|
|
|
|
let m = this.get('store').normalize('page-meta', page.meta);
|
|
|
|
m = this.get('store').push(m);
|
|
|
|
|
|
|
|
let pending = A([]);
|
|
|
|
page.pending.forEach((i) => {
|
|
|
|
let p = this.get('store').normalize('page', i.page);
|
|
|
|
p = this.get('store').push(p);
|
2018-03-19 12:24:58 +00:00
|
|
|
|
2018-01-22 10:31:03 +00:00
|
|
|
let m = this.get('store').normalize('page-meta', i.meta);
|
|
|
|
m = this.get('store').push(m);
|
|
|
|
|
|
|
|
let belongsToMe = p.get('userId') === currentUserId;
|
|
|
|
let pageStatus = p.get('status');
|
|
|
|
|
|
|
|
let pi = {
|
|
|
|
id: p.get('id'),
|
|
|
|
page: p,
|
|
|
|
meta: m,
|
|
|
|
owner: i.owner,
|
|
|
|
changePending: pageStatus === constants.ChangeState.Pending || pageStatus === constants.ChangeState.PendingNew,
|
|
|
|
changeAwaitingReview: pageStatus === constants.ChangeState.UnderReview,
|
|
|
|
changeRejected: pageStatus === constants.ChangeState.Rejected,
|
|
|
|
userHasChangePending: belongsToMe && (pageStatus === constants.ChangeState.Pending || pageStatus === constants.ChangeState.PendingNew),
|
|
|
|
userHasChangeAwaitingReview: belongsToMe && pageStatus === constants.ChangeState.UnderReview,
|
|
|
|
userHasChangeRejected: belongsToMe && pageStatus === constants.ChangeState.Rejected
|
|
|
|
};
|
|
|
|
|
|
|
|
let pim = this.get('store').normalize('page-pending', pi);
|
|
|
|
pim = this.get('store').push(pim);
|
|
|
|
pending.pushObject(pim);
|
|
|
|
|
|
|
|
if (p.get('status') === constants.ChangeState.Pending || p.get('status') === constants.ChangeState.PendingNew) {
|
|
|
|
changePending = true;
|
|
|
|
userHasChangePending = belongsToMe;
|
|
|
|
}
|
|
|
|
if (p.get('status') === constants.ChangeState.UnderReview) {
|
|
|
|
changeAwaitingReview = true;
|
|
|
|
userHasChangeAwaitingReview = belongsToMe;
|
|
|
|
}
|
|
|
|
if (p.get('status') === constants.ChangeState.Rejected) {
|
|
|
|
changeRejected = p.get('status') === constants.ChangeState.Rejected;
|
|
|
|
userHasChangeRejected = changeRejected && belongsToMe;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
let pi = {
|
|
|
|
id: p.get('id'),
|
|
|
|
page: p,
|
|
|
|
meta: m,
|
|
|
|
pending: pending,
|
|
|
|
changePending: changePending,
|
|
|
|
changeAwaitingReview: changeAwaitingReview,
|
|
|
|
changeRejected: changeRejected,
|
|
|
|
userHasChangePending: userHasChangePending,
|
|
|
|
userHasChangeAwaitingReview: userHasChangeAwaitingReview,
|
|
|
|
userHasChangeRejected: userHasChangeRejected,
|
|
|
|
userHasNewPagePending: p.isNewPageUserPending(this.get('sessionService.user.id'))
|
|
|
|
};
|
2018-03-19 12:24:58 +00:00
|
|
|
|
2018-01-22 10:31:03 +00:00
|
|
|
let pim = this.get('store').normalize('page-container', pi);
|
|
|
|
pim = this.get('store').push(pim);
|
|
|
|
data.pushObject(pim);
|
|
|
|
});
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}).catch((error) => {
|
|
|
|
return error;
|
|
|
|
});
|
2017-01-20 14:24:38 -08:00
|
|
|
}
|
2016-07-07 18:54:16 -07:00
|
|
|
});
|
2016-08-16 13:34:09 +02:00
|
|
|
|
|
|
|
function isObject(a) {
|
|
|
|
return (!!a) && (a.constructor === Object);
|
|
|
|
}
|