diff --git a/lib/Viewer.js b/lib/Viewer.js
index 8d3c0fd4..ca7631ca 100644
--- a/lib/Viewer.js
+++ b/lib/Viewer.js
@@ -199,6 +199,16 @@ Viewer.prototype.importXML = function(xml, done) {
* Export the currently displayed BPMN 2.0 diagram as
* a BPMN 2.0 XML document.
*
+ * ## Life-Cycle Events
+ *
+ * During XML saving the viewer will fire life-cycle events:
+ *
+ * * saveXML.start (before serialization)
+ * * saveXML.serialized (after xml generation)
+ * * saveXML.done (everything done)
+ *
+ * You can use these events to hook into the life-cycle.
+ *
* @param {Object} [options] export options
* @param {Boolean} [options.format=false] output formated XML
* @param {Boolean} [options.preamble=true] output preamble
@@ -212,19 +222,52 @@ Viewer.prototype.saveXML = function(options, done) {
options = {};
}
+ var self = this;
+
var definitions = this._definitions;
if (!definitions) {
return done(new Error('no definitions loaded'));
}
- this._moddle.toXML(definitions, options, done);
+ // allow to fiddle around with definitions
+ definitions = this._emit('saveXML.start', {
+ definitions: definitions
+ }) || definitions;
+
+ this._moddle.toXML(definitions, options, function(err, xml) {
+
+ try {
+ xml = self._emit('saveXML.serialized', {
+ error: err,
+ xml: xml
+ }) || xml;
+
+ self._emit('saveXML.done', {
+ error: err,
+ xml: xml
+ });
+ } catch (e) {
+ console.error('error in saveXML life-cycle listener', e);
+ }
+
+ done(err, xml);
+ });
};
/**
* Export the currently displayed BPMN 2.0 diagram as
* an SVG image.
*
+ * ## Life-Cycle Events
+ *
+ * During SVG saving the viewer will fire life-cycle events:
+ *
+ * * saveSVG.start (before serialization)
+ * * saveSVG.done (everything done)
+ *
+ * You can use these events to hook into the life-cycle.
+ *
* @param {Object} [options]
* @param {Function} done invoked with (err, svgStr)
*/
@@ -235,6 +278,8 @@ Viewer.prototype.saveSVG = function(options, done) {
options = {};
}
+ this._emit('saveSVG.start');
+
var svg, err;
try {
@@ -261,6 +306,11 @@ Viewer.prototype.saveSVG = function(options, done) {
err = e;
}
+ this._emit('saveSVG.done', {
+ error: err,
+ svg: svg
+ });
+
done(err, svg);
};
diff --git a/test/spec/ViewerSpec.js b/test/spec/ViewerSpec.js
index 83538a46..c9d0f404 100644
--- a/test/spec/ViewerSpec.js
+++ b/test/spec/ViewerSpec.js
@@ -294,162 +294,6 @@ describe('Viewer', function() {
});
- describe('export', function() {
-
- function currentTime() {
- return new Date().getTime();
- }
-
- function validSVG(svg) {
- var expectedStart = '';
- var expectedEnd = '';
-
- expect(svg.indexOf(expectedStart)).to.equal(0);
- expect(svg.indexOf(expectedEnd)).to.equal(svg.length - expectedEnd.length);
-
- // ensure correct rendering of SVG contents
- expect(svg.indexOf('undefined')).to.equal(-1);
-
- // expect header to be written only once
- expect(svg.indexOf('';
+
+ expect(svg.indexOf(expectedStart)).to.equal(0);
+ expect(svg.indexOf(expectedEnd)).to.equal(svg.length - expectedEnd.length);
+
+ // ensure correct rendering of SVG contents
+ expect(svg.indexOf('undefined')).to.equal(-1);
+
+ // expect header to be written only once
+ expect(svg.indexOf('