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';
|
2016-08-09 13:16:29 +02:00
|
|
|
|
|
|
|
const {
|
|
|
|
inject: { service }
|
|
|
|
} = Ember;
|
2016-07-07 18:54:16 -07:00
|
|
|
|
|
|
|
export default Ember.Service.extend({
|
2016-08-09 13:16:29 +02:00
|
|
|
sessionService: service('session'),
|
|
|
|
ajax: service(),
|
|
|
|
store: service(),
|
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) => {
|
|
|
|
let documents = Ember.ArrayProxy.create({
|
|
|
|
content: Ember.A([])
|
|
|
|
});
|
|
|
|
|
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;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
// getDocumentsByTag returns all documents for specified tag (not folder!).
|
|
|
|
getAllByTag(tag) {
|
|
|
|
return this.get('ajax').request(`documents?filter=tag&tag=${tag}`, {
|
|
|
|
method: "GET"
|
|
|
|
}).then((response) => {
|
|
|
|
let documents = Ember.ArrayProxy.create({
|
|
|
|
content: Ember.A([])
|
|
|
|
});
|
|
|
|
|
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;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
// 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
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
changePageSequence: function (documentId, payload) {
|
|
|
|
let url = `documents/${documentId}/pages/sequence`;
|
|
|
|
|
|
|
|
return this.get('ajax').post(url, {
|
|
|
|
data: JSON.stringify(payload),
|
|
|
|
contentType: 'json'
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
changePageLevel(documentId, payload) {
|
|
|
|
let url = `documents/${documentId}/pages/level`;
|
|
|
|
|
|
|
|
return this.get('ajax').post(url, {
|
|
|
|
data: JSON.stringify(payload),
|
|
|
|
contentType: 'json'
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
deleteDocument: function (documentId) {
|
|
|
|
let url = `documents/${documentId}`;
|
|
|
|
|
|
|
|
return this.get('ajax').request(url, {
|
|
|
|
method: 'DELETE'
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
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
|
|
|
|
2016-08-29 18:44:30 +02:00
|
|
|
Ember.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);
|
2016-07-07 18:54:16 -07:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
// addPage inserts new page to an existing document.
|
|
|
|
addPage: function (documentId, payload) {
|
|
|
|
let url = `documents/${documentId}/pages`;
|
|
|
|
|
|
|
|
return this.get('ajax').post(url, {
|
|
|
|
data: JSON.stringify(payload),
|
|
|
|
contentType: 'json'
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
// Nukes multiple pages from the document.
|
|
|
|
deletePages: function (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.
|
|
|
|
deletePage: function (documentId, pageId) {
|
|
|
|
let url = `documents/${documentId}/pages/${pageId}`;
|
|
|
|
|
|
|
|
return this.get('ajax').request(url, {
|
|
|
|
method: 'DELETE'
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
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"
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
// document meta referes to number of views, edits, approvals, etc.
|
2017-04-04 17:55:17 +01:00
|
|
|
getActivity(documentId) {
|
|
|
|
return this.get('ajax').request(`documents/${documentId}/activity`, {
|
2016-07-07 18:54:16 -07:00
|
|
|
method: "GET"
|
2017-03-10 11:16:31 +00:00
|
|
|
}).then((response) => {
|
2017-04-04 17:55:17 +01:00
|
|
|
let data = [];
|
|
|
|
data = response.map((obj) => {
|
|
|
|
let data = this.get('store').normalize('documentActivity', obj);
|
|
|
|
return this.get('store').push(data);
|
|
|
|
});
|
|
|
|
|
|
|
|
return data;
|
2017-03-10 11:16:31 +00:00
|
|
|
}).catch(() => {
|
2017-04-04 17:55:17 +01:00
|
|
|
return [];
|
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) => {
|
|
|
|
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;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
// Returns all document pages with content
|
|
|
|
getPages(documentId) {
|
|
|
|
return this.get('ajax').request(`documents/${documentId}/pages`, {
|
|
|
|
method: 'GET'
|
|
|
|
}).then((response) => {
|
|
|
|
let pages = [];
|
|
|
|
|
2016-08-12 14:02:57 +02:00
|
|
|
pages = response.map((page) => {
|
2016-08-09 13:16:29 +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-07-07 18:54:16 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
return pages;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
// Returns document page with content
|
|
|
|
getPage(documentId, pageId) {
|
|
|
|
|
|
|
|
return this.get('ajax').request(`documents/${documentId}/pages/${pageId}`, {
|
|
|
|
method: 'GET'
|
|
|
|
}).then((response) => {
|
2016-08-09 13:16:29 +02:00
|
|
|
let data = this.get('store').normalize('page', response);
|
2016-08-26 13:13:26 +02:00
|
|
|
return this.get('store').push(data);
|
2016-07-07 18:54:16 -07:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
// Returns document page meta object
|
|
|
|
getPageMeta(documentId, pageId) {
|
|
|
|
|
|
|
|
return this.get('ajax').request(`documents/${documentId}/pages/${pageId}/meta`, {
|
|
|
|
method: 'GET'
|
|
|
|
}).then((response) => {
|
2016-08-09 13:16:29 +02:00
|
|
|
let data = this.get('store').normalize('page-meta', response);
|
2016-08-26 13:13:26 +02:00
|
|
|
return this.get('store').push(data);
|
2017-03-10 11:16:31 +00:00
|
|
|
}).catch(() => {
|
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) => {
|
|
|
|
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-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);
|
|
|
|
}
|