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';
|
|
|
|
import NotifierMixin from '../../mixins/notifier';
|
|
|
|
import TooltipMixin from '../../mixins/tooltip';
|
|
|
|
import tocUtil from '../../utils/toc';
|
|
|
|
|
|
|
|
export default Ember.Component.extend(NotifierMixin, TooltipMixin, {
|
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
|
|
|
|
},
|
|
|
|
emptyState: Ember.computed('pages', function () {
|
|
|
|
return this.get('pages.length') === 0;
|
|
|
|
}),
|
|
|
|
|
|
|
|
didReceiveAttrs: function () {
|
2017-03-07 16:10:13 +00:00
|
|
|
this._super(...arguments);
|
|
|
|
|
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
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
didRender: function () {
|
2017-03-07 16:10:13 +00:00
|
|
|
this._super(...arguments);
|
|
|
|
|
2016-11-08 16:10:19 -08:00
|
|
|
if (this.session.authenticated) {
|
|
|
|
this.addTooltip(document.getElementById("toc-up-button"));
|
|
|
|
this.addTooltip(document.getElementById("toc-down-button"));
|
|
|
|
this.addTooltip(document.getElementById("toc-outdent-button"));
|
|
|
|
this.addTooltip(document.getElementById("toc-indent-button"));
|
|
|
|
}
|
2016-07-07 18:54:16 -07:00
|
|
|
},
|
|
|
|
|
2016-11-08 16:10:19 -08:00
|
|
|
didInsertElement() {
|
2017-03-07 16:10:13 +00:00
|
|
|
this._super(...arguments);
|
|
|
|
|
2016-11-08 16:10:19 -08:00
|
|
|
this.eventBus.subscribe('documentPageAdded', this, 'onDocumentPageAdded');
|
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);
|
|
|
|
|
2016-11-08 16:10:19 -08:00
|
|
|
this.eventBus.unsubscribe('documentPageAdded');
|
2016-07-07 18:54:16 -07:00
|
|
|
this.destroyTooltips();
|
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);
|
|
|
|
},
|
2016-07-07 18:54:16 -07:00
|
|
|
|
2016-11-08 16:10:19 -08:00
|
|
|
// Controls what user can do with the toc (left sidebar).
|
|
|
|
// Identifies the target pages.
|
|
|
|
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);
|
|
|
|
|
2016-11-08 16:10:19 -08:00
|
|
|
if (!this.get('isEditor') || 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: {
|
|
|
|
// Page up - above pages shunt down.
|
|
|
|
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
|
|
|
|
2016-11-08 16:10:19 -08:00
|
|
|
// Move down -- pages below shift up.
|
|
|
|
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
|
|
|
|
2016-11-08 16:10:19 -08:00
|
|
|
// Indent - changes a page from H2 to H3, etc.
|
|
|
|
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
|
|
|
|
2016-11-08 16:10:19 -08:00
|
|
|
// Outdent - changes a page from H3 to H2, etc.
|
|
|
|
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
|
|
|
});
|