Skip to content

Commit

Permalink
dialog for new diagram, isExecutable to true
Browse files Browse the repository at this point in the history
Co-Authored-By: LaviniaStiliadou <livia_16@live.de>
  • Loading branch information
SharonNaemi and LaviniaStiliadou committed Sep 17, 2023
1 parent 0b3b030 commit f24afa8
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 28 deletions.
43 changes: 43 additions & 0 deletions components/bpmn-q/modeler-component/editor/ui/ConfirmationModal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* Copyright (c) 2023 Institute of Architecture of Application Systems -
* University of Stuttgart
*
* This program and the accompanying materials are made available under the
* terms the Apache Software License 2.0
* which is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* SPDX-License-Identifier: Apache-2.0
*/
import React from 'react';
import Modal from '../ui/modal/Modal';

// polyfill upcoming structural components
const Title = Modal.Title || (({children}) => <h2>{children}</h2>);
const Body = Modal.Body || (({children}) => <div>{children}</div>);
const Footer = Modal.Footer || (({children}) => <div>{children}</div>);

/**
* Modal component for confirming the discard of changes in the editor.
*
* @param onClose Function called when the modal is closed.
* @param onConfirm Function called when the "Yes" button is clicked to confirm discarding changes.
* @returns {JSX.Element} The modal as a React component.
* @constructor
*/
export default function ConfirmationModal({onClose, onConfirm}) {

return <Modal onClose={onClose}>
<Title>
Confirm Discard Changes
</Title>
<Body>
There are unsaved changes. Are you sure you want to discard all changes and generate a new diagram?
</Body>
<Footer>
<div id="configFormButtons">
<button type="submit" className="qwm-btn qwm-btn-primary" onClick={() => onConfirm()}>Yes</button>
<button type="button" className="qwm-btn qwm-btn-secondary" onClick={() => onClose()}>No</button>
</div>
</Footer>
</Modal>;
}
80 changes: 59 additions & 21 deletions components/bpmn-q/modeler-component/editor/ui/NewDiagramButton.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,63 @@
import React from "react";
import { createNewDiagram } from "../util/IoUtilities";
import React, { useState, useEffect } from 'react';
import { createNewDiagram } from '../util/IoUtilities';
import { getModeler } from '../ModelerHandler';
import ConfirmationModal from './ConfirmationModal';

/**
* React button which creates a new workflow.
*
* @param props
* @returns {JSX.Element}
* @constructor
*/
export default function NewDiagramButton(props) {
const { modeler } = props;
const { modeler } = props;
const [unsavedChanges, setUnsavedChanges] = useState(false);
const [showConfirmationModal, setShowConfirmationModal] = useState(false);

return (
<button
className="qwm-toolbar-btn"
title="Create new workflow diagram"
onClick={() => createNewDiagram(modeler)}
>
<span className="qwm-icon-new-file">
<span className="qwm-indent">New Diagram</span>
</span>
</button>
);
useEffect(() => {
if (unsavedChanges) {
setShowConfirmationModal(true);
} else {
createNewDiagram(modeler);
}
}, [unsavedChanges, modeler]);

const checkUnsavedChanges = () => {
getModeler().saveXML({ format: true }, function (err, xml) {
if (!err) {
let oldXml = getModeler().oldXml;
if (oldXml !== undefined) {
oldXml = oldXml.trim();
}
if (oldXml !== xml.trim() && oldXml !== undefined) {
setUnsavedChanges(true);
setShowConfirmationModal(true);
} else {
setShowConfirmationModal(false);
createNewDiagram(modeler);
}
}
});
};

const handleConfirmDiscard = () => {
createNewDiagram(modeler);
setUnsavedChanges(false);
setShowConfirmationModal(false);
};

const handleCancelDiscard = () => {
setShowConfirmationModal(false);
};

return (
<div>
<button className="qwm-toolbar-btn" title="Create a new workflow diagram" onClick={checkUnsavedChanges}>
<span className="qwm-icon-new-file">
<span className="qwm-indent">New Diagram</span>
</span>
</button>

{showConfirmationModal && (
<ConfirmationModal
onClose={handleCancelDiscard}
onConfirm={handleConfirmDiscard}
/>
)}
</div>
);
}
18 changes: 11 additions & 7 deletions components/bpmn-q/modeler-component/editor/util/IoUtilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ let FormData = require("form-data");
// workflow with a start event to use as template for new workflows
const NEW_DIAGRAM_XML =
'<?xml version="1.0" encoding="UTF-8"?>\n' +
'<bpmn2:definitions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:bpmn2="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" xmlns:di="http://www.omg.org/spec/DD/20100524/DI" xsi:schemaLocation="http://www.omg.org/spec/BPMN/20100524/MODEL BPMN20.xsd" id="sample-diagram" targetNamespace="http://bpmn.io/schema/bpmn">\n' +
' <bpmn2:process id="Process_1" isExecutable="false">\n' +
' <bpmn2:startEvent id="StartEvent_1"/>\n' +
'<bpmn2:definitions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:bpmn2="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" id="sample-diagram" targetNamespace="http://bpmn.io/schema/bpmn" xsi:schemaLocation="http://www.omg.org/spec/BPMN/20100524/MODEL BPMN20.xsd">\n' +
' <bpmn2:process id="Process_1" isExecutable="true">\n' +
' <bpmn2:startEvent id="StartEvent_1" />\n' +
" </bpmn2:process>\n" +
' <bpmndi:BPMNDiagram id="BPMNDiagram_1">\n' +
' <bpmndi:BPMNPlane id="BPMNPlane_1" bpmnElement="Process_1">\n' +
' <bpmndi:BPMNShape id="_BPMNShape_StartEvent_2" bpmnElement="StartEvent_1">\n' +
' <dc:Bounds height="36.0" width="36.0" x="412.0" y="240.0"/>\n' +
' <dc:Bounds x="412" y="240" width="36" height="36" />\n' +
" </bpmndi:BPMNShape>\n" +
" </bpmndi:BPMNPlane>\n" +
" </bpmndi:BPMNDiagram>\n" +
Expand Down Expand Up @@ -72,8 +72,9 @@ export async function saveModelerAsLocalFile(
fileFormat === saveFileFormats.ALL
) {
if (openWindow) {
await openFileDialog(xml, fileName, saveFileFormats.BPMN);
await openFileDialog(modeler, xml, fileName, saveFileFormats.BPMN);
} else {
modeler.oldXml = xml;
await saveXmlAsLocalFile(xml, fileName);
}
}
Expand Down Expand Up @@ -346,7 +347,7 @@ export async function saveWorkflowAsSVG(modeler, fileName, fileFormat) {
fileFormat === saveFileFormats.ALL ||
fileFormat === saveFileFormats.SVG
) {
openFileDialog(svg, fileName, saveFileFormats.SVG);
openFileDialog(modeler, svg, fileName, saveFileFormats.SVG);
}
if (
fileFormat === saveFileFormats.ALL ||
Expand Down Expand Up @@ -378,7 +379,7 @@ function downloadPng(pngDataUrl, fileName, fileFormat) {
openFileUrlDialog(pngDataUrl, fileName, fileFormat);
}

async function openFileDialog(content, fileName, fileFormat) {
async function openFileDialog(modeler, content, fileName, fileFormat) {
let suggestedName = fileName;
if (suggestedName.includes(".bpmn")) {
suggestedName = fileName.split(".bpmn")[0];
Expand All @@ -398,6 +399,9 @@ async function openFileDialog(content, fileName, fileFormat) {
],
});
writeFile(fileHandle, content);
if (fileFormat === saveFileFormats.BPMN || fileFormat === saveFileFormats.ALL) {
modeler.oldXml = content;
}
}

async function openFileUrlDialog(content, fileName, fileFormat) {
Expand Down

0 comments on commit f24afa8

Please sign in to comment.