1 /* Copyright (c) 2019 AT&T Intellectual Property. #
\r
3 # Licensed under the Apache License, Version 2.0 (the "License"); #
\r
4 # you may not use this file except in compliance with the License. #
\r
5 # You may obtain a copy of the License at #
\r
7 # http://www.apache.org/licenses/LICENSE-2.0 #
\r
9 # Unless required by applicable law or agreed to in writing, software #
\r
10 # distributed under the License is distributed on an "AS IS" BASIS, #
\r
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
\r
12 # See the License for the specific language governing permissions and #
\r
13 # limitations under the License. #
\r
14 ##############################################################################*/
\r
17 import Modeler from 'bpmn-js/lib/Modeler';
\r
18 import Viewer from 'bpmn-js/lib/NavigatedViewer';
\r
19 import { FileTransferService } from 'app/shared/services/file-transfer.service';
\r
20 import { Observable } from 'rxjs';
\r
21 import { TestDefinitionService } from 'app/shared/services/test-definition.service';
\r
23 import { saveAs } from 'file-saver';
\r
24 //import { parseString } from 'xml2js';
\r
25 import { HostListener } from '@angular/core';
\r
27 export interface BpmnOptions {
\r
28 mode: 'viewer' | 'modeler',
\r
36 protected model: any;
\r
37 protected bpmnXml: String;
\r
38 private options: BpmnOptions;
\r
40 constructor(bpmnXml: String, options: BpmnOptions) {
\r
41 //check for required options
\r
42 if (!options.mode) {
\r
43 console.error('Bpmn options require: mode');
\r
46 this.bpmnXml = bpmnXml;
\r
47 this.options = options;
\r
53 this.renderDiagram();
\r
62 public async getBpmnXml() {
\r
63 return new Promise((resolve, reject) => {
\r
64 this.model.saveXML({ format: true }, function (err, xml) {
\r
75 private setModel(options?) {
\r
81 let op = this.options.options;
\r
88 console.error('Options for the viewer/modeler must be provided');
\r
92 //handle the mode (viewer or modeler)
\r
93 switch (this.options.mode.toLowerCase()) {
\r
95 this.model = new Viewer(op);
\r
99 this.model = new Modeler(op);
\r
103 console.error('Mode must either be "viewer" or "modeler"');
\r
109 public async setBpmnXml(xml) {
\r
110 this.bpmnXml = xml;
\r
111 await this.renderDiagram();
\r
116 public async renderDiagram() {
\r
117 return new Promise((resolve, reject) => {
\r
118 if (this.bpmnXml) {
\r
119 this.model.importXML(this.bpmnXml, (err) => {
\r
121 this.model.get('canvas').zoom('fit-viewport');
\r
124 console.error(err);
\r
133 this.model.get('canvas').zoom('fit-viewport');
\r
136 public download(saveName?) {
\r
138 this.model.saveXML({ format: true }, function (err, xml) {
\r
140 let parser = new DOMParser();
\r
141 let xmlDoc = parser.parseFromString(xml.toString(), "text/xml");
\r
143 let id = xmlDoc.getElementsByTagName("bpmn:process")[0].attributes.getNamedItem("id").value;
\r
148 saveName = 'workflow';
\r
152 saveName += ".bpmn";
\r
154 let blob = new Blob([xml], { type: "application/xml" });
\r
155 saveAs(blob, saveName);
\r