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

545 lines
15 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 { set } from '@ember/object';
import { A } from '@ember/array';
import stringUtil from '../utils/string';
import ArrayProxy from '@ember/array/proxy';
import Service, { inject as service } from '@ember/service';
2016-07-07 18:54:16 -07:00
export default Service.extend({
sessionService: service('session'),
storageSvc: service('localStorage'),
folderService: service('folder'),
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);
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
});
},
// 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) => {
let documents = ArrayProxy.create({
content: A([])
2016-07-07 18:54:16 -07: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) => {
let data = this.get('store').normalize('document', doc);
return this.get('store').push(data);
2016-07-07 18:54:16 -07:00
});
return documents;
}).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',
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
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);
}).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) => {
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.
deletePages(documentId, pageId, payload) {
let url = `documents/${documentId}/pages`;
2016-07-07 18:54:16 -07:00
return this.get('ajax').request(url, {
2016-07-07 18:54:16 -07:00
data: JSON.stringify(payload),
contentType: 'json',
method: 'DELETE'
2016-07-07 18:54:16 -07:00
});
},
// Nukes a single page from the document.
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) => {
if (!_.isArray(response)) response = [];
2016-07-07 18:54:16 -07:00
let data = [];
2016-08-12 14:02:57 +02:00
data = response.map((obj) => {
let data = this.get('store').normalize('page', obj);
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);
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) => {
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);
});
},
// 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);
});
},
//**************************************************
// 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');
if (!_.isNull(id) && !_.isUndefined(id) && id.length >= 16) {
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
//**************************************************
// 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
});
},
//**************************************************
// Secure document attachment download
//**************************************************
downloadAttachment(fileId) {
return this.get('ajax').get(`attachment/${fileId}`, {});
},
//**************************************************
// 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: {},
folders: [],
folder: {},
links: [],
2018-03-19 12:24:58 +00:00
versions: [],
attachments: [],
};
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);
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);
let folders = response.folders.map((obj) => {
let data = this.get('store').normalize('folder', obj);
return this.get('store').push(data);
});
let attachments = response.attachments.map((obj) => {
let data = this.get('store').normalize('attachment', obj);
return this.get('store').push(data);
});
data.document = doc;
data.permissions = perms;
2017-12-26 13:25:10 +00:00
data.roles = roles;
data.folders = folders;
data.folder = folders.findBy('id', doc.get('spaceId'));
data.links = response.links;
2018-03-19 12:24:58 +00:00
data.versions = response.versions;
data.attachments = attachments;
return data;
}).catch((error) => {
return error;
});
},
// 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
// that can tell us quickly about pending changes for UI display.
// Source - optional identifier of (document) referrer.
fetchPages(documentId, currentUserId, source) {
let constants = this.get('constants');
let changePending = false;
let changeAwaitingReview = false;
let changeRejected = false;
let userHasChangePending = false;
let userHasChangeAwaitingReview = false;
let userHasChangeRejected = false;
if (_.isNull(source) || _.isUndefined(source)) source = "";
2018-04-05 19:59:57 +01:00
return this.get('ajax').request(`fetch/page/${documentId}?source=${source}`, {
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
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
let pim = this.get('store').normalize('page-container', pi);
pim = this.get('store').push(pim);
data.pushObject(pim);
});
return data;
}).catch((error) => {
return error;
});
}
2016-07-07 18:54:16 -07:00
});
2016-08-16 13:34:09 +02:00
function isObject(a) {
return (!!a) && (a.constructor === Object);
}