@studiometa/js-toolkit 2.0.0-rc.2 → 2.1.0

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.
Files changed (151) hide show
  1. package/Base/index.cjs +54 -18
  2. package/Base/index.d.ts +47 -26
  3. package/Base/index.js +54 -18
  4. package/Base/managers/AbstractManager.cjs +56 -0
  5. package/Base/managers/AbstractManager.d.ts +44 -0
  6. package/Base/managers/AbstractManager.js +35 -0
  7. package/Base/managers/ChildrenManager.cjs +79 -100
  8. package/Base/managers/ChildrenManager.d.ts +8 -83
  9. package/Base/managers/ChildrenManager.js +76 -100
  10. package/Base/managers/EventsManager.cjs +75 -99
  11. package/Base/managers/EventsManager.d.ts +37 -113
  12. package/Base/managers/EventsManager.js +75 -99
  13. package/Base/managers/OptionsManager.cjs +65 -79
  14. package/Base/managers/OptionsManager.d.ts +41 -80
  15. package/Base/managers/OptionsManager.js +66 -80
  16. package/Base/managers/RefsManager.cjs +44 -68
  17. package/Base/managers/RefsManager.d.ts +12 -66
  18. package/Base/managers/RefsManager.js +41 -69
  19. package/Base/managers/ResponsiveOptionsManager.cjs +62 -0
  20. package/Base/managers/ResponsiveOptionsManager.d.ts +26 -0
  21. package/Base/managers/ResponsiveOptionsManager.js +37 -0
  22. package/Base/managers/ServicesManager.cjs +7 -10
  23. package/Base/managers/ServicesManager.d.ts +16 -19
  24. package/Base/managers/ServicesManager.js +7 -10
  25. package/Base/utils.d.ts +4 -4
  26. package/decorators/index.cjs +2 -0
  27. package/decorators/index.d.ts +1 -0
  28. package/decorators/index.js +6 -4
  29. package/decorators/withBreakpointManager.cjs +0 -1
  30. package/decorators/withBreakpointManager.d.ts +2 -2
  31. package/decorators/withBreakpointManager.js +0 -1
  32. package/decorators/withBreakpointObserver.cjs +1 -2
  33. package/decorators/withBreakpointObserver.d.ts +1 -1
  34. package/decorators/withBreakpointObserver.js +1 -2
  35. package/decorators/withDrag.d.ts +1 -1
  36. package/decorators/withExtraConfig.d.ts +1 -1
  37. package/decorators/withFreezedOptions.cjs +12 -3
  38. package/decorators/withFreezedOptions.d.ts +1 -1
  39. package/decorators/withFreezedOptions.js +13 -3
  40. package/decorators/withIntersectionObserver.cjs +0 -1
  41. package/decorators/withIntersectionObserver.d.ts +1 -1
  42. package/decorators/withIntersectionObserver.js +0 -1
  43. package/decorators/withMountWhenInView.cjs +0 -1
  44. package/decorators/withMountWhenInView.d.ts +1 -1
  45. package/decorators/withMountWhenInView.js +0 -1
  46. package/decorators/withResponsiveOptions.cjs +39 -0
  47. package/decorators/withResponsiveOptions.d.ts +8 -0
  48. package/decorators/withResponsiveOptions.js +14 -0
  49. package/decorators/withScrolledInView.cjs +58 -23
  50. package/decorators/withScrolledInView.d.ts +5 -3
  51. package/decorators/withScrolledInView.js +59 -24
  52. package/helpers/createApp.d.ts +3 -26
  53. package/helpers/getInstanceFromElement.cjs +32 -0
  54. package/helpers/getInstanceFromElement.d.ts +14 -0
  55. package/helpers/getInstanceFromElement.js +10 -0
  56. package/helpers/importOnInteraction.d.ts +3 -5
  57. package/helpers/importWhenIdle.d.ts +3 -5
  58. package/helpers/importWhenVisible.d.ts +4 -6
  59. package/helpers/index.cjs +2 -0
  60. package/helpers/index.d.ts +1 -0
  61. package/helpers/index.js +8 -6
  62. package/helpers/utils.d.ts +3 -4
  63. package/package.json +2 -1
  64. package/services/Service.d.ts +9 -9
  65. package/services/drag.cjs +0 -1
  66. package/services/drag.d.ts +3 -3
  67. package/services/drag.js +0 -1
  68. package/services/key.d.ts +1 -1
  69. package/services/load.d.ts +1 -1
  70. package/services/pointer.d.ts +1 -1
  71. package/services/raf.d.ts +1 -1
  72. package/services/resize.cjs +2 -2
  73. package/services/resize.d.ts +1 -1
  74. package/services/resize.js +2 -2
  75. package/services/scroll.cjs +3 -1
  76. package/services/scroll.d.ts +1 -1
  77. package/services/scroll.js +3 -1
  78. package/utils/collide/boundingRectToCircle.d.ts +5 -5
  79. package/utils/collide/collideCircleCircle.d.ts +4 -4
  80. package/utils/collide/collideCircleRect.d.ts +8 -8
  81. package/utils/collide/collidePointCircle.d.ts +6 -6
  82. package/utils/collide/collidePointRect.d.ts +7 -7
  83. package/utils/collide/collideRectRect.d.ts +5 -5
  84. package/utils/css/animate.cjs +190 -0
  85. package/utils/css/animate.d.ts +57 -0
  86. package/utils/css/animate.js +165 -0
  87. package/utils/css/classes.cjs +3 -3
  88. package/utils/css/classes.d.ts +10 -10
  89. package/utils/css/classes.js +3 -3
  90. package/utils/css/getOffsetSizes.cjs +47 -0
  91. package/utils/css/getOffsetSizes.d.ts +15 -0
  92. package/utils/css/getOffsetSizes.js +25 -0
  93. package/utils/css/index.cjs +6 -0
  94. package/utils/css/index.d.ts +3 -0
  95. package/utils/css/index.js +9 -4
  96. package/utils/css/matrix.d.ts +14 -15
  97. package/utils/css/styles.d.ts +2 -2
  98. package/utils/css/transform.cjs +91 -0
  99. package/utils/css/transform.d.ts +43 -0
  100. package/utils/css/transform.js +66 -0
  101. package/utils/css/transition.cjs +2 -0
  102. package/utils/css/transition.d.ts +2 -2
  103. package/utils/css/transition.js +2 -0
  104. package/utils/debounce.d.ts +4 -4
  105. package/utils/focusTrap.cjs +1 -1
  106. package/utils/focusTrap.d.ts +1 -0
  107. package/utils/focusTrap.js +1 -1
  108. package/utils/history.d.ts +13 -11
  109. package/utils/index.cjs +4 -0
  110. package/utils/index.d.ts +2 -0
  111. package/utils/index.js +4 -0
  112. package/utils/isDefined.cjs +28 -0
  113. package/utils/isDefined.d.ts +7 -0
  114. package/utils/isDefined.js +6 -0
  115. package/utils/isFunction.cjs +28 -0
  116. package/utils/isFunction.d.ts +7 -0
  117. package/utils/isFunction.js +6 -0
  118. package/utils/math/clamp.d.ts +1 -1
  119. package/utils/math/clamp01.d.ts +1 -1
  120. package/utils/math/damp.d.ts +6 -6
  121. package/utils/math/ease.d.ts +7 -7
  122. package/utils/math/index.cjs +2 -0
  123. package/utils/math/index.d.ts +1 -0
  124. package/utils/math/index.js +2 -0
  125. package/utils/math/inertiaFinalValue.d.ts +5 -5
  126. package/utils/math/lerp.d.ts +4 -4
  127. package/utils/math/map.d.ts +6 -6
  128. package/utils/math/round.d.ts +4 -4
  129. package/utils/memoize.d.ts +1 -1
  130. package/utils/nextFrame.d.ts +3 -4
  131. package/utils/nextMicrotask.d.ts +1 -1
  132. package/utils/nextTick.d.ts +1 -1
  133. package/utils/object/autoBind.d.ts +3 -3
  134. package/utils/object/getAllProperties.d.ts +3 -3
  135. package/utils/object/isObject.d.ts +1 -1
  136. package/utils/scrollTo.cjs +1 -1
  137. package/utils/scrollTo.d.ts +1 -1
  138. package/utils/scrollTo.js +1 -1
  139. package/utils/string/withLeadingCharacters.d.ts +3 -2
  140. package/utils/string/withLeadingSlash.d.ts +1 -1
  141. package/utils/string/withTrailingCharacters.d.ts +3 -2
  142. package/utils/string/withTrailingSlash.d.ts +1 -1
  143. package/utils/string/withoutLeadingCharacters.d.ts +3 -2
  144. package/utils/string/withoutLeadingCharactersRecursive.d.ts +3 -2
  145. package/utils/string/withoutLeadingSlash.d.ts +1 -1
  146. package/utils/string/withoutTrailingCharacters.d.ts +3 -2
  147. package/utils/string/withoutTrailingCharactersRecursive.d.ts +3 -2
  148. package/utils/string/withoutTrailingSlash.d.ts +1 -1
  149. package/utils/throttle.cjs +1 -1
  150. package/utils/throttle.d.ts +3 -3
  151. package/utils/throttle.js +1 -1
@@ -27,85 +27,84 @@ var __publicField = (obj, key, value) => {
27
27
  // packages/js-toolkit/Base/managers/OptionsManager.js
28
28
  var OptionsManager_exports = {};
29
29
  __export(OptionsManager_exports, {
30
+ __getPropertyName: () => __getPropertyName,
30
31
  default: () => OptionsManager
31
32
  });
32
33
  module.exports = __toCommonJS(OptionsManager_exports);
33
34
  var import_deepmerge = __toESM(require("deepmerge"), 1);
35
+ var import_AbstractManager = __toESM(require("./AbstractManager.cjs"), 1);
34
36
  var import_isObject = __toESM(require("../../utils/object/isObject.cjs"), 1);
35
- var _OptionsManager = class {
36
- constructor(element, schema, baseConfig) {
37
- __publicField(this, "__element");
37
+ var types = /* @__PURE__ */ new Set([String, Number, Boolean, Array, Object]);
38
+ var __defaultValues = {
39
+ String: "",
40
+ Number: 0,
41
+ Boolean: false,
42
+ Array: () => [],
43
+ Object: () => ({})
44
+ };
45
+ function __hasProperty(that, prop) {
46
+ return typeof that.__element.dataset[prop] !== "undefined";
47
+ }
48
+ var __propertyNameCache = /* @__PURE__ */ new Map();
49
+ function __getPropertyName(name, prefix = "") {
50
+ const key = name + prefix;
51
+ if (__propertyNameCache.has(key)) {
52
+ return __propertyNameCache.get(key);
53
+ }
54
+ const propertyName = `option${prefix}${name.replace(/^\w/, (c) => c.toUpperCase())}`;
55
+ __propertyNameCache.set(key, propertyName);
56
+ return propertyName;
57
+ }
58
+ function __register(that, name, config) {
59
+ var _a;
60
+ if (!types.has(config.type)) {
61
+ throw new Error(`The "${name}" option has an invalid type. The allowed types are: String, Number, Boolean, Array and Object.`);
62
+ }
63
+ config.default = (_a = config.default) != null ? _a : __defaultValues[config.type.name];
64
+ if ((config.type === Array || config.type === Object) && typeof config.default !== "function") {
65
+ throw new Error(`The default value for options of type "${config.type.name}" must be returned by a function.`);
66
+ }
67
+ Object.defineProperty(that, name, {
68
+ get: () => {
69
+ return that.get(name, config);
70
+ },
71
+ set: (value) => {
72
+ that.set(name, value, config);
73
+ },
74
+ enumerable: true
75
+ });
76
+ }
77
+ var OptionsManager = class extends import_AbstractManager.default {
78
+ constructor(base) {
79
+ super(base);
38
80
  __publicField(this, "__values", {});
39
- __publicField(this, "__defaultValues", {
40
- String: "",
41
- Number: 0,
42
- Boolean: false,
43
- Array: () => [],
44
- Object: () => ({})
45
- });
46
81
  __publicField(this, "name", "Base");
47
82
  __publicField(this, "debug", false);
48
83
  __publicField(this, "log", false);
49
84
  var _a, _b;
50
- Object.defineProperties(this, {
51
- __element: {
52
- enumerable: false,
53
- writable: false,
54
- value: element
55
- },
56
- __values: {
57
- enumerable: false,
58
- writable: false,
59
- value: this.__values
60
- },
61
- __defaultValues: {
62
- enumerable: false,
63
- writable: false,
64
- value: this.__defaultValues
65
- }
66
- });
67
- this.name = baseConfig.name;
85
+ this.__hideProperties(["__values", "__defaultValues"]);
86
+ const schema = this.__config.options || {};
87
+ this.name = this.__config.name;
68
88
  schema.debug = {
69
89
  type: Boolean,
70
- default: (_a = baseConfig.debug) != null ? _a : false
90
+ default: (_a = this.__config.debug) != null ? _a : false
71
91
  };
72
92
  schema.log = {
73
93
  type: Boolean,
74
- default: (_b = baseConfig.log) != null ? _b : false
94
+ default: (_b = this.__config.log) != null ? _b : false
75
95
  };
76
96
  Object.entries(schema).forEach(([name, config]) => {
77
- this.__register(name, config);
97
+ __register(this, name, types.has(config) ? { type: config } : config);
78
98
  });
79
- return this;
80
99
  }
81
- __register(name, config) {
82
- const isObjectConfig = typeof config === "object";
83
- const hasDefaultValue = !_OptionsManager.types.includes(config);
84
- const type = isObjectConfig ? config.type : config;
85
- if (!_OptionsManager.types.includes(type)) {
86
- throw new Error(`The "${name}" option has an invalid type. The allowed types are: String, Number, Boolean, Array and Object.`);
87
- }
88
- const defaultValue = hasDefaultValue ? config.default : this.__defaultValues[type.name];
89
- if ((type === Array || type === Object) && typeof defaultValue !== "function") {
90
- throw new Error(`The default value for options of type "${type.name}" must be returned by a function.`);
91
- }
92
- Object.defineProperty(this, name, {
93
- get: () => {
94
- return this.get(name, type, defaultValue, isObjectConfig ? config.merge : void 0);
95
- },
96
- set: (value) => {
97
- this.set(name, type, value, defaultValue);
98
- },
99
- enumerable: true
100
- });
101
- }
102
- get(name, type, defaultValue, merge) {
103
- const propertyName = _OptionsManager.__getPropertyName(name);
104
- const hasProperty = this.__hasProperty(propertyName);
100
+ get(name, config) {
101
+ const { type, default: defaultValue } = config;
102
+ const propertyName = __getPropertyName(name);
103
+ const hasProperty = __hasProperty(this, propertyName);
105
104
  if (type === Boolean) {
106
105
  if (defaultValue) {
107
- const negatedPropertyName = _OptionsManager.__getPropertyName(name, "No");
108
- const hasNegatedProperty = this.__hasProperty(negatedPropertyName);
106
+ const negatedPropertyName = __getPropertyName(name, "No");
107
+ const hasNegatedProperty = __hasProperty(this, negatedPropertyName);
109
108
  return !hasNegatedProperty;
110
109
  }
111
110
  return hasProperty || defaultValue;
@@ -115,10 +114,11 @@ var _OptionsManager = class {
115
114
  return hasProperty ? Number(value) : defaultValue;
116
115
  }
117
116
  if (type === Array || type === Object) {
117
+ config = type === Array ? config : config;
118
118
  if (!this.__values[name]) {
119
- let val = hasProperty ? JSON.parse(value) : defaultValue();
120
- if (typeof merge !== "undefined") {
121
- val = typeof merge === "boolean" ? (0, import_deepmerge.default)(defaultValue(), val) : (0, import_deepmerge.default)(defaultValue(), val, merge);
119
+ let val = hasProperty ? JSON.parse(value) : config.default();
120
+ if (typeof config.merge !== "undefined") {
121
+ val = typeof config.merge === "boolean" ? (0, import_deepmerge.default)(config.default(), val) : (0, import_deepmerge.default)(config.default(), val, config.merge);
122
122
  }
123
123
  this.__values[name] = val;
124
124
  }
@@ -126,8 +126,9 @@ var _OptionsManager = class {
126
126
  }
127
127
  return hasProperty ? value : defaultValue;
128
128
  }
129
- set(name, type, value, defaultValue) {
130
- const propertyName = _OptionsManager.__getPropertyName(name);
129
+ set(name, value, config) {
130
+ const { type, default: defaultValue } = config;
131
+ const propertyName = __getPropertyName(name);
131
132
  if (value.constructor.name !== type.name) {
132
133
  const val = Array.isArray(value) || (0, import_isObject.default)(value) ? JSON.stringify(value) : value;
133
134
  throw new TypeError(`The "${val}" value for the "${name}" option must be of type "${type.name}"`);
@@ -135,7 +136,7 @@ var _OptionsManager = class {
135
136
  switch (type) {
136
137
  case Boolean:
137
138
  if (defaultValue) {
138
- const negatedPropertyName = _OptionsManager.__getPropertyName(name, "No");
139
+ const negatedPropertyName = __getPropertyName(name, "No");
139
140
  if (value) {
140
141
  delete this.__element.dataset[negatedPropertyName];
141
142
  } else {
@@ -155,20 +156,5 @@ var _OptionsManager = class {
155
156
  this.__element.dataset[propertyName] = value;
156
157
  }
157
158
  }
158
- __hasProperty(prop) {
159
- return typeof this.__element.dataset[prop] !== "undefined";
160
- }
161
- static __getPropertyName(name, prefix = "") {
162
- const key = name + prefix;
163
- if (_OptionsManager.__propertyNameCache.has(key)) {
164
- return _OptionsManager.__propertyNameCache.get(key);
165
- }
166
- const propertyName = `option${prefix}${name.replace(/^\w/, (c) => c.toUpperCase())}`;
167
- _OptionsManager.__propertyNameCache.set(key, propertyName);
168
- return propertyName;
169
- }
170
159
  };
171
- var OptionsManager = _OptionsManager;
172
- __publicField(OptionsManager, "types", [String, Number, Boolean, Array, Object]);
173
- __publicField(OptionsManager, "__propertyNameCache", /* @__PURE__ */ new Map());
174
160
  if (module.exports.default) module.exports = module.exports.default;
@@ -1,65 +1,24 @@
1
1
  /**
2
- * @typedef {import('deepmerge').Options} DeepmergeOptions
3
- * @typedef {import('../index.js').BaseConfig} BaseConfig
4
- * @typedef {StringConstructor|NumberConstructor|BooleanConstructor|ArrayConstructor|ObjectConstructor} OptionType
5
- * @typedef {{ type: OptionType, default: String|Number|Boolean|(() => Array|Object), merge?: boolean|DeepmergeOptions }} OptionObject
6
- * @typedef {OptionType | OptionObject} OptionValue
7
- * @typedef {{ [name:string]: OptionValue }} OptionsSchema
8
- * @typedef {{ [optionName:string]: any }} OptionsInterface
2
+ * Get a property name.
3
+ *
4
+ * @param {string} name
5
+ * @param {string} [prefix]
6
+ * @returns {string}
7
+ * @protected
9
8
  */
9
+ export function __getPropertyName(name: string, prefix?: string): string;
10
10
  /**
11
11
  * Class options to manage options as data attributes on an HTML element.
12
12
  *
13
13
  * @todo Use `MutationObserver` to update values? Might be more performant.
14
- * @augments {OptionsInterface}
15
14
  */
16
- export default class OptionsManager {
17
- /**
18
- * List of allowed types.
19
- * @type {Array}
20
- * @private
21
- */
22
- private static types;
23
- /**
24
- * Property name cache.
25
- * @type {Map}
26
- * @private
27
- */
28
- private static __propertyNameCache;
29
- /**
30
- * Get a property name.
31
- *
32
- * @param {string} name
33
- * @return {string}
34
- * @private
35
- */
36
- private static __getPropertyName;
37
- /**
38
- * Class constructor.
39
- *
40
- * @param {HTMLElement} element The HTML element storing the options.
41
- * @param {OptionsSchema} schema A Base class config options.
42
- * @param {BaseConfig} baseConfig A Base class config.
43
- */
44
- constructor(element: HTMLElement, schema: OptionsSchema, baseConfig: BaseConfig);
45
- /**
46
- * The HTML element holding the options attributes.
47
- * @type {HTMLElement}
48
- * @private
49
- */
50
- private __element;
15
+ export default class OptionsManager extends AbstractManager {
51
16
  /**
52
17
  * An object to store Array and Object values for reference.
53
18
  * @type {Object}
54
19
  * @private
55
20
  */
56
21
  private __values;
57
- /**
58
- * The default values to return for each available type.
59
- * @type {Object}
60
- * @private
61
- */
62
- private __defaultValues;
63
22
  /**
64
23
  * Default value for the name property.
65
24
  * @type {string}
@@ -72,55 +31,57 @@ export default class OptionsManager {
72
31
  debug: boolean;
73
32
  /**
74
33
  * Default value for the log property.
75
- * @type {Boolean}
34
+ * @type {boolean}
76
35
  */
77
36
  log: boolean;
78
- /**
79
- * Register an option.
80
- *
81
- * @param {string} name
82
- * @param {OptionValue} config
83
- * @return {void}
84
- * @private
85
- */
86
- private __register;
87
37
  /**
88
38
  * Get an option value.
89
39
  *
90
- * @param {String} name The option name.
91
- * @param {OptionType} type The option data's type.
92
- * @param {any} defaultValue The default value for this option.
93
- * @param {boolean|DeepmergeOptions} [merge] Wether to merge the value with the default or not.
40
+ * @param {string} name The option name.
41
+ * @param {OptionObject} config The option config.
42
+ * @returns {any}
94
43
  */
95
- get(name: string, type: OptionType, defaultValue: any, merge?: boolean | DeepmergeOptions): any;
44
+ get(name: string, config: OptionObject): any;
96
45
  /**
97
46
  * Set an option value.
98
47
  *
99
- * @param {String} name The option name.
100
- * @param {OptionType} type The option data's type.
48
+ * @param {string} name The option name.
101
49
  * @param {any} value The new value for this option.
50
+ * @param {OptionObject} config The option config.
102
51
  */
103
- set(name: string, type: OptionType, value: any, defaultValue: any): void;
104
- /**
105
- * Test if the element has a given property.
106
- *
107
- * @param {string} prop
108
- * @return {boolean}
109
- */
110
- __hasProperty(prop: string): boolean;
52
+ set(name: string, value: any, config: OptionObject): void;
111
53
  }
112
54
  export type DeepmergeOptions = import('deepmerge').Options;
55
+ export type Base = import('../index.js').default;
113
56
  export type BaseConfig = import('../index.js').BaseConfig;
114
57
  export type OptionType = StringConstructor | NumberConstructor | BooleanConstructor | ArrayConstructor | ObjectConstructor;
115
- export type OptionObject = {
116
- type: OptionType;
117
- default: string | number | boolean | (() => any[] | any);
118
- merge?: boolean | DeepmergeOptions;
58
+ export type ArrayOption = {
59
+ type: ArrayConstructor;
60
+ default?: () => any[];
61
+ merge?: boolean;
62
+ };
63
+ export type ObjectOption = {
64
+ type: ObjectConstructor;
65
+ default?: () => any;
66
+ merge?: boolean;
67
+ };
68
+ export type StringOption = {
69
+ type: StringConstructor;
70
+ default?: string;
71
+ };
72
+ export type NumberOption = {
73
+ type: NumberConstructor;
74
+ default?: number;
75
+ };
76
+ export type BooleanOption = {
77
+ type: BooleanConstructor;
78
+ default?: boolean;
119
79
  };
120
- export type OptionValue = OptionType | OptionObject;
80
+ export type OptionObject = ArrayOption | ObjectOption | StringOption | NumberOption | BooleanOption;
121
81
  export type OptionsSchema = {
122
- [name: string]: OptionValue;
82
+ [name: string]: OptionType | OptionObject;
123
83
  };
124
84
  export type OptionsInterface = {
125
85
  [optionName: string]: any;
126
86
  };
87
+ import AbstractManager from "./AbstractManager.js";
@@ -5,81 +5,79 @@ var __publicField = (obj, key, value) => {
5
5
  return value;
6
6
  };
7
7
  import deepmerge from "deepmerge";
8
+ import AbstractManager from "./AbstractManager.js";
8
9
  import isObject from "../../utils/object/isObject.js";
9
- const _OptionsManager = class {
10
- constructor(element, schema, baseConfig) {
11
- __publicField(this, "__element");
10
+ const types = /* @__PURE__ */ new Set([String, Number, Boolean, Array, Object]);
11
+ const __defaultValues = {
12
+ String: "",
13
+ Number: 0,
14
+ Boolean: false,
15
+ Array: () => [],
16
+ Object: () => ({})
17
+ };
18
+ function __hasProperty(that, prop) {
19
+ return typeof that.__element.dataset[prop] !== "undefined";
20
+ }
21
+ const __propertyNameCache = /* @__PURE__ */ new Map();
22
+ function __getPropertyName(name, prefix = "") {
23
+ const key = name + prefix;
24
+ if (__propertyNameCache.has(key)) {
25
+ return __propertyNameCache.get(key);
26
+ }
27
+ const propertyName = `option${prefix}${name.replace(/^\w/, (c) => c.toUpperCase())}`;
28
+ __propertyNameCache.set(key, propertyName);
29
+ return propertyName;
30
+ }
31
+ function __register(that, name, config) {
32
+ var _a;
33
+ if (!types.has(config.type)) {
34
+ throw new Error(`The "${name}" option has an invalid type. The allowed types are: String, Number, Boolean, Array and Object.`);
35
+ }
36
+ config.default = (_a = config.default) != null ? _a : __defaultValues[config.type.name];
37
+ if ((config.type === Array || config.type === Object) && typeof config.default !== "function") {
38
+ throw new Error(`The default value for options of type "${config.type.name}" must be returned by a function.`);
39
+ }
40
+ Object.defineProperty(that, name, {
41
+ get: () => {
42
+ return that.get(name, config);
43
+ },
44
+ set: (value) => {
45
+ that.set(name, value, config);
46
+ },
47
+ enumerable: true
48
+ });
49
+ }
50
+ class OptionsManager extends AbstractManager {
51
+ constructor(base) {
52
+ super(base);
12
53
  __publicField(this, "__values", {});
13
- __publicField(this, "__defaultValues", {
14
- String: "",
15
- Number: 0,
16
- Boolean: false,
17
- Array: () => [],
18
- Object: () => ({})
19
- });
20
54
  __publicField(this, "name", "Base");
21
55
  __publicField(this, "debug", false);
22
56
  __publicField(this, "log", false);
23
57
  var _a, _b;
24
- Object.defineProperties(this, {
25
- __element: {
26
- enumerable: false,
27
- writable: false,
28
- value: element
29
- },
30
- __values: {
31
- enumerable: false,
32
- writable: false,
33
- value: this.__values
34
- },
35
- __defaultValues: {
36
- enumerable: false,
37
- writable: false,
38
- value: this.__defaultValues
39
- }
40
- });
41
- this.name = baseConfig.name;
58
+ this.__hideProperties(["__values", "__defaultValues"]);
59
+ const schema = this.__config.options || {};
60
+ this.name = this.__config.name;
42
61
  schema.debug = {
43
62
  type: Boolean,
44
- default: (_a = baseConfig.debug) != null ? _a : false
63
+ default: (_a = this.__config.debug) != null ? _a : false
45
64
  };
46
65
  schema.log = {
47
66
  type: Boolean,
48
- default: (_b = baseConfig.log) != null ? _b : false
67
+ default: (_b = this.__config.log) != null ? _b : false
49
68
  };
50
69
  Object.entries(schema).forEach(([name, config]) => {
51
- this.__register(name, config);
70
+ __register(this, name, types.has(config) ? { type: config } : config);
52
71
  });
53
- return this;
54
72
  }
55
- __register(name, config) {
56
- const isObjectConfig = typeof config === "object";
57
- const hasDefaultValue = !_OptionsManager.types.includes(config);
58
- const type = isObjectConfig ? config.type : config;
59
- if (!_OptionsManager.types.includes(type)) {
60
- throw new Error(`The "${name}" option has an invalid type. The allowed types are: String, Number, Boolean, Array and Object.`);
61
- }
62
- const defaultValue = hasDefaultValue ? config.default : this.__defaultValues[type.name];
63
- if ((type === Array || type === Object) && typeof defaultValue !== "function") {
64
- throw new Error(`The default value for options of type "${type.name}" must be returned by a function.`);
65
- }
66
- Object.defineProperty(this, name, {
67
- get: () => {
68
- return this.get(name, type, defaultValue, isObjectConfig ? config.merge : void 0);
69
- },
70
- set: (value) => {
71
- this.set(name, type, value, defaultValue);
72
- },
73
- enumerable: true
74
- });
75
- }
76
- get(name, type, defaultValue, merge) {
77
- const propertyName = _OptionsManager.__getPropertyName(name);
78
- const hasProperty = this.__hasProperty(propertyName);
73
+ get(name, config) {
74
+ const { type, default: defaultValue } = config;
75
+ const propertyName = __getPropertyName(name);
76
+ const hasProperty = __hasProperty(this, propertyName);
79
77
  if (type === Boolean) {
80
78
  if (defaultValue) {
81
- const negatedPropertyName = _OptionsManager.__getPropertyName(name, "No");
82
- const hasNegatedProperty = this.__hasProperty(negatedPropertyName);
79
+ const negatedPropertyName = __getPropertyName(name, "No");
80
+ const hasNegatedProperty = __hasProperty(this, negatedPropertyName);
83
81
  return !hasNegatedProperty;
84
82
  }
85
83
  return hasProperty || defaultValue;
@@ -89,10 +87,11 @@ const _OptionsManager = class {
89
87
  return hasProperty ? Number(value) : defaultValue;
90
88
  }
91
89
  if (type === Array || type === Object) {
90
+ config = type === Array ? config : config;
92
91
  if (!this.__values[name]) {
93
- let val = hasProperty ? JSON.parse(value) : defaultValue();
94
- if (typeof merge !== "undefined") {
95
- val = typeof merge === "boolean" ? deepmerge(defaultValue(), val) : deepmerge(defaultValue(), val, merge);
92
+ let val = hasProperty ? JSON.parse(value) : config.default();
93
+ if (typeof config.merge !== "undefined") {
94
+ val = typeof config.merge === "boolean" ? deepmerge(config.default(), val) : deepmerge(config.default(), val, config.merge);
96
95
  }
97
96
  this.__values[name] = val;
98
97
  }
@@ -100,8 +99,9 @@ const _OptionsManager = class {
100
99
  }
101
100
  return hasProperty ? value : defaultValue;
102
101
  }
103
- set(name, type, value, defaultValue) {
104
- const propertyName = _OptionsManager.__getPropertyName(name);
102
+ set(name, value, config) {
103
+ const { type, default: defaultValue } = config;
104
+ const propertyName = __getPropertyName(name);
105
105
  if (value.constructor.name !== type.name) {
106
106
  const val = Array.isArray(value) || isObject(value) ? JSON.stringify(value) : value;
107
107
  throw new TypeError(`The "${val}" value for the "${name}" option must be of type "${type.name}"`);
@@ -109,7 +109,7 @@ const _OptionsManager = class {
109
109
  switch (type) {
110
110
  case Boolean:
111
111
  if (defaultValue) {
112
- const negatedPropertyName = _OptionsManager.__getPropertyName(name, "No");
112
+ const negatedPropertyName = __getPropertyName(name, "No");
113
113
  if (value) {
114
114
  delete this.__element.dataset[negatedPropertyName];
115
115
  } else {
@@ -129,22 +129,8 @@ const _OptionsManager = class {
129
129
  this.__element.dataset[propertyName] = value;
130
130
  }
131
131
  }
132
- __hasProperty(prop) {
133
- return typeof this.__element.dataset[prop] !== "undefined";
134
- }
135
- static __getPropertyName(name, prefix = "") {
136
- const key = name + prefix;
137
- if (_OptionsManager.__propertyNameCache.has(key)) {
138
- return _OptionsManager.__propertyNameCache.get(key);
139
- }
140
- const propertyName = `option${prefix}${name.replace(/^\w/, (c) => c.toUpperCase())}`;
141
- _OptionsManager.__propertyNameCache.set(key, propertyName);
142
- return propertyName;
143
- }
144
- };
145
- let OptionsManager = _OptionsManager;
146
- __publicField(OptionsManager, "types", [String, Number, Boolean, Array, Object]);
147
- __publicField(OptionsManager, "__propertyNameCache", /* @__PURE__ */ new Map());
132
+ }
148
133
  export {
134
+ __getPropertyName,
149
135
  OptionsManager as default
150
136
  };