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
|
|
|
|
|
2017-11-16 13:28:05 +00:00
|
|
|
import { computed } from '@ember/object';
|
2017-12-14 13:25:05 +00:00
|
|
|
import { schedule } from '@ember/runloop';
|
2017-11-16 13:28:05 +00:00
|
|
|
import Component from '@ember/component';
|
2017-10-03 17:52:03 -04:00
|
|
|
import tocUtil from '../../utils/toc';
|
2016-07-07 18:54:16 -07:00
|
|
|
import NotifierMixin from '../../mixins/notifier';
|
|
|
|
|
2017-12-09 12:21:14 +00:00
|
|
|
export default Component.extend(NotifierMixin, {
|
2016-11-08 16:10:19 -08:00
|
|
|
document: {},
|
|
|
|
folder: {},
|
|
|
|
pages: [],
|
2017-03-09 12:51:21 +00:00
|
|
|
currentPageId: "",
|
2016-07-07 18:54:16 -07:00
|
|
|
state: {
|
|
|
|
actionablePage: false,
|
2016-11-08 16:10:19 -08:00
|
|
|
upDisabled: true,
|
|
|
|
downDisabled: true,
|
|
|
|
indentDisabled: true,
|
|
|
|
outdentDisabled: true
|
|
|
|
},
|
2017-11-16 13:28:05 +00:00
|
|
|
emptyState: computed('pages', function () {
|
2016-11-08 16:10:19 -08:00
|
|
|
return this.get('pages.length') === 0;
|
|
|
|
}),
|
2017-12-14 13:25:05 +00:00
|
|
|
isDesktop: false,
|
2016-11-08 16:10:19 -08:00
|
|
|
|
2017-10-03 17:52:03 -04:00
|
|
|
didReceiveAttrs() {
|
2017-03-07 16:10:13 +00:00
|
|
|
this._super(...arguments);
|
2017-09-18 13:02:15 +01:00
|
|
|
|
2016-11-11 16:17:43 -08:00
|
|
|
this.set('showToc', is.not.undefined(this.get('pages')) && this.get('pages').get('length') > 0);
|
2017-03-07 16:10:13 +00:00
|
|
|
|
2017-03-09 12:51:21 +00:00
|
|
|
if (is.not.null(this.get('currentPageId'))) {
|
|
|
|
this.send('onEntryClick', this.get('currentPageId'));
|
2016-11-08 16:10:19 -08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
didInsertElement() {
|
2017-03-07 16:10:13 +00:00
|
|
|
this._super(...arguments);
|
2017-09-18 13:02:15 +01:00
|
|
|
|
2017-12-12 16:21:05 +00:00
|
|
|
this.setSize();
|
|
|
|
|
2016-11-08 16:10:19 -08:00
|
|
|
this.eventBus.subscribe('documentPageAdded', this, 'onDocumentPageAdded');
|
2017-12-12 16:21:05 +00:00
|
|
|
this.eventBus.subscribe('resized', this, 'onResize');
|
2017-12-14 13:25:05 +00:00
|
|
|
|
|
|
|
schedule('afterRender', () => {
|
|
|
|
// let dg = $('#doc-toc').draggabilly({});
|
|
|
|
// this.set('dg', dg);
|
|
|
|
});
|
2016-10-05 17:12:47 -07:00
|
|
|
},
|
2016-07-07 18:54:16 -07:00
|
|
|
|
2016-11-08 16:10:19 -08:00
|
|
|
willDestroyElement() {
|
2017-03-07 16:10:13 +00:00
|
|
|
this._super(...arguments);
|
2017-09-18 13:02:15 +01:00
|
|
|
|
2016-11-08 16:10:19 -08:00
|
|
|
this.eventBus.unsubscribe('documentPageAdded');
|
2017-12-12 16:21:05 +00:00
|
|
|
this.eventBus.unsubscribe('resized');
|
2017-12-14 14:07:40 +00:00
|
|
|
|
|
|
|
let t = '#doc-toc';
|
|
|
|
if (interact.isSet(t)) {
|
|
|
|
interact(t).unset();
|
|
|
|
}
|
2016-11-08 16:10:19 -08:00
|
|
|
},
|
2016-07-07 18:54:16 -07:00
|
|
|
|
2016-11-08 16:10:19 -08:00
|
|
|
onDocumentPageAdded(pageId) {
|
|
|
|
this.send('onEntryClick', pageId);
|
2017-12-12 16:21:05 +00:00
|
|
|
this.setSize();
|
|
|
|
},
|
|
|
|
|
|
|
|
onResize() {
|
|
|
|
this.setSize();
|
|
|
|
},
|
|
|
|
|
|
|
|
setSize() {
|
2017-12-14 13:25:05 +00:00
|
|
|
let isDesktop = $(window).width() >= 1800;
|
|
|
|
this.set('isDesktop', isDesktop);
|
|
|
|
|
|
|
|
if (isDesktop) {
|
|
|
|
let h = $(window).height() - $("#nav-bar").height() - 140;
|
|
|
|
$("#doc-toc").css('max-height', h);
|
|
|
|
|
|
|
|
let i = $("#doc-view").offset();
|
|
|
|
|
|
|
|
if (is.not.undefined(i)) {
|
|
|
|
let l = i.left - 100;
|
|
|
|
if (l > 350) l = 350;
|
|
|
|
$("#doc-toc").width(l);
|
|
|
|
}
|
|
|
|
}
|
2017-12-12 16:21:05 +00:00
|
|
|
|
2017-12-14 13:25:05 +00:00
|
|
|
schedule('afterRender', () => {
|
|
|
|
interact('#doc-toc')
|
|
|
|
.draggable({
|
|
|
|
autoScroll: true,
|
|
|
|
onmove: dragMoveListener,
|
|
|
|
// inertia: true,
|
|
|
|
restrict: {
|
|
|
|
// restriction: ".body",
|
|
|
|
// endOnly: true,
|
|
|
|
// elementRect: { top: 0, left: 0, bottom: 1, right: 1 }
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.resizable({
|
|
|
|
// resize from all edges and corners
|
|
|
|
edges: { left: true, right: true, bottom: true, top: true },
|
|
|
|
// keep the edges inside the parent
|
|
|
|
// restrictEdges: {
|
|
|
|
// outer: 'parent',
|
|
|
|
// endOnly: true,
|
|
|
|
// },
|
|
|
|
// minimum size
|
|
|
|
restrictSize: {
|
|
|
|
min: { width: 250, height: 65 },
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.on('resizemove', function (event) {
|
|
|
|
var target = event.target,
|
|
|
|
x = (parseFloat(target.getAttribute('data-x')) || 0),
|
|
|
|
y = (parseFloat(target.getAttribute('data-y')) || 0);
|
|
|
|
|
|
|
|
// update the element's style
|
|
|
|
target.style.width = event.rect.width + 'px';
|
|
|
|
target.style.height = event.rect.height + 'px';
|
|
|
|
|
|
|
|
// translate when resizing from top or left edges
|
|
|
|
x += event.deltaRect.left;
|
|
|
|
y += event.deltaRect.top;
|
|
|
|
|
|
|
|
target.style.webkitTransform = target.style.transform = 'translate(' + x + 'px,' + y + 'px)';
|
|
|
|
|
|
|
|
target.setAttribute('data-x', x);
|
|
|
|
target.setAttribute('data-y', y);
|
|
|
|
target.style.position = 'fixed';
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
function dragMoveListener (event) {
|
|
|
|
var target = event.target,
|
|
|
|
// keep the dragged position in the data-x/data-y attributes
|
|
|
|
x = (parseFloat(target.getAttribute('data-x')) || 0) + event.dx,
|
|
|
|
y = (parseFloat(target.getAttribute('data-y')) || 0) + event.dy;
|
|
|
|
|
|
|
|
// translate the element
|
|
|
|
target.style.webkitTransform = target.style.transform = 'translate(' + x + 'px, ' + y + 'px)';
|
|
|
|
|
|
|
|
// update the posiion attributes
|
|
|
|
target.setAttribute('data-x', x);
|
|
|
|
target.setAttribute('data-y', y);
|
|
|
|
target.style.position = 'fixed';
|
2017-12-12 16:21:05 +00:00
|
|
|
}
|
2017-12-14 13:25:05 +00:00
|
|
|
|
|
|
|
// this is used later in the resizing and gesture demos
|
|
|
|
window.dragMoveListener = dragMoveListener;
|
2016-11-08 16:10:19 -08:00
|
|
|
},
|
2016-07-07 18:54:16 -07:00
|
|
|
|
2017-12-12 16:21:05 +00:00
|
|
|
// Controls what user can do with the toc (left sidebar)
|
|
|
|
// Identifies the target pages
|
2016-11-08 16:10:19 -08:00
|
|
|
setState(pageId) {
|
2017-03-09 12:51:21 +00:00
|
|
|
this.set('currentPageId', pageId);
|
2016-07-07 18:54:16 -07:00
|
|
|
|
2016-11-08 16:10:19 -08:00
|
|
|
let toc = this.get('pages');
|
|
|
|
let page = _.findWhere(toc, { id: pageId });
|
2016-07-07 18:54:16 -07:00
|
|
|
let state = tocUtil.getState(toc, page);
|
|
|
|
|
2017-09-18 13:02:15 +01:00
|
|
|
if (!this.get('permissions.documentEdit') || is.empty(pageId)) {
|
2016-11-12 12:09:05 -08:00
|
|
|
state.actionablePage = false;
|
|
|
|
state.upDisabled = state.downDisabled = state.indentDisabled = state.outdentDisabled = true;
|
2016-11-08 16:10:19 -08:00
|
|
|
}
|
2016-07-07 18:54:16 -07:00
|
|
|
|
|
|
|
this.set('state', state);
|
2016-11-08 16:10:19 -08:00
|
|
|
},
|
2016-07-07 18:54:16 -07:00
|
|
|
|
2016-11-08 16:10:19 -08:00
|
|
|
actions: {
|
2017-12-12 16:21:05 +00:00
|
|
|
// Page up -- above pages shunt down
|
2016-11-08 16:10:19 -08:00
|
|
|
pageUp() {
|
2016-07-07 18:54:16 -07:00
|
|
|
if (this.get('state.upDisabled')) {
|
2016-11-08 16:10:19 -08:00
|
|
|
return;
|
|
|
|
}
|
2016-07-07 18:54:16 -07:00
|
|
|
|
|
|
|
let state = this.get('state');
|
|
|
|
let pages = this.get('pages');
|
2017-03-09 12:51:21 +00:00
|
|
|
let page = _.findWhere(pages, { id: this.get('currentPageId') });
|
2016-07-07 18:54:16 -07:00
|
|
|
let pendingChanges = tocUtil.moveUp(state, pages, page);
|
|
|
|
|
|
|
|
if (pendingChanges.length > 0) {
|
2017-03-07 16:10:13 +00:00
|
|
|
this.attrs.onPageSequenceChange(pendingChanges);
|
2016-07-07 18:54:16 -07:00
|
|
|
|
2017-03-09 12:51:21 +00:00
|
|
|
this.send('onEntryClick', this.get('currentPageId'));
|
2016-11-08 16:10:19 -08:00
|
|
|
this.showNotification("Moved up");
|
2016-07-07 18:54:16 -07:00
|
|
|
}
|
2016-11-08 16:10:19 -08:00
|
|
|
},
|
2016-07-07 18:54:16 -07:00
|
|
|
|
2017-12-12 16:21:05 +00:00
|
|
|
// Move down -- pages below shift up
|
2016-11-08 16:10:19 -08:00
|
|
|
pageDown() {
|
2016-07-07 18:54:16 -07:00
|
|
|
if (this.get('state.downDisabled')) {
|
2016-11-08 16:10:19 -08:00
|
|
|
return;
|
|
|
|
}
|
2016-07-07 18:54:16 -07:00
|
|
|
|
|
|
|
let state = this.get('state');
|
2016-11-08 16:10:19 -08:00
|
|
|
var pages = this.get('pages');
|
2017-03-09 12:51:21 +00:00
|
|
|
var page = _.findWhere(pages, { id: this.get('currentPageId') });
|
2016-07-07 18:54:16 -07:00
|
|
|
let pendingChanges = tocUtil.moveDown(state, pages, page);
|
|
|
|
|
|
|
|
if (pendingChanges.length > 0) {
|
2017-03-07 16:10:13 +00:00
|
|
|
this.attrs.onPageSequenceChange(pendingChanges);
|
2016-07-07 18:54:16 -07:00
|
|
|
|
2017-03-09 12:51:21 +00:00
|
|
|
this.send('onEntryClick', this.get('currentPageId'));
|
2016-11-08 16:10:19 -08:00
|
|
|
this.showNotification("Moved down");
|
2016-07-07 18:54:16 -07:00
|
|
|
}
|
2016-11-08 16:10:19 -08:00
|
|
|
},
|
2016-07-07 18:54:16 -07:00
|
|
|
|
2017-12-12 16:21:05 +00:00
|
|
|
// Indent -- changes a page from H2 to H3, etc.
|
2016-11-08 16:10:19 -08:00
|
|
|
pageIndent() {
|
2016-07-07 18:54:16 -07:00
|
|
|
if (this.get('state.indentDisabled')) {
|
2016-11-08 16:10:19 -08:00
|
|
|
return;
|
|
|
|
}
|
2016-07-07 18:54:16 -07:00
|
|
|
|
|
|
|
let state = this.get('state');
|
2016-11-08 16:10:19 -08:00
|
|
|
var pages = this.get('pages');
|
2017-03-09 12:51:21 +00:00
|
|
|
var page = _.findWhere(pages, { id: this.get('currentPageId') });
|
2016-07-07 18:54:16 -07:00
|
|
|
let pendingChanges = tocUtil.indent(state, pages, page);
|
|
|
|
|
|
|
|
if (pendingChanges.length > 0) {
|
2017-03-07 16:10:13 +00:00
|
|
|
this.attrs.onPageLevelChange(pendingChanges);
|
2016-07-07 18:54:16 -07:00
|
|
|
|
2016-11-08 16:10:19 -08:00
|
|
|
this.showNotification("Indent");
|
2017-03-09 12:51:21 +00:00
|
|
|
this.send('onEntryClick', this.get('currentPageId'));
|
2016-07-07 18:54:16 -07:00
|
|
|
}
|
2016-11-08 16:10:19 -08:00
|
|
|
},
|
2016-07-07 18:54:16 -07:00
|
|
|
|
2017-12-12 16:21:05 +00:00
|
|
|
// Outdent -- changes a page from H3 to H2, etc.
|
2016-11-08 16:10:19 -08:00
|
|
|
pageOutdent() {
|
|
|
|
if (this.get('state.outdentDisabled')) {
|
|
|
|
return;
|
|
|
|
}
|
2016-07-07 18:54:16 -07:00
|
|
|
|
|
|
|
let state = this.get('state');
|
2016-11-08 16:10:19 -08:00
|
|
|
var pages = this.get('pages');
|
2017-03-09 12:51:21 +00:00
|
|
|
var page = _.findWhere(pages, { id: this.get('currentPageId') });
|
2016-07-07 18:54:16 -07:00
|
|
|
let pendingChanges = tocUtil.outdent(state, pages, page);
|
|
|
|
|
|
|
|
if (pendingChanges.length > 0) {
|
2017-03-07 16:10:13 +00:00
|
|
|
this.attrs.onPageLevelChange(pendingChanges);
|
2016-07-07 18:54:16 -07:00
|
|
|
|
2016-11-08 16:10:19 -08:00
|
|
|
this.showNotification("Outdent");
|
2017-03-09 12:51:21 +00:00
|
|
|
this.send('onEntryClick', this.get('currentPageId'));
|
2016-07-07 18:54:16 -07:00
|
|
|
}
|
2016-11-08 16:10:19 -08:00
|
|
|
},
|
2016-07-07 18:54:16 -07:00
|
|
|
|
2016-11-08 16:10:19 -08:00
|
|
|
onEntryClick(id) {
|
|
|
|
this.setState(id);
|
2017-03-07 16:10:13 +00:00
|
|
|
this.attrs.onGotoPage(id);
|
2016-11-10 15:37:16 -08:00
|
|
|
}
|
|
|
|
}
|
2016-07-07 18:54:16 -07:00
|
|
|
});
|