@kubik-cms/wysiwyg 0.1.8

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.
@@ -0,0 +1,1277 @@
1
+ function camelize(value) {
2
+ return value.replace(/(?:[_-])([a-z0-9])/g, (_, char) => char.toUpperCase());
3
+ }
4
+ function capitalize(value) {
5
+ return value.charAt(0).toUpperCase() + value.slice(1);
6
+ }
7
+ function dasherize(value) {
8
+ return value.replace(/([A-Z])/g, (_, char) => `-${char.toLowerCase()}`);
9
+ }
10
+ function readInheritableStaticArrayValues(constructor, propertyName) {
11
+ const ancestors = getAncestorsForConstructor(constructor);
12
+ return Array.from(ancestors.reduce((values, constructor2) => {
13
+ getOwnStaticArrayValues(constructor2, propertyName).forEach((name) => values.add(name));
14
+ return values;
15
+ }, /* @__PURE__ */ new Set()));
16
+ }
17
+ function readInheritableStaticObjectPairs(constructor, propertyName) {
18
+ const ancestors = getAncestorsForConstructor(constructor);
19
+ return ancestors.reduce((pairs, constructor2) => {
20
+ pairs.push(...getOwnStaticObjectPairs(constructor2, propertyName));
21
+ return pairs;
22
+ }, []);
23
+ }
24
+ function getAncestorsForConstructor(constructor) {
25
+ const ancestors = [];
26
+ while (constructor) {
27
+ ancestors.push(constructor);
28
+ constructor = Object.getPrototypeOf(constructor);
29
+ }
30
+ return ancestors.reverse();
31
+ }
32
+ function getOwnStaticArrayValues(constructor, propertyName) {
33
+ const definition = constructor[propertyName];
34
+ return Array.isArray(definition) ? definition : [];
35
+ }
36
+ function getOwnStaticObjectPairs(constructor, propertyName) {
37
+ const definition = constructor[propertyName];
38
+ return definition ? Object.keys(definition).map((key) => [key, definition[key]]) : [];
39
+ }
40
+ (() => {
41
+ function extendWithReflect(constructor) {
42
+ function extended() {
43
+ return Reflect.construct(constructor, arguments, new.target);
44
+ }
45
+ extended.prototype = Object.create(constructor.prototype, {
46
+ constructor: { value: extended }
47
+ });
48
+ Reflect.setPrototypeOf(extended, constructor);
49
+ return extended;
50
+ }
51
+ function testReflectExtension() {
52
+ const a = function() {
53
+ this.a.call(this);
54
+ };
55
+ const b = extendWithReflect(a);
56
+ b.prototype.a = function() {
57
+ };
58
+ return new b();
59
+ }
60
+ try {
61
+ testReflectExtension();
62
+ return extendWithReflect;
63
+ } catch (error) {
64
+ return (constructor) => class extended extends constructor {
65
+ };
66
+ }
67
+ })();
68
+ function ClassPropertiesBlessing(constructor) {
69
+ const classes = readInheritableStaticArrayValues(constructor, "classes");
70
+ return classes.reduce((properties, classDefinition) => {
71
+ return Object.assign(properties, propertiesForClassDefinition(classDefinition));
72
+ }, {});
73
+ }
74
+ function propertiesForClassDefinition(key) {
75
+ return {
76
+ [`${key}Class`]: {
77
+ get() {
78
+ const { classes } = this;
79
+ if (classes.has(key)) {
80
+ return classes.get(key);
81
+ } else {
82
+ const attribute = classes.getAttributeName(key);
83
+ throw new Error(`Missing attribute "${attribute}"`);
84
+ }
85
+ }
86
+ },
87
+ [`${key}Classes`]: {
88
+ get() {
89
+ return this.classes.getAll(key);
90
+ }
91
+ },
92
+ [`has${capitalize(key)}Class`]: {
93
+ get() {
94
+ return this.classes.has(key);
95
+ }
96
+ }
97
+ };
98
+ }
99
+ function TargetPropertiesBlessing(constructor) {
100
+ const targets = readInheritableStaticArrayValues(constructor, "targets");
101
+ return targets.reduce((properties, targetDefinition) => {
102
+ return Object.assign(properties, propertiesForTargetDefinition(targetDefinition));
103
+ }, {});
104
+ }
105
+ function propertiesForTargetDefinition(name) {
106
+ return {
107
+ [`${name}Target`]: {
108
+ get() {
109
+ const target = this.targets.find(name);
110
+ if (target) {
111
+ return target;
112
+ } else {
113
+ throw new Error(`Missing target element "${name}" for "${this.identifier}" controller`);
114
+ }
115
+ }
116
+ },
117
+ [`${name}Targets`]: {
118
+ get() {
119
+ return this.targets.findAll(name);
120
+ }
121
+ },
122
+ [`has${capitalize(name)}Target`]: {
123
+ get() {
124
+ return this.targets.has(name);
125
+ }
126
+ }
127
+ };
128
+ }
129
+ function ValuePropertiesBlessing(constructor) {
130
+ const valueDefinitionPairs = readInheritableStaticObjectPairs(constructor, "values");
131
+ const propertyDescriptorMap = {
132
+ valueDescriptorMap: {
133
+ get() {
134
+ return valueDefinitionPairs.reduce((result, valueDefinitionPair) => {
135
+ const valueDescriptor = parseValueDefinitionPair(valueDefinitionPair, this.identifier);
136
+ const attributeName = this.data.getAttributeNameForKey(valueDescriptor.key);
137
+ return Object.assign(result, { [attributeName]: valueDescriptor });
138
+ }, {});
139
+ }
140
+ }
141
+ };
142
+ return valueDefinitionPairs.reduce((properties, valueDefinitionPair) => {
143
+ return Object.assign(properties, propertiesForValueDefinitionPair(valueDefinitionPair));
144
+ }, propertyDescriptorMap);
145
+ }
146
+ function propertiesForValueDefinitionPair(valueDefinitionPair, controller) {
147
+ const definition = parseValueDefinitionPair(valueDefinitionPair, controller);
148
+ const { key, name, reader: read, writer: write } = definition;
149
+ return {
150
+ [name]: {
151
+ get() {
152
+ const value = this.data.get(key);
153
+ if (value !== null) {
154
+ return read(value);
155
+ } else {
156
+ return definition.defaultValue;
157
+ }
158
+ },
159
+ set(value) {
160
+ if (value === void 0) {
161
+ this.data.delete(key);
162
+ } else {
163
+ this.data.set(key, write(value));
164
+ }
165
+ }
166
+ },
167
+ [`has${capitalize(name)}`]: {
168
+ get() {
169
+ return this.data.has(key) || definition.hasCustomDefaultValue;
170
+ }
171
+ }
172
+ };
173
+ }
174
+ function parseValueDefinitionPair([token, typeDefinition], controller) {
175
+ return valueDescriptorForTokenAndTypeDefinition({
176
+ controller,
177
+ token,
178
+ typeDefinition
179
+ });
180
+ }
181
+ function parseValueTypeConstant(constant) {
182
+ switch (constant) {
183
+ case Array:
184
+ return "array";
185
+ case Boolean:
186
+ return "boolean";
187
+ case Number:
188
+ return "number";
189
+ case Object:
190
+ return "object";
191
+ case String:
192
+ return "string";
193
+ }
194
+ }
195
+ function parseValueTypeDefault(defaultValue) {
196
+ switch (typeof defaultValue) {
197
+ case "boolean":
198
+ return "boolean";
199
+ case "number":
200
+ return "number";
201
+ case "string":
202
+ return "string";
203
+ }
204
+ if (Array.isArray(defaultValue))
205
+ return "array";
206
+ if (Object.prototype.toString.call(defaultValue) === "[object Object]")
207
+ return "object";
208
+ }
209
+ function parseValueTypeObject(payload) {
210
+ const typeFromObject = parseValueTypeConstant(payload.typeObject.type);
211
+ if (!typeFromObject)
212
+ return;
213
+ const defaultValueType = parseValueTypeDefault(payload.typeObject.default);
214
+ if (typeFromObject !== defaultValueType) {
215
+ const propertyPath = payload.controller ? `${payload.controller}.${payload.token}` : payload.token;
216
+ throw new Error(`The specified default value for the Stimulus Value "${propertyPath}" must match the defined type "${typeFromObject}". The provided default value of "${payload.typeObject.default}" is of type "${defaultValueType}".`);
217
+ }
218
+ return typeFromObject;
219
+ }
220
+ function parseValueTypeDefinition(payload) {
221
+ const typeFromObject = parseValueTypeObject({
222
+ controller: payload.controller,
223
+ token: payload.token,
224
+ typeObject: payload.typeDefinition
225
+ });
226
+ const typeFromDefaultValue = parseValueTypeDefault(payload.typeDefinition);
227
+ const typeFromConstant = parseValueTypeConstant(payload.typeDefinition);
228
+ const type = typeFromObject || typeFromDefaultValue || typeFromConstant;
229
+ if (type)
230
+ return type;
231
+ const propertyPath = payload.controller ? `${payload.controller}.${payload.typeDefinition}` : payload.token;
232
+ throw new Error(`Unknown value type "${propertyPath}" for "${payload.token}" value`);
233
+ }
234
+ function defaultValueForDefinition(typeDefinition) {
235
+ const constant = parseValueTypeConstant(typeDefinition);
236
+ if (constant)
237
+ return defaultValuesByType[constant];
238
+ const defaultValue = typeDefinition.default;
239
+ if (defaultValue !== void 0)
240
+ return defaultValue;
241
+ return typeDefinition;
242
+ }
243
+ function valueDescriptorForTokenAndTypeDefinition(payload) {
244
+ const key = `${dasherize(payload.token)}-value`;
245
+ const type = parseValueTypeDefinition(payload);
246
+ return {
247
+ type,
248
+ key,
249
+ name: camelize(key),
250
+ get defaultValue() {
251
+ return defaultValueForDefinition(payload.typeDefinition);
252
+ },
253
+ get hasCustomDefaultValue() {
254
+ return parseValueTypeDefault(payload.typeDefinition) !== void 0;
255
+ },
256
+ reader: readers[type],
257
+ writer: writers[type] || writers.default
258
+ };
259
+ }
260
+ const defaultValuesByType = {
261
+ get array() {
262
+ return [];
263
+ },
264
+ boolean: false,
265
+ number: 0,
266
+ get object() {
267
+ return {};
268
+ },
269
+ string: ""
270
+ };
271
+ const readers = {
272
+ array(value) {
273
+ const array = JSON.parse(value);
274
+ if (!Array.isArray(array)) {
275
+ throw new TypeError(`expected value of type "array" but instead got value "${value}" of type "${parseValueTypeDefault(array)}"`);
276
+ }
277
+ return array;
278
+ },
279
+ boolean(value) {
280
+ return !(value == "0" || String(value).toLowerCase() == "false");
281
+ },
282
+ number(value) {
283
+ return Number(value);
284
+ },
285
+ object(value) {
286
+ const object = JSON.parse(value);
287
+ if (object === null || typeof object != "object" || Array.isArray(object)) {
288
+ throw new TypeError(`expected value of type "object" but instead got value "${value}" of type "${parseValueTypeDefault(object)}"`);
289
+ }
290
+ return object;
291
+ },
292
+ string(value) {
293
+ return value;
294
+ }
295
+ };
296
+ const writers = {
297
+ default: writeString,
298
+ array: writeJSON,
299
+ object: writeJSON
300
+ };
301
+ function writeJSON(value) {
302
+ return JSON.stringify(value);
303
+ }
304
+ function writeString(value) {
305
+ return `${value}`;
306
+ }
307
+ class Controller {
308
+ constructor(context) {
309
+ this.context = context;
310
+ }
311
+ static get shouldLoad() {
312
+ return true;
313
+ }
314
+ get application() {
315
+ return this.context.application;
316
+ }
317
+ get scope() {
318
+ return this.context.scope;
319
+ }
320
+ get element() {
321
+ return this.scope.element;
322
+ }
323
+ get identifier() {
324
+ return this.scope.identifier;
325
+ }
326
+ get targets() {
327
+ return this.scope.targets;
328
+ }
329
+ get classes() {
330
+ return this.scope.classes;
331
+ }
332
+ get data() {
333
+ return this.scope.data;
334
+ }
335
+ initialize() {
336
+ }
337
+ connect() {
338
+ }
339
+ disconnect() {
340
+ }
341
+ dispatch(eventName, { target = this.element, detail = {}, prefix = this.identifier, bubbles = true, cancelable = true } = {}) {
342
+ const type = prefix ? `${prefix}:${eventName}` : eventName;
343
+ const event = new CustomEvent(type, { detail, bubbles, cancelable });
344
+ target.dispatchEvent(event);
345
+ return event;
346
+ }
347
+ }
348
+ Controller.blessings = [ClassPropertiesBlessing, TargetPropertiesBlessing, ValuePropertiesBlessing];
349
+ Controller.targets = [];
350
+ Controller.values = {};
351
+ var commonjsGlobal = typeof globalThis !== "undefined" ? globalThis : typeof window !== "undefined" ? window : typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : {};
352
+ var freeGlobal$1 = typeof commonjsGlobal == "object" && commonjsGlobal && commonjsGlobal.Object === Object && commonjsGlobal;
353
+ var _freeGlobal = freeGlobal$1;
354
+ var freeGlobal = _freeGlobal;
355
+ var freeSelf = typeof self == "object" && self && self.Object === Object && self;
356
+ var root$3 = freeGlobal || freeSelf || Function("return this")();
357
+ var _root = root$3;
358
+ var root$2 = _root;
359
+ var Symbol$4 = root$2.Symbol;
360
+ var _Symbol = Symbol$4;
361
+ var Symbol$3 = _Symbol;
362
+ var objectProto$5 = Object.prototype;
363
+ var hasOwnProperty$5 = objectProto$5.hasOwnProperty;
364
+ var nativeObjectToString$1 = objectProto$5.toString;
365
+ var symToStringTag$1 = Symbol$3 ? Symbol$3.toStringTag : void 0;
366
+ function getRawTag$1(value) {
367
+ var isOwn = hasOwnProperty$5.call(value, symToStringTag$1), tag = value[symToStringTag$1];
368
+ try {
369
+ value[symToStringTag$1] = void 0;
370
+ var unmasked = true;
371
+ } catch (e) {
372
+ }
373
+ var result = nativeObjectToString$1.call(value);
374
+ if (unmasked) {
375
+ if (isOwn) {
376
+ value[symToStringTag$1] = tag;
377
+ } else {
378
+ delete value[symToStringTag$1];
379
+ }
380
+ }
381
+ return result;
382
+ }
383
+ var _getRawTag = getRawTag$1;
384
+ var objectProto$4 = Object.prototype;
385
+ var nativeObjectToString = objectProto$4.toString;
386
+ function objectToString$1(value) {
387
+ return nativeObjectToString.call(value);
388
+ }
389
+ var _objectToString = objectToString$1;
390
+ var Symbol$2 = _Symbol, getRawTag = _getRawTag, objectToString = _objectToString;
391
+ var nullTag = "[object Null]", undefinedTag = "[object Undefined]";
392
+ var symToStringTag = Symbol$2 ? Symbol$2.toStringTag : void 0;
393
+ function baseGetTag$2(value) {
394
+ if (value == null) {
395
+ return value === void 0 ? undefinedTag : nullTag;
396
+ }
397
+ return symToStringTag && symToStringTag in Object(value) ? getRawTag(value) : objectToString(value);
398
+ }
399
+ var _baseGetTag = baseGetTag$2;
400
+ function isObject$5(value) {
401
+ var type = typeof value;
402
+ return value != null && (type == "object" || type == "function");
403
+ }
404
+ var isObject_1 = isObject$5;
405
+ var baseGetTag$1 = _baseGetTag, isObject$4 = isObject_1;
406
+ var asyncTag = "[object AsyncFunction]", funcTag = "[object Function]", genTag = "[object GeneratorFunction]", proxyTag = "[object Proxy]";
407
+ function isFunction$1(value) {
408
+ if (!isObject$4(value)) {
409
+ return false;
410
+ }
411
+ var tag = baseGetTag$1(value);
412
+ return tag == funcTag || tag == genTag || tag == asyncTag || tag == proxyTag;
413
+ }
414
+ var isFunction_1 = isFunction$1;
415
+ var root$1 = _root;
416
+ var coreJsData$1 = root$1["__core-js_shared__"];
417
+ var _coreJsData = coreJsData$1;
418
+ var coreJsData = _coreJsData;
419
+ var maskSrcKey = function() {
420
+ var uid = /[^.]+$/.exec(coreJsData && coreJsData.keys && coreJsData.keys.IE_PROTO || "");
421
+ return uid ? "Symbol(src)_1." + uid : "";
422
+ }();
423
+ function isMasked$1(func) {
424
+ return !!maskSrcKey && maskSrcKey in func;
425
+ }
426
+ var _isMasked = isMasked$1;
427
+ var funcProto$1 = Function.prototype;
428
+ var funcToString$1 = funcProto$1.toString;
429
+ function toSource$1(func) {
430
+ if (func != null) {
431
+ try {
432
+ return funcToString$1.call(func);
433
+ } catch (e) {
434
+ }
435
+ try {
436
+ return func + "";
437
+ } catch (e) {
438
+ }
439
+ }
440
+ return "";
441
+ }
442
+ var _toSource = toSource$1;
443
+ var isFunction = isFunction_1, isMasked = _isMasked, isObject$3 = isObject_1, toSource = _toSource;
444
+ var reRegExpChar = /[\\^$.*+?()[\]{}|]/g;
445
+ var reIsHostCtor = /^\[object .+?Constructor\]$/;
446
+ var funcProto = Function.prototype, objectProto$3 = Object.prototype;
447
+ var funcToString = funcProto.toString;
448
+ var hasOwnProperty$4 = objectProto$3.hasOwnProperty;
449
+ var reIsNative = RegExp(
450
+ "^" + funcToString.call(hasOwnProperty$4).replace(reRegExpChar, "\\$&").replace(/hasOwnProperty|(function).*?(?=\\\()| for .+?(?=\\\])/g, "$1.*?") + "$"
451
+ );
452
+ function baseIsNative$1(value) {
453
+ if (!isObject$3(value) || isMasked(value)) {
454
+ return false;
455
+ }
456
+ var pattern = isFunction(value) ? reIsNative : reIsHostCtor;
457
+ return pattern.test(toSource(value));
458
+ }
459
+ var _baseIsNative = baseIsNative$1;
460
+ function getValue$1(object, key) {
461
+ return object == null ? void 0 : object[key];
462
+ }
463
+ var _getValue = getValue$1;
464
+ var baseIsNative = _baseIsNative, getValue = _getValue;
465
+ function getNative$3(object, key) {
466
+ var value = getValue(object, key);
467
+ return baseIsNative(value) ? value : void 0;
468
+ }
469
+ var _getNative = getNative$3;
470
+ var getNative$2 = _getNative;
471
+ var defineProperty$1 = function() {
472
+ try {
473
+ var func = getNative$2(Object, "defineProperty");
474
+ func({}, "", {});
475
+ return func;
476
+ } catch (e) {
477
+ }
478
+ }();
479
+ var _defineProperty = defineProperty$1;
480
+ var defineProperty = _defineProperty;
481
+ function baseAssignValue$1(object, key, value) {
482
+ if (key == "__proto__" && defineProperty) {
483
+ defineProperty(object, key, {
484
+ "configurable": true,
485
+ "enumerable": true,
486
+ "value": value,
487
+ "writable": true
488
+ });
489
+ } else {
490
+ object[key] = value;
491
+ }
492
+ }
493
+ var _baseAssignValue = baseAssignValue$1;
494
+ function eq$2(value, other) {
495
+ return value === other || value !== value && other !== other;
496
+ }
497
+ var eq_1 = eq$2;
498
+ var baseAssignValue = _baseAssignValue, eq$1 = eq_1;
499
+ var objectProto$2 = Object.prototype;
500
+ var hasOwnProperty$3 = objectProto$2.hasOwnProperty;
501
+ function assignValue$1(object, key, value) {
502
+ var objValue = object[key];
503
+ if (!(hasOwnProperty$3.call(object, key) && eq$1(objValue, value)) || value === void 0 && !(key in object)) {
504
+ baseAssignValue(object, key, value);
505
+ }
506
+ }
507
+ var _assignValue = assignValue$1;
508
+ var isArray$4 = Array.isArray;
509
+ var isArray_1 = isArray$4;
510
+ function isObjectLike$1(value) {
511
+ return value != null && typeof value == "object";
512
+ }
513
+ var isObjectLike_1 = isObjectLike$1;
514
+ var baseGetTag = _baseGetTag, isObjectLike = isObjectLike_1;
515
+ var symbolTag = "[object Symbol]";
516
+ function isSymbol$3(value) {
517
+ return typeof value == "symbol" || isObjectLike(value) && baseGetTag(value) == symbolTag;
518
+ }
519
+ var isSymbol_1 = isSymbol$3;
520
+ var isArray$3 = isArray_1, isSymbol$2 = isSymbol_1;
521
+ var reIsDeepProp = /\.|\[(?:[^[\]]*|(["'])(?:(?!\1)[^\\]|\\.)*?\1)\]/, reIsPlainProp = /^\w*$/;
522
+ function isKey$1(value, object) {
523
+ if (isArray$3(value)) {
524
+ return false;
525
+ }
526
+ var type = typeof value;
527
+ if (type == "number" || type == "symbol" || type == "boolean" || value == null || isSymbol$2(value)) {
528
+ return true;
529
+ }
530
+ return reIsPlainProp.test(value) || !reIsDeepProp.test(value) || object != null && value in Object(object);
531
+ }
532
+ var _isKey = isKey$1;
533
+ var getNative$1 = _getNative;
534
+ var nativeCreate$4 = getNative$1(Object, "create");
535
+ var _nativeCreate = nativeCreate$4;
536
+ var nativeCreate$3 = _nativeCreate;
537
+ function hashClear$1() {
538
+ this.__data__ = nativeCreate$3 ? nativeCreate$3(null) : {};
539
+ this.size = 0;
540
+ }
541
+ var _hashClear = hashClear$1;
542
+ function hashDelete$1(key) {
543
+ var result = this.has(key) && delete this.__data__[key];
544
+ this.size -= result ? 1 : 0;
545
+ return result;
546
+ }
547
+ var _hashDelete = hashDelete$1;
548
+ var nativeCreate$2 = _nativeCreate;
549
+ var HASH_UNDEFINED$1 = "__lodash_hash_undefined__";
550
+ var objectProto$1 = Object.prototype;
551
+ var hasOwnProperty$2 = objectProto$1.hasOwnProperty;
552
+ function hashGet$1(key) {
553
+ var data = this.__data__;
554
+ if (nativeCreate$2) {
555
+ var result = data[key];
556
+ return result === HASH_UNDEFINED$1 ? void 0 : result;
557
+ }
558
+ return hasOwnProperty$2.call(data, key) ? data[key] : void 0;
559
+ }
560
+ var _hashGet = hashGet$1;
561
+ var nativeCreate$1 = _nativeCreate;
562
+ var objectProto = Object.prototype;
563
+ var hasOwnProperty$1 = objectProto.hasOwnProperty;
564
+ function hashHas$1(key) {
565
+ var data = this.__data__;
566
+ return nativeCreate$1 ? data[key] !== void 0 : hasOwnProperty$1.call(data, key);
567
+ }
568
+ var _hashHas = hashHas$1;
569
+ var nativeCreate = _nativeCreate;
570
+ var HASH_UNDEFINED = "__lodash_hash_undefined__";
571
+ function hashSet$1(key, value) {
572
+ var data = this.__data__;
573
+ this.size += this.has(key) ? 0 : 1;
574
+ data[key] = nativeCreate && value === void 0 ? HASH_UNDEFINED : value;
575
+ return this;
576
+ }
577
+ var _hashSet = hashSet$1;
578
+ var hashClear = _hashClear, hashDelete = _hashDelete, hashGet = _hashGet, hashHas = _hashHas, hashSet = _hashSet;
579
+ function Hash$1(entries) {
580
+ var index2 = -1, length = entries == null ? 0 : entries.length;
581
+ this.clear();
582
+ while (++index2 < length) {
583
+ var entry = entries[index2];
584
+ this.set(entry[0], entry[1]);
585
+ }
586
+ }
587
+ Hash$1.prototype.clear = hashClear;
588
+ Hash$1.prototype["delete"] = hashDelete;
589
+ Hash$1.prototype.get = hashGet;
590
+ Hash$1.prototype.has = hashHas;
591
+ Hash$1.prototype.set = hashSet;
592
+ var _Hash = Hash$1;
593
+ function listCacheClear$1() {
594
+ this.__data__ = [];
595
+ this.size = 0;
596
+ }
597
+ var _listCacheClear = listCacheClear$1;
598
+ var eq = eq_1;
599
+ function assocIndexOf$4(array, key) {
600
+ var length = array.length;
601
+ while (length--) {
602
+ if (eq(array[length][0], key)) {
603
+ return length;
604
+ }
605
+ }
606
+ return -1;
607
+ }
608
+ var _assocIndexOf = assocIndexOf$4;
609
+ var assocIndexOf$3 = _assocIndexOf;
610
+ var arrayProto = Array.prototype;
611
+ var splice = arrayProto.splice;
612
+ function listCacheDelete$1(key) {
613
+ var data = this.__data__, index2 = assocIndexOf$3(data, key);
614
+ if (index2 < 0) {
615
+ return false;
616
+ }
617
+ var lastIndex = data.length - 1;
618
+ if (index2 == lastIndex) {
619
+ data.pop();
620
+ } else {
621
+ splice.call(data, index2, 1);
622
+ }
623
+ --this.size;
624
+ return true;
625
+ }
626
+ var _listCacheDelete = listCacheDelete$1;
627
+ var assocIndexOf$2 = _assocIndexOf;
628
+ function listCacheGet$1(key) {
629
+ var data = this.__data__, index2 = assocIndexOf$2(data, key);
630
+ return index2 < 0 ? void 0 : data[index2][1];
631
+ }
632
+ var _listCacheGet = listCacheGet$1;
633
+ var assocIndexOf$1 = _assocIndexOf;
634
+ function listCacheHas$1(key) {
635
+ return assocIndexOf$1(this.__data__, key) > -1;
636
+ }
637
+ var _listCacheHas = listCacheHas$1;
638
+ var assocIndexOf = _assocIndexOf;
639
+ function listCacheSet$1(key, value) {
640
+ var data = this.__data__, index2 = assocIndexOf(data, key);
641
+ if (index2 < 0) {
642
+ ++this.size;
643
+ data.push([key, value]);
644
+ } else {
645
+ data[index2][1] = value;
646
+ }
647
+ return this;
648
+ }
649
+ var _listCacheSet = listCacheSet$1;
650
+ var listCacheClear = _listCacheClear, listCacheDelete = _listCacheDelete, listCacheGet = _listCacheGet, listCacheHas = _listCacheHas, listCacheSet = _listCacheSet;
651
+ function ListCache$1(entries) {
652
+ var index2 = -1, length = entries == null ? 0 : entries.length;
653
+ this.clear();
654
+ while (++index2 < length) {
655
+ var entry = entries[index2];
656
+ this.set(entry[0], entry[1]);
657
+ }
658
+ }
659
+ ListCache$1.prototype.clear = listCacheClear;
660
+ ListCache$1.prototype["delete"] = listCacheDelete;
661
+ ListCache$1.prototype.get = listCacheGet;
662
+ ListCache$1.prototype.has = listCacheHas;
663
+ ListCache$1.prototype.set = listCacheSet;
664
+ var _ListCache = ListCache$1;
665
+ var getNative = _getNative, root = _root;
666
+ var Map$1 = getNative(root, "Map");
667
+ var _Map = Map$1;
668
+ var Hash = _Hash, ListCache = _ListCache, Map = _Map;
669
+ function mapCacheClear$1() {
670
+ this.size = 0;
671
+ this.__data__ = {
672
+ "hash": new Hash(),
673
+ "map": new (Map || ListCache)(),
674
+ "string": new Hash()
675
+ };
676
+ }
677
+ var _mapCacheClear = mapCacheClear$1;
678
+ function isKeyable$1(value) {
679
+ var type = typeof value;
680
+ return type == "string" || type == "number" || type == "symbol" || type == "boolean" ? value !== "__proto__" : value === null;
681
+ }
682
+ var _isKeyable = isKeyable$1;
683
+ var isKeyable = _isKeyable;
684
+ function getMapData$4(map, key) {
685
+ var data = map.__data__;
686
+ return isKeyable(key) ? data[typeof key == "string" ? "string" : "hash"] : data.map;
687
+ }
688
+ var _getMapData = getMapData$4;
689
+ var getMapData$3 = _getMapData;
690
+ function mapCacheDelete$1(key) {
691
+ var result = getMapData$3(this, key)["delete"](key);
692
+ this.size -= result ? 1 : 0;
693
+ return result;
694
+ }
695
+ var _mapCacheDelete = mapCacheDelete$1;
696
+ var getMapData$2 = _getMapData;
697
+ function mapCacheGet$1(key) {
698
+ return getMapData$2(this, key).get(key);
699
+ }
700
+ var _mapCacheGet = mapCacheGet$1;
701
+ var getMapData$1 = _getMapData;
702
+ function mapCacheHas$1(key) {
703
+ return getMapData$1(this, key).has(key);
704
+ }
705
+ var _mapCacheHas = mapCacheHas$1;
706
+ var getMapData = _getMapData;
707
+ function mapCacheSet$1(key, value) {
708
+ var data = getMapData(this, key), size = data.size;
709
+ data.set(key, value);
710
+ this.size += data.size == size ? 0 : 1;
711
+ return this;
712
+ }
713
+ var _mapCacheSet = mapCacheSet$1;
714
+ var mapCacheClear = _mapCacheClear, mapCacheDelete = _mapCacheDelete, mapCacheGet = _mapCacheGet, mapCacheHas = _mapCacheHas, mapCacheSet = _mapCacheSet;
715
+ function MapCache$1(entries) {
716
+ var index2 = -1, length = entries == null ? 0 : entries.length;
717
+ this.clear();
718
+ while (++index2 < length) {
719
+ var entry = entries[index2];
720
+ this.set(entry[0], entry[1]);
721
+ }
722
+ }
723
+ MapCache$1.prototype.clear = mapCacheClear;
724
+ MapCache$1.prototype["delete"] = mapCacheDelete;
725
+ MapCache$1.prototype.get = mapCacheGet;
726
+ MapCache$1.prototype.has = mapCacheHas;
727
+ MapCache$1.prototype.set = mapCacheSet;
728
+ var _MapCache = MapCache$1;
729
+ var MapCache = _MapCache;
730
+ var FUNC_ERROR_TEXT = "Expected a function";
731
+ function memoize$1(func, resolver) {
732
+ if (typeof func != "function" || resolver != null && typeof resolver != "function") {
733
+ throw new TypeError(FUNC_ERROR_TEXT);
734
+ }
735
+ var memoized = function() {
736
+ var args = arguments, key = resolver ? resolver.apply(this, args) : args[0], cache = memoized.cache;
737
+ if (cache.has(key)) {
738
+ return cache.get(key);
739
+ }
740
+ var result = func.apply(this, args);
741
+ memoized.cache = cache.set(key, result) || cache;
742
+ return result;
743
+ };
744
+ memoized.cache = new (memoize$1.Cache || MapCache)();
745
+ return memoized;
746
+ }
747
+ memoize$1.Cache = MapCache;
748
+ var memoize_1 = memoize$1;
749
+ var memoize = memoize_1;
750
+ var MAX_MEMOIZE_SIZE = 500;
751
+ function memoizeCapped$1(func) {
752
+ var result = memoize(func, function(key) {
753
+ if (cache.size === MAX_MEMOIZE_SIZE) {
754
+ cache.clear();
755
+ }
756
+ return key;
757
+ });
758
+ var cache = result.cache;
759
+ return result;
760
+ }
761
+ var _memoizeCapped = memoizeCapped$1;
762
+ var memoizeCapped = _memoizeCapped;
763
+ var rePropName = /[^.[\]]+|\[(?:(-?\d+(?:\.\d+)?)|(["'])((?:(?!\2)[^\\]|\\.)*?)\2)\]|(?=(?:\.|\[\])(?:\.|\[\]|$))/g;
764
+ var reEscapeChar = /\\(\\)?/g;
765
+ var stringToPath$1 = memoizeCapped(function(string) {
766
+ var result = [];
767
+ if (string.charCodeAt(0) === 46) {
768
+ result.push("");
769
+ }
770
+ string.replace(rePropName, function(match, number, quote, subString) {
771
+ result.push(quote ? subString.replace(reEscapeChar, "$1") : number || match);
772
+ });
773
+ return result;
774
+ });
775
+ var _stringToPath = stringToPath$1;
776
+ function arrayMap$1(array, iteratee) {
777
+ var index2 = -1, length = array == null ? 0 : array.length, result = Array(length);
778
+ while (++index2 < length) {
779
+ result[index2] = iteratee(array[index2], index2, array);
780
+ }
781
+ return result;
782
+ }
783
+ var _arrayMap = arrayMap$1;
784
+ var Symbol$1 = _Symbol, arrayMap = _arrayMap, isArray$2 = isArray_1, isSymbol$1 = isSymbol_1;
785
+ var INFINITY$1 = 1 / 0;
786
+ var symbolProto = Symbol$1 ? Symbol$1.prototype : void 0, symbolToString = symbolProto ? symbolProto.toString : void 0;
787
+ function baseToString$1(value) {
788
+ if (typeof value == "string") {
789
+ return value;
790
+ }
791
+ if (isArray$2(value)) {
792
+ return arrayMap(value, baseToString$1) + "";
793
+ }
794
+ if (isSymbol$1(value)) {
795
+ return symbolToString ? symbolToString.call(value) : "";
796
+ }
797
+ var result = value + "";
798
+ return result == "0" && 1 / value == -INFINITY$1 ? "-0" : result;
799
+ }
800
+ var _baseToString = baseToString$1;
801
+ var baseToString = _baseToString;
802
+ function toString$1(value) {
803
+ return value == null ? "" : baseToString(value);
804
+ }
805
+ var toString_1 = toString$1;
806
+ var isArray$1 = isArray_1, isKey = _isKey, stringToPath = _stringToPath, toString = toString_1;
807
+ function castPath$1(value, object) {
808
+ if (isArray$1(value)) {
809
+ return value;
810
+ }
811
+ return isKey(value, object) ? [value] : stringToPath(toString(value));
812
+ }
813
+ var _castPath = castPath$1;
814
+ var MAX_SAFE_INTEGER = 9007199254740991;
815
+ var reIsUint = /^(?:0|[1-9]\d*)$/;
816
+ function isIndex$1(value, length) {
817
+ var type = typeof value;
818
+ length = length == null ? MAX_SAFE_INTEGER : length;
819
+ return !!length && (type == "number" || type != "symbol" && reIsUint.test(value)) && (value > -1 && value % 1 == 0 && value < length);
820
+ }
821
+ var _isIndex = isIndex$1;
822
+ var isSymbol = isSymbol_1;
823
+ var INFINITY = 1 / 0;
824
+ function toKey$1(value) {
825
+ if (typeof value == "string" || isSymbol(value)) {
826
+ return value;
827
+ }
828
+ var result = value + "";
829
+ return result == "0" && 1 / value == -INFINITY ? "-0" : result;
830
+ }
831
+ var _toKey = toKey$1;
832
+ var assignValue = _assignValue, castPath = _castPath, isIndex = _isIndex, isObject$2 = isObject_1, toKey = _toKey;
833
+ function baseSet$1(object, path, value, customizer) {
834
+ if (!isObject$2(object)) {
835
+ return object;
836
+ }
837
+ path = castPath(path, object);
838
+ var index2 = -1, length = path.length, lastIndex = length - 1, nested = object;
839
+ while (nested != null && ++index2 < length) {
840
+ var key = toKey(path[index2]), newValue = value;
841
+ if (key === "__proto__" || key === "constructor" || key === "prototype") {
842
+ return object;
843
+ }
844
+ if (index2 != lastIndex) {
845
+ var objValue = nested[key];
846
+ newValue = customizer ? customizer(objValue, key, nested) : void 0;
847
+ if (newValue === void 0) {
848
+ newValue = isObject$2(objValue) ? objValue : isIndex(path[index2 + 1]) ? [] : {};
849
+ }
850
+ }
851
+ assignValue(nested, key, newValue);
852
+ nested = nested[key];
853
+ }
854
+ return object;
855
+ }
856
+ var _baseSet = baseSet$1;
857
+ var baseSet = _baseSet;
858
+ function set(object, path, value) {
859
+ return object == null ? object : baseSet(object, path, value);
860
+ }
861
+ var set_1 = set;
862
+ const isDate = (d) => d instanceof Date;
863
+ const isEmpty = (o) => Object.keys(o).length === 0;
864
+ const isObject$1 = (o) => o != null && typeof o === "object";
865
+ const hasOwnProperty = (o, ...args) => Object.prototype.hasOwnProperty.call(o, ...args);
866
+ const isEmptyObject = (o) => isObject$1(o) && isEmpty(o);
867
+ const makeObjectWithoutPrototype = () => /* @__PURE__ */ Object.create(null);
868
+ const updatedDiff = (lhs, rhs) => {
869
+ if (lhs === rhs)
870
+ return {};
871
+ if (!isObject$1(lhs) || !isObject$1(rhs))
872
+ return rhs;
873
+ if (isDate(lhs) || isDate(rhs)) {
874
+ if (lhs.valueOf() == rhs.valueOf())
875
+ return {};
876
+ return rhs;
877
+ }
878
+ return Object.keys(rhs).reduce((acc, key) => {
879
+ if (hasOwnProperty(lhs, key)) {
880
+ const difference = updatedDiff(lhs[key], rhs[key]);
881
+ if (isEmptyObject(difference) && !isDate(difference) && (isEmptyObject(lhs[key]) || !isEmptyObject(rhs[key])))
882
+ return acc;
883
+ acc[key] = difference;
884
+ return acc;
885
+ }
886
+ return acc;
887
+ }, makeObjectWithoutPrototype());
888
+ };
889
+ function isObject(value) {
890
+ return value !== null && typeof value === "object" && !(value instanceof Date);
891
+ }
892
+ var isArray = Array.isArray;
893
+ function deepKeys(obj, stack, parent, intermediate) {
894
+ Object.keys(obj).forEach(function(el) {
895
+ var escaped = el.replace(/\./g, "\\.");
896
+ if (isObject(obj[el]) && !isArray(obj[el])) {
897
+ var p = parent ? parent + "." + escaped : parent;
898
+ if (intermediate)
899
+ stack.push(parent ? p : escaped);
900
+ deepKeys(obj[el], stack, p || escaped, intermediate);
901
+ } else {
902
+ var key = parent ? parent + "." + escaped : escaped;
903
+ stack.push(key);
904
+ }
905
+ });
906
+ return stack;
907
+ }
908
+ var deepKeys_1 = function(obj, intermediate) {
909
+ return deepKeys(obj, [], null, intermediate);
910
+ };
911
+ function getMetaValue(name) {
912
+ const element = document.head.querySelector(`meta[name="${name}"]`);
913
+ return element.getAttribute("content");
914
+ }
915
+ function array_move(arr, old_index, new_index) {
916
+ if (new_index >= arr.length) {
917
+ var k = new_index - arr.length + 1;
918
+ while (k--) {
919
+ arr.push(void 0);
920
+ }
921
+ }
922
+ arr.splice(new_index, 0, arr.splice(old_index, 1)[0]);
923
+ return arr;
924
+ }
925
+ class kubik_widget_controller_default extends Controller {
926
+ connect() {
927
+ this.getNewWidget();
928
+ }
929
+ moveItemUp(event) {
930
+ const item = event.currentTarget;
931
+ this.moveItemToPosition(item, -1);
932
+ }
933
+ moveItemDown(event) {
934
+ const item = event.currentTarget;
935
+ this.moveItemToPosition(item, 1);
936
+ }
937
+ moveItemToPosition(item, change) {
938
+ const itemIndex = parseInt(item.dataset.itemIndex);
939
+ const tab = item.dataset.targetList;
940
+ const items = this.dataValue[tab]["repeated_items"];
941
+ const newOrder = array_move(items, itemIndex, itemIndex + change);
942
+ const newValues = Object.assign({}, this.dataValue);
943
+ newValues[tab]["repeated_items"] = newOrder;
944
+ this.dataValue = newValues;
945
+ }
946
+ removeItem(event) {
947
+ const target = event.currentTarget;
948
+ const index2 = target.dataset.itemIndex;
949
+ const tab = target.dataset.targetList;
950
+ const newValues = Object.assign({}, this.dataValue);
951
+ newValues[tab]["repeated_items"].splice(index2, 1);
952
+ this.dataValue = newValues;
953
+ this.getNewWidget();
954
+ }
955
+ addItem(event) {
956
+ const target = event.currentTarget;
957
+ const tab = target.dataset.targetList;
958
+ if (typeof this.dataValue[tab] == "undefined") {
959
+ this.dataValue = Object.assign({}, this.dataValue, { [tab]: { "repeated_items": [] } });
960
+ }
961
+ this.dataValue = Object.assign({}, this.dataValue, { [tab]: { "repeated_items": [...this.dataValue[tab]["repeated_items"], {}] } });
962
+ this.getNewWidget();
963
+ }
964
+ dataValueChanged(value, previousValue) {
965
+ const diff = updatedDiff(value, previousValue);
966
+ const changedKeys = deepKeys_1(diff);
967
+ const addedItem = diff["items"] && diff["items"]["repeated_items"] && JSON.stringify(Object.values(diff["items"]["repeated_items"])[0]) === JSON.stringify({});
968
+ if (addedItem || changedKeys.filter((key) => key.match(/^id$|\.id$/g)).length > 0) {
969
+ this.getNewWidget();
970
+ }
971
+ }
972
+ updateField(event) {
973
+ const name = event.currentTarget.name;
974
+ const duplicateData = set_1(this.dataValue, name, event.currentTarget.value);
975
+ this.dataValue = duplicateData;
976
+ }
977
+ selectResult(e) {
978
+ const target = e.currentTarget;
979
+ const returnObject = JSON.parse(target.dataset.returnObject);
980
+ const duplicateData = set_1(this.dataValue, target.dataset.fieldName, returnObject.id);
981
+ this.dataValue = duplicateData;
982
+ this.getNewWidget();
983
+ }
984
+ removeResource(e) {
985
+ const target = e.currentTarget;
986
+ const duplicateData = set_1(this.dataValue, target.dataset.fieldName, null);
987
+ this.dataValue = duplicateData;
988
+ this.getNewWidget();
989
+ }
990
+ receiveModalReturn(return_value) {
991
+ const returnObject = return_value;
992
+ const duplicateData = set_1(this.dataValue, returnObject["return_payload"]["field_name"], returnObject["payload"]["id"]);
993
+ this.dataValue = duplicateData;
994
+ this.getNewWidget();
995
+ }
996
+ getNewWidget() {
997
+ fetch(`${this.setupValue["src"]}.json`, {
998
+ method: "POST",
999
+ headers: {
1000
+ "Content-Type": "application/json",
1001
+ "X-CSRF-Token": getMetaValue("csrf-token")
1002
+ },
1003
+ body: JSON.stringify({
1004
+ widget_id: this.widgetIdValue,
1005
+ data: this.dataValue,
1006
+ setup: this.setupValue,
1007
+ max_items: this.maxItemsValue
1008
+ })
1009
+ }).then((response) => response.json()).then((data) => {
1010
+ this.element.innerHTML = data.html_data;
1011
+ });
1012
+ }
1013
+ }
1014
+ kubik_widget_controller_default.values = {
1015
+ widgetId: String,
1016
+ setup: Object,
1017
+ data: Object,
1018
+ maxItems: { type: Number, default: 0 }
1019
+ };
1020
+ function makeElement(tagName, classNames = [], attributes = {}, textContent = "") {
1021
+ const el = document.createElement(tagName);
1022
+ el.classList.add(...classNames);
1023
+ for (const attrName in attributes) {
1024
+ el.setAttribute(attrName, attributes[attrName]);
1025
+ }
1026
+ if (typeof textContent == "string") {
1027
+ el.innerText = textContent;
1028
+ }
1029
+ return el;
1030
+ }
1031
+ class kubik_autocomplete_controller_default extends Controller {
1032
+ connect() {
1033
+ this._loadDefaultResults();
1034
+ }
1035
+ showForm() {
1036
+ this.element.classList.add(this.activeListClass);
1037
+ }
1038
+ cancel(event) {
1039
+ event.preventDefault();
1040
+ this.element.classList.remove(this.activeListClass);
1041
+ }
1042
+ errorValueChanged() {
1043
+ if (this.errorValue == "") {
1044
+ this.element.classList.remove(this.fetchErrorClass);
1045
+ } else {
1046
+ this.element.classList.add(this.fetchErrorClass);
1047
+ }
1048
+ }
1049
+ resultActiveValueChanged() {
1050
+ this.resultTargets.forEach((result) => {
1051
+ result.classList.remove(this.activeResultClass);
1052
+ });
1053
+ if (!isNaN(this.resultActiveValue)) {
1054
+ const activeResult = this.resultActiveValue;
1055
+ this.resultTargets[activeResult].classList.add(this.activeResultClass);
1056
+ }
1057
+ }
1058
+ mouseOver(e) {
1059
+ const target = e.currentTarget;
1060
+ this.resultActiveValue = parseInt(target.dataset["index"]);
1061
+ }
1062
+ mouseOut(e) {
1063
+ this.resultActiveValue = null;
1064
+ }
1065
+ resultsValueChanged() {
1066
+ const fieldNameValue = this.fieldNameValue;
1067
+ this.resultTargets.forEach((result) => result.remove());
1068
+ this.resultActiveValue = null;
1069
+ this.resultsValue.forEach((result, index2) => {
1070
+ const returnObject = Object.assign(
1071
+ { receive_index: this.inputTarget.dataset["itemIndex"] },
1072
+ result
1073
+ );
1074
+ const li = makeElement("li", [
1075
+ "kubik-wysiwyg-component--item-autocomplete-results-list-item"
1076
+ ], {
1077
+ "data-kubik-autocomplete-target": "result",
1078
+ "data-index": index2,
1079
+ "data-return-id": returnObject["id"],
1080
+ "data-return-object": JSON.stringify(returnObject),
1081
+ "data-field-name": fieldNameValue,
1082
+ "data-action": "mouseout->kubik-autocomplete#mouseOut mouseover->kubik-autocomplete#mouseOver mousedown->kubik-widget#selectResult mousedown->kubik-autocomplete#blur"
1083
+ }, result["return_object"]["display_name"]);
1084
+ this.resultsListTarget.insertBefore(li, this.feedbackTarget);
1085
+ });
1086
+ if (this.resultActiveValue > this.resultsValue.length) {
1087
+ this.resultActiveValue = this.resultsValue.length - 1;
1088
+ }
1089
+ }
1090
+ load() {
1091
+ this._fetchResults();
1092
+ }
1093
+ focus() {
1094
+ this.element.classList.add(this.activeClass);
1095
+ }
1096
+ blur() {
1097
+ this.errorValue = "";
1098
+ this.inputTarget.blur();
1099
+ this.connectionController.abort();
1100
+ this.element.classList.remove(this.fetchErrorClass);
1101
+ this.element.classList.remove(this.loadingClass);
1102
+ this.element.classList.remove(this.activeClass);
1103
+ }
1104
+ _sourceURL() {
1105
+ const baseUrl = [window.location.protocol, window.location.host].join("//");
1106
+ const params = new URLSearchParams({
1107
+ kubik_search: "1",
1108
+ q: this.inputTarget.value
1109
+ });
1110
+ const url = new URL([this.srcValue, ".json?", params.toString()].join(""), baseUrl);
1111
+ return url;
1112
+ }
1113
+ _loadDefaultResults() {
1114
+ this._fetchResults();
1115
+ }
1116
+ keyCheck(e) {
1117
+ switch (e.key) {
1118
+ case "Down":
1119
+ case "ArrowDown":
1120
+ this._nextResult();
1121
+ e.preventDefault();
1122
+ e.stopImmediatePropagation();
1123
+ return;
1124
+ case "Up":
1125
+ case "ArrowUp":
1126
+ this._previousResult();
1127
+ e.preventDefault();
1128
+ e.stopImmediatePropagation();
1129
+ return;
1130
+ case "Enter":
1131
+ const selectedElement = this.resultTargets[this.resultActiveValue];
1132
+ if (selectedElement) {
1133
+ selectedElement.dispatchEvent(new Event("mousedown"));
1134
+ }
1135
+ e.preventDefault();
1136
+ e.stopImmediatePropagation();
1137
+ return;
1138
+ case "Backspace":
1139
+ e.stopImmediatePropagation();
1140
+ return;
1141
+ }
1142
+ }
1143
+ _previousResult() {
1144
+ if (isNaN(this.resultActiveValue)) {
1145
+ this.resultActiveValue = this.resultTargets.length > 1 ? this.resultTargets.length - 1 : 0;
1146
+ } else {
1147
+ this.resultActiveValue = this.resultActiveValue == 0 ? this.resultTargets.length - 1 : this.resultActiveValue - 1;
1148
+ }
1149
+ }
1150
+ _nextResult() {
1151
+ if (isNaN(this.resultActiveValue)) {
1152
+ this.resultActiveValue = 0;
1153
+ } else {
1154
+ this.resultActiveValue = this.resultActiveValue + 1 == this.resultTargets.length ? 0 : this.resultActiveValue + 1;
1155
+ }
1156
+ }
1157
+ _fetchResults() {
1158
+ this.errorValue = "";
1159
+ this.feedbackTarget.innerHTML = "Loading...";
1160
+ this.connectionController = new AbortController();
1161
+ fetch(this._sourceURL(), {
1162
+ headers: {
1163
+ "Content-Type": "application/json"
1164
+ }
1165
+ }).then((response) => {
1166
+ return response.json();
1167
+ }).then((result) => {
1168
+ this.errorValue = "";
1169
+ if (result.length > 0) {
1170
+ this.resultsValue = result;
1171
+ this.queryValue = this.inputTarget.value;
1172
+ }
1173
+ if (this.inputTarget.value == "") {
1174
+ this.queryValue = "";
1175
+ this.feedbackTarget.innerHTML = "";
1176
+ this.feedbackTarget.insertAdjacentHTML("beforeend", `Showing latest <span>${result.length}</span> results`);
1177
+ } else {
1178
+ this.feedbackTarget.innerHTML = "";
1179
+ if (this.queryValue == "") {
1180
+ this.feedbackTarget.insertAdjacentHTML("beforeend", `Showing latest <span>${result.length}</span> results`);
1181
+ } else {
1182
+ this.feedbackTarget.insertAdjacentHTML("beforeend", `Showing results for <span>${this.queryValue}</span>`);
1183
+ }
1184
+ }
1185
+ if (this.queryValue == this.inputTarget.value) {
1186
+ this.element.classList.remove(this.noResultsClass);
1187
+ } else {
1188
+ this.element.classList.add(this.noResultsClass);
1189
+ }
1190
+ }).catch((error) => {
1191
+ this.feedbackTarget.innerHTML = "";
1192
+ this.errorValue = "Error loading the results";
1193
+ });
1194
+ }
1195
+ }
1196
+ kubik_autocomplete_controller_default.classes = ["fetchError", "loading", "active", "activeResult", "noResults", "activeList"];
1197
+ kubik_autocomplete_controller_default.targets = ["feedback", "input", "resultsList", "result"];
1198
+ kubik_autocomplete_controller_default.values = {
1199
+ returnController: String,
1200
+ src: String,
1201
+ fieldName: String,
1202
+ query: { type: String, default: "" },
1203
+ error: { type: String, default: "" },
1204
+ results: { type: Array, default: [] },
1205
+ resultActive: Number
1206
+ };
1207
+ const widgetWrapper = function widgetWrapper2(details = {}, data) {
1208
+ let wrapperAttributes = {
1209
+ "data-controller": "kubik-widget",
1210
+ "data-kubik-widget-setup-value": JSON.stringify(details.setup),
1211
+ "data-kubik-widget-data-value": JSON.stringify(data),
1212
+ "data-kubik-widget-widget-id-value": details.setup.widget_id,
1213
+ id: details.setup.widget_id
1214
+ };
1215
+ if (details.items_limit) {
1216
+ wrapperAttributes["data-kubik-widget-items-max-items-value"] = details.items_limit;
1217
+ }
1218
+ const wrapper = makeElement(
1219
+ "div",
1220
+ [
1221
+ "kubik_media_wrapper",
1222
+ "kubik-wysiwyg-component"
1223
+ ],
1224
+ wrapperAttributes
1225
+ );
1226
+ return wrapper;
1227
+ };
1228
+ class PluginFactory {
1229
+ static get defaultWidgetConfig() {
1230
+ return {
1231
+ data_src: "/admin/kubik_wysiwyg_widget/show"
1232
+ };
1233
+ }
1234
+ constructor({ data, api }) {
1235
+ this.data = data;
1236
+ this.api = api;
1237
+ this.randomString = Math.random().toString(36).substring(2, 7);
1238
+ this.label = this.constructor.toolbox.title;
1239
+ const defaultConfig = this.constructor.defaultWidgetConfig;
1240
+ const localConfig = this.constructor.widgetConfig;
1241
+ this.config = Object.assign(
1242
+ defaultConfig,
1243
+ localConfig
1244
+ );
1245
+ }
1246
+ render() {
1247
+ const widgetId = [this.config.widget_name, this.randomString].join("-");
1248
+ const wrapper = widgetWrapper({
1249
+ setup: {
1250
+ label: this.label,
1251
+ widget_model: this.config.widget_model,
1252
+ src: this.config.data_src,
1253
+ widget_id: widgetId,
1254
+ widget_type: this.config.widget_name,
1255
+ config: this.config
1256
+ }
1257
+ }, this.data);
1258
+ return wrapper;
1259
+ }
1260
+ save(blockContent) {
1261
+ const data = JSON.parse(blockContent.attributes["data-kubik-widget-data-value"].value);
1262
+ let widgetData = {};
1263
+ this.config.tabs.forEach((tab) => {
1264
+ widgetData[tab["name"]] = data[tab["name"]];
1265
+ });
1266
+ return widgetData;
1267
+ }
1268
+ validate(savedData) {
1269
+ return true;
1270
+ }
1271
+ }
1272
+ var index = {
1273
+ KubikWidgetController: kubik_widget_controller_default,
1274
+ PluginFactory,
1275
+ KubikAutocompleteController: kubik_autocomplete_controller_default
1276
+ };
1277
+ export { index as default };