2016-10-23 18:33:07 -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';
|
|
|
|
|
|
|
|
const {
|
|
|
|
inject: { service }
|
|
|
|
} = Ember;
|
|
|
|
|
|
|
|
export default Ember.Service.extend({
|
|
|
|
sessionService: service('session'),
|
|
|
|
ajax: service(),
|
|
|
|
appMeta: service(),
|
2016-10-26 17:31:05 -07:00
|
|
|
store: service(),
|
2016-10-23 18:33:07 -07:00
|
|
|
|
2016-11-11 17:42:49 -08:00
|
|
|
// Returns links within specified document
|
|
|
|
getDocumentLinks(documentId) {
|
|
|
|
return this.get('ajax').request(`documents/${documentId}/links`, {
|
|
|
|
method: "GET"
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2016-10-23 18:33:07 -07:00
|
|
|
// Returns candidate links using provided parameters
|
2016-10-27 13:40:54 -07:00
|
|
|
getCandidates(folderId, documentId, pageId) {
|
2016-10-26 17:31:05 -07:00
|
|
|
return this.get('ajax').request(`links/${folderId}/${documentId}/${pageId}`, {
|
2016-10-23 18:33:07 -07:00
|
|
|
method: 'GET'
|
|
|
|
}).then((response) => {
|
|
|
|
return response;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2016-10-27 13:40:54 -07:00
|
|
|
// Returns keyword-based candidates
|
|
|
|
searchCandidates(keywords) {
|
|
|
|
let url = "links?keywords=" + encodeURIComponent(keywords);
|
|
|
|
|
|
|
|
return this.get('ajax').request(url, {
|
|
|
|
method: 'GET'
|
|
|
|
}).then((response) => {
|
|
|
|
return response;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
// getUsers returns all users for organization.
|
|
|
|
find(keywords) {
|
|
|
|
let url = "search?keywords=" + encodeURIComponent(keywords);
|
|
|
|
|
|
|
|
return this.get('ajax').request(url, {
|
|
|
|
method: "GET"
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2016-10-23 18:33:07 -07:00
|
|
|
buildLink(link) {
|
|
|
|
let result = "";
|
|
|
|
let href = "";
|
|
|
|
let endpoint = this.get('appMeta').get('endpoint');
|
|
|
|
let orgId = this.get('appMeta').get('orgId');
|
|
|
|
|
2016-11-11 17:42:49 -08:00
|
|
|
if (link.linkType === "section" || link.linkType === "tab" || link.linkType === "document") {
|
2016-10-23 18:33:07 -07:00
|
|
|
href = `/link/${link.linkType}/${link.id}`;
|
2016-10-27 15:44:40 -07:00
|
|
|
result = `<a data-documize='true' data-link-space-id='${link.folderId}' data-link-id='${link.id}' data-link-target-document-id='${link.documentId}' data-link-target-id='${link.targetId}' data-link-type='${link.linkType}' href='${href}'>${link.title}</a>`;
|
2016-10-23 18:33:07 -07:00
|
|
|
}
|
|
|
|
if (link.linkType === "file") {
|
2016-10-25 15:56:08 -07:00
|
|
|
href = `${endpoint}/public/attachments/${orgId}/${link.targetId}`;
|
2016-10-27 15:44:40 -07:00
|
|
|
result = `<a data-documize='true' data-link-space-id='${link.folderId}' data-link-id='${link.id}' data-link-target-document-id='${link.documentId}' data-link-target-id='${link.targetId}' data-link-type='${link.linkType}' href='${href}'>${link.title}</a>`;
|
2016-10-23 18:33:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2016-10-26 17:31:05 -07:00
|
|
|
},
|
|
|
|
|
2016-10-27 15:44:40 -07:00
|
|
|
getLinkObject(outboundLinks, a) {
|
2016-10-26 17:31:05 -07:00
|
|
|
let link = {
|
|
|
|
linkId: a.attributes["data-link-id"].value,
|
|
|
|
linkType: a.attributes["data-link-type"].value,
|
2016-10-27 15:44:40 -07:00
|
|
|
documentId: a.attributes["data-link-target-document-id"].value,
|
2016-10-26 17:31:05 -07:00
|
|
|
folderId: a.attributes["data-link-space-id"].value,
|
|
|
|
targetId: a.attributes["data-link-target-id"].value,
|
|
|
|
url: a.attributes["href"].value,
|
|
|
|
orphan: false
|
|
|
|
};
|
|
|
|
|
|
|
|
link.orphan = _.isEmpty(link.linkId) || _.isEmpty(link.documentId) || _.isEmpty(link.folderId) || _.isEmpty(link.targetId);
|
|
|
|
|
2016-10-27 15:44:40 -07:00
|
|
|
// we check latest state of link using database data
|
|
|
|
let existing = outboundLinks.findBy('id', link.linkId);
|
|
|
|
|
|
|
|
if (_.isUndefined(existing)) {
|
|
|
|
link.orphan = true;
|
|
|
|
} else {
|
|
|
|
link.orphan = existing.orphan;
|
|
|
|
}
|
|
|
|
|
2016-10-26 17:31:05 -07:00
|
|
|
return link;
|
|
|
|
},
|
|
|
|
|
|
|
|
linkClick(doc, link) {
|
|
|
|
if (link.orphan) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let router = this.get('router');
|
|
|
|
let targetFolder = this.get('store').peekRecord('folder', link.folderId);
|
|
|
|
let targetDocument = this.get('store').peekRecord('document', link.documentId);
|
|
|
|
let folderSlug = is.null(targetFolder) ? "s" : targetFolder.get('slug');
|
|
|
|
let documentSlug = is.null(targetDocument) ? "d" : targetDocument.get('slug');
|
|
|
|
|
|
|
|
// handle section link
|
2017-03-07 20:25:13 +00:00
|
|
|
if (link.linkType === "section" || link.linkType === "tab") {
|
2016-10-26 17:31:05 -07:00
|
|
|
let options = {};
|
2017-03-07 20:25:13 +00:00
|
|
|
options['pageId'] = link.targetId;
|
2016-10-26 17:31:05 -07:00
|
|
|
router.transitionTo('document', link.folderId, folderSlug, link.documentId, documentSlug, { queryParams: options });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// handle document link
|
2016-10-27 13:40:54 -07:00
|
|
|
if (link.linkType === "document") {
|
2016-10-26 17:31:05 -07:00
|
|
|
router.transitionTo('document', link.folderId, folderSlug, link.documentId, documentSlug);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// handle attachment links
|
|
|
|
if (link.linkType === "file") {
|
|
|
|
window.location.href = link.url;
|
|
|
|
return;
|
|
|
|
}
|
2016-10-23 18:33:07 -07:00
|
|
|
}
|
|
|
|
});
|