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
|
|
|
|
|
2017-11-16 13:28:05 +00:00
|
|
|
import Service, { inject as service } from '@ember/service';
|
2018-07-09 14:41:55 -04:00
|
|
|
import Notifier from '../mixins/notifier';
|
2016-10-23 18:33:07 -07:00
|
|
|
|
2018-07-09 14:41:55 -04:00
|
|
|
export default Service.extend(Notifier, {
|
2018-12-21 17:38:55 +00:00
|
|
|
session: service('session'),
|
2016-10-23 18:33:07 -07:00
|
|
|
ajax: service(),
|
|
|
|
appMeta: service(),
|
2016-10-26 17:31:05 -07:00
|
|
|
store: service(),
|
2018-07-09 14:41:55 -04:00
|
|
|
eventBus: 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}`;
|
2018-10-23 13:47:13 +01:00
|
|
|
result = `<a data-documize='true' data-link-space-id='${link.spaceId}' 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") {
|
2019-01-16 16:55:43 +00:00
|
|
|
href = `${endpoint}/public/attachment/${orgId}/${link.targetId}`;
|
2018-10-23 13:47:13 +01:00
|
|
|
result = `<a data-documize='true' data-link-space-id='${link.spaceId}' 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
|
|
|
}
|
2018-07-09 14:41:55 -04:00
|
|
|
if (link.linkType === "network") {
|
|
|
|
href = `fileto://${link.externalId}`;
|
2018-10-23 13:47:13 +01:00
|
|
|
result = `<a data-documize='true' data-link-space-id='${link.spaceId}' data-link-id='${link.id}' data-link-target-document-id='${link.documentId}' data-link-target-id='${link.targetId}' data-link-external-id='${link.externalId}' data-link-type='${link.linkType}' href='${href}'>${link.title}</a>`;
|
2018-07-09 14:41:55 -04:00
|
|
|
}
|
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,
|
2018-10-12 17:54:15 +01:00
|
|
|
spaceId: a.attributes["data-link-space-id"].value,
|
2016-10-26 17:31:05 -07:00
|
|
|
targetId: a.attributes["data-link-target-id"].value,
|
2019-03-03 13:10:04 +00:00
|
|
|
externalId: _.isUndefined(a.attributes["data-link-external-id"]) ? '' : a.attributes["data-link-external-id"].value,
|
2016-10-26 17:31:05 -07:00
|
|
|
url: a.attributes["href"].value,
|
|
|
|
orphan: false
|
|
|
|
};
|
|
|
|
|
2018-10-23 13:47:13 +01:00
|
|
|
link.orphan = _.isEmpty(link.linkId) || _.isEmpty(link.documentId) || _.isEmpty(link.spaceId) || (_.isEmpty(link.targetId) && _.isEmpty(link.externalId));
|
2016-10-26 17:31:05 -07:00
|
|
|
|
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');
|
2018-10-23 13:47:13 +01:00
|
|
|
let targetFolder = this.get('store').peekRecord('folder', link.spaceId);
|
2016-10-26 17:31:05 -07:00
|
|
|
let targetDocument = this.get('store').peekRecord('document', link.documentId);
|
2019-03-03 13:10:04 +00:00
|
|
|
let folderSlug = _.isNull(targetFolder) ? "s" : targetFolder.get('slug');
|
|
|
|
let documentSlug = _.isNull(targetDocument) ? "d" : targetDocument.get('slug');
|
2016-10-26 17:31:05 -07:00
|
|
|
|
|
|
|
// 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;
|
2018-10-23 13:47:13 +01:00
|
|
|
router.transitionTo('document', link.spaceId, folderSlug, link.documentId, documentSlug, { queryParams: options });
|
2016-10-26 17:31:05 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// handle document link
|
2016-10-27 13:40:54 -07:00
|
|
|
if (link.linkType === "document") {
|
2018-10-23 13:47:13 +01:00
|
|
|
router.transitionTo('document', link.spaceId, folderSlug, link.documentId, documentSlug);
|
2016-10-26 17:31:05 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// handle attachment links
|
|
|
|
if (link.linkType === "file") {
|
2019-01-16 16:55:43 +00:00
|
|
|
// For authenticated users we send server auth token.
|
|
|
|
let qry = '';
|
|
|
|
if (this.get('session.hasSecureToken')) {
|
|
|
|
qry = '?secure=' + this.get('session.secureToken');
|
|
|
|
} else if (this.get('session.authenticated')) {
|
|
|
|
qry = '?token=' + this.get('session.authToken');
|
|
|
|
}
|
|
|
|
|
2018-12-21 17:38:55 +00:00
|
|
|
link.url = link.url.replace('attachments/', 'attachment/');
|
2019-01-16 16:55:43 +00:00
|
|
|
window.location.href = link.url + qry;
|
2016-10-26 17:31:05 -07:00
|
|
|
return;
|
|
|
|
}
|
2018-07-09 14:41:55 -04:00
|
|
|
|
|
|
|
// handle network share/drive links
|
|
|
|
if (link.linkType === "network") {
|
|
|
|
// window.location.href = link.externalId;
|
|
|
|
const el = document.createElement('textarea');
|
|
|
|
el.value = link.externalId;
|
|
|
|
el.setAttribute('readonly', '');
|
|
|
|
el.style.position = 'absolute';
|
|
|
|
el.style.left = '-9999px';
|
|
|
|
document.body.appendChild(el);
|
|
|
|
el.select();
|
|
|
|
document.execCommand('copy');
|
|
|
|
document.body.removeChild(el);
|
|
|
|
|
2018-12-05 13:44:10 +00:00
|
|
|
this.notifyInfo('Copied location to clipboard');
|
2018-07-09 14:41:55 -04:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2016-10-23 18:33:07 -07:00
|
|
|
}
|
|
|
|
});
|