@panneau/core 4.0.38 → 4.0.40-alpha.1

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.
package/dist/utils.js CHANGED
@@ -1,38 +1,26 @@
1
1
  import isNumber from 'lodash/isNumber';
2
2
  import { snakeCase } from 'snake-case';
3
- import _regenerator from '@babel/runtime/helpers/regenerator';
4
- import _asyncToGenerator from '@babel/runtime/helpers/asyncToGenerator';
5
3
  import isObject from 'lodash/isObject';
6
4
  import { useEffect, isValidElement } from 'react';
7
- import _defineProperty from '@babel/runtime/helpers/defineProperty';
8
- import _objectSpread from '@babel/runtime/helpers/objectSpread2';
9
- import ReactIs from 'react-is';
10
- import _objectWithoutProperties from '@babel/runtime/helpers/objectWithoutProperties';
5
+ import { isValidElementType } from 'react-is';
11
6
  import isString from 'lodash/isString';
12
7
  import { pascalCase, kebabCase, snakeCase as snakeCase$1 } from 'change-case';
13
8
  export { kebabCase as dashCase, pascalCase, snakeCase } from 'change-case';
14
9
  import isArray from 'lodash/isArray';
15
- import _toConsumableArray from '@babel/runtime/helpers/toConsumableArray';
16
10
  import get from 'lodash/get';
17
11
  import capitalize from 'lodash/capitalize';
18
12
  import EventEmitter from 'wolfy87-eventemitter';
19
13
  import slugify from 'slugify';
20
- import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
21
- import uniqBy from 'lodash/uniqBy';
22
-
23
- function checkClickable(el) {
24
- var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
25
- var parentDistance = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 1;
26
- var _ref = options || {},
27
- _ref$maxParentDistanc = _ref.maxParentDistance,
28
- maxParentDistance = _ref$maxParentDistanc === void 0 ? 5 : _ref$maxParentDistanc,
29
- _ref$tags = _ref.tags,
30
- tags = _ref$tags === void 0 ? ['BUTTON', 'A', 'INPUT', 'I', 'TEXTAREA'] : _ref$tags;
31
- var _ref2 = el || {},
32
- _ref2$tagName = _ref2.tagName,
33
- tagName = _ref2$tagName === void 0 ? null : _ref2$tagName,
34
- _ref2$parentNode = _ref2.parentNode,
35
- parentNode = _ref2$parentNode === void 0 ? null : _ref2$parentNode;
14
+
15
+ function checkClickable(el, options = {}, parentDistance = 1) {
16
+ const {
17
+ maxParentDistance = 5,
18
+ tags = ['BUTTON', 'A', 'INPUT', 'I', 'TEXTAREA']
19
+ } = options || {};
20
+ const {
21
+ tagName = null,
22
+ parentNode = null
23
+ } = el || {};
36
24
  if (tagName === 'BODY') {
37
25
  return false;
38
26
  }
@@ -46,9 +34,7 @@ function checkClickable(el) {
46
34
  // return true;
47
35
  // }
48
36
 
49
- if (tagName && tags.map(function (it) {
50
- return it.toLowerCase();
51
- }).indexOf(tagName.toLowerCase()) !== -1) {
37
+ if (tagName && tags.map(it => it.toLowerCase()).indexOf(tagName.toLowerCase()) !== -1) {
52
38
  return true;
53
39
  }
54
40
  if (parentDistance < maxParentDistance) {
@@ -58,19 +44,14 @@ function checkClickable(el) {
58
44
  }
59
45
 
60
46
  function convertStyleToString(style) {
61
- return style !== null ? Object.keys(style).map(function (key) {
62
- return "".concat(snakeCase(key), ":").concat(isNumber(style[key]) ? "".concat(style[key], "px") : style[key], ";");
63
- }).join('\n') : '';
47
+ return style !== null ? Object.keys(style).map(key => `${snakeCase(key)}:${isNumber(style[key]) ? `${style[key]}px` : style[key]};`).join('\n') : '';
64
48
  }
65
49
 
66
- function createNullableOnChange() {
67
- var onChange = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
50
+ function createNullableOnChange(onChange = null) {
68
51
  return function nullableOnChange(newValue) {
69
- var nullableValue = newValue;
52
+ let nullableValue = newValue;
70
53
  if (isObject(newValue)) {
71
- var allNull = Object.keys(newValue).reduce(function (acc, key) {
72
- return acc && newValue[key] === null;
73
- }, true);
54
+ const allNull = Object.keys(newValue).reduce((acc, key) => acc && newValue[key] === null, true);
74
55
  if (allNull) {
75
56
  nullableValue = null;
76
57
  }
@@ -82,13 +63,12 @@ function createNullableOnChange() {
82
63
  }
83
64
 
84
65
  function createUseEvent(eventsManager) {
85
- return function useEvent(event, callback) {
86
- var enabled = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
87
- useEffect(function () {
66
+ return function useEvent(event, callback, enabled = true) {
67
+ useEffect(() => {
88
68
  if (enabled && eventsManager !== null) {
89
69
  eventsManager.subscribe(event, callback);
90
70
  }
91
- return function () {
71
+ return () => {
92
72
  if (enabled && eventsManager !== null) {
93
73
  eventsManager.unsubscribe(event, callback);
94
74
  }
@@ -105,248 +85,184 @@ function createUseEvent(eventsManager) {
105
85
  function makeError() {
106
86
  return new DOMException('The request is not allowed', 'NotAllowedError');
107
87
  }
108
- function copyClipboardApi(_x) {
109
- return _copyClipboardApi.apply(this, arguments);
110
- }
111
- function _copyClipboardApi() {
112
- _copyClipboardApi = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(text) {
113
- return _regenerator().w(function (_context) {
114
- while (1) switch (_context.n) {
115
- case 0:
116
- if (navigator.clipboard) {
117
- _context.n = 1;
118
- break;
119
- }
120
- throw makeError();
121
- case 1:
122
- return _context.a(2, navigator.clipboard.writeText(text));
123
- }
124
- }, _callee);
125
- }));
126
- return _copyClipboardApi.apply(this, arguments);
127
- }
128
- function copyExecCommand(_x2) {
129
- return _copyExecCommand.apply(this, arguments);
130
- }
131
- function _copyExecCommand() {
132
- _copyExecCommand = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee2(text) {
133
- var span, selection, range, success;
134
- return _regenerator().w(function (_context2) {
135
- while (1) switch (_context2.n) {
136
- case 0:
137
- // Put the text to copy into a <span>
138
- span = document.createElement('span');
139
- span.textContent = text;
140
-
141
- // Preserve consecutive spaces and newlines
142
- span.style.whiteSpace = 'pre';
143
- span.style.webkitUserSelect = 'auto';
144
- span.style.userSelect = 'all';
145
-
146
- // Add the <span> to the page
147
- document.body.appendChild(span);
148
-
149
- // Make a selection object representing the range of text selected by the user
150
- selection = window.getSelection();
151
- range = window.document.createRange();
152
- selection === null || selection === void 0 || selection.removeAllRanges();
153
- range.selectNode(span);
154
- selection === null || selection === void 0 || selection.addRange(range);
155
-
156
- // Copy text to the clipboard
157
- success = false;
158
- try {
159
- success = window.document.execCommand('copy');
160
- } finally {
161
- // Cleanup
162
- selection === null || selection === void 0 || selection.removeAllRanges();
163
- window.document.body.removeChild(span);
164
- }
165
- if (success) {
166
- _context2.n = 1;
167
- break;
168
- }
169
- throw makeError();
170
- case 1:
171
- return _context2.a(2);
172
- }
173
- }, _callee2);
174
- }));
175
- return _copyExecCommand.apply(this, arguments);
176
- }
177
- function copyToClipboard(_x3) {
178
- return _copyToClipboard.apply(this, arguments);
179
- }
180
- function _copyToClipboard() {
181
- _copyToClipboard = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee3(text) {
182
- var _t, _t2;
183
- return _regenerator().w(function (_context3) {
184
- while (1) switch (_context3.p = _context3.n) {
185
- case 0:
186
- _context3.p = 0;
187
- _context3.n = 1;
188
- return copyClipboardApi(text);
189
- case 1:
190
- _context3.n = 6;
191
- break;
192
- case 2:
193
- _context3.p = 2;
194
- _t = _context3.v;
195
- _context3.p = 3;
196
- _context3.n = 4;
197
- return copyExecCommand(text);
198
- case 4:
199
- _context3.n = 6;
200
- break;
201
- case 5:
202
- _context3.p = 5;
203
- _t2 = _context3.v;
204
- throw _t2 || _t || makeError();
205
- case 6:
206
- return _context3.a(2);
207
- }
208
- }, _callee3, null, [[3, 5], [0, 2]]);
209
- }));
210
- return _copyToClipboard.apply(this, arguments);
88
+ async function copyClipboardApi(text) {
89
+ // Use the Async Clipboard API when available. Requires a secure browsing
90
+ // context (i.e. HTTPS)
91
+ if (!navigator.clipboard) {
92
+ throw makeError();
93
+ }
94
+ return navigator.clipboard.writeText(text);
95
+ }
96
+ async function copyExecCommand(text) {
97
+ // Put the text to copy into a <span>
98
+ const span = document.createElement('span');
99
+ span.textContent = text;
100
+
101
+ // Preserve consecutive spaces and newlines
102
+ span.style.whiteSpace = 'pre';
103
+ span.style.webkitUserSelect = 'auto';
104
+ span.style.userSelect = 'all';
105
+
106
+ // Add the <span> to the page
107
+ document.body.appendChild(span);
108
+
109
+ // Make a selection object representing the range of text selected by the user
110
+ const selection = window.getSelection();
111
+ const range = window.document.createRange();
112
+ selection?.removeAllRanges();
113
+ range.selectNode(span);
114
+ selection?.addRange(range);
115
+
116
+ // Copy text to the clipboard
117
+ let success = false;
118
+ try {
119
+ success = window.document.execCommand('copy');
120
+ } finally {
121
+ // Cleanup
122
+ selection?.removeAllRanges();
123
+ window.document.body.removeChild(span);
124
+ }
125
+ if (!success) throw makeError();
126
+ }
127
+ async function copyToClipboard(text) {
128
+ try {
129
+ await copyClipboardApi(text);
130
+ } catch (err) {
131
+ // ...Otherwise, use document.execCommand() fallback
132
+ try {
133
+ await copyExecCommand(text);
134
+ } catch (err2) {
135
+ throw err2 || err || makeError();
136
+ }
137
+ }
211
138
  }
212
139
 
213
- function flattenComponents(components) {
214
- var prefix = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
140
+ function flattenComponents(components, prefix = null) {
215
141
  if (components === null) {
216
142
  return null;
217
143
  }
218
- return Object.keys(components).reduce(function (newMap, key) {
219
- return ReactIs.isValidElementType(components[key]) ? _objectSpread(_objectSpread({}, newMap), {}, _defineProperty({}, prefix !== null ? "".concat(prefix, ".").concat(key) : key, components[key])) : _objectSpread(_objectSpread({}, newMap), flattenComponents(components[key], key));
144
+ return Object.keys(components).reduce((newMap, key) => isValidElementType(components[key]) ? {
145
+ ...newMap,
146
+ [prefix !== null ? `${prefix}.${key}` : key]: components[key]
147
+ } : {
148
+ ...newMap,
149
+ ...flattenComponents(components[key], key)
220
150
  }, {});
221
151
  }
222
152
 
223
153
  function formatDuration(timeInSeconds) {
224
- var numberOfSeconds = parseInt(String(timeInSeconds), 10); // don't forget the second param
225
- var hours = Math.floor(numberOfSeconds / 3600);
226
- var minutes = Math.floor((numberOfSeconds - hours * 3600) / 60);
227
- var seconds = numberOfSeconds - hours * 3600 - minutes * 60;
154
+ const numberOfSeconds = parseInt(String(timeInSeconds), 10); // don't forget the second param
155
+ let hours = Math.floor(numberOfSeconds / 3600);
156
+ let minutes = Math.floor((numberOfSeconds - hours * 3600) / 60);
157
+ let seconds = numberOfSeconds - hours * 3600 - minutes * 60;
228
158
  if (hours < 10) {
229
- hours = "0".concat(hours);
159
+ hours = `0${hours}`;
230
160
  }
231
161
  if (minutes < 10) {
232
- minutes = "0".concat(minutes);
162
+ minutes = `0${minutes}`;
233
163
  }
234
164
  if (seconds < 10) {
235
- seconds = "0".concat(seconds);
165
+ seconds = `0${seconds}`;
236
166
  }
237
- return "".concat(hours, ":").concat(minutes, ":").concat(seconds);
167
+ return `${hours}:${minutes}:${seconds}`;
238
168
  }
239
169
 
240
- var _excluded$1 = ["component"];
241
170
  function getComponent(component) {
242
- var _ref = isObject(component) ? component : {
243
- component: component
244
- };
245
- _ref.component;
246
- var props = _objectWithoutProperties(_ref, _excluded$1);
171
+ const {
172
+ component: name,
173
+ ...props
174
+ } = isObject(component) ? component : {
175
+ component
176
+ };
247
177
  return {
248
178
  name: component,
249
179
  props: Object.keys(props).length > 0 ? props : null
250
180
  };
251
181
  }
252
182
 
253
- var _excluded = ["id", "field"];
254
183
  function getColumnFromField(field) {
255
184
  if (field === null) {
256
185
  return null;
257
186
  }
258
187
  // eslint-disable-next-line camelcase
259
- var _ref = field,
260
- name = _ref.name,
261
- _ref$components = _ref.components,
262
- _ref$components2 = _ref$components === void 0 ? {} : _ref$components,
263
- _ref$components2$disp = _ref$components2.display,
264
- display = _ref$components2$disp === void 0 ? null : _ref$components2$disp,
265
- label = _ref.label;
266
- var _getComponent = getComponent(display),
267
- _getComponent$name = _getComponent.name,
268
- componentName = _getComponent$name === void 0 ? null : _getComponent$name,
269
- _getComponent$props = _getComponent.props,
270
- componentProps = _getComponent$props === void 0 ? null : _getComponent$props;
271
- return _objectSpread(_objectSpread({}, componentProps), {}, {
188
+ const {
189
+ name,
190
+ components: {
191
+ display = null
192
+ } = {},
193
+ label
194
+ } = field;
195
+ const {
196
+ name: componentName = null,
197
+ props: componentProps = null
198
+ } = getComponent(display);
199
+ return {
200
+ ...componentProps,
272
201
  id: name,
273
- label: label,
202
+ label,
274
203
  valueKey: name,
275
204
  component: componentName,
276
- field: field
277
- });
205
+ field
206
+ };
278
207
  }
279
208
  function getColumnsWithFields(resource, columns) {
280
- var _resource$fields = resource.fields,
281
- fields = _resource$fields === void 0 ? [] : _resource$fields;
282
- var newColumns = columns !== null ? columns.map(function (column) {
283
- var _ref2 = isString(column) ? {
284
- field: column
285
- } : column,
286
- _ref2$id = _ref2.id,
287
- colId = _ref2$id === void 0 ? null : _ref2$id,
288
- _ref2$field = _ref2.field,
289
- colField = _ref2$field === void 0 ? null : _ref2$field,
290
- otherProps = _objectWithoutProperties(_ref2, _excluded);
291
- var fieldName = colField || colId;
292
- return _objectSpread(_objectSpread(_objectSpread({}, fieldName !== null ? getColumnFromField(fields.find(function (it) {
293
- return it.name === fieldName;
294
- }) || null) : null), otherProps), {}, {
209
+ const {
210
+ fields = []
211
+ } = resource;
212
+ const newColumns = columns !== null ? columns.map(column => {
213
+ const {
214
+ id: colId = null,
215
+ field: colField = null,
216
+ ...otherProps
217
+ } = isString(column) ? {
218
+ field: column
219
+ } : column;
220
+ const fieldName = colField || colId;
221
+ return {
222
+ ...(fieldName !== null ? getColumnFromField(fields.find(it => it.name === fieldName) || null) : null),
223
+ ...otherProps,
295
224
  id: colId || (isString(column) ? column : null) || null
296
- });
297
- }).filter(function (it) {
298
- return it !== null;
299
- }) : [];
225
+ };
226
+ }).filter(it => it !== null) : [];
300
227
  return newColumns;
301
228
  }
302
229
 
303
- function getComponentFromName() {
304
- var name = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
305
- var components = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
306
- var defaultComponent = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
230
+ function getComponentFromName(name = null, components = {}, defaultComponent = null) {
307
231
  if (components === null || name === null) {
308
232
  return defaultComponent;
309
233
  }
310
- var pascalName = pascalCase(name);
311
- var component = components[pascalName] || components[name] || defaultComponent;
234
+ const pascalName = pascalCase(name);
235
+ const component = components[pascalName] || components[name] || defaultComponent;
312
236
  if (!component) {
313
237
  console.warn('Could not find component from name', name, pascalName, components);
314
238
  }
315
239
  return component;
316
240
  }
317
241
 
318
- function getDefinitionFromId() {
319
- var id = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
320
- var items = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
321
- var defaultObject = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
242
+ function getDefinitionFromId(id = null, items = [], defaultObject = null) {
322
243
  if (items === null || id === null || isArray(items) && items.length === 0) {
323
244
  return defaultObject;
324
245
  }
325
- var pascalId = pascalCase(id);
326
- return items.find(function (i) {
327
- return i.id === pascalId;
328
- }) || items.find(function (i) {
329
- return i.id === id;
330
- }) || defaultObject;
246
+ const pascalId = pascalCase(id);
247
+ return items.find(i => i.id === pascalId) || items.find(i => i.id === id) || defaultObject;
331
248
  }
332
249
 
333
- function getDisplayName(_ref) {
334
- var _ref$displayName = _ref.displayName,
335
- displayName = _ref$displayName === void 0 ? null : _ref$displayName,
336
- _ref$name = _ref.name,
337
- name = _ref$name === void 0 ? null : _ref$name;
250
+ function getDisplayName({
251
+ displayName = null,
252
+ name = null
253
+ }) {
338
254
  return displayName || name || 'Component';
339
255
  }
340
256
 
341
257
  function getFieldByName(fields, name) {
342
- return fields.reduce(function (foundField, it) {
258
+ return fields.reduce((foundField, it) => {
343
259
  if (foundField !== null) {
344
260
  return foundField;
345
261
  }
346
- var _it$name = it.name,
347
- fieldName = _it$name === void 0 ? null : _it$name,
348
- _it$fields = it.fields,
349
- subFields = _it$fields === void 0 ? [] : _it$fields;
262
+ const {
263
+ name: fieldName = null,
264
+ fields: subFields = []
265
+ } = it;
350
266
  if (name !== null && fieldName === name) {
351
267
  return it;
352
268
  }
@@ -355,73 +271,68 @@ function getFieldByName(fields, name) {
355
271
  }
356
272
 
357
273
  function getFieldFromPath(path, fields, fieldManager) {
358
- return (isArray(path) ? path : [path]).reduce(function (foundField, key) {
274
+ return (isArray(path) ? path : [path]).reduce((foundField, key) => {
359
275
  if (foundField === null) {
360
276
  return null;
361
277
  }
362
- var _foundField$type = foundField.type,
363
- type = _foundField$type === void 0 ? null : _foundField$type,
364
- _foundField$fields = foundField.fields,
365
- fieldFields = _foundField$fields === void 0 ? null : _foundField$fields,
366
- _foundField$field = foundField.field,
367
- field = _foundField$field === void 0 ? null : _foundField$field;
368
- var finalType = field !== null ? field.type || type : type;
369
- var _ref = finalType !== null ? fieldManager.getDefinition(finalType) : foundField,
370
- _ref$fields = _ref.fields,
371
- subFields = _ref$fields === void 0 ? null : _ref$fields,
372
- _ref$settings = _ref.settings,
373
- settings = _ref$settings === void 0 ? null : _ref$settings,
374
- _ref$itemsField = _ref.itemsField,
375
- itemsField = _ref$itemsField === void 0 ? null : _ref$itemsField;
278
+ const {
279
+ type = null,
280
+ fields: fieldFields = null,
281
+ field = null
282
+ } = foundField;
283
+ const finalType = field !== null ? field.type || type : type;
284
+ const {
285
+ fields: subFields = null,
286
+ settings = null,
287
+ itemsField = null
288
+ } = finalType !== null ? fieldManager.getDefinition(finalType) : foundField;
376
289
  if (itemsField !== null && key.match(/^[0-9]+$/)) {
377
- return _objectSpread(_objectSpread({}, itemsField), {}, {
290
+ return {
291
+ ...itemsField,
378
292
  name: (isArray(path) ? path : [path]).join('/'),
379
293
  listItems: true
380
- });
294
+ };
381
295
  }
382
- return getFieldByName([].concat(_toConsumableArray(fieldFields || []), _toConsumableArray(subFields || []), _toConsumableArray(settings || [])), key);
296
+ return getFieldByName([...(fieldFields || []), ...(subFields || []), ...(settings || [])], key);
383
297
  }, {
384
- fields: fields
298
+ fields
385
299
  });
386
300
  }
387
301
 
388
- function getFileName() {
389
- var url = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
302
+ function getFileName(url = null) {
390
303
  if (url === null || typeof url.match === 'undefined') {
391
304
  return null;
392
305
  }
393
- var match = url.match(/([^/]+)(\?.*)?$/);
306
+ const match = url.match(/([^/]+)(\?.*)?$/);
394
307
  return match ? match[1] : url;
395
308
  }
396
309
 
397
310
  function getPathValue(it, path) {
398
- var value = get(it, path, null);
399
- return value !== null && isObject(value) ? Object.values(value).reduce(function (foundValue, val) {
400
- return foundValue !== null ? foundValue : val;
401
- }, null) : value;
311
+ const value = get(it, path, null);
312
+ return value !== null && isObject(value) ? Object.values(value).reduce((foundValue, val) => foundValue !== null ? foundValue : val, null) : value;
402
313
  }
403
314
 
404
315
  function getSelectOptions(options) {
405
- return options.map(function (it) {
316
+ return options.map(it => {
406
317
  if (isObject(it)) {
407
- var _ref = it,
408
- _ref$label = _ref.label,
409
- label = _ref$label === void 0 ? null : _ref$label,
410
- value = _ref.value;
318
+ const {
319
+ label = null,
320
+ value
321
+ } = it;
411
322
  if (label === null) {
412
323
  return {
413
- value: value,
414
- label: capitalize("".concat(value))
324
+ value,
325
+ label: capitalize(`${value}`)
415
326
  };
416
327
  }
417
328
  return {
418
- value: value,
419
- label: /*#__PURE__*/isValidElement(label) ? label : "".concat(label)
329
+ value,
330
+ label: /*#__PURE__*/isValidElement(label) ? label : `${label}`
420
331
  };
421
332
  }
422
333
  return {
423
334
  value: it,
424
- label: capitalize("".concat(it))
335
+ label: capitalize(`${it}`)
425
336
  };
426
337
  });
427
338
  }
@@ -433,135 +344,102 @@ function isMessage(message) {
433
344
  /**
434
345
  * Locale loader
435
346
  */
436
- var loaders = {};
437
- var events = new EventEmitter();
347
+ const loaders = {};
348
+ const events = new EventEmitter();
438
349
  function loadPackage(name, loader) {
439
350
  if (typeof loaders[name] === 'undefined') {
440
351
  loaders[name] = {
441
- loader: loader().then(function (response) {
352
+ loader: loader().then(response => {
442
353
  loaders[name].loader = null;
443
354
  loaders[name].loaded = true;
444
355
  loaders[name].resource = response;
445
- events.emit("loaded:".concat(name), response);
356
+ events.emit(`loaded:${name}`, response);
446
357
  return response;
447
358
  }),
448
359
  loaded: false,
449
360
  resource: null
450
361
  };
451
362
  }
452
- return new Promise(function (resolve) {
453
- var _ref = loaders[name] || {},
454
- _ref$loaded = _ref.loaded,
455
- loaded = _ref$loaded === void 0 ? false : _ref$loaded,
456
- resource = _ref.resource;
363
+ return new Promise(resolve => {
364
+ const {
365
+ loaded = false,
366
+ resource
367
+ } = loaders[name] || {};
457
368
  if (loaded === true) {
458
369
  resolve(resource);
459
370
  } else {
460
- events.once("loaded:".concat(name), function (response) {
371
+ events.once(`loaded:${name}`, response => {
461
372
  resolve(response);
462
373
  });
463
374
  }
464
375
  });
465
376
  }
466
377
 
467
- function filterNullItems(items) {
468
- return isArray(items) ? items.filter(function (it) {
469
- return it !== null;
470
- }) : items;
471
- }
472
- function getItemsArray(items) {
473
- return (items !== null && isArray(items) ? items.filter(function (it) {
474
- return it !== null;
475
- }) : [items]).filter(function (it) {
476
- return it !== null;
477
- });
478
- }
479
- function selectItem(item, selectedItems, onSelectionChange) {
480
- var multipleSelection = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
481
- var _ref = item || {},
482
- _ref$id = _ref.id,
483
- itemId = _ref$id === void 0 ? null : _ref$id;
484
- var selectedItemsArray = getItemsArray(selectedItems);
485
- var oldItem = selectedItemsArray.find(function (_ref2) {
486
- var id = _ref2.id;
487
- return id === itemId;
488
- }) || null;
489
- var newItems = [];
490
- if (oldItem === null) {
491
- newItems = selectedItems !== null && multipleSelection ? [].concat(_toConsumableArray(selectedItemsArray), [item]) : [item];
492
- } else {
493
- newItems = selectedItemsArray.filter(function (_ref3) {
494
- var id = _ref3.id;
495
- return id !== itemId;
496
- });
497
- }
498
- if (onSelectionChange !== null) {
499
- var _ref4 = newItems || [],
500
- _ref5 = _slicedToArray(_ref4, 1),
501
- _ref5$ = _ref5[0],
502
- firstItem = _ref5$ === void 0 ? null : _ref5$;
503
- var value = multipleSelection ? filterNullItems(newItems) : firstItem;
504
- onSelectionChange(value);
505
- }
506
- }
507
- function selectPage(pageSelected, items, selectedItems, onSelectionChange) {
508
- var nextItems = [];
509
- if (!pageSelected) {
510
- nextItems = uniqBy([].concat(_toConsumableArray(items || []), _toConsumableArray(selectedItems || [])), function () {
511
- var _ref6 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
512
- _ref6$id = _ref6.id,
513
- id = _ref6$id === void 0 ? null : _ref6$id;
514
- return id;
515
- });
516
- } else {
517
- var ids = (items || []).map(function () {
518
- var _ref7 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
519
- _ref7$id = _ref7.id,
520
- id = _ref7$id === void 0 ? null : _ref7$id;
521
- return id;
522
- }).filter(function (id) {
523
- return id !== null;
524
- });
525
- nextItems = uniqBy((selectedItems || []).filter(function (it) {
526
- var _ref8 = it || {},
527
- _ref8$id = _ref8.id,
528
- id = _ref8$id === void 0 ? null : _ref8$id;
529
- return ids.indexOf(id) === -1;
530
- }), function () {
531
- var _ref9 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
532
- _ref9$id = _ref9.id,
533
- id = _ref9$id === void 0 ? null : _ref9$id;
534
- return id;
535
- });
536
- }
537
- var finalNextItems = nextItems;
538
- if (onSelectionChange !== null) {
539
- onSelectionChange(finalNextItems);
378
+ function toggleSelectedItem(items, item, {
379
+ multiple: multipleSelection = false
380
+ } = {}) {
381
+ const {
382
+ id: itemId = null
383
+ } = item || {};
384
+ const currentItem = (items || []).find(({
385
+ id
386
+ }) => id === itemId) || null;
387
+ if (currentItem !== null) {
388
+ return items.filter(({
389
+ id
390
+ }) => id !== itemId);
540
391
  }
392
+ return multipleSelection ? [...(items || []), item] : [item];
393
+ }
394
+ function selectItems(items, itemsToSelect) {
395
+ const newItems = itemsToSelect.filter(item => {
396
+ const {
397
+ id: itemId = null
398
+ } = item || {};
399
+ const currentItem = (items || []).find(({
400
+ id
401
+ }) => id === itemId) || null;
402
+ return currentItem === null;
403
+ });
404
+ return newItems.length > 0 ? [...(items || []), ...newItems] : items;
405
+ }
406
+ function unselectItems(items, itemsToUnselect) {
407
+ const newItems = (items || []).filter(item => {
408
+ const {
409
+ id: itemId = null
410
+ } = item || {};
411
+ const currentItem = itemsToUnselect.find(({
412
+ id
413
+ }) => id === itemId) || null;
414
+ return currentItem === null;
415
+ });
416
+ return items !== null && newItems.length !== items.length ? newItems : items;
541
417
  }
542
418
 
543
419
  function setValue(value, keyParts, fieldValue) {
544
- var key = keyParts.shift();
420
+ const key = keyParts.shift();
545
421
  if (!key) {
546
422
  return value;
547
423
  }
548
- var isArray = key.match(/^[0-9]+$/) !== null;
424
+ const isArray = key.match(/^[0-9]+$/) !== null;
549
425
  if (value !== null || fieldValue !== null) {
550
426
  if (isArray) {
551
- var index = parseInt(key, 10);
552
- var arrayValue = value;
553
- var newArrayValue = fieldValue !== null ? [].concat(_toConsumableArray(arrayValue.slice(0, index)), [keyParts.length > 0 ? setValue(arrayValue !== null ? arrayValue[index] || null : null, keyParts, fieldValue) : fieldValue], _toConsumableArray(arrayValue.slice(index + 1))) : [].concat(_toConsumableArray(arrayValue.slice(0, index)), _toConsumableArray(arrayValue.slice(index + 1)));
427
+ const index = parseInt(key, 10);
428
+ const arrayValue = value;
429
+ const newArrayValue = fieldValue !== null ? [...arrayValue.slice(0, index), keyParts.length > 0 ? setValue(arrayValue !== null ? arrayValue[index] || null : null, keyParts, fieldValue) : fieldValue, ...arrayValue.slice(index + 1)] : [...arrayValue.slice(0, index), ...arrayValue.slice(index + 1)];
554
430
  return newArrayValue.length > 0 ? newArrayValue : null;
555
431
  }
556
- var objectValue = value;
557
- return _objectSpread(_objectSpread({}, objectValue), {}, _defineProperty({}, key, keyParts.length > 0 ? setValue(objectValue !== null ? objectValue[key] || null : null, keyParts, fieldValue) : fieldValue));
432
+ const objectValue = value;
433
+ return {
434
+ ...objectValue,
435
+ [key]: keyParts.length > 0 ? setValue(objectValue !== null ? objectValue[key] || null : null, keyParts, fieldValue) : fieldValue
436
+ };
558
437
  }
559
438
  return null;
560
439
  }
561
440
 
562
- function slug(str) {
563
- var separator = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
564
- var toSlug;
441
+ function slug(str, separator = null) {
442
+ let toSlug;
565
443
  if (separator === '-') {
566
444
  toSlug = kebabCase(str);
567
445
  } else {
@@ -573,16 +451,16 @@ function slug(str) {
573
451
  }
574
452
 
575
453
  function validateFields(fields, value) {
576
- return fields.reduce(function (acc, field) {
454
+ return fields.reduce((acc, field) => {
577
455
  if (acc === true) {
578
456
  if (field.type === 'fields' && field.fields) {
579
457
  return validateFields(field.fields, value);
580
458
  }
581
- var val = value && field.name && value[field.name] ? value[field.name] : false;
459
+ const val = value && field.name && value[field.name] ? value[field.name] : false;
582
460
  return !(field.required && !val);
583
461
  }
584
462
  return acc;
585
463
  }, true);
586
464
  }
587
465
 
588
- export { checkClickable, convertStyleToString, copyToClipboard, createNullableOnChange, createUseEvent, flattenComponents, formatDuration, getColumnsWithFields, getComponent, getComponentFromName, getDefinitionFromId, getDisplayName, getFieldByName, getFieldFromPath, getFileName, getPathValue, getSelectOptions, isMessage, loadPackage, selectItem, selectPage, setValue as setFieldValue, slug, validateFields };
466
+ export { checkClickable, convertStyleToString, copyToClipboard, createNullableOnChange, createUseEvent, flattenComponents, formatDuration, getColumnsWithFields, getComponent, getComponentFromName, getDefinitionFromId, getDisplayName, getFieldByName, getFieldFromPath, getFileName, getPathValue, getSelectOptions, isMessage, loadPackage, selectItems, setValue as setFieldValue, slug, toggleSelectedItem, unselectItems, validateFields };