Skip to content

Instantly share code, notes, and snippets.

@PastorPL
Created November 9, 2020 12:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PastorPL/e5bccfa4a46ed15f843bf1fdd7544f80 to your computer and use it in GitHub Desktop.
Save PastorPL/e5bccfa4a46ed15f843bf1fdd7544f80 to your computer and use it in GitHub Desktop.
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