1
0
Fork 0
mirror of https://github.com/documize/community.git synced 2025-07-19 21:29:42 +02:00
documize/gui/app/components/document/view-content.js
2018-06-22 12:44:20 +01:00

189 lines
5.1 KiB
JavaScript

// 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 $ from 'jquery';
import { notEmpty } from '@ember/object/computed';
import { inject as service } from '@ember/service';
import { computed } from '@ember/object';
import TooltipMixin from '../../mixins/tooltip';
import Notifier from '../../mixins/notifier';
import Component from '@ember/component';
export default Component.extend(TooltipMixin, Notifier, {
documentService: service('document'),
sectionService: service('section'),
store: service(),
appMeta: service(),
link: service(),
hasPages: notEmpty('pages'),
showInsertSectionModal: false,
newSectionLocation: '',
toEdit: '',
canEdit: computed('permissions', 'document.protection', function() {
let canEdit = this.get('document.protection') !== this.get('constants').ProtectionType.Lock && this.get('permissions.documentEdit');
return canEdit;
}),
voteThanks: false,
showLikes: false,
showDeleteBlockDialog: false,
deleteBlockId: '',
didReceiveAttrs() {
this._super(...arguments);
this.set('showLikes', this.get('folder.allowLikes') && this.get('document.isLive'));
},
didRender() {
this._super(...arguments);
this.contentLinkHandler();
},
didInsertElement() {
this._super(...arguments);
if (this.get('session.authenticated')) {
this.renderTooltips();
}
this.jumpToSection(this.get('currentPageId'));
},
willDestroyElement() {
this._super(...arguments);
if (this.get('session.authenticated')) {
this.removeTooltips();
}
},
contentLinkHandler() {
let links = this.get('link');
let doc = this.get('document');
let self = this;
$("a[data-documize='true']").off('click').on('click', function (e) {
let link = links.getLinkObject(self.get('links'), this);
// local link? exists?
if ((link.linkType === "section" || link.linkType === "tab") && link.documentId === doc.get('id')) {
let exists = self.get('pages').findBy('id', link.targetId);
if (_.isUndefined(exists)) {
link.orphan = true;
} else {
if (link.linkType === "section") {
self.jumpToSection(link.targetId);
}
}
}
if (link.orphan) {
$(this).addClass('broken-link');
e.preventDefault();
e.stopPropagation();
return false;
}
links.linkClick(doc, link);
return false;
});
},
jumpToSection(cp) {
if (is.not.empty(cp) && is.not.undefined(cp) && is.not.null(cp)) {
this.get('browser').scrollTo(`#page-${cp}`)
}
},
addSection(model) {
let constants = this.get('constants');
if (this.get('document.protection') === constants.ProtectionType.Review) {
model.page.set('status', model.page.get('relativeId') === '' ? constants.ChangeState.PendingNew : constants.ChangeState.Pending);
}
return this.get('onInsertSection')(model);
},
actions: {
onSavePageAsBlock(block) {
let cb = this.get('onSavePageAsBlock');
const promise = cb(block);
promise.then(() => {
let refresh = this.get('refresh');
refresh();
});
},
onCopyPage(pageId, documentId) {
let cb = this.get('onCopyPage');
cb(pageId, documentId);
},
onMovePage(pageId, documentId) {
let cb = this.get('onMovePage');
cb(pageId, documentId);
},
onDeletePage(params) {
let cb = this.get('onDeletePage');
cb(params);
},
onSavePage(page, meta) {
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', '');
this.get('onSavePage')(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', '');
this.get('onSavePage')(page, meta);
}
break;
case constants.ProtectionType.None:
// for un-protected documents, edits welcome!
this.set('toEdit', '');
this.get('onSavePage')(page, meta);
break;
}
},
onShowSectionWizard(beforePage) {
this.set('newSectionLocation', beforePage);
this.set('showInsertSectionModal', true)
},
onShowDeleteBlockModal(id) {
this.set('deleteBlockId', id);
this.set('showDeleteBlockDialog', true);
},
onVote(vote) {
this.get('documentService').vote(this.get('document.id'), vote);
this.set('voteThanks', true);
}
}
});