stimulus-library 0.7.4 → 0.7.5
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/CHANGELOG.md +7 -0
- package/README.md +1 -1
- package/dist/mixins/create_mixin.d.ts +1 -1
- package/dist/mixins/create_mixin.d.ts.map +1 -1
- package/dist/mixins/install_class_methods.d.ts +1 -1
- package/dist/mixins/install_class_methods.d.ts.map +1 -1
- package/dist/mixins/use_click_outside.d.ts +1 -1
- package/dist/mixins/use_click_outside.d.ts.map +1 -1
- package/dist/mixins/use_event_bus.d.ts +1 -1
- package/dist/mixins/use_event_bus.d.ts.map +1 -1
- package/dist/mixins/use_event_listener.d.ts +1 -1
- package/dist/mixins/use_event_listener.d.ts.map +1 -1
- package/dist/mixins/use_fullscreen.d.ts +1 -1
- package/dist/mixins/use_fullscreen.d.ts.map +1 -1
- package/dist/mixins/use_geolocation.d.ts +1 -1
- package/dist/mixins/use_geolocation.d.ts.map +1 -1
- package/dist/mixins/use_hover.d.ts +1 -1
- package/dist/mixins/use_hover.d.ts.map +1 -1
- package/dist/mixins/use_injected_html.d.ts +1 -1
- package/dist/mixins/use_injected_html.d.ts.map +1 -1
- package/dist/mixins/use_intersection.d.ts +1 -1
- package/dist/mixins/use_intersection.d.ts.map +1 -1
- package/dist/mixins/use_interval.d.ts +1 -1
- package/dist/mixins/use_interval.d.ts.map +1 -1
- package/dist/mixins/use_localstorage.d.ts +1 -1
- package/dist/mixins/use_localstorage.d.ts.map +1 -1
- package/dist/mixins/use_mutation_observer.d.ts +1 -1
- package/dist/mixins/use_mutation_observer.d.ts.map +1 -1
- package/dist/mixins/use_temporary_content.d.ts +1 -1
- package/dist/mixins/use_temporary_content.d.ts.map +1 -1
- package/dist/mixins/use_timeout.d.ts +1 -1
- package/dist/mixins/use_timeout.d.ts.map +1 -1
- package/dist/mixins/use_trix_modifiers.d.ts +1 -1
- package/dist/mixins/use_trix_modifiers.d.ts.map +1 -1
- package/dist/stimulus-library.cjs.js +1 -1
- package/dist/stimulus-library.cjs.js.map +1 -1
- package/dist/stimulus-library.es.js +340 -1
- package/dist/stimulus-library.es.js.map +1 -1
- package/dist/stimulus-library.umd.js +1 -1
- package/dist/stimulus-library.umd.js.map +1 -1
- package/dist/utilities/base_controller.d.ts +1 -1
- package/dist/utilities/base_controller.d.ts.map +1 -1
- package/dist/utilities/events.d.ts +1 -1
- package/dist/utilities/events.d.ts.map +1 -1
- package/dist/utilities/logging.d.ts +1 -1
- package/dist/utilities/logging.d.ts.map +1 -1
- package/dist/utilities/stimulus.d.ts +1 -1
- package/dist/utilities/stimulus.d.ts.map +1 -1
- package/package.json +7 -10
- package/dist/mixins/use_resize.d.ts +0 -5
- package/dist/mixins/use_resize.d.ts.map +0 -1
|
@@ -18,9 +18,348 @@ var __publicField = (obj, key, value) => {
|
|
|
18
18
|
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
19
19
|
return value;
|
|
20
20
|
};
|
|
21
|
-
import { Controller } from "stimulus";
|
|
22
21
|
import { camelCase, debounce, upperFirst, clamp, get, set } from "lodash-es";
|
|
23
22
|
import { isPast, intervalToDuration, formatDuration, toDate, formatDistanceToNow } from "date-fns";
|
|
23
|
+
function camelize(value) {
|
|
24
|
+
return value.replace(/(?:[_-])([a-z0-9])/g, (_, char) => char.toUpperCase());
|
|
25
|
+
}
|
|
26
|
+
function capitalize(value) {
|
|
27
|
+
return value.charAt(0).toUpperCase() + value.slice(1);
|
|
28
|
+
}
|
|
29
|
+
function dasherize(value) {
|
|
30
|
+
return value.replace(/([A-Z])/g, (_, char) => `-${char.toLowerCase()}`);
|
|
31
|
+
}
|
|
32
|
+
function readInheritableStaticArrayValues(constructor, propertyName) {
|
|
33
|
+
const ancestors = getAncestorsForConstructor(constructor);
|
|
34
|
+
return Array.from(ancestors.reduce((values, constructor2) => {
|
|
35
|
+
getOwnStaticArrayValues(constructor2, propertyName).forEach((name) => values.add(name));
|
|
36
|
+
return values;
|
|
37
|
+
}, new Set()));
|
|
38
|
+
}
|
|
39
|
+
function readInheritableStaticObjectPairs(constructor, propertyName) {
|
|
40
|
+
const ancestors = getAncestorsForConstructor(constructor);
|
|
41
|
+
return ancestors.reduce((pairs, constructor2) => {
|
|
42
|
+
pairs.push(...getOwnStaticObjectPairs(constructor2, propertyName));
|
|
43
|
+
return pairs;
|
|
44
|
+
}, []);
|
|
45
|
+
}
|
|
46
|
+
function getAncestorsForConstructor(constructor) {
|
|
47
|
+
const ancestors = [];
|
|
48
|
+
while (constructor) {
|
|
49
|
+
ancestors.push(constructor);
|
|
50
|
+
constructor = Object.getPrototypeOf(constructor);
|
|
51
|
+
}
|
|
52
|
+
return ancestors.reverse();
|
|
53
|
+
}
|
|
54
|
+
function getOwnStaticArrayValues(constructor, propertyName) {
|
|
55
|
+
const definition = constructor[propertyName];
|
|
56
|
+
return Array.isArray(definition) ? definition : [];
|
|
57
|
+
}
|
|
58
|
+
function getOwnStaticObjectPairs(constructor, propertyName) {
|
|
59
|
+
const definition = constructor[propertyName];
|
|
60
|
+
return definition ? Object.keys(definition).map((key) => [key, definition[key]]) : [];
|
|
61
|
+
}
|
|
62
|
+
(() => {
|
|
63
|
+
function extendWithReflect(constructor) {
|
|
64
|
+
function extended() {
|
|
65
|
+
return Reflect.construct(constructor, arguments, new.target);
|
|
66
|
+
}
|
|
67
|
+
extended.prototype = Object.create(constructor.prototype, {
|
|
68
|
+
constructor: { value: extended }
|
|
69
|
+
});
|
|
70
|
+
Reflect.setPrototypeOf(extended, constructor);
|
|
71
|
+
return extended;
|
|
72
|
+
}
|
|
73
|
+
function testReflectExtension() {
|
|
74
|
+
const a = function() {
|
|
75
|
+
this.a.call(this);
|
|
76
|
+
};
|
|
77
|
+
const b = extendWithReflect(a);
|
|
78
|
+
b.prototype.a = function() {
|
|
79
|
+
};
|
|
80
|
+
return new b();
|
|
81
|
+
}
|
|
82
|
+
try {
|
|
83
|
+
testReflectExtension();
|
|
84
|
+
return extendWithReflect;
|
|
85
|
+
} catch (error) {
|
|
86
|
+
return (constructor) => class extended extends constructor {
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
})();
|
|
90
|
+
function ClassPropertiesBlessing(constructor) {
|
|
91
|
+
const classes = readInheritableStaticArrayValues(constructor, "classes");
|
|
92
|
+
return classes.reduce((properties, classDefinition) => {
|
|
93
|
+
return Object.assign(properties, propertiesForClassDefinition(classDefinition));
|
|
94
|
+
}, {});
|
|
95
|
+
}
|
|
96
|
+
function propertiesForClassDefinition(key) {
|
|
97
|
+
return {
|
|
98
|
+
[`${key}Class`]: {
|
|
99
|
+
get() {
|
|
100
|
+
const { classes } = this;
|
|
101
|
+
if (classes.has(key)) {
|
|
102
|
+
return classes.get(key);
|
|
103
|
+
} else {
|
|
104
|
+
const attribute = classes.getAttributeName(key);
|
|
105
|
+
throw new Error(`Missing attribute "${attribute}"`);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
},
|
|
109
|
+
[`${key}Classes`]: {
|
|
110
|
+
get() {
|
|
111
|
+
return this.classes.getAll(key);
|
|
112
|
+
}
|
|
113
|
+
},
|
|
114
|
+
[`has${capitalize(key)}Class`]: {
|
|
115
|
+
get() {
|
|
116
|
+
return this.classes.has(key);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
function TargetPropertiesBlessing(constructor) {
|
|
122
|
+
const targets = readInheritableStaticArrayValues(constructor, "targets");
|
|
123
|
+
return targets.reduce((properties, targetDefinition) => {
|
|
124
|
+
return Object.assign(properties, propertiesForTargetDefinition(targetDefinition));
|
|
125
|
+
}, {});
|
|
126
|
+
}
|
|
127
|
+
function propertiesForTargetDefinition(name) {
|
|
128
|
+
return {
|
|
129
|
+
[`${name}Target`]: {
|
|
130
|
+
get() {
|
|
131
|
+
const target = this.targets.find(name);
|
|
132
|
+
if (target) {
|
|
133
|
+
return target;
|
|
134
|
+
} else {
|
|
135
|
+
throw new Error(`Missing target element "${name}" for "${this.identifier}" controller`);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
},
|
|
139
|
+
[`${name}Targets`]: {
|
|
140
|
+
get() {
|
|
141
|
+
return this.targets.findAll(name);
|
|
142
|
+
}
|
|
143
|
+
},
|
|
144
|
+
[`has${capitalize(name)}Target`]: {
|
|
145
|
+
get() {
|
|
146
|
+
return this.targets.has(name);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
function ValuePropertiesBlessing(constructor) {
|
|
152
|
+
const valueDefinitionPairs = readInheritableStaticObjectPairs(constructor, "values");
|
|
153
|
+
const propertyDescriptorMap = {
|
|
154
|
+
valueDescriptorMap: {
|
|
155
|
+
get() {
|
|
156
|
+
return valueDefinitionPairs.reduce((result, valueDefinitionPair) => {
|
|
157
|
+
const valueDescriptor = parseValueDefinitionPair(valueDefinitionPair);
|
|
158
|
+
const attributeName = this.data.getAttributeNameForKey(valueDescriptor.key);
|
|
159
|
+
return Object.assign(result, { [attributeName]: valueDescriptor });
|
|
160
|
+
}, {});
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
};
|
|
164
|
+
return valueDefinitionPairs.reduce((properties, valueDefinitionPair) => {
|
|
165
|
+
return Object.assign(properties, propertiesForValueDefinitionPair(valueDefinitionPair));
|
|
166
|
+
}, propertyDescriptorMap);
|
|
167
|
+
}
|
|
168
|
+
function propertiesForValueDefinitionPair(valueDefinitionPair) {
|
|
169
|
+
const definition = parseValueDefinitionPair(valueDefinitionPair);
|
|
170
|
+
const { key, name, reader: read, writer: write } = definition;
|
|
171
|
+
return {
|
|
172
|
+
[name]: {
|
|
173
|
+
get() {
|
|
174
|
+
const value = this.data.get(key);
|
|
175
|
+
if (value !== null) {
|
|
176
|
+
return read(value);
|
|
177
|
+
} else {
|
|
178
|
+
return definition.defaultValue;
|
|
179
|
+
}
|
|
180
|
+
},
|
|
181
|
+
set(value) {
|
|
182
|
+
if (value === void 0) {
|
|
183
|
+
this.data.delete(key);
|
|
184
|
+
} else {
|
|
185
|
+
this.data.set(key, write(value));
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
},
|
|
189
|
+
[`has${capitalize(name)}`]: {
|
|
190
|
+
get() {
|
|
191
|
+
return this.data.has(key) || definition.hasCustomDefaultValue;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
function parseValueDefinitionPair([token, typeDefinition]) {
|
|
197
|
+
return valueDescriptorForTokenAndTypeDefinition(token, typeDefinition);
|
|
198
|
+
}
|
|
199
|
+
function parseValueTypeConstant(constant) {
|
|
200
|
+
switch (constant) {
|
|
201
|
+
case Array:
|
|
202
|
+
return "array";
|
|
203
|
+
case Boolean:
|
|
204
|
+
return "boolean";
|
|
205
|
+
case Number:
|
|
206
|
+
return "number";
|
|
207
|
+
case Object:
|
|
208
|
+
return "object";
|
|
209
|
+
case String:
|
|
210
|
+
return "string";
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
function parseValueTypeDefault(defaultValue) {
|
|
214
|
+
switch (typeof defaultValue) {
|
|
215
|
+
case "boolean":
|
|
216
|
+
return "boolean";
|
|
217
|
+
case "number":
|
|
218
|
+
return "number";
|
|
219
|
+
case "string":
|
|
220
|
+
return "string";
|
|
221
|
+
}
|
|
222
|
+
if (Array.isArray(defaultValue))
|
|
223
|
+
return "array";
|
|
224
|
+
if (Object.prototype.toString.call(defaultValue) === "[object Object]")
|
|
225
|
+
return "object";
|
|
226
|
+
}
|
|
227
|
+
function parseValueTypeObject(typeObject) {
|
|
228
|
+
const typeFromObject = parseValueTypeConstant(typeObject.type);
|
|
229
|
+
if (typeFromObject) {
|
|
230
|
+
const defaultValueType = parseValueTypeDefault(typeObject.default);
|
|
231
|
+
if (typeFromObject !== defaultValueType) {
|
|
232
|
+
throw new Error(`Type "${typeFromObject}" must match the type of the default value. Given default value: "${typeObject.default}" as "${defaultValueType}"`);
|
|
233
|
+
}
|
|
234
|
+
return typeFromObject;
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
function parseValueTypeDefinition(typeDefinition) {
|
|
238
|
+
const typeFromObject = parseValueTypeObject(typeDefinition);
|
|
239
|
+
const typeFromDefaultValue = parseValueTypeDefault(typeDefinition);
|
|
240
|
+
const typeFromConstant = parseValueTypeConstant(typeDefinition);
|
|
241
|
+
const type = typeFromObject || typeFromDefaultValue || typeFromConstant;
|
|
242
|
+
if (type)
|
|
243
|
+
return type;
|
|
244
|
+
throw new Error(`Unknown value type "${typeDefinition}"`);
|
|
245
|
+
}
|
|
246
|
+
function defaultValueForDefinition(typeDefinition) {
|
|
247
|
+
const constant = parseValueTypeConstant(typeDefinition);
|
|
248
|
+
if (constant)
|
|
249
|
+
return defaultValuesByType[constant];
|
|
250
|
+
const defaultValue = typeDefinition.default;
|
|
251
|
+
if (defaultValue !== void 0)
|
|
252
|
+
return defaultValue;
|
|
253
|
+
return typeDefinition;
|
|
254
|
+
}
|
|
255
|
+
function valueDescriptorForTokenAndTypeDefinition(token, typeDefinition) {
|
|
256
|
+
const key = `${dasherize(token)}-value`;
|
|
257
|
+
const type = parseValueTypeDefinition(typeDefinition);
|
|
258
|
+
return {
|
|
259
|
+
type,
|
|
260
|
+
key,
|
|
261
|
+
name: camelize(key),
|
|
262
|
+
get defaultValue() {
|
|
263
|
+
return defaultValueForDefinition(typeDefinition);
|
|
264
|
+
},
|
|
265
|
+
get hasCustomDefaultValue() {
|
|
266
|
+
return parseValueTypeDefault(typeDefinition) !== void 0;
|
|
267
|
+
},
|
|
268
|
+
reader: readers[type],
|
|
269
|
+
writer: writers[type] || writers.default
|
|
270
|
+
};
|
|
271
|
+
}
|
|
272
|
+
const defaultValuesByType = {
|
|
273
|
+
get array() {
|
|
274
|
+
return [];
|
|
275
|
+
},
|
|
276
|
+
boolean: false,
|
|
277
|
+
number: 0,
|
|
278
|
+
get object() {
|
|
279
|
+
return {};
|
|
280
|
+
},
|
|
281
|
+
string: ""
|
|
282
|
+
};
|
|
283
|
+
const readers = {
|
|
284
|
+
array(value) {
|
|
285
|
+
const array = JSON.parse(value);
|
|
286
|
+
if (!Array.isArray(array)) {
|
|
287
|
+
throw new TypeError("Expected array");
|
|
288
|
+
}
|
|
289
|
+
return array;
|
|
290
|
+
},
|
|
291
|
+
boolean(value) {
|
|
292
|
+
return !(value == "0" || value == "false");
|
|
293
|
+
},
|
|
294
|
+
number(value) {
|
|
295
|
+
return Number(value);
|
|
296
|
+
},
|
|
297
|
+
object(value) {
|
|
298
|
+
const object = JSON.parse(value);
|
|
299
|
+
if (object === null || typeof object != "object" || Array.isArray(object)) {
|
|
300
|
+
throw new TypeError("Expected object");
|
|
301
|
+
}
|
|
302
|
+
return object;
|
|
303
|
+
},
|
|
304
|
+
string(value) {
|
|
305
|
+
return value;
|
|
306
|
+
}
|
|
307
|
+
};
|
|
308
|
+
const writers = {
|
|
309
|
+
default: writeString,
|
|
310
|
+
array: writeJSON,
|
|
311
|
+
object: writeJSON
|
|
312
|
+
};
|
|
313
|
+
function writeJSON(value) {
|
|
314
|
+
return JSON.stringify(value);
|
|
315
|
+
}
|
|
316
|
+
function writeString(value) {
|
|
317
|
+
return `${value}`;
|
|
318
|
+
}
|
|
319
|
+
class Controller {
|
|
320
|
+
constructor(context) {
|
|
321
|
+
this.context = context;
|
|
322
|
+
}
|
|
323
|
+
static get shouldLoad() {
|
|
324
|
+
return true;
|
|
325
|
+
}
|
|
326
|
+
get application() {
|
|
327
|
+
return this.context.application;
|
|
328
|
+
}
|
|
329
|
+
get scope() {
|
|
330
|
+
return this.context.scope;
|
|
331
|
+
}
|
|
332
|
+
get element() {
|
|
333
|
+
return this.scope.element;
|
|
334
|
+
}
|
|
335
|
+
get identifier() {
|
|
336
|
+
return this.scope.identifier;
|
|
337
|
+
}
|
|
338
|
+
get targets() {
|
|
339
|
+
return this.scope.targets;
|
|
340
|
+
}
|
|
341
|
+
get classes() {
|
|
342
|
+
return this.scope.classes;
|
|
343
|
+
}
|
|
344
|
+
get data() {
|
|
345
|
+
return this.scope.data;
|
|
346
|
+
}
|
|
347
|
+
initialize() {
|
|
348
|
+
}
|
|
349
|
+
connect() {
|
|
350
|
+
}
|
|
351
|
+
disconnect() {
|
|
352
|
+
}
|
|
353
|
+
dispatch(eventName, { target = this.element, detail = {}, prefix = this.identifier, bubbles = true, cancelable = true } = {}) {
|
|
354
|
+
const type = prefix ? `${prefix}:${eventName}` : eventName;
|
|
355
|
+
const event = new CustomEvent(type, { detail, bubbles, cancelable });
|
|
356
|
+
target.dispatchEvent(event);
|
|
357
|
+
return event;
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
Controller.blessings = [ClassPropertiesBlessing, TargetPropertiesBlessing, ValuePropertiesBlessing];
|
|
361
|
+
Controller.targets = [];
|
|
362
|
+
Controller.values = {};
|
|
24
363
|
function logProperty(prop) {
|
|
25
364
|
switch (prop) {
|
|
26
365
|
case "application":
|