1
0
Fork 0
mirror of https://github.com/documize/community.git synced 2025-07-19 05:09:42 +02:00
documize/gui/app/components/document/view-content.js

313 lines
7.4 KiB
JavaScript
Raw Normal View History

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 { notEmpty, empty } from '@ember/object/computed';
import { schedule } from '@ember/runloop';
import { inject as service } from '@ember/service';
import Component from '@ember/component';
2016-07-07 18:54:16 -07:00
import TooltipMixin from '../../mixins/tooltip';
2017-12-15 16:14:44 +00:00
export default Component.extend(TooltipMixin, {
documentService: service('document'),
sectionService: service('section'),
appMeta: service(),
link: service(),
hasPages: notEmpty('pages'),
2017-12-15 16:14:44 +00:00
newSectionName: '',
newSectionNameMissing: empty('newSectionName'),
newSectionLocation: '',
2017-12-15 16:14:44 +00:00
beforePage: null,
toEdit: '',
2017-12-15 18:43:22 +00:00
showDeleteBlockDialog: false,
deleteBlockId: '',
2017-03-01 18:46:25 +00:00
didReceiveAttrs() {
this._super(...arguments);
2017-03-04 21:07:39 +00:00
this.loadBlocks();
2017-03-01 18:46:25 +00:00
},
2016-07-07 18:54:16 -07:00
2016-10-26 17:31:05 -07:00
didRender() {
this._super(...arguments);
2017-03-09 12:51:21 +00:00
this.contentLinkHandler();
2017-03-01 18:46:25 +00:00
},
didInsertElement() {
this._super(...arguments);
this.setupAddWizard();
2017-03-04 17:33:43 +00:00
if (this.attrs.onGotoPage !== null) {
this.attrs.onGotoPage(this.get('pageId'));
}
2017-12-14 16:48:12 +00:00
2017-12-09 12:21:14 +00:00
this.renderTooltips();
2016-10-26 17:31:05 -07:00
},
2016-07-07 18:54:16 -07:00
willDestroyElement() {
this._super(...arguments);
$('.start-section:not(.start-section-empty-state)').off('.hoverIntent');
2017-10-09 10:56:59 -04:00
2017-12-09 12:21:14 +00:00
this.removeTooltips();
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") {
2017-05-10 08:13:53 +01:00
self.attrs.onGotoPage(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;
});
},
setupAddWizard() {
schedule('afterRender', () => {
$('.start-section:not(.start-section-empty-state)').off('.hoverIntent');
$('.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-10-09 10:56:59 -04:00
});
},
addSection(model) {
// calculate sequence of page (position in document)
let sequence = 0;
let level = 1;
let beforePage = this.get('beforePage');
if (is.not.null(beforePage)) {
level = beforePage.get('level');
// get any page before the beforePage so we can insert this new section between them
let index = _.findIndex(this.get('pages'), function(p) { return p.get('id') === beforePage.get('id'); });
2017-03-04 16:54:04 +00:00
if (index !== -1) {
let beforeBeforePage = this.get('pages')[index-1];
if (is.not.undefined(beforeBeforePage)) {
sequence = (beforePage.get('sequence') + beforeBeforePage.get('sequence')) / 2;
} else {
sequence = beforePage.get('sequence') / 2;
}
}
}
model.page.sequence = sequence;
model.page.level = level;
this.send('onHideSectionWizard');
2017-03-04 21:07:39 +00:00
return this.get('onInsertSection')(model);
},
loadBlocks() {
2017-12-19 16:52:40 +00:00
if (is.not.undefined(this.get('folder'))) {
this.get('sectionService').getSpaceBlocks(this.get('folder.id')).then((blocks) => {
if (this.get('isDestroyed') || this.get('isDestroying')) {
return;
}
this.set('blocks', blocks);
this.set('hasBlocks', blocks.get('length') > 0);
});
}
},
2016-07-07 18:54:16 -07:00
actions: {
onSavePageAsBlock(block) {
2017-03-04 21:07:39 +00:00
const promise = this.attrs.onSavePageAsBlock(block);
promise.then(() => {
this.loadBlocks();
});
},
2017-01-22 14:12:10 -08:00
onCopyPage(pageId, documentId) {
this.attrs.onCopyPage(pageId, documentId);
},
onMovePage(pageId, documentId) {
this.attrs.onMovePage(pageId, documentId);
},
onDeletePage(params) {
2016-07-07 18:54:16 -07:00
this.attrs.onDeletePage(params);
},
onSavePage(page, meta) {
this.set('toEdit', '');
this.attrs.onSavePage(page, meta);
2017-03-01 18:46:25 +00:00
},
onShowSectionWizard(page) {
2017-03-04 16:54:04 +00:00
if (is.undefined(page)) {
page = { id: '0' };
}
this.set('pageId', '');
2017-03-04 16:54:04 +00:00
let beforePage = this.get('beforePage');
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;
}
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-01 18:46:25 +00:00
$("#new-section-wizard").insertAfter(`#add-section-button-${page.id}`);
$("#new-section-wizard").velocity("transition.slideDownIn", { duration: 300, complete:
function() {
$("#new-section-name").focus();
}});
2017-03-01 18:46:25 +00:00
},
onHideSectionWizard() {
this.set('newSectionLocation', '');
this.set('beforePage', null);
$("#new-section-wizard").insertAfter('#wizard-placeholder');
$("#new-section-wizard").velocity("transition.slideUpOut", { duration: 300 });
2017-03-01 18:46:25 +00:00
},
onInsertSection(section) {
let sectionName = this.get('newSectionName');
if (is.empty(sectionName)) {
$("#new-section-name").focus();
return;
}
let page = {
documentId: this.get('document.id'),
title: sectionName,
level: 1,
sequence: 0, // calculated elsewhere
body: "",
contentType: section.get('contentType'),
pageType: section.get('pageType')
};
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) => {
this.set('pageId', id);
2017-03-04 21:07:39 +00:00
if (model.page.pageType === 'section') {
this.set('toEdit', id);
} else {
this.set('toEdit', '');
2017-03-04 21:07:39 +00:00
}
2017-10-09 10:56:59 -04:00
this.setupAddWizard();
2017-03-04 21:07:39 +00:00
});
2017-03-01 18:46:25 +00:00
},
onInsertBlock(block) {
let sectionName = this.get('newSectionName');
if (is.empty(sectionName)) {
$("#new-section-name").focus();
return;
}
let page = {
documentId: this.get('document.id'),
title: `${block.get('title')}`,
level: 1,
sequence: 0, // calculated elsewhere
body: block.get('body'),
contentType: block.get('contentType'),
pageType: block.get('pageType'),
blockId: block.get('id')
};
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
};
const promise = this.addSection(model);
promise.then((id) => {
this.set('pageId', id);
2017-10-09 10:56:59 -04:00
this.setupAddWizard();
});
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');
2017-03-04 21:07:39 +00:00
const promise = this.attrs.onDeleteBlock(id);
promise.then(() => {
2017-12-15 18:43:22 +00:00
this.set('deleteBlockId', '');
2017-03-04 21:07:39 +00:00
this.loadBlocks();
});
return true;
2017-10-09 10:56:59 -04:00
}
2016-07-07 18:54:16 -07:00
}
});