Created
November 9, 2020 12:47
-
-
Save PastorPL/e5bccfa4a46ed15f843bf1fdd7544f80 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Component, AfterContentInit, ElementRef, Input, OnChanges, OnDestroy, Output, ViewChild, SimpleChanges, EventEmitter } from '@angular/core'; | |
import { HttpClient } from '@angular/common/http'; | |
import { catchError } from 'rxjs/operators'; | |
import * as BpmnJS from 'bpmn-js/dist/bpmn-modeler.production.min.js'; | |
import { importDiagram } from './rx'; | |
import { throwError } from 'rxjs'; | |
@Component({ | |
selector: 'app-modeler', | |
template: ` | |
<div #ref class="diagram-container"></div> | |
`, | |
styles: [ | |
` | |
.diagram-container { | |
height: 100%; | |
width: 100%; | |
} | |
` | |
] | |
}) | |
export class ModelerComponent implements AfterContentInit, OnChanges, OnDestroy { | |
private bpmnJS: BpmnJS; | |
@ViewChild('ref', { static: true }) private el: ElementRef; | |
@Output() private importDone: EventEmitter<any> = new EventEmitter(); | |
@Input() private url: string; | |
constructor(private http: HttpClient) { | |
this.bpmnJS = new BpmnJS(); | |
this.bpmnJS.on('import.done', ({ error }) => { | |
if (!error) { | |
console.log('No error'); | |
this.bpmnJS.get('canvas').zoom('fit-viewport'); | |
}else{ | |
console.log('No error '+error); | |
} | |
}); | |
} | |
ngAfterContentInit(): void { | |
this.bpmnJS.attachTo(this.el.nativeElement); | |
} | |
ngOnChanges(changes: SimpleChanges) { | |
// re-import whenever the url changes | |
if (changes.url) { | |
this.loadUrl(changes.url.currentValue); | |
} | |
} | |
ngOnDestroy(): void { | |
this.bpmnJS.destroy(); | |
} | |
/** | |
* Load diagram from URL and emit completion event | |
*/ | |
loadUrl(url: string) { | |
return ( | |
this.http.get(url, { responseType: 'text' }).pipe( | |
catchError(err => throwError(err)), | |
importDiagram(this.bpmnJS) | |
).subscribe( | |
(warnings) => { | |
this.importDone.emit({ | |
type: 'success', | |
warnings | |
}); | |
}, | |
(err) => { | |
this.importDone.emit({ | |
type: 'error', | |
error: err | |
}); | |
} | |
) | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment