@panneau/core 3.0.140 → 3.0.145

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/lib/utils.js DELETED
@@ -1,604 +0,0 @@
1
- 'use strict';
2
-
3
- var _regeneratorRuntime = require('@babel/runtime/helpers/regeneratorRuntime');
4
- var _asyncToGenerator = require('@babel/runtime/helpers/asyncToGenerator');
5
- var EventEmitter = require('wolfy87-eventemitter');
6
- var isNumber = require('lodash/isNumber');
7
- var tslib = require('tslib');
8
- var isObject = require('lodash/isObject');
9
- var react = require('react');
10
- var _defineProperty = require('@babel/runtime/helpers/defineProperty');
11
- var _objectSpread = require('@babel/runtime/helpers/objectSpread2');
12
- var ReactIs = require('react-is');
13
- var _objectWithoutProperties = require('@babel/runtime/helpers/objectWithoutProperties');
14
- var isString = require('lodash/isString');
15
- var isArray = require('lodash/isArray');
16
- var _toConsumableArray = require('@babel/runtime/helpers/toConsumableArray');
17
- var get = require('lodash/get');
18
- var capitalize = require('lodash/capitalize');
19
- var slugify = require('slugify');
20
-
21
- /**
22
- * Source: ftp://ftp.unicode.org/Public/UCD/latest/ucd/SpecialCasing.txt
23
- */
24
- /**
25
- * Lower case as a function.
26
- */
27
- function lowerCase(str) {
28
- return str.toLowerCase();
29
- }
30
-
31
- // Support camel case ("camelCase" -> "camel Case" and "CAMELCase" -> "CAMEL Case").
32
- var DEFAULT_SPLIT_REGEXP = [/([a-z0-9])([A-Z])/g, /([A-Z])([A-Z][a-z])/g];
33
- // Remove all non-word characters.
34
- var DEFAULT_STRIP_REGEXP = /[^A-Z0-9]+/gi;
35
- /**
36
- * Normalize the string into something other libraries can manipulate easier.
37
- */
38
- function noCase(input, options) {
39
- if (options === void 0) {
40
- options = {};
41
- }
42
- var _a = options.splitRegexp,
43
- splitRegexp = _a === void 0 ? DEFAULT_SPLIT_REGEXP : _a,
44
- _b = options.stripRegexp,
45
- stripRegexp = _b === void 0 ? DEFAULT_STRIP_REGEXP : _b,
46
- _c = options.transform,
47
- transform = _c === void 0 ? lowerCase : _c,
48
- _d = options.delimiter,
49
- delimiter = _d === void 0 ? " " : _d;
50
- var result = replace(replace(input, splitRegexp, "$1\0$2"), stripRegexp, "\0");
51
- var start = 0;
52
- var end = result.length;
53
- // Trim the delimiter from around the output string.
54
- while (result.charAt(start) === "\0") start++;
55
- while (result.charAt(end - 1) === "\0") end--;
56
- // Transform each token independently.
57
- return result.slice(start, end).split("\0").map(transform).join(delimiter);
58
- }
59
- /**
60
- * Replace `re` in the input string with the replacement value.
61
- */
62
- function replace(input, re, value) {
63
- if (re instanceof RegExp) return input.replace(re, value);
64
- return re.reduce(function (input, re) {
65
- return input.replace(re, value);
66
- }, input);
67
- }
68
-
69
- function dotCase(input, options) {
70
- if (options === void 0) {
71
- options = {};
72
- }
73
- return noCase(input, tslib.__assign({
74
- delimiter: "."
75
- }, options));
76
- }
77
-
78
- function snakeCase(input, options) {
79
- if (options === void 0) {
80
- options = {};
81
- }
82
- return dotCase(input, tslib.__assign({
83
- delimiter: "_"
84
- }, options));
85
- }
86
-
87
- var convertStyleToString = function convertStyleToString(style) {
88
- return style !== null ? Object.keys(style).map(function (key) {
89
- return "".concat(snakeCase(key), ":").concat(isNumber(style[key]) ? "".concat(style[key], "px") : style[key], ";");
90
- }).join('\n') : '';
91
- };
92
- var convertStyleToString$1 = convertStyleToString;
93
-
94
- var createNullableOnChange = function createNullableOnChange() {
95
- var onChange = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
96
- return function (newValue) {
97
- var nullableValue = newValue;
98
- if (isObject(newValue)) {
99
- var allNull = Object.keys(newValue).reduce(function (acc, key) {
100
- return acc && newValue[key] === null;
101
- }, true);
102
- if (allNull) {
103
- nullableValue = null;
104
- }
105
- }
106
- if (onChange !== null) {
107
- onChange(nullableValue);
108
- }
109
- };
110
- };
111
- var createNullableOnChange$1 = createNullableOnChange;
112
-
113
- var createUseEvent = function createUseEvent(eventsManager) {
114
- return function (event, callback) {
115
- var enabled = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
116
- react.useEffect(function () {
117
- if (enabled && eventsManager !== null) {
118
- eventsManager.subscribe(event, callback);
119
- }
120
- return function () {
121
- if (enabled && eventsManager !== null) {
122
- eventsManager.unsubscribe(event, callback);
123
- }
124
- };
125
- }, [eventsManager, event, callback, enabled]);
126
- };
127
- };
128
- var createUseEvent$1 = createUseEvent;
129
-
130
- /*! clipboard-copy. MIT License. Feross Aboukhadijeh <https://feross.org/opensource> */
131
-
132
- // @note vendoring this: https://github.com/feross/clipboard-copy/blob/master/index.js
133
- // we might want to add that to the npm deps and just use it
134
-
135
- function makeError() {
136
- return new DOMException('The request is not allowed', 'NotAllowedError');
137
- }
138
- function copyClipboardApi(_x) {
139
- return _copyClipboardApi.apply(this, arguments);
140
- }
141
- function _copyClipboardApi() {
142
- _copyClipboardApi = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(text) {
143
- return _regeneratorRuntime().wrap(function _callee$(_context) {
144
- while (1) switch (_context.prev = _context.next) {
145
- case 0:
146
- if (navigator.clipboard) {
147
- _context.next = 2;
148
- break;
149
- }
150
- throw makeError();
151
- case 2:
152
- return _context.abrupt("return", navigator.clipboard.writeText(text));
153
- case 3:
154
- case "end":
155
- return _context.stop();
156
- }
157
- }, _callee);
158
- }));
159
- return _copyClipboardApi.apply(this, arguments);
160
- }
161
- function copyExecCommand(_x2) {
162
- return _copyExecCommand.apply(this, arguments);
163
- }
164
- function _copyExecCommand() {
165
- _copyExecCommand = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(text) {
166
- var span, selection, range, success;
167
- return _regeneratorRuntime().wrap(function _callee2$(_context2) {
168
- while (1) switch (_context2.prev = _context2.next) {
169
- case 0:
170
- // Put the text to copy into a <span>
171
- span = document.createElement('span');
172
- span.textContent = text;
173
-
174
- // Preserve consecutive spaces and newlines
175
- span.style.whiteSpace = 'pre';
176
- span.style.webkitUserSelect = 'auto';
177
- span.style.userSelect = 'all';
178
-
179
- // Add the <span> to the page
180
- document.body.appendChild(span);
181
-
182
- // Make a selection object representing the range of text selected by the user
183
- selection = window.getSelection();
184
- range = window.document.createRange();
185
- selection.removeAllRanges();
186
- range.selectNode(span);
187
- selection.addRange(range);
188
-
189
- // Copy text to the clipboard
190
- success = false;
191
- try {
192
- success = window.document.execCommand('copy');
193
- } finally {
194
- // Cleanup
195
- selection.removeAllRanges();
196
- window.document.body.removeChild(span);
197
- }
198
- if (success) {
199
- _context2.next = 15;
200
- break;
201
- }
202
- throw makeError();
203
- case 15:
204
- case "end":
205
- return _context2.stop();
206
- }
207
- }, _callee2);
208
- }));
209
- return _copyExecCommand.apply(this, arguments);
210
- }
211
- function copyToClipboard(_x3) {
212
- return _copyToClipboard.apply(this, arguments);
213
- }
214
- function _copyToClipboard() {
215
- _copyToClipboard = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3(text) {
216
- return _regeneratorRuntime().wrap(function _callee3$(_context3) {
217
- while (1) switch (_context3.prev = _context3.next) {
218
- case 0:
219
- _context3.prev = 0;
220
- _context3.next = 3;
221
- return copyClipboardApi(text);
222
- case 3:
223
- _context3.next = 15;
224
- break;
225
- case 5:
226
- _context3.prev = 5;
227
- _context3.t0 = _context3["catch"](0);
228
- _context3.prev = 7;
229
- _context3.next = 10;
230
- return copyExecCommand(text);
231
- case 10:
232
- _context3.next = 15;
233
- break;
234
- case 12:
235
- _context3.prev = 12;
236
- _context3.t1 = _context3["catch"](7);
237
- throw _context3.t1 || _context3.t0 || makeError();
238
- case 15:
239
- case "end":
240
- return _context3.stop();
241
- }
242
- }, _callee3, null, [[0, 5], [7, 12]]);
243
- }));
244
- return _copyToClipboard.apply(this, arguments);
245
- }
246
-
247
- function pascalCaseTransform(input, index) {
248
- var firstChar = input.charAt(0);
249
- var lowerChars = input.substr(1).toLowerCase();
250
- if (index > 0 && firstChar >= "0" && firstChar <= "9") {
251
- return "_" + firstChar + lowerChars;
252
- }
253
- return "" + firstChar.toUpperCase() + lowerChars;
254
- }
255
- function pascalCase(input, options) {
256
- if (options === void 0) {
257
- options = {};
258
- }
259
- return noCase(input, tslib.__assign({
260
- delimiter: "",
261
- transform: pascalCaseTransform
262
- }, options));
263
- }
264
-
265
- function paramCase(input, options) {
266
- if (options === void 0) {
267
- options = {};
268
- }
269
- return dotCase(input, tslib.__assign({
270
- delimiter: "-"
271
- }, options));
272
- }
273
-
274
- var flattenComponents = function flattenComponents(components) {
275
- var prefix = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
276
- if (components === null) {
277
- return null;
278
- }
279
- return Object.keys(components).reduce(function (newMap, key) {
280
- 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));
281
- }, {});
282
- };
283
- var flattenComponents$1 = flattenComponents;
284
-
285
- var formatDuration = function formatDuration(timeInSeconds) {
286
- var numberOfSeconds = parseInt(timeInSeconds, 10); // don't forget the second param
287
- var hours = Math.floor(numberOfSeconds / 3600);
288
- var minutes = Math.floor((numberOfSeconds - hours * 3600) / 60);
289
- var seconds = numberOfSeconds - hours * 3600 - minutes * 60;
290
- if (hours < 10) {
291
- hours = "0".concat(hours);
292
- }
293
- if (minutes < 10) {
294
- minutes = "0".concat(minutes);
295
- }
296
- if (seconds < 10) {
297
- seconds = "0".concat(seconds);
298
- }
299
- return "".concat(hours, ":").concat(minutes, ":").concat(seconds);
300
- };
301
- var formatDuration$1 = formatDuration;
302
-
303
- var _excluded$1 = ["component"];
304
- var getComponent = function getComponent(component) {
305
- var _ref = isObject(component) ? component : {
306
- component: component
307
- };
308
- _ref.component;
309
- var props = _objectWithoutProperties(_ref, _excluded$1);
310
- return {
311
- name: component,
312
- props: props
313
- };
314
- };
315
- var getComponent$1 = getComponent;
316
-
317
- var _excluded = ["id", "field"];
318
- var getColumnFromField = function getColumnFromField(field) {
319
- if (field === null) {
320
- return null;
321
- }
322
- // eslint-disable-next-line camelcase
323
- var name = field.name,
324
- _field$components = field.components,
325
- _field$components2 = _field$components === void 0 ? {} : _field$components,
326
- _field$components2$di = _field$components2.display,
327
- display = _field$components2$di === void 0 ? null : _field$components2$di,
328
- label = field.label;
329
- var _getComponent = getComponent$1(display),
330
- _getComponent$name = _getComponent.name,
331
- componentName = _getComponent$name === void 0 ? null : _getComponent$name,
332
- _getComponent$props = _getComponent.props,
333
- componentProps = _getComponent$props === void 0 ? null : _getComponent$props;
334
- return _objectSpread(_objectSpread({}, componentProps), {}, {
335
- id: name,
336
- label: label,
337
- valueKey: name,
338
- component: componentName,
339
- field: field
340
- });
341
- };
342
- var getColumnsWithFields = function getColumnsWithFields(resource, columns) {
343
- var fields = resource.fields;
344
- var newColumns = columns !== null ? columns.map(function (column) {
345
- var _ref = isString(column) ? {
346
- field: column
347
- } : column,
348
- _ref$id = _ref.id,
349
- colId = _ref$id === void 0 ? null : _ref$id,
350
- _ref$field = _ref.field,
351
- colField = _ref$field === void 0 ? null : _ref$field,
352
- otherProps = _objectWithoutProperties(_ref, _excluded);
353
- var fieldName = colField || colId;
354
- return _objectSpread(_objectSpread(_objectSpread({}, fieldName !== null ? getColumnFromField(fields.find(function (it) {
355
- return it.name === fieldName;
356
- }) || null) : null), otherProps), {}, {
357
- id: colId
358
- });
359
- }).filter(function (it) {
360
- return it !== null;
361
- }) : [];
362
- return newColumns;
363
- };
364
- var getColumnsWithFields$1 = getColumnsWithFields;
365
-
366
- var getComponentFromName = function getComponentFromName() {
367
- var name = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
368
- var components = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
369
- var defaultComponent = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
370
- if (components === null || name === null) {
371
- return defaultComponent;
372
- }
373
- var pascalName = pascalCase(name);
374
- // console.log(name, pascalName, components);
375
- var component = components[pascalName] || components[name] || defaultComponent;
376
- if (!component) {
377
- console.warn('Could not find component from name', name, pascalName, components);
378
- }
379
- return component;
380
- };
381
- var getComponentFromName$1 = getComponentFromName;
382
-
383
- var getDefinitionFromId = function getDefinitionFromId() {
384
- var id = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
385
- var items = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
386
- var defaultObject = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
387
- if (items === null || id === null || isArray(items) && items.length === 0) {
388
- return defaultObject;
389
- }
390
- var pascalId = pascalCase(id);
391
- return items.find(function (i) {
392
- return i.id === pascalId;
393
- }) || items.find(function (i) {
394
- return i.id === id;
395
- }) || defaultObject;
396
- };
397
- var getDefinitionFromId$1 = getDefinitionFromId;
398
-
399
- var getDisplayName = function getDisplayName(_ref) {
400
- var _ref$displayName = _ref.displayName,
401
- displayName = _ref$displayName === void 0 ? null : _ref$displayName,
402
- _ref$name = _ref.name,
403
- name = _ref$name === void 0 ? null : _ref$name;
404
- return displayName || name || 'Component';
405
- };
406
- var getDisplayName$1 = getDisplayName;
407
-
408
- var getFieldByName = function getFieldByName(fields, name) {
409
- return fields.reduce(function (foundField, it) {
410
- if (foundField !== null) {
411
- return foundField;
412
- }
413
- var _it$name = it.name,
414
- fieldName = _it$name === void 0 ? null : _it$name,
415
- _it$fields = it.fields,
416
- subFields = _it$fields === void 0 ? [] : _it$fields;
417
- if (name !== null && fieldName === name) {
418
- return it;
419
- }
420
- return getFieldByName(subFields, name);
421
- }, null);
422
- };
423
- var getFieldByName$1 = getFieldByName;
424
-
425
- var getFieldFromPath = function getFieldFromPath(path, fields, fieldManager) {
426
- return (isArray(path) ? path : [path]).reduce(function (foundField, key) {
427
- if (foundField === null) {
428
- return null;
429
- }
430
- var _foundField$type = foundField.type,
431
- type = _foundField$type === void 0 ? null : _foundField$type,
432
- _foundField$fields = foundField.fields,
433
- fieldFields = _foundField$fields === void 0 ? null : _foundField$fields,
434
- _foundField$field = foundField.field,
435
- field = _foundField$field === void 0 ? null : _foundField$field;
436
- var finalType = field !== null ? field.type || type : type;
437
- var _ref = finalType !== null ? fieldManager.getDefinition(finalType) : foundField,
438
- _ref$fields = _ref.fields,
439
- subFields = _ref$fields === void 0 ? null : _ref$fields,
440
- _ref$settings = _ref.settings,
441
- settings = _ref$settings === void 0 ? null : _ref$settings,
442
- _ref$itemsField = _ref.itemsField,
443
- itemsField = _ref$itemsField === void 0 ? null : _ref$itemsField;
444
- if (itemsField !== null && key.match(/^[0-9]+$/)) {
445
- return _objectSpread(_objectSpread({}, itemsField), {}, {
446
- name: path.join('/'),
447
- listItems: true
448
- });
449
- }
450
- return getFieldByName$1([].concat(_toConsumableArray(fieldFields || []), _toConsumableArray(subFields || []), _toConsumableArray(settings || [])), key);
451
- }, {
452
- fields: fields
453
- });
454
- };
455
- var getFieldFromPath$1 = getFieldFromPath;
456
-
457
- var getFileName = function getFileName() {
458
- var url = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
459
- if (url === null || typeof url.match === 'undefined') {
460
- return null;
461
- }
462
- return url.match(/([^/]+)(\?.*)?$/)[1] || url;
463
- };
464
- var getFileName$1 = getFileName;
465
-
466
- var getPathValue = function getPathValue(it, path) {
467
- var value = get(it, path, null);
468
- return value !== null && isObject(value) ? Object.values(value).reduce(function (foundValue, value) {
469
- return foundValue !== null ? foundValue : value;
470
- }, null) : value;
471
- };
472
- var getPathValue$1 = getPathValue;
473
-
474
- var getSelectOptions = function getSelectOptions(options) {
475
- return options.map(function (it) {
476
- if (isObject(it)) {
477
- var _it$label = it.label,
478
- label = _it$label === void 0 ? null : _it$label,
479
- value = it.value;
480
- if (label === null) {
481
- return {
482
- value: value,
483
- label: capitalize("".concat(value))
484
- };
485
- }
486
- return {
487
- value: value,
488
- label: "".concat(label)
489
- };
490
- }
491
- return {
492
- value: it,
493
- label: capitalize("".concat(it))
494
- };
495
- });
496
- };
497
- var getSelectOptions$1 = getSelectOptions;
498
-
499
- var isMessage = function isMessage(message) {
500
- return isObject(message) && typeof message.defaultMessage !== 'undefined';
501
- };
502
- var isMessage$1 = isMessage;
503
-
504
- /**
505
- * Locale loader
506
- */
507
- var loaders = {};
508
- var events = new EventEmitter();
509
- function loadPackage(name, loader) {
510
- if (typeof loaders[name] === 'undefined') {
511
- loaders[name] = {
512
- loader: loader().then(function (response) {
513
- loaders[name].loader = null;
514
- loaders[name].loaded = true;
515
- loaders[name].resource = response;
516
- events.emit("loaded:".concat(name), response);
517
- return response;
518
- }),
519
- loaded: false,
520
- resource: null
521
- };
522
- }
523
- return new Promise(function (resolve) {
524
- var _ref = loaders[name] || {},
525
- _ref$loaded = _ref.loaded,
526
- loaded = _ref$loaded === void 0 ? false : _ref$loaded,
527
- resource = _ref.resource;
528
- if (loaded === true) {
529
- resolve(resource);
530
- } else {
531
- events.once("loaded:".concat(name), function (response) {
532
- resolve(response);
533
- });
534
- }
535
- });
536
- }
537
-
538
- var setValue = function setValue(value, keyParts, fieldValue) {
539
- var key = keyParts.shift();
540
- var isArray = key.match(/^[0-9]+$/) !== null;
541
- if (value !== null || fieldValue !== null) {
542
- if (isArray) {
543
- var index = parseInt(key, 10);
544
- var newArrayValue = fieldValue !== null ? [].concat(_toConsumableArray(value.slice(0, index)), [keyParts.length > 0 ? setValue(value !== null ? value[index] || null : null, keyParts, fieldValue) : fieldValue], _toConsumableArray(value.slice(index + 1))) : [].concat(_toConsumableArray(value.slice(0, index)), _toConsumableArray(value.slice(index + 1)));
545
- return newArrayValue.length > 0 ? newArrayValue : null;
546
- }
547
- return _objectSpread(_objectSpread({}, value), {}, _defineProperty({}, key, keyParts.length > 0 ? setValue(value !== null ? value[key] || null : null, keyParts, fieldValue) : fieldValue));
548
- }
549
- return null;
550
- };
551
- var setValue$1 = setValue;
552
-
553
- var slug = function slug(str) {
554
- var separator = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
555
- var toSlug;
556
- if (separator === '-') {
557
- toSlug = paramCase(str);
558
- } else {
559
- toSlug = snakeCase(str);
560
- }
561
- return slugify(toSlug, {
562
- lower: true
563
- });
564
- };
565
- var slug$1 = slug;
566
-
567
- var validateFields = function validateFields(fields, value) {
568
- return fields.reduce(function (acc, field) {
569
- if (acc === true) {
570
- if (field.type === 'fields' && field.fields) {
571
- return validateFields(field.fields, value);
572
- }
573
- var val = value && value[field.name] ? value[field.name] : false;
574
- return !(field.required && !val);
575
- }
576
- return acc;
577
- }, true);
578
- };
579
- var validateFields$1 = validateFields;
580
-
581
- exports.convertStyleToString = convertStyleToString$1;
582
- exports.copyToClipboard = copyToClipboard;
583
- exports.createNullableOnChange = createNullableOnChange$1;
584
- exports.createUseEvent = createUseEvent$1;
585
- exports.dashCase = paramCase;
586
- exports.flattenComponents = flattenComponents$1;
587
- exports.formatDuration = formatDuration$1;
588
- exports.getColumnsWithFields = getColumnsWithFields$1;
589
- exports.getComponent = getComponent$1;
590
- exports.getComponentFromName = getComponentFromName$1;
591
- exports.getDefinitionFromId = getDefinitionFromId$1;
592
- exports.getDisplayName = getDisplayName$1;
593
- exports.getFieldByName = getFieldByName$1;
594
- exports.getFieldFromPath = getFieldFromPath$1;
595
- exports.getFileName = getFileName$1;
596
- exports.getPathValue = getPathValue$1;
597
- exports.getSelectOptions = getSelectOptions$1;
598
- exports.isMessage = isMessage$1;
599
- exports.loadPackage = loadPackage;
600
- exports.pascalCase = pascalCase;
601
- exports.setFieldValue = setValue$1;
602
- exports.slug = slug$1;
603
- exports.snakeCase = snakeCase;
604
- exports.validateFields = validateFields$1;