@jylups/flowable-bpmn-js 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,4 @@
1
- /*! @jylups/flowable-bpmn-js 0.1.1 | MIT | bpmn-js (c) Camunda Services GmbH, bpmn.io license; keep the bpmn.io watermark visible */
1
+ /*! @jylups/flowable-bpmn-js 0.1.2 | MIT | bpmn-js (c) Camunda Services GmbH, bpmn.io license; keep the bpmn.io watermark visible */
2
2
  var FlowableBpmnJS = (function () {
3
3
  'use strict';
4
4
 
@@ -27889,6 +27889,181 @@ var FlowableBpmnJS = (function () {
27889
27889
  definition.$parent = definitions;
27890
27890
  }
27891
27891
 
27892
+ /**
27893
+ * Keeps a Flowable model as it was written where bpmn-js would change it
27894
+ * although nothing was edited:
27895
+ *
27896
+ * - Expanded subprocesses: Flowable Design and the Flowable engine write an
27897
+ * expanded subprocess shape without `isExpanded`, which BPMN DI reads as
27898
+ * collapsed. bpmn-js would draw it closed and, on save, move its children's
27899
+ * shapes to a diagram of their own with new coordinates. A subprocess shape
27900
+ * without `isExpanded` whose children are drawn on the same plane, or which
27901
+ * has no children, is read as expanded; the attribute isn't written back.
27902
+ * - Exclusive gateway markers: bpmn-js sets `isMarkerVisible` on a gateway
27903
+ * shape that has none, so the marker shows as it does in Flowable. It isn't
27904
+ * written back where the model had no such attribute.
27905
+ * - Attributes with their default value, such as `typeLanguage`,
27906
+ * `expressionLanguage` or `isInterrupting="true"`: moddle doesn't write a
27907
+ * value equal to the default, so a model that states it would lose it. A
27908
+ * value the model sets (read from the file or set by an edit) is written,
27909
+ * even when it equals the default.
27910
+ */
27911
+
27912
+ /**
27913
+ * Reads the subprocess and gateway shapes as Flowable does. Call it when
27914
+ * the model is parsed, before bpmn-js draws it.
27915
+ *
27916
+ * @param {ModdleElement} definitions
27917
+ *
27918
+ * @return {{ expanded: Set<ModdleElement>, markers: Set<ModdleElement> }} the
27919
+ * shapes that got `isExpanded`, and the gateway shapes without
27920
+ * `isMarkerVisible`, so saving can leave the attributes out again
27921
+ */
27922
+ function readAsFlowable(definitions) {
27923
+ const expanded = new Set();
27924
+ const markers = new Set();
27925
+
27926
+ const planes = (definitions.get('diagrams') || [])
27927
+ .map(diagram => diagram.plane)
27928
+ .filter(Boolean);
27929
+
27930
+ for (const plane of planes) {
27931
+ const shapes = (plane.get('planeElement') || []).filter(di => di.$instanceOf('bpmndi:BPMNShape'));
27932
+ const drawn = new Set(shapes.map(di => di.bpmnElement).filter(Boolean));
27933
+
27934
+ for (const di of shapes) {
27935
+ const element = di.bpmnElement;
27936
+
27937
+ if (!element) {
27938
+ continue;
27939
+ }
27940
+
27941
+ if (element.$instanceOf('bpmn:SubProcess') && !hasOwn(di, 'isExpanded') && isDrawnExpanded(element, drawn)) {
27942
+ di.isExpanded = true;
27943
+ expanded.add(di);
27944
+ }
27945
+
27946
+ if (element.$instanceOf('bpmn:ExclusiveGateway') && !hasOwn(di, 'isMarkerVisible')) {
27947
+ markers.add(di);
27948
+ }
27949
+ }
27950
+ }
27951
+
27952
+ return { expanded, markers };
27953
+ }
27954
+
27955
+ /**
27956
+ * Prepares the model to be written as it was read (see the module comment).
27957
+ * Call it when saving starts, and the returned function when saving is done.
27958
+ *
27959
+ * @param {ModdleElement} definitions
27960
+ * @param {{ expanded: Set<ModdleElement>, markers: Set<ModdleElement> }} read
27961
+ * what readAsFlowable returned
27962
+ *
27963
+ * @return {Function} restores the model as the editor uses it
27964
+ */
27965
+ function writeAsRead(definitions, { expanded, markers }) {
27966
+ const restore = [];
27967
+
27968
+ // leave out what reading added, while it still has the value reading gave it
27969
+ for (const di of expanded) {
27970
+ leaveOut(di, 'isExpanded', true, restore);
27971
+ }
27972
+
27973
+ for (const di of markers) {
27974
+ leaveOut(di, 'isMarkerVisible', true, restore);
27975
+ }
27976
+
27977
+ // write values that equal the default as generic attributes, which moddle always writes
27978
+ forEachElement(definitions, element => {
27979
+ for (const property of element.$descriptor.properties) {
27980
+ if (!property.isAttr || property.isMany || property.default === undefined) {
27981
+ continue;
27982
+ }
27983
+
27984
+ if (!hasOwn(element, property.name) || element[property.name] !== property.default) {
27985
+ continue;
27986
+ }
27987
+
27988
+ const name = attributeName(element, property);
27989
+ const attrs = element.$attrs;
27990
+
27991
+ if (attrs[name] === undefined) {
27992
+ attrs[name] = String(property.default);
27993
+ restore.push(() => delete attrs[name]);
27994
+ }
27995
+ }
27996
+ });
27997
+
27998
+ return function() {
27999
+ restore.reverse().forEach(fn => fn());
28000
+ };
28001
+ }
28002
+
28003
+
28004
+ function isDrawnExpanded(subProcess, drawn) {
28005
+ const children = [
28006
+ ...(subProcess.get('flowElements') || []),
28007
+ ...(subProcess.get('artifacts') || [])
28008
+ ];
28009
+
28010
+ return !children.length || children.some(child => drawn.has(child));
28011
+ }
28012
+
28013
+ function leaveOut(element, name, value, restore) {
28014
+ if (element[name] === value) {
28015
+ delete element[name];
28016
+ restore.push(() => {
28017
+ element[name] = value;
28018
+ });
28019
+ }
28020
+ }
28021
+
28022
+ /**
28023
+ * The name the attribute has in the XML: without a prefix in the element's
28024
+ * own namespace, with its prefix in another one (an extension attribute).
28025
+ */
28026
+ function attributeName(element, property) {
28027
+ const ns = property.ns;
28028
+
28029
+ return ns.prefix === element.$descriptor.ns.prefix ? ns.localName : ns.prefix + ':' + ns.localName;
28030
+ }
28031
+
28032
+ /**
28033
+ * Calls fn for the element and every model element it contains (not the
28034
+ * ones it refers to).
28035
+ */
28036
+ function forEachElement(element, fn, seen = new Set()) {
28037
+ if (!isModdleElement(element) || seen.has(element)) {
28038
+ return;
28039
+ }
28040
+
28041
+ seen.add(element);
28042
+ fn(element);
28043
+
28044
+ for (const property of element.$descriptor.properties) {
28045
+ if (property.isReference || property.isAttr) {
28046
+ continue;
28047
+ }
28048
+
28049
+ const value = element[property.name];
28050
+
28051
+ if (Array.isArray(value)) {
28052
+ value.forEach(child => forEachElement(child, fn, seen));
28053
+ } else {
28054
+ forEachElement(value, fn, seen);
28055
+ }
28056
+ }
28057
+ }
28058
+
28059
+ function isModdleElement(value) {
28060
+ return !!value && typeof value === 'object' && !!value.$descriptor && !!value.$attrs;
28061
+ }
28062
+
28063
+ function hasOwn(object, name) {
28064
+ return Object.prototype.hasOwnProperty.call(object, name);
28065
+ }
28066
+
27892
28067
  /**
27893
28068
  * Runs before bpmn-js claims the imported IDs, so created definitions are
27894
28069
  * claimed too.
@@ -27907,11 +28082,17 @@ var FlowableBpmnJS = (function () {
27907
28082
  * signal or escalation they stand for (createMissingDefinitions from the
27908
28083
  * behaviors package), in the viewers too;
27909
28084
  * - on save, other prefixes bound to the Flowable namespace are dropped, so
27910
- * everything is written as flowable: (useFlowablePrefix).
28085
+ * everything is written as flowable: (useFlowablePrefix);
28086
+ * - an unedited model is written as it was read: expanded subprocesses
28087
+ * without `isExpanded`, gateway markers and attributes with their default
28088
+ * value stay as the file had them (see WriteAsRead.js).
27911
28089
  */
27912
28090
  class FlowableXML {
27913
28091
 
27914
28092
  constructor(eventBus, moddle) {
28093
+ let read = { expanded: new Set(), markers: new Set() };
28094
+ let restore = null;
28095
+
27915
28096
  eventBus.on('import.parse.start', ({ xml }) => {
27916
28097
  const migrated = migrateActivitiXML(xml);
27917
28098
 
@@ -27923,10 +28104,24 @@ var FlowableBpmnJS = (function () {
27923
28104
  eventBus.on('import.parse.complete', HIGH_PRIORITY$1, ({ error, definitions, warnings, elementsById }) => {
27924
28105
  if (!error && definitions) {
27925
28106
  createMissingDefinitions(definitions, { warnings, elementsById });
28107
+
28108
+ // before bpmn-js draws the model (and moves the shapes of collapsed subprocesses)
28109
+ read = readAsFlowable(definitions);
27926
28110
  }
27927
28111
  });
27928
28112
 
27929
- eventBus.on('saveXML.start', ({ definitions }) => useFlowablePrefix(definitions));
28113
+ eventBus.on('saveXML.start', ({ definitions }) => {
28114
+ useFlowablePrefix(definitions);
28115
+
28116
+ restore = writeAsRead(definitions, read);
28117
+ });
28118
+
28119
+ eventBus.on('saveXML.done', () => {
28120
+ if (restore) {
28121
+ restore();
28122
+ restore = null;
28123
+ }
28124
+ });
27930
28125
  }
27931
28126
  }
27932
28127