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 { debounce } from '@ember/runloop';
|
|
|
|
import { computed, set } from '@ember/object';
|
|
|
|
import Component from '@ember/component';
|
|
|
|
import { inject as service } from '@ember/service';
|
|
|
|
import TooltipMixin from '../../mixins/tooltip';
|
2016-10-23 18:33:07 -07:00
|
|
|
|
2017-11-16 13:28:05 +00:00
|
|
|
export default Component.extend(TooltipMixin, {
|
2016-10-23 18:33:07 -07:00
|
|
|
link: service(),
|
|
|
|
linkName: '',
|
2016-10-24 19:20:29 -07:00
|
|
|
keywords: '',
|
2016-10-23 18:33:07 -07:00
|
|
|
selection: null,
|
2016-10-27 13:40:54 -07:00
|
|
|
matches: {
|
|
|
|
documents: [],
|
|
|
|
pages: [],
|
|
|
|
attachments: []
|
|
|
|
},
|
2016-10-26 17:31:05 -07:00
|
|
|
tabs: [
|
|
|
|
{ label: 'Section', selected: true },
|
|
|
|
{ label: 'Attachment', selected: false },
|
|
|
|
{ label: 'Search', selected: false }
|
|
|
|
],
|
2017-11-16 13:28:05 +00:00
|
|
|
contentLinkerButtonId: computed('page', function () {
|
2017-02-28 17:50:03 +00:00
|
|
|
let page = this.get('page');
|
|
|
|
return `content-linker-button-${page.id}`;
|
|
|
|
}),
|
2016-10-26 17:31:05 -07:00
|
|
|
|
2017-11-16 13:28:05 +00:00
|
|
|
showSections: computed('tabs.@each.selected', function () {
|
2016-10-26 17:31:05 -07:00
|
|
|
return this.get('tabs').findBy('label', 'Section').selected;
|
|
|
|
}),
|
2017-11-16 13:28:05 +00:00
|
|
|
showAttachments: computed('tabs.@each.selected', function () {
|
2016-10-26 17:31:05 -07:00
|
|
|
return this.get('tabs').findBy('label', 'Attachment').selected;
|
|
|
|
}),
|
2017-11-16 13:28:05 +00:00
|
|
|
showSearch: computed('tabs.@each.selected', function () {
|
2016-10-26 17:31:05 -07:00
|
|
|
return this.get('tabs').findBy('label', 'Search').selected;
|
|
|
|
}),
|
2017-11-16 13:28:05 +00:00
|
|
|
hasMatches: computed('matches', function () {
|
2016-10-27 13:40:54 -07:00
|
|
|
let m = this.get('matches');
|
|
|
|
return m.documents.length || m.pages.length || m.attachments.length;
|
|
|
|
}),
|
|
|
|
|
2016-10-23 18:33:07 -07:00
|
|
|
init() {
|
|
|
|
this._super(...arguments);
|
|
|
|
let self = this;
|
|
|
|
|
2016-10-26 17:31:05 -07:00
|
|
|
let folderId = this.get('folder.id');
|
2016-10-23 18:33:07 -07:00
|
|
|
let documentId = this.get('document.id');
|
|
|
|
let pageId = this.get('page.id');
|
|
|
|
|
2016-10-26 17:31:05 -07:00
|
|
|
this.get('link').getCandidates(folderId, documentId, pageId).then(function (candidates) {
|
2016-10-23 18:33:07 -07:00
|
|
|
self.set('candidates', candidates);
|
|
|
|
self.set('hasSections', is.not.null(candidates.pages) && candidates.pages.length);
|
|
|
|
self.set('hasAttachments', is.not.null(candidates.attachments) && candidates.attachments.length);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2016-10-24 19:20:29 -07:00
|
|
|
didRender() {
|
|
|
|
this.addTooltip(document.getElementById("content-linker-button"));
|
2016-10-28 19:21:54 -07:00
|
|
|
this.addTooltip(document.getElementById("content-counter-button"));
|
2016-10-24 19:20:29 -07:00
|
|
|
},
|
2016-10-23 18:33:07 -07:00
|
|
|
|
2016-10-24 19:20:29 -07:00
|
|
|
willDestroyElement() {
|
|
|
|
this.destroyTooltips();
|
|
|
|
},
|
2016-10-23 18:33:07 -07:00
|
|
|
|
2016-10-27 13:40:54 -07:00
|
|
|
onKeywordChange: function () {
|
2017-11-16 13:28:05 +00:00
|
|
|
debounce(this, this.fetch, 750);
|
2016-10-27 13:40:54 -07:00
|
|
|
}.observes('keywords'),
|
|
|
|
|
|
|
|
fetch() {
|
|
|
|
let keywords = this.get('keywords');
|
|
|
|
let self = this;
|
|
|
|
|
|
|
|
if (_.isEmpty(keywords)) {
|
|
|
|
this.set('matches', { documents: [], pages: [], attachments: [] });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.get('link').searchCandidates(keywords).then(function (matches) {
|
|
|
|
self.set('matches', matches);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2016-10-23 18:33:07 -07:00
|
|
|
actions: {
|
2016-10-24 19:20:29 -07:00
|
|
|
setSelection(i) {
|
|
|
|
let candidates = this.get('candidates');
|
2016-10-27 13:40:54 -07:00
|
|
|
let matches = this.get('matches');
|
|
|
|
|
|
|
|
this.set('selection', i);
|
2016-10-24 19:20:29 -07:00
|
|
|
|
|
|
|
candidates.pages.forEach(c => {
|
2017-11-16 13:28:05 +00:00
|
|
|
set(c, 'selected', c.id === i.id);
|
2016-10-24 19:20:29 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
candidates.attachments.forEach(c => {
|
2017-11-16 13:28:05 +00:00
|
|
|
set(c, 'selected', c.id === i.id);
|
2016-10-24 19:20:29 -07:00
|
|
|
});
|
2016-10-27 13:40:54 -07:00
|
|
|
|
|
|
|
matches.documents.forEach(c => {
|
2017-11-16 13:28:05 +00:00
|
|
|
set(c, 'selected', c.id === i.id);
|
2016-10-27 13:40:54 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
matches.pages.forEach(c => {
|
2017-11-16 13:28:05 +00:00
|
|
|
set(c, 'selected', c.id === i.id);
|
2016-10-27 13:40:54 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
matches.attachments.forEach(c => {
|
2017-11-16 13:28:05 +00:00
|
|
|
set(c, 'selected', c.id === i.id);
|
2016-10-27 13:40:54 -07:00
|
|
|
});
|
2016-10-24 19:20:29 -07:00
|
|
|
},
|
|
|
|
|
2016-10-23 18:33:07 -07:00
|
|
|
onInsertLink() {
|
|
|
|
let selection = this.get('selection');
|
|
|
|
|
2016-10-24 19:20:29 -07:00
|
|
|
if (is.null(selection)) {
|
|
|
|
return;
|
2016-10-23 18:33:07 -07:00
|
|
|
}
|
|
|
|
|
2016-10-24 19:20:29 -07:00
|
|
|
return this.get('onInsertLink')(selection);
|
2016-10-26 17:31:05 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
onTabSelect(tabs) {
|
|
|
|
this.set('tabs', tabs);
|
2016-10-23 18:33:07 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|