UNPKG

5.65 kBJavaScriptView Raw
1'use strict';
2
3var domQuery = require('min-dom').query,
4 domClear = require('min-dom').clear,
5 is = require('bpmn-js/lib/util/ModelUtil').is,
6 forEach = require('lodash/forEach'),
7 domify = require('min-dom').domify,
8 Ids = require('ids').default;
9
10var SPACE_REGEX = /\s/;
11
12// for QName validation as per http://www.w3.org/TR/REC-xml/#NT-NameChar
13var QNAME_REGEX = /^([a-z][\w-.]*:)?[a-z_][\w-.]*$/i;
14
15// for ID validation as per BPMN Schema (QName - Namespace)
16var ID_REGEX = /^[a-z_][\w-.]*$/i;
17
18var PLACEHOLDER_REGEX = /\$\{([^}]*)\}/g;
19
20var HTML_ESCAPE_MAP = {
21 '&': '&',
22 '<': '&lt;',
23 '>': '&gt;',
24 '"': '&quot;',
25 '\'': '&#39;'
26};
27
28function selectedOption(selectBox) {
29 if (selectBox.selectedIndex >= 0) {
30 return selectBox.options[selectBox.selectedIndex].value;
31 }
32}
33
34module.exports.selectedOption = selectedOption;
35
36
37function selectedType(elementSyntax, inputNode) {
38 var typeSelect = domQuery(elementSyntax, inputNode);
39 return selectedOption(typeSelect);
40}
41
42module.exports.selectedType = selectedType;
43
44
45/**
46 * Retrieve the root element the document this
47 * business object is contained in.
48 *
49 * @return {ModdleElement}
50 */
51function getRoot(businessObject) {
52 var parent = businessObject;
53 while (parent.$parent) {
54 parent = parent.$parent;
55 }
56 return parent;
57}
58
59module.exports.getRoot = getRoot;
60
61
62/**
63 * filters all elements in the list which have a given type.
64 * removes a new list
65 */
66function filterElementsByType(objectList, type) {
67 var list = objectList || [];
68 var result = [];
69 forEach(list, function(obj) {
70 if (is(obj, type)) {
71 result.push(obj);
72 }
73 });
74 return result;
75}
76
77module.exports.filterElementsByType = filterElementsByType;
78
79
80function findRootElementsByType(businessObject, referencedType) {
81 var root = getRoot(businessObject);
82
83 return filterElementsByType(root.rootElements, referencedType);
84}
85
86module.exports.findRootElementsByType = findRootElementsByType;
87
88
89function removeAllChildren(domElement) {
90 while (domElement.firstChild) {
91 domElement.removeChild(domElement.firstChild);
92 }
93}
94
95module.exports.removeAllChildren = removeAllChildren;
96
97
98/**
99 * adds an empty option to the list
100 */
101function addEmptyParameter(list) {
102 return list.push({ 'label': '', 'value': '', 'name': '' });
103}
104
105module.exports.addEmptyParameter = addEmptyParameter;
106
107
108/**
109 * returns a list with all root elements for the given parameter 'referencedType'
110 */
111function refreshOptionsModel(businessObject, referencedType) {
112 var model = [];
113 var referableObjects = findRootElementsByType(businessObject, referencedType);
114 forEach(referableObjects, function(obj) {
115 model.push({
116 label: (obj.name || '') + ' (id='+obj.id+')',
117 value: obj.id,
118 name: obj.name
119 });
120 });
121 return model;
122}
123
124module.exports.refreshOptionsModel = refreshOptionsModel;
125
126
127/**
128 * fills the drop down with options
129 */
130function updateOptionsDropDown(domSelector, businessObject, referencedType, entryNode) {
131 var options = refreshOptionsModel(businessObject, referencedType);
132 addEmptyParameter(options);
133 var selectBox = domQuery(domSelector, entryNode);
134 domClear(selectBox);
135
136 forEach(options, function(option) {
137 var optionEntry = domify('<option value="' + escapeHTML(option.value) + '">' + escapeHTML(option.label) + '</option>');
138 selectBox.appendChild(optionEntry);
139 });
140 return options;
141}
142
143module.exports.updateOptionsDropDown = updateOptionsDropDown;
144
145
146/**
147 * checks whether the id value is valid
148 *
149 * @param {ModdleElement} bo
150 * @param {String} idValue
151 * @param {Function} translate
152 *
153 * @return {String} error message
154 */
155function isIdValid(bo, idValue, translate) {
156 var assigned = bo.$model.ids.assigned(idValue);
157
158 var idExists = assigned && assigned !== bo;
159
160 if (!idValue || idExists) {
161 return translate('Element must have an unique id.');
162 }
163
164 return validateId(idValue, translate);
165}
166
167module.exports.isIdValid = isIdValid;
168
169
170function validateId(idValue, translate) {
171
172 idValue = stripPlaceholders(idValue);
173
174 if (containsSpace(idValue)) {
175 return translate('Id must not contain spaces.');
176 }
177
178 if (!ID_REGEX.test(idValue)) {
179
180 if (QNAME_REGEX.test(idValue)) {
181 return translate('Id must not contain prefix.');
182 }
183
184 return translate('Id must be a valid QName.');
185 }
186}
187
188module.exports.validateId = validateId;
189
190
191function containsSpace(value) {
192 return SPACE_REGEX.test(value);
193}
194
195module.exports.containsSpace = containsSpace;
196
197
198function stripPlaceholders(idValue) {
199
200 // replace expression e.g. ${VERSION_TAG}
201 // use only the content between ${}
202 // for the REGEX check
203 return idValue.replace(PLACEHOLDER_REGEX, '$1');
204}
205
206/**
207 * generate a semantic id with given prefix
208 */
209function nextId(prefix) {
210 var ids = new Ids([32,32,1]);
211
212 return ids.nextPrefixed(prefix);
213}
214
215module.exports.nextId = nextId;
216
217
218function triggerClickEvent(element) {
219 var evt;
220 var eventType = 'click';
221
222 if (document.createEvent) {
223 try {
224 // Chrome, Safari, Firefox
225 evt = new MouseEvent((eventType), { view: window, bubbles: true, cancelable: true });
226 } catch (e) {
227 // IE 11, PhantomJS (wat!)
228 evt = document.createEvent('MouseEvent');
229
230 evt.initEvent((eventType), true, true);
231 }
232 return element.dispatchEvent(evt);
233 } else {
234 // Welcome IE
235 evt = document.createEventObject();
236
237 return element.fireEvent('on' + eventType, evt);
238 }
239}
240
241module.exports.triggerClickEvent = triggerClickEvent;
242
243
244function escapeHTML(str) {
245 str = '' + str;
246
247 return str && str.replace(/[&<>"']/g, function(match) {
248 return HTML_ESCAPE_MAP[match];
249 });
250}
251
252module.exports.escapeHTML = escapeHTML;
\No newline at end of file