From 5a8e98c41055183e8ced4d1c58e4a5bec8fc5ac2 Mon Sep 17 00:00:00 2001 From: Oana-Lavinia Florean Date: Tue, 16 Jun 2020 11:41:26 +0300 Subject: [PATCH] Add support for disabling the features that require an external server #105 (#157) * add a property for disabling these features in DiagramConfig and based on this stop the export of pdf from diagram editor or png / jpeg export for IE --- .../main/resources/Diagram/DiagramConfig.xml | 18 ++++++++ .../resources/Diagram/DiagramConfigClass.xml | 15 +++++++ .../resources/Diagram/DiagramConfigSheet.xml | 34 ++++++++------- .../resources/Diagram/DiagramEditSheet.xml | 42 ++++++++++++++++++- .../main/resources/Diagram/DiagramSheet.xml | 10 ++++- .../resources/Diagram/DiagramTranslations.xml | 5 ++- 6 files changed, 106 insertions(+), 18 deletions(-) diff --git a/application-diagram-ui/src/main/resources/Diagram/DiagramConfig.xml b/application-diagram-ui/src/main/resources/Diagram/DiagramConfig.xml index f565d2a..3a75e36 100644 --- a/application-diagram-ui/src/main/resources/Diagram/DiagramConfig.xml +++ b/application-diagram-ui/src/main/resources/Diagram/DiagramConfig.xml @@ -54,6 +54,21 @@ + + + 0 + 0 + select + + + disableExternalServices + 2 + Disable External Services + 0 + + + com.xpn.xwiki.objects.classes.BooleanClass + 0 @@ -68,6 +83,9 @@ com.xpn.xwiki.objects.classes.StringClass + + 0 + https://exp.draw.io/ImageExport4/export diff --git a/application-diagram-ui/src/main/resources/Diagram/DiagramConfigClass.xml b/application-diagram-ui/src/main/resources/Diagram/DiagramConfigClass.xml index 79e7880..8cbcf1b 100644 --- a/application-diagram-ui/src/main/resources/Diagram/DiagramConfigClass.xml +++ b/application-diagram-ui/src/main/resources/Diagram/DiagramConfigClass.xml @@ -49,6 +49,21 @@ + + + 0 + 0 + select + + + disableExternalServices + 2 + Disable External Services + 0 + + + com.xpn.xwiki.objects.classes.BooleanClass + 0 diff --git a/application-diagram-ui/src/main/resources/Diagram/DiagramConfigSheet.xml b/application-diagram-ui/src/main/resources/Diagram/DiagramConfigSheet.xml index 950437f..89f45b1 100644 --- a/application-diagram-ui/src/main/resources/Diagram/DiagramConfigSheet.xml +++ b/application-diagram-ui/src/main/resources/Diagram/DiagramConfigSheet.xml @@ -43,6 +43,23 @@ #macro (stripHTMLMacro $displayOutput) $stringtool.removeEnd($stringtool.removeStart($displayOutput, '{{html clean="false" wiki="false"}}'), '{{/html}}') #end +#macro (showProperty $propertyName) + <dl> + <dt> + #set ($propertyClass = $xclass.get($propertyName)) + <label#if ($editing) for="${diagramConfigClassName}_0_${propertyName}"#end> + $escapetool.xml($propertyClass.translatedPrettyName) + </label> + #set ($hint = $propertyClass.hint) + #if ("$!hint" == '') + #set ($hint = $services.localization.render("${diagramConfigClassName}_${propertyName}_hint")) + #end + <span class="xHint">$escapetool.xml($hint)</span> + </dt> + #set ($displayOutput = $doc.display($propertyName, $mode)) + <dd>#stripHTMLMacro($displayOutput)</dd> + </dl> +#end {{/velocity}} {{velocity}} @@ -61,21 +78,8 @@ #else <div class="diagram-config xform"> #end - <dl> - <dt> - #set ($propertyClass = $xclass.get('exportURL')) - <label#if ($editing) for="${diagramConfigClassName}_0_exportURL"#end> - $escapetool.xml($propertyClass.translatedPrettyName) - </label> - #set ($hint = $propertyClass.hint) - #if ("$!hint" == '') - #set ($hint = $services.localization.render("${diagramConfigClassName}_exportURL_hint")) - #end - <span class="xHint">$escapetool.xml($hint)</span> - </dt> - #set ($displayOutput = $doc.display('exportURL', $mode)) - <dd>#stripHTMLMacro($displayOutput)</dd> - </dl> + #showProperty('exportURL') + #showProperty('disableExternalServices') #if ($xcontext.action == 'admin') <p> <input type="submit" class="btn btn-primary" name="action_saveandcontinue" diff --git a/application-diagram-ui/src/main/resources/Diagram/DiagramEditSheet.xml b/application-diagram-ui/src/main/resources/Diagram/DiagramEditSheet.xml index 691510c..19d3d8b 100644 --- a/application-diagram-ui/src/main/resources/Diagram/DiagramEditSheet.xml +++ b/application-diagram-ui/src/main/resources/Diagram/DiagramEditSheet.xml @@ -1051,6 +1051,45 @@ define('diagram-url-io', ['diagram-utils', 'draw.io'], function(diagramUtils) { } }); +/** + * In case external services are disabled, stop the features that require it (export of pdf from diagram editor + * or png / jpeg export on IE) and show an info dialog. + */ +define('diagram-external-services', ['jquery', 'diagram-config', 'draw.io'], function($, diagramConfig) { + var showDisabledServicesDialog = function(editorUi) { + var errorMessage = $('<div></div') + .html($jsontool.serialize($services.localization.render('diagram.editor.disabledExternalServices'))); + + errorMessage.addClass('externalServicesDialog'); + var dlg = new CustomDialog(/*editorUi*/ editorUi, /*content*/ errorMessage[0], /*okFn*/ null, /*cancelFn*/ null, + /*okButtonText*/ mxResources.get('ok'), /*helpLink*/ null, /*buttonsContent*/ null, + /*hideCancel*/ true); + editorUi.showDialog(dlg.container, 250, 75, true, true); + }; + + // This is used from File -> Export as. + var originalCreateDownloadRequest = EditorUi.prototype.createDownloadRequest; + EditorUi.prototype.createDownloadRequest = function(filename, format, ignoreSelection, base64, transparent, + currentPage, scale, border, grid, includeXml) { + if (diagramConfig.disableExternalServices) { + showDisabledServicesDialog(this); + return; + } + return originalCreateDownloadRequest.apply(this, arguments); + }; + + // This is used from File -> Export as -> Advanced... + var originalExportFile = ExportDialog.exportFile; + ExportDialog.exportFile = function(editorUi, name, format, bg, s, b, dpi) { + if (diagramConfig.disableExternalServices && (format == 'pdf' || + (['png', 'jpg', 'jpeg'].indexOf(format) != -1 && !editorUi.isExportToCanvas()))) { + showDisabledServicesDialog(editorUi); + return; + } + return originalExportFile.apply(this, arguments); + }; +}); + /** * Integrates draw.io diagram editor in XWiki. */ @@ -1061,7 +1100,8 @@ define('diagram-editor', [ 'diagram-graph-xml-filter', 'diagram-link-editor', 'diagram-image-editor', - 'diagram-url-io' + 'diagram-url-io', + 'diagram-external-services' ], function($, diagramStore, diagramUtils) { // These variables are used to decide if an image should be uploaded at original resolution or diff --git a/application-diagram-ui/src/main/resources/Diagram/DiagramSheet.xml b/application-diagram-ui/src/main/resources/Diagram/DiagramSheet.xml index 0b9d133..c0ddb11 100644 --- a/application-diagram-ui/src/main/resources/Diagram/DiagramSheet.xml +++ b/application-diagram-ui/src/main/resources/Diagram/DiagramSheet.xml @@ -49,7 +49,8 @@ "mxGraphEditorBasePath": $services.webjars.url('org.xwiki.contrib:mxgraph-editor', ''), "drawIOBasePath": $services.webjars.url('org.xwiki.contrib:draw.io', ''), "exportURL": $xwiki.getDocument('Diagram.DiagramConfig').getValue('exportURL'), - "pdfImageExportZoom": $diagramObj.getValue('pdfImageExportZoom') + "pdfImageExportZoom": $diagramObj.getValue('pdfImageExportZoom'), + "disableExternalServices": $xwiki.getDocument('Diagram.DiagramConfig').getValue('disableExternalServices') }) #if ($xcontext.action == 'edit') {{include reference="Diagram.DiagramEditSheet" /}} @@ -1022,6 +1023,13 @@ define('diagram-utils', ['jquery', 'diagram-link-handler'], function($, diagramL .diagram-container > .thumbnail .box { margin-bottom: 0; +} + +.externalServicesDialog { + text-align: center; + padding: 2%; + overflow: auto; + line-height: 1.3em; } diff --git a/application-diagram-ui/src/main/resources/Diagram/DiagramTranslations.xml b/application-diagram-ui/src/main/resources/Diagram/DiagramTranslations.xml index 761c0b6..f08e4cd 100644 --- a/application-diagram-ui/src/main/resources/Diagram/DiagramTranslations.xml +++ b/application-diagram-ui/src/main/resources/Diagram/DiagramTranslations.xml @@ -72,10 +72,13 @@ diagram.macro.viewNotAllowed=You are not allowed to view this diagram. # Diagram editor diagram.editor.saveAsImageAttachmentError=Error when saving the diagram as an image attachment. +diagram.editor.disabledExternalServices=External services are disabled. # Diagram Configuration Diagram.DiagramConfigClass_exportURL=Export URL -Diagram.DiagramConfigClass_exportURL_hint=The URL used to export the diagrams as image or PDF. +Diagram.DiagramConfigClass_exportURL_hint=The URL used to export the diagrams as image or PDF. +Diagram.DiagramConfigClass_disableExternalServices=Disable External Services +Diagram.DiagramConfigClass_disableExternalServices_hint=Disable features that require an external server Diagram.DiagramTranslations 0