2017-02-28 03:25:06 +00: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';
|
2019-04-17 17:13:18 +01:00
|
|
|
import { empty, notEmpty } from '@ember/object/computed';
|
2017-11-16 13:28:05 +00:00
|
|
|
import { computed } from '@ember/object';
|
2019-04-17 17:13:18 +01:00
|
|
|
import { inject as service } from '@ember/service';
|
|
|
|
import Modals from '../../mixins/modal';
|
|
|
|
import Notifier from '../../mixins/notifier';
|
2017-11-16 13:28:05 +00:00
|
|
|
import Component from '@ember/component';
|
2017-02-28 03:25:06 +00:00
|
|
|
|
2019-04-17 17:13:18 +01:00
|
|
|
export default Component.extend(Modals, Notifier, {
|
|
|
|
appMeta: service(),
|
|
|
|
session: service(),
|
|
|
|
documentSvc: service('document'),
|
2017-02-28 03:25:06 +00:00
|
|
|
busy: false,
|
2017-03-22 10:24:30 +00:00
|
|
|
mousetrap: null,
|
2017-12-07 19:43:46 +00:00
|
|
|
showLinkModal: false,
|
2019-04-17 17:13:18 +01:00
|
|
|
files: null,
|
|
|
|
downloadQuery: '',
|
|
|
|
hasAttachments: notEmpty('files'),
|
2017-11-16 13:28:05 +00:00
|
|
|
hasNameError: empty('page.title'),
|
2017-12-08 17:24:39 +00:00
|
|
|
hasDescError: empty('page.excerpt'),
|
2017-11-16 13:28:05 +00:00
|
|
|
pageId: computed('page', function () {
|
2017-02-28 03:25:06 +00:00
|
|
|
let page = this.get('page');
|
|
|
|
return `page-editor-${page.id}`;
|
|
|
|
}),
|
2019-04-17 17:13:18 +01:00
|
|
|
uploadId: computed('page', function () {
|
|
|
|
let page = this.get('page');
|
|
|
|
return `page-uploader-${page.id}`;
|
|
|
|
}),
|
2017-12-08 17:24:39 +00:00
|
|
|
previewText: 'Preview',
|
2018-04-26 12:01:58 +01:00
|
|
|
pageTitle: '',
|
|
|
|
|
|
|
|
didReceiveAttrs() {
|
|
|
|
this._super(...arguments);
|
|
|
|
this.set('pageTitle', this.get('page.title'));
|
|
|
|
},
|
2017-02-28 03:25:06 +00:00
|
|
|
|
|
|
|
didRender() {
|
2018-02-04 15:51:14 +00:00
|
|
|
this._super(...arguments);
|
2018-04-26 12:01:58 +01:00
|
|
|
|
2017-12-07 19:43:46 +00:00
|
|
|
let msContainer = document.getElementById('section-editor-' + this.get('containerId'));
|
2017-04-13 10:57:57 +01:00
|
|
|
let mousetrap = this.get('mousetrap');
|
2019-03-03 13:10:04 +00:00
|
|
|
if (_.isNull(mousetrap)) {
|
2017-04-13 10:57:57 +01:00
|
|
|
mousetrap = new Mousetrap(msContainer);
|
|
|
|
}
|
2017-03-22 10:24:30 +00:00
|
|
|
|
|
|
|
mousetrap.bind('esc', () => {
|
|
|
|
this.send('onCancel');
|
2017-02-28 03:25:06 +00:00
|
|
|
return false;
|
|
|
|
});
|
2017-03-22 10:24:30 +00:00
|
|
|
mousetrap.bind(['ctrl+s', 'command+s'], () => {
|
|
|
|
this.send('onAction');
|
2017-02-28 03:25:06 +00:00
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
2017-03-22 10:24:30 +00:00
|
|
|
this.set('mousetrap', mousetrap);
|
|
|
|
|
2017-02-28 03:25:06 +00:00
|
|
|
$('#' + this.get('pageId')).focus(function() {
|
|
|
|
$(this).select();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2019-04-17 17:13:18 +01:00
|
|
|
didInsertElement() {
|
|
|
|
this._super(...arguments);
|
|
|
|
|
|
|
|
let self = this;
|
|
|
|
let documentId = this.get('document.id');
|
|
|
|
let pageId = this.get('page.id');
|
|
|
|
let url = this.get('appMeta.endpoint');
|
|
|
|
let uploadUrl = `${url}/documents/${documentId}/attachments?page=${pageId}`;
|
|
|
|
let uploadId = this.get('uploadId');
|
|
|
|
|
|
|
|
// Handle upload clicks on button and anything inside that button.
|
|
|
|
let sel = ['#' + uploadId, '#' + uploadId + ' > div'];
|
|
|
|
for (var i=0; i < 2; i++) {
|
|
|
|
let dzone = new Dropzone(sel[i], {
|
|
|
|
headers: {
|
|
|
|
'Authorization': 'Bearer ' + self.get('session.authToken')
|
|
|
|
},
|
|
|
|
url: uploadUrl,
|
|
|
|
method: "post",
|
|
|
|
paramName: 'attachment',
|
|
|
|
clickable: true,
|
|
|
|
maxFilesize: 250,
|
|
|
|
parallelUploads: 5,
|
|
|
|
uploadMultiple: false,
|
|
|
|
addRemoveLinks: false,
|
|
|
|
autoProcessQueue: true,
|
|
|
|
|
|
|
|
init: function () {
|
|
|
|
this.on("success", function (/*file, response*/ ) {
|
|
|
|
});
|
|
|
|
|
|
|
|
this.on("queuecomplete", function () {
|
|
|
|
self.notifySuccess('Uploaded file');
|
|
|
|
self.getAttachments();
|
|
|
|
});
|
|
|
|
|
|
|
|
this.on("addedfile", function ( /*file*/ ) {
|
|
|
|
});
|
|
|
|
|
|
|
|
this.on("error", function (error, msg) {
|
|
|
|
self.notifyError(msg);
|
|
|
|
self.notifyError(error);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
dzone.on("complete", function (file) {
|
|
|
|
dzone.removeFile(file);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// For authenticated users we send server auth token.
|
|
|
|
let qry = '';
|
|
|
|
if (this.get('session.hasSecureToken')) {
|
|
|
|
qry = '?secure=' + this.get('session.secureToken');
|
|
|
|
} else if (this.get('session.authenticated')) {
|
|
|
|
qry = '?token=' + this.get('session.authToken');
|
|
|
|
}
|
|
|
|
this.set('downloadQuery', qry);
|
|
|
|
},
|
|
|
|
|
2017-02-28 03:25:06 +00:00
|
|
|
willDestroyElement() {
|
2017-12-07 19:43:46 +00:00
|
|
|
this._super(...arguments);
|
2019-02-28 15:11:46 +00:00
|
|
|
this.set('showLinkModal', false);
|
2017-12-07 19:43:46 +00:00
|
|
|
|
2017-03-22 10:24:30 +00:00
|
|
|
let mousetrap = this.get('mousetrap');
|
2019-03-03 13:10:04 +00:00
|
|
|
if (!_.isNull(mousetrap)) {
|
2017-03-22 10:24:30 +00:00
|
|
|
mousetrap.unbind('esc');
|
|
|
|
mousetrap.unbind(['ctrl+s', 'command+s']);
|
|
|
|
}
|
2017-02-28 03:25:06 +00:00
|
|
|
},
|
|
|
|
|
2019-04-17 17:13:18 +01:00
|
|
|
getAttachments() {
|
|
|
|
this.get('documentSvc').getAttachments(this.get('document.id')).then((files) => {
|
|
|
|
this.set('files', files);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2017-02-28 03:25:06 +00:00
|
|
|
actions: {
|
|
|
|
onAction() {
|
2019-03-03 13:10:04 +00:00
|
|
|
if (this.get('busy') || _.isEmpty(this.get('pageTitle'))) {
|
2017-02-28 03:25:06 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-02-28 17:50:03 +00:00
|
|
|
if (this.get('isDestroyed') || this.get('isDestroying')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-22 10:31:03 +00:00
|
|
|
let cb = this.get('onAction');
|
2018-04-26 12:01:58 +01:00
|
|
|
cb(this.get('pageTitle'));
|
2017-02-28 03:25:06 +00:00
|
|
|
},
|
|
|
|
|
2017-12-07 19:43:46 +00:00
|
|
|
onCancel() {
|
2018-01-22 10:31:03 +00:00
|
|
|
let isDirty = this.get('isDirty');
|
|
|
|
if (isDirty() !== null && isDirty()) {
|
2017-12-07 19:43:46 +00:00
|
|
|
this.modalOpen('#discard-modal-' + this.get('page.id'), {show: true});
|
|
|
|
return;
|
|
|
|
}
|
2017-02-28 03:25:06 +00:00
|
|
|
|
2018-01-22 10:31:03 +00:00
|
|
|
let cb = this.get('onCancel');
|
|
|
|
cb();
|
2017-03-05 19:11:01 +00:00
|
|
|
},
|
|
|
|
|
2017-12-07 19:43:46 +00:00
|
|
|
onDiscard() {
|
|
|
|
this.modalClose('#discard-modal-' + this.get('page.id'));
|
2018-01-22 10:31:03 +00:00
|
|
|
let cb = this.get('onCancel');
|
|
|
|
cb();
|
2017-12-07 19:43:46 +00:00
|
|
|
},
|
2017-03-05 19:11:01 +00:00
|
|
|
|
|
|
|
onPreview() {
|
2017-12-08 17:24:39 +00:00
|
|
|
let pt = this.get('previewText');
|
|
|
|
this.set('previewText', pt === 'Preview' ? 'Edit Mode' : 'Preview');
|
2017-03-05 19:11:01 +00:00
|
|
|
return this.get('onPreview')();
|
2017-12-07 19:43:46 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
onShowLinkModal() {
|
|
|
|
this.set('showLinkModal', true);
|
|
|
|
},
|
|
|
|
|
|
|
|
onInsertLink(selection) {
|
|
|
|
this.set('showLinkModal', false);
|
|
|
|
return this.get('onInsertLink')(selection);
|
|
|
|
}
|
2017-02-28 03:25:06 +00:00
|
|
|
}
|
|
|
|
});
|