aboutsummaryrefslogtreecommitdiffstats
path: root/workflow-designer-ui/src/main/frontend/src/features/version/composition/CompositionView.js
blob: d549456fff9265ac84bee49846fd9efbc5ddc094 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
* Copyright © 2018 European Support Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*http://www.apache.org/licenses/LICENSE-2.0
*
 * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import React, { Component } from 'react';
import fileSaver from 'file-saver';
import CustomModeler from './custom-modeler';
import propertiesPanelModule from 'bpmn-js-properties-panel';
import propertiesProviderModule from 'bpmn-js-properties-panel/lib/provider/camunda';
import camundaModuleDescriptor from 'camunda-bpmn-moddle/resources/camunda';
import newDiagramXML from './newDiagram.bpmn';
import PropTypes from 'prop-types';
import CompositionButtons from './components/CompositionButtonsPanel';

class CompositionView extends Component {
    static propTypes = {
        compositionUpdate: PropTypes.func,
        showErrorModal: PropTypes.func,
        composition: PropTypes.string,
        name: PropTypes.string
    };
    constructor() {
        super();
        this.generatedId = 'bpmn-container' + Date.now();
        this.fileInput = React.createRef();
        this.state = {
            diagram: false
        };
    }

    componentDidMount() {
        const { composition } = this.props;

        this.modeler = new CustomModeler({
            propertiesPanel: {
                parent: '#js-properties-panel'
            },
            additionalModules: [
                propertiesPanelModule,
                propertiesProviderModule
            ],
            moddleExtensions: {
                camunda: camundaModuleDescriptor
            }
        });
        window.modeler = this.modeler;
        this.modeler.attachTo('#' + this.generatedId);
        this.setDiagram(composition ? composition : newDiagramXML);
        var eventBus = this.modeler.get('eventBus');
        eventBus.on('element.out', () => {
            this.exportDiagramToStore();
        });
    }

    setDiagram = diagram => {
        this.setState(
            {
                diagram
            },
            this.importXML
        );
    };

    importXML = () => {
        const { diagram } = this.state;
        let modeler = this.modeler;
        this.modeler.importXML(diagram, err => {
            if (err) {
                //TDOD add i18n
                return this.props.showErrorModal('could not import diagram');
            }
            let canvas = modeler.get('canvas');
            canvas.zoom('fit-viewport');
        });
    };

    exportDiagramToStore = () => {
        this.modeler.saveXML({ format: true }, (err, xml) => {
            if (err) {
                //TODO   add i18n
                return this.props.showErrorModal('could not save diagram');
            }
            return this.props.compositionUpdate(xml);
        });
    };

    exportDiagram = () => {
        const { name, showErrorModal } = this.props;
        this.modeler.saveXML({ format: true }, (err, xml) => {
            if (err) {
                //TODO add i18n
                return showErrorModal('could not save diagram');
            }
            const blob = new Blob([xml], { type: 'text/html;charset=utf-8' });
            fileSaver.saveAs(blob, `${name}-diagram.bpmn`);
        });
    };

    loadNewDiagram = () => {
        this.setDiagram(newDiagramXML);
    };

    uploadDiagram = () => {
        this.fileInput.current.click();
    };

    handleFileInputChange = filesList => {
        const file = filesList[0];
        const reader = new FileReader();
        reader.onloadend = event => {
            var xml = event.target.result;
            this.setDiagram(xml);
            this.fileInput.value = '';
        };
        reader.readAsText(file);
    };

    render() {
        return (
            <div className="composition-view content">
                <input
                    ref={this.fileInput}
                    onChange={e => this.handleFileInputChange(e.target.files)}
                    id="file-input"
                    accept=".bpmn, .xml"
                    type="file"
                    name="file-input"
                    style={{ display: 'none' }}
                />
                <div
                    onBlur={() => {
                        this.exportDiagramToStore();
                    }}
                    className="bpmn-container"
                    id={this.generatedId}
                />
                <div className="bpmn-sidebar">
                    <div
                        className="properties-panel"
                        id="js-properties-panel"
                    />
                    <CompositionButtons
                        onClean={this.loadNewDiagram}
                        onDownload={this.exportDiagram}
                        onUpload={this.uploadDiagram}
                    />
                </div>
            </div>
        );
    }
}

export default CompositionView;