This repository has been archived by the owner on Feb 14, 2024. It is now read-only.
/
UpdatePropertiesHandler.js
240 lines (176 loc) · 5.99 KB
/
UpdatePropertiesHandler.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
'use strict';
var reduce = require('min-dash').reduce,
keys = require('min-dash').keys,
forEach = require('min-dash').forEach,
assign = require('min-dash').assign;
var getBusinessObject = require('../../../util/ModelUtil').getBusinessObject;
var NAME = 'name',
STANDARD_EVENT = 'standardEvent',
ID = 'id',
DEFINITION_REF = 'definitionRef',
SENTRY_REF = 'sentryRef',
IS_STANDARD_EVENT_VISIBLE = 'isStandardEventVisible';
var flatten = require('min-dash').flatten;
/**
* A handler that implements a CMMN 1.1 property update.
*
* This should be used to set simple properties on elements with
* an underlying CMMN business object.
*
* Use respective diagram-js provided handlers if you would
* like to perform automated modeling.
*/
function UpdatePropertiesHandler(elementRegistry, itemRegistry, moddle) {
this._elementRegistry = elementRegistry;
this._itemRegistry = itemRegistry;
this._moddle = moddle;
}
UpdatePropertiesHandler.$inject = [ 'elementRegistry', 'itemRegistry', 'moddle' ];
module.exports = UpdatePropertiesHandler;
// api /////////////////
UpdatePropertiesHandler.prototype.preExecute = function(context) {
var element = context.element,
shape = context.shape;
if (!element) {
throw new Error('element required');
}
var bo = element.businessObject || element;
context.businessObject = bo;
var changed = [];
if (element.businessObject) {
changed.push(element);
}
if (shape) {
changed.push(shape);
}
context.changed = flatten(changed);
};
/**
* Updates a CMMN element with a list of new properties
*
* @param {Object} context
* @param {djs.model.Base} context.element the element to update
* @param {Object} context.properties a list of properties to set on the element's
* businessObject (the CMMN model element)
*
* @return {Array<djs.mode.Base>} the updated element
*/
UpdatePropertiesHandler.prototype.execute = function(context) {
var elementRegistry = this._elementRegistry,
itemRegistry = this._itemRegistry,
ids = this._moddle.ids;
var changed = context.changed;
var element = context.element,
businessObject = context.businessObject,
properties = unwrapBusinessObjects(context.properties),
oldProperties = context.oldProperties || getProperties(businessObject, keys(properties));
if (isIdChange(properties, businessObject)) {
ids.unclaim(businessObject[ID]);
itemRegistry.updateId(businessObject, properties[ID]);
var shape = elementRegistry.get(element.id);
if (shape) {
elementRegistry.updateId(shape, properties[ID]);
}
ids.claim(properties[ID], businessObject);
}
if (isDefinitionRefChange(properties, businessObject)) {
itemRegistry.updateReference(businessObject, properties[DEFINITION_REF]);
}
if (isSentryRefChange(properties, businessObject)) {
itemRegistry.updateReference(businessObject, properties[SENTRY_REF]);
}
if (NAME in properties) {
forEach(changed, function(changedShape) {
if (changedShape.label) {
changed.push(changedShape.label);
}
});
}
if (STANDARD_EVENT in properties || IS_STANDARD_EVENT_VISIBLE in properties) {
forEach(changed, function(changedShape) {
if (changedShape.label) {
changed.push(changedShape.label);
}
});
}
// update properties
setProperties(businessObject, properties);
// store old values
context.oldProperties = oldProperties;
// indicate changed on objects affected by the update
return changed;
};
/**
* Reverts the update on a CMMN elements properties.
*
* @param {Object} context
*
* @return {djs.mode.Base} the updated element
*/
UpdatePropertiesHandler.prototype.revert = function(context) {
var properties = context.properties,
oldProperties = context.oldProperties,
businessObject = context.businessObject,
elementRegistry = this._elementRegistry,
itemRegistry = this._itemRegistry,
ids = this._moddle.ids;
if (isIdChange(oldProperties, businessObject)) {
itemRegistry.updateId(businessObject, oldProperties[ID]);
}
if (isDefinitionRefChange(oldProperties, businessObject)) {
itemRegistry.updateReference(businessObject, oldProperties[DEFINITION_REF]);
}
if (isSentryRefChange(oldProperties, businessObject)) {
itemRegistry.updateReference(businessObject, oldProperties[SENTRY_REF]);
}
// update properties
setProperties(businessObject, oldProperties);
if (isIdChange(properties, businessObject)) {
ids.unclaim(properties[ID]);
var shape = elementRegistry.get(properties[ID]);
if (shape) {
elementRegistry.updateId(shape, oldProperties[ID]);
}
ids.claim(oldProperties[ID], businessObject);
}
return context.changed;
};
function isIdChange(properties, businessObject) {
return ID in properties && properties[ID] !== businessObject[ID];
}
function isDefinitionRefChange(properties, businessObject) {
return DEFINITION_REF in properties && properties[DEFINITION_REF] !== businessObject[DEFINITION_REF];
}
function isSentryRefChange(properties, businessObject) {
return SENTRY_REF in properties && properties[SENTRY_REF] !== businessObject[SENTRY_REF];
}
function getProperties(businessObject, propertyNames) {
return reduce(propertyNames, function(result, key) {
result[key] = businessObject.get(key);
return result;
}, {});
}
function setProperties(businessObject, properties) {
forEach(properties, function(value, key) {
businessObject.set(key, value);
});
}
var referencePropertyNames = [ 'default' ];
/**
* Make sure we unwrap the actual business object
* behind diagram element that may have been
* passed as arguments.
*
* @param {Object} properties
*
* @return {Object} unwrappedProps
*/
function unwrapBusinessObjects(properties) {
var unwrappedProps = assign({}, properties);
referencePropertyNames.forEach(function(name) {
if (name in properties) {
unwrappedProps[name] = getBusinessObject(unwrappedProps[name]);
}
});
return unwrappedProps;
}