UNPKG

7.8 kBTypeScriptView Raw
1/**
2 * A base viewer for BPMN 2.0 diagrams.
3 *
4 * Have a look at {@link Viewer}, {@link NavigatedViewer} or {@link Modeler} for
5 * bundles that include actual features.
6 *
7 */
8export default class BaseViewer extends Diagram<null> {
9 /**
10 * @param options The options to configure the viewer.
11 */
12 constructor(options?: BaseViewerOptions);
13
14 /**
15 * Parse and render a BPMN 2.0 diagram.
16 *
17 * Once finished the viewer reports back the result to the
18 * provided callback function with (err, warnings).
19 *
20 * ## Life-Cycle Events
21 *
22 * During import the viewer will fire life-cycle events:
23 *
24 * * import.parse.start (about to read model from XML)
25 * * import.parse.complete (model read; may have worked or not)
26 * * import.render.start (graphical import start)
27 * * import.render.complete (graphical import finished)
28 * * import.done (everything done)
29 *
30 * You can use these events to hook into the life-cycle.
31 *
32 * @throws {ImportXMLError} An error thrown during the import of the XML.
33 *
34 * @fires BaseViewer#ImportParseStartEvent
35 * @fires BaseViewer#ImportParseCompleteEvent
36 * @fires Importer#ImportRenderStartEvent
37 * @fires Importer#ImportRenderCompleteEvent
38 * @fires BaseViewer#ImportDoneEvent
39 *
40 * @param xml The BPMN 2.0 XML to be imported.
41 * @param bpmnDiagram The optional diagram or Id of the BPMN diagram to open.
42 *
43 * @return A promise resolving with warnings that were produced during the import.
44 */
45 importXML(xml: string, bpmnDiagram?: ModdleElement | string): Promise<ImportXMLResult>;
46
47 /**
48 * Import parsed definitions and render a BPMN 2.0 diagram.
49 *
50 * Once finished the viewer reports back the result to the
51 * provided callback function with (err, warnings).
52 *
53 * ## Life-Cycle Events
54 *
55 * During import the viewer will fire life-cycle events:
56 *
57 * * import.render.start (graphical import start)
58 * * import.render.complete (graphical import finished)
59 *
60 * You can use these events to hook into the life-cycle.
61 *
62 * @throws {ImportDefinitionsError} An error thrown during the import of the definitions.
63 *
64 * @param definitions The definitions.
65 * @param bpmnDiagram The optional diagram or ID of the BPMN diagram to open.
66 *
67 * @return A promise resolving with warnings that were produced during the import.
68 */
69 importDefinitions(definitions: ModdleElement, bpmnDiagram?: ModdleElement | string): Promise<ImportDefinitionsResult>;
70
71 /**
72 * Open diagram of previously imported XML.
73 *
74 * Once finished the viewer reports back the result to the
75 * provided callback function with (err, warnings).
76 *
77 * ## Life-Cycle Events
78 *
79 * During switch the viewer will fire life-cycle events:
80 *
81 * * import.render.start (graphical import start)
82 * * import.render.complete (graphical import finished)
83 *
84 * You can use these events to hook into the life-cycle.
85 *
86 * @throws {OpenError} An error thrown during opening.
87 *
88 * @param bpmnDiagramOrId The diagram or Id of the BPMN diagram to open.
89 *
90 * @return A promise resolving with warnings that were produced during opening.
91 */
92 open(bpmnDiagramOrId: ModdleElement | string): Promise<OpenResult>;
93
94 /**
95 * Export the currently displayed BPMN 2.0 diagram as
96 * a BPMN 2.0 XML document.
97 *
98 * ## Life-Cycle Events
99 *
100 * During XML saving the viewer will fire life-cycle events:
101 *
102 * * saveXML.start (before serialization)
103 * * saveXML.serialized (after xml generation)
104 * * saveXML.done (everything done)
105 *
106 * You can use these events to hook into the life-cycle.
107 *
108 * @throws {Error} An error thrown during export.
109 *
110 * @fires BaseViewer#SaveXMLStart
111 * @fires BaseViewer#SaveXMLDone
112 *
113 * @param options The options.
114 *
115 * @return A promise resolving with the XML.
116 */
117 saveXML(options?: SaveXMLOptions): Promise<SaveXMLResult>;
118
119 /**
120 * Export the currently displayed BPMN 2.0 diagram as
121 * an SVG image.
122 *
123 * ## Life-Cycle Events
124 *
125 * During SVG saving the viewer will fire life-cycle events:
126 *
127 * * saveSVG.start (before serialization)
128 * * saveSVG.done (everything done)
129 *
130 * You can use these events to hook into the life-cycle.
131 *
132 * @throws {Error} An error thrown during export.
133 *
134 * @fires BaseViewer#SaveSVGDone
135 *
136 * @return A promise resolving with the SVG.
137 */
138 saveSVG(): Promise<SaveSVGResult>;
139
140 /**
141 * Return modules to instantiate with.
142 *
143 * @return The modules.
144 */
145 getModules(): ModuleDeclaration[];
146
147 /**
148 * Register an event listener.
149 *
150 * Remove an event listener via {@link BaseViewer#off}.
151 *
152 *
153 * @param events The event(s) to listen to.
154 * @param callback The callback.
155 * @param that Value of `this` the callback will be called with.
156 */
157 on<T>(events: string | string[], callback: EventBusEventCallback<T>, that?: any): any;
158
159 /**
160 * Register an event listener.
161 *
162 * Remove an event listener via {@link BaseViewer#off}.
163 *
164 *
165 * @param events The event(s) to listen to.
166 * @param priority The priority with which to listen.
167 * @param callback The callback.
168 * @param that Value of `this` the callback will be called with.
169 */
170 on<T>(
171 events: string | string[],
172 priority: number,
173 callback: EventBusEventCallback<T>,
174 that?: any
175 ): any;
176
177 /**
178 * Remove an event listener.
179 *
180 * @param events The event(s).
181 * @param callback The callback.
182 */
183 off(events: string | string[], callback?: Function): void;
184
185 /**
186 * Attach the viewer to an HTML element.
187 *
188 * @param parentNode The parent node to attach to.
189 */
190 attachTo(parentNode: HTMLElement): void;
191
192 /**
193 * Get the definitions model element.
194 *
195 * @return The definitions model element.
196 */
197 getDefinitions(): ModdleElement;
198
199 /**
200 * Detach the viewer.
201 *
202 * @fires BaseViewer#DetachEvent
203 */
204 detach(): void;
205}
206
207type EventBusEventCallback<T> = import('diagram-js/lib/core/EventBus').EventBusEventCallback<T>;
208type ModuleDeclaration = import('didi').ModuleDeclaration;
209type Moddle = import('./model/Types').Moddle;
210type ModdleElement = import('./model/Types').ModdleElement;
211type ModdleExtension = import('./model/Types').ModdleExtension;
212
213export type BaseViewerOptions = {
214 width?: number | string;
215 height?: number | string;
216 position?: string;
217 container?: string | HTMLElement;
218 moddleExtensions?: ModdleExtensions;
219 additionalModules?: ModuleDeclaration[];
220} & Record<string, any>;
221
222export type ModdleElementsById = Record<string, ModdleElement>;
223
224export type ModdleExtensions = {
225 [key: string]: import("./model/Types").ModdleExtension;
226};
227
228export type ImportXMLResult = {
229 warnings: string[];
230};
231
232export type ImportXMLError = ImportXMLResult & Error;
233export type ImportDefinitionsResult = ImportXMLResult;
234export type ImportDefinitionsError = ImportXMLError;
235export type OpenResult = ImportXMLResult;
236export type OpenError = ImportXMLError;
237
238export type SaveXMLOptions = {
239 format?: boolean;
240 preamble?: boolean;
241};
242
243export type SaveXMLResult = {
244 xml?: string;
245 error?: Error;
246};
247
248export type SaveSVGResult = {
249 svg: string;
250};
251
252export type ImportParseStartEvent = {
253 xml: string;
254};
255
256export type ImportParseCompleteEvent = {
257 error?: ImportXMLError;
258 definitions?: ModdleElement;
259 elementsById?: ModdleElementsById;
260 references?: ModdleElement[];
261 warnings: string[];
262};
263
264export type ImportDoneEvent = {
265 error?: ImportXMLError;
266 warnings: string[];
267};
268
269export type SaveXMLStartEvent = {
270 definitions: ModdleElement;
271};
272
273export type SaveXMLDoneEvent = SaveXMLResult;
274
275export type SaveSVGDoneEvent = {
276 error?: Error;
277 svg: string;
278};
279
280import Diagram from 'diagram-js';