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
|
|
|
|
|
2018-01-22 10:31:03 +00:00
|
|
|
import $ from 'jquery';
|
2017-11-16 13:28:05 +00:00
|
|
|
import { notEmpty, empty } from '@ember/object/computed';
|
|
|
|
import { inject as service } from '@ember/service';
|
2018-01-22 10:31:03 +00:00
|
|
|
import { computed } from '@ember/object';
|
2017-11-16 13:28:05 +00:00
|
|
|
import Component from '@ember/component';
|
2016-07-07 18:54:16 -07:00
|
|
|
import TooltipMixin from '../../mixins/tooltip';
|
2018-01-22 10:31:03 +00:00
|
|
|
import models from '../../utils/model';
|
2016-07-07 18:54:16 -07:00
|
|
|
|
2017-12-15 16:14:44 +00:00
|
|
|
export default Component.extend(TooltipMixin, {
|
2017-11-16 13:28:05 +00:00
|
|
|
documentService: service('document'),
|
|
|
|
sectionService: service('section'),
|
|
|
|
appMeta: service(),
|
|
|
|
link: service(),
|
|
|
|
hasPages: notEmpty('pages'),
|
2017-12-15 16:14:44 +00:00
|
|
|
newSectionName: '',
|
2017-11-16 13:28:05 +00:00
|
|
|
newSectionNameMissing: empty('newSectionName'),
|
2017-03-02 20:30:26 +00:00
|
|
|
newSectionLocation: '',
|
2017-12-15 16:14:44 +00:00
|
|
|
beforePage: null,
|
2017-03-02 20:30:26 +00:00
|
|
|
toEdit: '',
|
2017-12-15 18:43:22 +00:00
|
|
|
showDeleteBlockDialog: false,
|
|
|
|
deleteBlockId: '',
|
2018-01-22 10:31:03 +00:00
|
|
|
canEdit: computed('permissions', 'document.protection', function() {
|
|
|
|
let canEdit = this.get('document.protection') !== this.get('constants').ProtectionType.Lock && this.get('permissions.documentEdit');
|
2017-03-02 20:30:26 +00:00
|
|
|
|
2018-02-07 19:16:23 +00:00
|
|
|
// if (canEdit) this.setupAddWizard();
|
2018-01-22 10:31:03 +00:00
|
|
|
return canEdit;
|
|
|
|
}),
|
|
|
|
hasBlocks: computed('blocks', function() {
|
|
|
|
return this.get('blocks.length') > 0;
|
|
|
|
}),
|
2018-02-07 19:16:23 +00:00
|
|
|
mousetrap: null,
|
2018-04-13 11:01:36 +01:00
|
|
|
voteThanks: false,
|
2016-07-07 18:54:16 -07:00
|
|
|
|
2016-10-26 17:31:05 -07:00
|
|
|
didRender() {
|
2017-03-06 18:30:55 +00:00
|
|
|
this._super(...arguments);
|
2017-03-09 12:51:21 +00:00
|
|
|
this.contentLinkHandler();
|
2018-02-07 19:16:23 +00:00
|
|
|
|
|
|
|
let mousetrap = this.get('mousetrap');
|
|
|
|
let msContainer = document.getElementById('new-section-wizard');
|
|
|
|
if (is.null(mousetrap)) mousetrap = new Mousetrap(msContainer);
|
|
|
|
|
|
|
|
mousetrap.bind('esc', () => {
|
|
|
|
this.send('onHideSectionWizard');
|
|
|
|
return false;
|
|
|
|
});
|
2017-03-01 18:46:25 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
didInsertElement() {
|
2017-03-06 18:30:55 +00:00
|
|
|
this._super(...arguments);
|
2017-03-04 17:33:43 +00:00
|
|
|
|
2018-01-22 10:31:03 +00:00
|
|
|
if (this.get('session.authenticated')) {
|
2018-02-07 19:16:23 +00:00
|
|
|
// this.setupAddWizard();
|
2018-01-22 10:31:03 +00:00
|
|
|
this.renderTooltips();
|
2017-12-19 14:00:53 +00:00
|
|
|
}
|
2018-01-25 13:20:22 +00:00
|
|
|
|
|
|
|
this.jumpToSection();
|
2016-10-26 17:31:05 -07:00
|
|
|
},
|
|
|
|
|
2016-07-07 18:54:16 -07:00
|
|
|
willDestroyElement() {
|
2017-03-06 18:30:55 +00:00
|
|
|
this._super(...arguments);
|
2017-10-09 10:56:59 -04:00
|
|
|
|
2018-01-22 10:31:03 +00:00
|
|
|
if (this.get('session.authenticated')) {
|
|
|
|
$('.start-section:not(.start-section-empty-state)').off('.hoverIntent');
|
|
|
|
this.removeTooltips();
|
|
|
|
}
|
2018-02-07 19:16:23 +00:00
|
|
|
|
|
|
|
let mousetrap = this.get('mousetrap');
|
|
|
|
if (is.not.null(mousetrap)) {
|
|
|
|
mousetrap.unbind('esc');
|
2018-04-13 11:01:36 +01:00
|
|
|
}
|
2016-07-07 18:54:16 -07:00
|
|
|
},
|
|
|
|
|
2016-10-26 17:31:05 -07:00
|
|
|
contentLinkHandler() {
|
|
|
|
let links = this.get('link');
|
|
|
|
let doc = this.get('document');
|
|
|
|
let self = this;
|
|
|
|
|
2016-11-09 15:16:44 -08:00
|
|
|
$("a[data-documize='true']").off('click').on('click', function (e) {
|
2016-11-11 17:42:49 -08:00
|
|
|
let link = links.getLinkObject(self.get('links'), this);
|
2016-10-26 17:31:05 -07:00
|
|
|
|
|
|
|
// local link? exists?
|
2016-11-11 17:42:49 -08:00
|
|
|
if ((link.linkType === "section" || link.linkType === "tab") && link.documentId === doc.get('id')) {
|
2017-03-03 20:17:49 +00:00
|
|
|
let exists = self.get('pages').findBy('id', link.targetId);
|
2016-10-26 17:31:05 -07:00
|
|
|
|
2016-10-27 15:44:40 -07:00
|
|
|
if (_.isUndefined(exists)) {
|
|
|
|
link.orphan = true;
|
2016-10-26 17:31:05 -07:00
|
|
|
} else {
|
2016-11-12 13:13:13 -08:00
|
|
|
if (link.linkType === "section") {
|
2018-01-25 13:20:22 +00:00
|
|
|
self.get('currentPageId', link.targetId)
|
|
|
|
self.jumpToSection();
|
|
|
|
// self.get('browser').scrollTo(`#page-${link.targetId}`);
|
2016-11-12 13:13:13 -08:00
|
|
|
}
|
2016-10-26 17:31:05 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-27 15:44:40 -07:00
|
|
|
if (link.orphan) {
|
|
|
|
$(this).addClass('broken-link');
|
2016-10-27 16:53:36 -07:00
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
2016-10-27 15:44:40 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-10-26 17:31:05 -07:00
|
|
|
links.linkClick(doc, link);
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2018-02-07 19:16:23 +00:00
|
|
|
// setupAddWizard() {
|
|
|
|
// schedule('afterRender', () => {
|
|
|
|
// $('.start-section:not(.start-section-empty-state)').off('.hoverIntent');
|
2017-03-22 09:40:34 +00:00
|
|
|
|
2018-02-07 19:16:23 +00:00
|
|
|
// $('.start-section:not(.start-section-empty-state)').hoverIntent({interval: 100, over: function() {
|
|
|
|
// // in
|
|
|
|
// $(this).find('.start-button').velocity("transition.slideDownIn", {duration: 300});
|
|
|
|
// }, out: function() {
|
|
|
|
// // out
|
|
|
|
// $(this).find('.start-button').velocity("transition.slideUpOut", {duration: 300});
|
|
|
|
// } });
|
|
|
|
// });
|
|
|
|
// },
|
2017-03-22 09:40:34 +00:00
|
|
|
|
2017-03-02 20:30:26 +00:00
|
|
|
addSection(model) {
|
|
|
|
let sequence = 0;
|
2017-03-07 16:27:23 +00:00
|
|
|
let level = 1;
|
2017-03-02 20:30:26 +00:00
|
|
|
let beforePage = this.get('beforePage');
|
2018-01-22 10:31:03 +00:00
|
|
|
let constants = this.get('constants');
|
2017-03-02 20:30:26 +00:00
|
|
|
|
2018-01-22 10:31:03 +00:00
|
|
|
// calculate sequence of page (position in document)
|
2017-03-02 20:30:26 +00:00
|
|
|
if (is.not.null(beforePage)) {
|
2017-03-07 16:27:23 +00:00
|
|
|
level = beforePage.get('level');
|
|
|
|
|
2017-03-02 20:30:26 +00:00
|
|
|
// get any page before the beforePage so we can insert this new section between them
|
2018-01-22 10:31:03 +00:00
|
|
|
let index = _.findIndex(this.get('pages'), function(item) { return item.get('page.id') === beforePage.get('id'); });
|
2017-03-02 20:30:26 +00:00
|
|
|
|
2017-03-04 16:54:04 +00:00
|
|
|
if (index !== -1) {
|
|
|
|
let beforeBeforePage = this.get('pages')[index-1];
|
|
|
|
|
|
|
|
if (is.not.undefined(beforeBeforePage)) {
|
2018-01-22 10:31:03 +00:00
|
|
|
sequence = (beforePage.get('sequence') + beforeBeforePage.get('page.sequence')) / 2;
|
2017-03-04 16:54:04 +00:00
|
|
|
} else {
|
|
|
|
sequence = beforePage.get('sequence') / 2;
|
|
|
|
}
|
2018-01-22 10:31:03 +00:00
|
|
|
|
|
|
|
model.page.set('sequence', sequence);
|
|
|
|
model.page.set('level', level);
|
2017-03-02 20:30:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-22 10:31:03 +00:00
|
|
|
if (this.get('document.protection') === constants.ProtectionType.Review) {
|
|
|
|
model.page.set('status', model.page.get('relativeId') === '' ? constants.ChangeState.PendingNew : constants.ChangeState.Pending);
|
|
|
|
}
|
2017-03-02 20:30:26 +00:00
|
|
|
|
|
|
|
this.send('onHideSectionWizard');
|
|
|
|
|
2017-03-04 21:07:39 +00:00
|
|
|
return this.get('onInsertSection')(model);
|
|
|
|
},
|
|
|
|
|
2018-01-25 13:20:22 +00:00
|
|
|
jumpToSection() {
|
|
|
|
let cp = this.get('currentPageId');
|
|
|
|
if (is.not.empty(cp) && is.not.undefined(cp) && is.not.null(cp)) {
|
|
|
|
this.get('browser').scrollTo(`#page-${cp}`)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-07-07 18:54:16 -07:00
|
|
|
actions: {
|
2017-03-02 20:30:26 +00:00
|
|
|
onSavePageAsBlock(block) {
|
2018-01-22 10:31:03 +00:00
|
|
|
let cb = this.get('onSavePageAsBlock');
|
|
|
|
const promise = cb(block);
|
|
|
|
|
2017-03-04 21:07:39 +00:00
|
|
|
promise.then(() => {
|
2018-01-22 10:31:03 +00:00
|
|
|
let refresh = this.get('refresh');
|
|
|
|
refresh();
|
2017-03-04 21:07:39 +00:00
|
|
|
});
|
2017-01-20 14:24:38 -08:00
|
|
|
},
|
|
|
|
|
2017-01-22 14:12:10 -08:00
|
|
|
onCopyPage(pageId, documentId) {
|
2018-01-22 10:31:03 +00:00
|
|
|
let cb = this.get('onCopyPage');
|
|
|
|
cb(pageId, documentId);
|
2017-01-22 14:12:10 -08:00
|
|
|
},
|
|
|
|
|
2017-01-22 15:22:14 -08:00
|
|
|
onMovePage(pageId, documentId) {
|
2018-01-22 10:31:03 +00:00
|
|
|
let cb = this.get('onMovePage');
|
|
|
|
cb(pageId, documentId);
|
2017-01-22 15:22:14 -08:00
|
|
|
},
|
|
|
|
|
2017-03-02 20:30:26 +00:00
|
|
|
onDeletePage(params) {
|
2018-01-22 10:31:03 +00:00
|
|
|
let cb = this.get('onDeletePage');
|
|
|
|
cb(params);
|
2016-07-07 18:54:16 -07:00
|
|
|
},
|
2017-02-28 03:25:06 +00:00
|
|
|
|
|
|
|
onSavePage(page, meta) {
|
2018-01-22 10:31:03 +00:00
|
|
|
let document = this.get('document');
|
|
|
|
let constants = this.get('constants');
|
|
|
|
|
|
|
|
switch (document.get('protection')) {
|
|
|
|
case constants.ProtectionType.Lock:
|
|
|
|
break;
|
|
|
|
case constants.ProtectionType.Review:
|
|
|
|
// detect edits to newly created pending page
|
|
|
|
if (page.get('relativeId') === '' && page.get('status') === constants.ChangeState.PendingNew) {
|
|
|
|
// new page, edits
|
|
|
|
this.set('toEdit', '');
|
|
|
|
let cb = this.get('onSavePage');
|
|
|
|
cb(page, meta);
|
|
|
|
} else if (page.get('relativeId') !== '' && page.get('status') === constants.ChangeState.Published) {
|
|
|
|
// existing page, first edit
|
|
|
|
const promise = this.addSection({ page: page, meta: meta });
|
|
|
|
promise.then((/*id*/) => { this.set('toEdit', ''); });
|
|
|
|
} else if (page.get('relativeId') !== '' && page.get('status') === constants.ChangeState.Pending) {
|
|
|
|
// existing page, subsequent edits
|
|
|
|
this.set('toEdit', '');
|
|
|
|
let cb = this.get('onSavePage');
|
|
|
|
cb(page, meta);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case constants.ProtectionType.None:
|
|
|
|
// for un-protected documents, edits welcome!
|
|
|
|
this.set('toEdit', '');
|
|
|
|
// let cb2 = this.get('onSavePage');
|
|
|
|
// cb2(page, meta);
|
|
|
|
this.attrs.onSavePage(page, meta); // eslint-disable-line ember/no-attrs-in-components
|
2018-04-13 11:01:36 +01:00
|
|
|
|
2018-01-22 10:31:03 +00:00
|
|
|
break;
|
|
|
|
}
|
2017-03-01 18:46:25 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
onShowSectionWizard(page) {
|
2018-01-22 10:31:03 +00:00
|
|
|
if (is.undefined(page)) page = { id: '0' };
|
2017-03-09 17:18:50 +00:00
|
|
|
|
2017-03-04 16:54:04 +00:00
|
|
|
let beforePage = this.get('beforePage');
|
2017-03-02 20:30:26 +00:00
|
|
|
if (is.not.null(beforePage) && $("#new-section-wizard").is(':visible') && beforePage.get('id') === page.id) {
|
2017-03-01 18:46:25 +00:00
|
|
|
this.send('onHideSectionWizard');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-03-02 20:30:26 +00:00
|
|
|
this.set('newSectionLocation', page.id);
|
2017-03-04 16:54:04 +00:00
|
|
|
|
|
|
|
if (page.id === '0') {
|
|
|
|
// this handles add section at the end of the document
|
|
|
|
// because we are not before another page
|
|
|
|
this.set('beforePage', null);
|
|
|
|
} else {
|
|
|
|
this.set('beforePage', page);
|
|
|
|
}
|
2017-03-02 20:30:26 +00:00
|
|
|
|
2017-03-01 18:46:25 +00:00
|
|
|
$("#new-section-wizard").insertAfter(`#add-section-button-${page.id}`);
|
2017-03-22 09:40:34 +00:00
|
|
|
$("#new-section-wizard").velocity("transition.slideDownIn", { duration: 300, complete:
|
2017-03-02 20:30:26 +00:00
|
|
|
function() {
|
|
|
|
$("#new-section-name").focus();
|
|
|
|
}});
|
2017-03-01 18:46:25 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
onHideSectionWizard() {
|
2017-03-02 20:30:26 +00:00
|
|
|
this.set('newSectionLocation', '');
|
|
|
|
this.set('beforePage', null);
|
2017-03-22 09:40:34 +00:00
|
|
|
$("#new-section-wizard").insertAfter('#wizard-placeholder');
|
2017-03-02 20:30:26 +00:00
|
|
|
$("#new-section-wizard").velocity("transition.slideUpOut", { duration: 300 });
|
2017-03-01 18:46:25 +00:00
|
|
|
},
|
|
|
|
|
2017-03-02 20:30:26 +00:00
|
|
|
onInsertSection(section) {
|
|
|
|
let sectionName = this.get('newSectionName');
|
|
|
|
if (is.empty(sectionName)) {
|
|
|
|
$("#new-section-name").focus();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-22 10:31:03 +00:00
|
|
|
let page = models.PageModel.create();
|
|
|
|
page.set('documentId', this.get('document.id'));
|
|
|
|
page.set('title', sectionName);
|
|
|
|
page.set('contentType', section.get('contentType'));
|
|
|
|
page.set('pageType', section.get('pageType'));
|
2017-03-02 20:30:26 +00:00
|
|
|
|
|
|
|
let meta = {
|
|
|
|
documentId: this.get('document.id'),
|
|
|
|
rawBody: "",
|
|
|
|
config: ""
|
|
|
|
};
|
|
|
|
|
|
|
|
let model = {
|
|
|
|
page: page,
|
|
|
|
meta: meta
|
|
|
|
};
|
|
|
|
|
2017-03-04 21:07:39 +00:00
|
|
|
const promise = this.addSection(model);
|
|
|
|
promise.then((id) => {
|
2018-01-22 10:31:03 +00:00
|
|
|
this.set('toEdit', model.page.pageType === 'section' ? id: '');
|
2018-02-07 19:16:23 +00:00
|
|
|
// this.setupAddWizard();
|
2017-03-04 21:07:39 +00:00
|
|
|
});
|
2017-03-01 18:46:25 +00:00
|
|
|
},
|
|
|
|
|
2017-03-02 20:30:26 +00:00
|
|
|
onInsertBlock(block) {
|
|
|
|
let sectionName = this.get('newSectionName');
|
|
|
|
if (is.empty(sectionName)) {
|
|
|
|
$("#new-section-name").focus();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-24 14:00:12 +00:00
|
|
|
let page = models.PageModel.create();
|
|
|
|
page.set('documentId', this.get('document.id'));
|
|
|
|
page.set('title', `${block.get('title')}`);
|
|
|
|
page.set('body', block.get('body'));
|
|
|
|
page.set('contentType', block.get('contentType'));
|
|
|
|
page.set('pageType', block.get('pageType'));
|
|
|
|
page.set('blockId', block.get('id'));
|
2017-03-02 20:30:26 +00:00
|
|
|
|
|
|
|
let meta = {
|
|
|
|
documentId: this.get('document.id'),
|
|
|
|
rawBody: block.get('rawBody'),
|
|
|
|
config: block.get('config'),
|
|
|
|
externalSource: block.get('externalSource')
|
|
|
|
};
|
|
|
|
|
|
|
|
let model = {
|
|
|
|
page: page,
|
|
|
|
meta: meta
|
|
|
|
};
|
|
|
|
|
2017-03-09 17:18:50 +00:00
|
|
|
const promise = this.addSection(model);
|
2018-01-25 16:14:37 +00:00
|
|
|
promise.then((id) => { // eslint-disable-line no-unused-vars
|
2018-02-07 19:16:23 +00:00
|
|
|
// this.setupAddWizard();
|
2017-03-09 17:18:50 +00:00
|
|
|
});
|
2017-03-01 18:46:25 +00:00
|
|
|
},
|
|
|
|
|
2017-12-15 18:43:22 +00:00
|
|
|
onShowDeleteBlockModal(id) {
|
|
|
|
this.set('deleteBlockId', id);
|
|
|
|
this.set('showDeleteBlockDialog', true);
|
|
|
|
},
|
|
|
|
|
|
|
|
onDeleteBlock() {
|
|
|
|
this.set('showDeleteBlockDialog', false);
|
|
|
|
|
|
|
|
let id = this.get('deleteBlockId');
|
2018-01-22 10:31:03 +00:00
|
|
|
|
|
|
|
let cb = this.get('onDeleteBlock');
|
|
|
|
let promise = cb(id);
|
2017-03-04 21:07:39 +00:00
|
|
|
|
|
|
|
promise.then(() => {
|
2017-12-15 18:43:22 +00:00
|
|
|
this.set('deleteBlockId', '');
|
2018-01-22 10:31:03 +00:00
|
|
|
let refresh = this.get('refresh');
|
|
|
|
refresh();
|
2017-03-04 21:07:39 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return true;
|
2018-04-13 11:01:36 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
onVote(vote) {
|
|
|
|
this.get('documentService').vote(this.get('document.id'), vote);
|
|
|
|
this.set('voteThanks', true);
|
2017-10-09 10:56:59 -04:00
|
|
|
}
|
2016-07-07 18:54:16 -07:00
|
|
|
}
|
2016-10-05 15:44:44 -07:00
|
|
|
});
|