@schukai/monster 4.38.2 → 4.38.3

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.
@@ -19,15 +19,15 @@ import { ProxyObserver } from "../../types/proxyobserver.mjs";
19
19
 
20
20
  import { addAttributeToken } from "../../dom/attributes.mjs";
21
21
  import {
22
- assembleMethodSymbol,
23
- registerCustomElement,
24
- updaterTransformerMethodsSymbol,
22
+ assembleMethodSymbol,
23
+ registerCustomElement,
24
+ updaterTransformerMethodsSymbol,
25
25
  } from "../../dom/customelement.mjs";
26
26
  import { isFunction, isObject } from "../../types/is.mjs";
27
27
  import { ToggleSwitchStyleSheet } from "./stylesheet/toggle-switch.mjs";
28
28
  import {
29
- ATTRIBUTE_ERRORMESSAGE,
30
- ATTRIBUTE_ROLE,
29
+ ATTRIBUTE_ERRORMESSAGE,
30
+ ATTRIBUTE_ROLE,
31
31
  } from "../../dom/constants.mjs";
32
32
  import { getWindow } from "../../dom/util.mjs";
33
33
  import { fireEvent } from "../../dom/events.mjs";
@@ -67,365 +67,365 @@ export const STATE_OFF = "off";
67
67
  * @fires monster-changed
68
68
  */
69
69
  class ToggleSwitch extends CustomControl {
70
- /**
71
- * To set the options via the HTML tag, the attribute `data-monster-options` must be used.
72
- * @see {@link https://monsterjs.org/en/doc/#configurate-a-monster-control}
73
- *
74
- * The individual configuration values can be found in the table.
75
- *
76
- * @property {string} value=current value of the element
77
- * @property {Boolean} disabled Disabled state
78
- * @property {Object} classes
79
- * @property {string} classes.on specifies the class for the on state.
80
- * @property {string} classes.off specifies the class for the off state.
81
- * @property {Object} values
82
- * @property {string} values.off specifies the value of the element if it is not selected
83
- * @property {Object} labels
84
- * @property {string} labels.on specifies the label for the on state.
85
- * @property {string} labels.off specifies the label for the off state.
86
- * @property {string} actions
87
- * @property {string} actions.on specifies the action for the on state.
88
- * @property {string} actions.off specifies the action for the off state.
89
- * @property {Object} templates
90
- * @property {string} templates.main the main template used by the control.
91
- */
92
- get defaults() {
93
- return Object.assign({}, super.defaults, {
94
- value: null,
95
- disabled: false,
96
- classes: {
97
- on: "monster-theme-on",
98
- off: "monster-theme-off",
99
- handle: "monster-theme-primary-1",
100
- error: "monster-theme-error-1",
101
- },
102
- values: {
103
- on: "on",
104
- off: "off",
105
- },
106
- labels: {
107
- toggleSwitchOn: "✔",
108
- toggleSwitchOff: "×",
109
- },
110
- templates: {
111
- main: getTemplate(),
112
- },
113
- actions: {
114
- on: () => {},
115
- off: () => {},
116
- },
117
- });
118
- }
119
-
120
- /**
121
- * @return {void}
122
- */
123
- [assembleMethodSymbol]() {
124
- const self = this;
125
- super[assembleMethodSymbol]();
126
-
127
- initControlReferences.call(this);
128
- initEventHandler.call(this);
129
-
130
- setTimeout(() => {
131
- /**
132
- * init value to off
133
- * if the value was not defined before inserting it into the HTML
134
- */
135
- if (self.getOption("value") === null) {
136
- self.setOption("value", self.getOption("values.off"));
137
- }
138
-
139
- /**
140
- * value from attribute
141
- */
142
- if (self.hasAttribute("value")) {
143
- self.setOption("value", self.getAttribute("value"));
144
- }
145
-
146
- /**
147
- * validate value
148
- */
149
- validateAndSetValue.call(self);
150
-
151
- // this state is a getter
152
- if (this.state === STATE_ON) {
153
- toggleOn.call(self);
154
- } else {
155
- toggleOff.call(self);
156
- }
157
- }, 0);
158
- }
159
-
160
- /**
161
- * updater transformer methods for pipe
162
- *
163
- * @return {function}
164
- */
165
- [updaterTransformerMethodsSymbol]() {
166
- return {
167
- "state-callback": () => {
168
- return this.state;
169
- },
170
- };
171
- }
172
-
173
- /**
174
- * @return [CSSStyleSheet]
175
- */
176
- static getCSSStyleSheet() {
177
- return [ToggleSwitchStyleSheet];
178
- }
179
-
180
- /**
181
- * toggle switch
182
- *
183
- * ```
184
- * e = document.querySelector('monster-toggle-switch');
185
- * e.click()
186
- * ```
187
- */
188
- click() {
189
- this.toggle();
190
- }
191
-
192
- /**
193
- * toggle switch on/off
194
- *
195
- * ```
196
- * e = document.querySelector('monster-toggle-switch');
197
- * e.toggle()
198
- * ```
199
- *
200
- * @return {ToggleSwitch}
201
- */
202
- toggle() {
203
- if (this.getOption("value") === this.getOption("values.on")) {
204
- return this.toggleOff();
205
- }
206
- return this.toggleOn();
207
- }
208
-
209
- /**
210
- * toggle switch on
211
- *
212
- * ```
213
- * e = document.querySelector('monster-toggle-switch');
214
- * e.toggleOn()
215
- * ```
216
- *
217
- * @return {ToggleSwitch}
218
- */
219
- toggleOn() {
220
- this.setOption("value", this.getOption("values.on"));
221
- fireEvent(this, "change");
222
- return this;
223
- }
224
-
225
- /**
226
- * toggle switch off
227
- *
228
- * ```
229
- * e = document.querySelector('monster-toggle-switch');
230
- * e.toggleOff()
231
- * ```
232
- *
233
- * @return {ToggleSwitch}
234
- */
235
- toggleOff() {
236
- this.setOption("value", this.getOption("values.off"));
237
- fireEvent(this, "change");
238
- return this;
239
- }
240
-
241
- /**
242
- * returns the status of the element
243
- *
244
- * ```
245
- * e = document.querySelector('monster-toggle-switch');
246
- * console.log(e.state)
247
- * // ↦ off
248
- * ```
249
- *
250
- * @return {string}
251
- */
252
- get state() {
253
- return this.getOption("value") === this.getOption("values.on")
254
- ? STATE_ON
255
- : STATE_OFF;
256
- }
257
-
258
- /**
259
- * The current value of the Switch
260
- *
261
- * ```
262
- * e = document.querySelector('monster-toggle-switch');
263
- * console.log(e.value)
264
- * // ↦ on
265
- * ```
266
- *
267
- * @return {string}
268
- */
269
- get value() {
270
- return this.getOption("value");
271
- }
272
-
273
- /**
274
- * Set value
275
- *
276
- * ```
277
- * e = document.querySelector('monster-toggle-switch');
278
- * e.value="on"
279
- * ```
280
- *
281
- * @property {string} value
282
- */
283
- set value(value) {
284
- if (
285
- value === this.getOption("values.on") ||
286
- value === this.getOption("values.off")
287
- ) {
288
- if (
289
- this.state !==
290
- (value === this.getOption("values.on") ? STATE_ON : STATE_OFF)
291
- ) {
292
- this.setOption("value", value);
293
- }
294
- return;
295
- }
296
-
297
- addErrorAttribute(
298
- this,
299
- 'The value "' +
300
- value +
301
- '" must be "' +
302
- this.getOption("values.on") +
303
- '" or "' +
304
- this.getOption("values.off"),
305
- );
306
-
307
- showError.call(this);
308
- }
309
-
310
- /**
311
- * This method is called by the `instanceof` operator.
312
- * @return {symbol}
313
- */
314
- static get [instanceSymbol]() {
315
- return Symbol.for(
316
- "@schukai/monster/components/form/toggle-switch@@instance",
317
- );
318
- }
319
-
320
- /**
321
- *
322
- * @returns {string}
323
- */
324
- static getTag() {
325
- return "monster-toggle-switch";
326
- }
70
+ /**
71
+ * To set the options via the HTML tag, the attribute `data-monster-options` must be used.
72
+ * @see {@link https://monsterjs.org/en/doc/#configurate-a-monster-control}
73
+ *
74
+ * The individual configuration values can be found in the table.
75
+ *
76
+ * @property {string} value=current value of the element
77
+ * @property {Boolean} disabled Disabled state
78
+ * @property {Object} classes
79
+ * @property {string} classes.on specifies the class for the on state.
80
+ * @property {string} classes.off specifies the class for the off state.
81
+ * @property {Object} values
82
+ * @property {string} values.off specifies the value of the element if it is not selected
83
+ * @property {Object} labels
84
+ * @property {string} labels.on specifies the label for the on state.
85
+ * @property {string} labels.off specifies the label for the off state.
86
+ * @property {string} actions
87
+ * @property {string} actions.on specifies the action for the on state.
88
+ * @property {string} actions.off specifies the action for the off state.
89
+ * @property {Object} templates
90
+ * @property {string} templates.main the main template used by the control.
91
+ */
92
+ get defaults() {
93
+ return Object.assign({}, super.defaults, {
94
+ value: null,
95
+ disabled: false,
96
+ classes: {
97
+ on: "monster-theme-on",
98
+ off: "monster-theme-off",
99
+ handle: "monster-theme-primary-1",
100
+ error: "monster-theme-error-1",
101
+ },
102
+ values: {
103
+ on: "on",
104
+ off: "off",
105
+ },
106
+ labels: {
107
+ toggleSwitchOn: "✔",
108
+ toggleSwitchOff: "×",
109
+ },
110
+ templates: {
111
+ main: getTemplate(),
112
+ },
113
+ actions: {
114
+ on: () => {},
115
+ off: () => {},
116
+ },
117
+ });
118
+ }
119
+
120
+ /**
121
+ * @return {void}
122
+ */
123
+ [assembleMethodSymbol]() {
124
+ const self = this;
125
+ super[assembleMethodSymbol]();
126
+
127
+ initControlReferences.call(this);
128
+ initEventHandler.call(this);
129
+
130
+ setTimeout(() => {
131
+ /**
132
+ * init value to off
133
+ * if the value was not defined before inserting it into the HTML
134
+ */
135
+ if (self.getOption("value") === null) {
136
+ self.setOption("value", self.getOption("values.off"));
137
+ }
138
+
139
+ /**
140
+ * value from attribute
141
+ */
142
+ if (self.hasAttribute("value")) {
143
+ self.setOption("value", self.getAttribute("value"));
144
+ }
145
+
146
+ /**
147
+ * validate value
148
+ */
149
+ validateAndSetValue.call(self);
150
+
151
+ // this state is a getter
152
+ if (this.state === STATE_ON) {
153
+ toggleOn.call(self);
154
+ } else {
155
+ toggleOff.call(self);
156
+ }
157
+ }, 0);
158
+ }
159
+
160
+ /**
161
+ * updater transformer methods for pipe
162
+ *
163
+ * @return {function}
164
+ */
165
+ [updaterTransformerMethodsSymbol]() {
166
+ return {
167
+ "state-callback": () => {
168
+ return this.state;
169
+ },
170
+ };
171
+ }
172
+
173
+ /**
174
+ * @return [CSSStyleSheet]
175
+ */
176
+ static getCSSStyleSheet() {
177
+ return [ToggleSwitchStyleSheet];
178
+ }
179
+
180
+ /**
181
+ * toggle switch
182
+ *
183
+ * ```
184
+ * e = document.querySelector('monster-toggle-switch');
185
+ * e.click()
186
+ * ```
187
+ */
188
+ click() {
189
+ this.toggle();
190
+ }
191
+
192
+ /**
193
+ * toggle switch on/off
194
+ *
195
+ * ```
196
+ * e = document.querySelector('monster-toggle-switch');
197
+ * e.toggle()
198
+ * ```
199
+ *
200
+ * @return {ToggleSwitch}
201
+ */
202
+ toggle() {
203
+ if (this.getOption("value") === this.getOption("values.on")) {
204
+ return this.toggleOff();
205
+ }
206
+ return this.toggleOn();
207
+ }
208
+
209
+ /**
210
+ * toggle switch on
211
+ *
212
+ * ```
213
+ * e = document.querySelector('monster-toggle-switch');
214
+ * e.toggleOn()
215
+ * ```
216
+ *
217
+ * @return {ToggleSwitch}
218
+ */
219
+ toggleOn() {
220
+ this.setOption("value", this.getOption("values.on"));
221
+ fireEvent(this, "change");
222
+ return this;
223
+ }
224
+
225
+ /**
226
+ * toggle switch off
227
+ *
228
+ * ```
229
+ * e = document.querySelector('monster-toggle-switch');
230
+ * e.toggleOff()
231
+ * ```
232
+ *
233
+ * @return {ToggleSwitch}
234
+ */
235
+ toggleOff() {
236
+ this.setOption("value", this.getOption("values.off"));
237
+ fireEvent(this, "change");
238
+ return this;
239
+ }
240
+
241
+ /**
242
+ * returns the status of the element
243
+ *
244
+ * ```
245
+ * e = document.querySelector('monster-toggle-switch');
246
+ * console.log(e.state)
247
+ * // ↦ off
248
+ * ```
249
+ *
250
+ * @return {string}
251
+ */
252
+ get state() {
253
+ return this.getOption("value") === this.getOption("values.on")
254
+ ? STATE_ON
255
+ : STATE_OFF;
256
+ }
257
+
258
+ /**
259
+ * The current value of the Switch
260
+ *
261
+ * ```
262
+ * e = document.querySelector('monster-toggle-switch');
263
+ * console.log(e.value)
264
+ * // ↦ on
265
+ * ```
266
+ *
267
+ * @return {string}
268
+ */
269
+ get value() {
270
+ return this.getOption("value");
271
+ }
272
+
273
+ /**
274
+ * Set value
275
+ *
276
+ * ```
277
+ * e = document.querySelector('monster-toggle-switch');
278
+ * e.value="on"
279
+ * ```
280
+ *
281
+ * @property {string} value
282
+ */
283
+ set value(value) {
284
+ if (
285
+ value === this.getOption("values.on") ||
286
+ value === this.getOption("values.off")
287
+ ) {
288
+ if (
289
+ this.state !==
290
+ (value === this.getOption("values.on") ? STATE_ON : STATE_OFF)
291
+ ) {
292
+ this.setOption("value", value);
293
+ }
294
+ return;
295
+ }
296
+
297
+ addErrorAttribute(
298
+ this,
299
+ 'The value "' +
300
+ value +
301
+ '" must be "' +
302
+ this.getOption("values.on") +
303
+ '" or "' +
304
+ this.getOption("values.off"),
305
+ );
306
+
307
+ showError.call(this);
308
+ }
309
+
310
+ /**
311
+ * This method is called by the `instanceof` operator.
312
+ * @return {symbol}
313
+ */
314
+ static get [instanceSymbol]() {
315
+ return Symbol.for(
316
+ "@schukai/monster/components/form/toggle-switch@@instance",
317
+ );
318
+ }
319
+
320
+ /**
321
+ *
322
+ * @returns {string}
323
+ */
324
+ static getTag() {
325
+ return "monster-toggle-switch";
326
+ }
327
327
  }
328
328
 
329
329
  /**
330
330
  * @private
331
331
  */
332
332
  function initControlReferences() {
333
- this[switchElementSymbol] = this.shadowRoot.querySelector(
334
- `[${ATTRIBUTE_ROLE}=switch]`,
335
- );
333
+ this[switchElementSymbol] = this.shadowRoot.querySelector(
334
+ `[${ATTRIBUTE_ROLE}=switch]`,
335
+ );
336
336
  }
337
337
 
338
338
  /**
339
339
  * @private
340
340
  */
341
341
  function toggleOn() {
342
- if (!this[switchElementSymbol]) {
343
- return;
344
- }
342
+ if (!this[switchElementSymbol]) {
343
+ return;
344
+ }
345
345
 
346
- this[switchElementSymbol].classList.remove(this.getOption("classes.off")); // change color
347
- this[switchElementSymbol].classList.add(this.getOption("classes.on")); // change color
346
+ this[switchElementSymbol].classList.remove(this.getOption("classes.off")); // change color
347
+ this[switchElementSymbol].classList.add(this.getOption("classes.on")); // change color
348
348
 
349
- const callback = this.getOption("actions.on");
350
- if (isFunction(callback)) {
351
- callback.call(this);
352
- }
349
+ const callback = this.getOption("actions.on");
350
+ if (isFunction(callback)) {
351
+ callback.call(this);
352
+ }
353
353
 
354
- if (typeof this.setFormValue === "function") {
355
- this.setFormValue(this.getOption("values.on"));
356
- }
354
+ if (typeof this.setFormValue === "function") {
355
+ this.setFormValue(this.getOption("values.on"));
356
+ }
357
357
  }
358
358
 
359
359
  /**
360
360
  * @private
361
361
  */
362
362
  function toggleOff() {
363
- if (!this[switchElementSymbol]) {
364
- return;
365
- }
363
+ if (!this[switchElementSymbol]) {
364
+ return;
365
+ }
366
366
 
367
- this[switchElementSymbol].classList.remove(this.getOption("classes.on")); // change color
368
- this[switchElementSymbol].classList.add(this.getOption("classes.off")); // change color
367
+ this[switchElementSymbol].classList.remove(this.getOption("classes.on")); // change color
368
+ this[switchElementSymbol].classList.add(this.getOption("classes.off")); // change color
369
369
 
370
- const callback = this.getOption("actions.off");
371
- if (isFunction(callback)) {
372
- callback.call(this);
373
- }
370
+ const callback = this.getOption("actions.off");
371
+ if (isFunction(callback)) {
372
+ callback.call(this);
373
+ }
374
374
 
375
- if (typeof this.setFormValue === "function") {
376
- this.setFormValue(this.getOption("values.off"));
377
- }
375
+ if (typeof this.setFormValue === "function") {
376
+ this.setFormValue(this.getOption("values.off"));
377
+ }
378
378
  }
379
379
 
380
380
  /**
381
381
  * @private
382
382
  */
383
383
  function showError() {
384
- if (!this[switchElementSymbol]) {
385
- return;
386
- }
384
+ if (!this[switchElementSymbol]) {
385
+ return;
386
+ }
387
387
 
388
- this[switchElementSymbol].classList.remove(this.getOption("classes.on"));
389
- this[switchElementSymbol].classList.remove(this.getOption("classes.off"));
390
- this[switchElementSymbol].classList.add(this.getOption("classes.error"));
388
+ this[switchElementSymbol].classList.remove(this.getOption("classes.on"));
389
+ this[switchElementSymbol].classList.remove(this.getOption("classes.off"));
390
+ this[switchElementSymbol].classList.add(this.getOption("classes.error"));
391
391
  }
392
392
 
393
393
  /**
394
394
  * @private
395
395
  */
396
396
  function validateAndSetValue() {
397
- const value = this.getOption("value");
398
-
399
- const validatedValues = [];
400
- validatedValues.push(this.getOption("values.on"));
401
- validatedValues.push(this.getOption("values.off"));
402
-
403
- if (validatedValues.includes(value) === false) {
404
- addAttributeToken(
405
- this,
406
- ATTRIBUTE_ERRORMESSAGE,
407
- 'The value "' +
408
- value +
409
- '" must be "' +
410
- this.getOption("values.on") +
411
- '" or "' +
412
- this.getOption("values.off"),
413
- );
414
- this.setOption("disabled", true);
415
- this.formDisabledCallback(true);
416
- showError.call(this);
417
- return;
418
- }
419
-
420
- this.setOption("disabled", false);
421
- this.formDisabledCallback(false);
422
-
423
- if (value === this.getOption("values.on")) {
424
- toggleOn.call(this);
425
- return;
426
- }
427
-
428
- toggleOff.call(this);
397
+ const value = this.getOption("value");
398
+
399
+ const validatedValues = [];
400
+ validatedValues.push(this.getOption("values.on"));
401
+ validatedValues.push(this.getOption("values.off"));
402
+
403
+ if (validatedValues.includes(value) === false) {
404
+ addAttributeToken(
405
+ this,
406
+ ATTRIBUTE_ERRORMESSAGE,
407
+ 'The value "' +
408
+ value +
409
+ '" must be "' +
410
+ this.getOption("values.on") +
411
+ '" or "' +
412
+ this.getOption("values.off"),
413
+ );
414
+ this.setOption("disabled", true);
415
+ this.formDisabledCallback(true);
416
+ showError.call(this);
417
+ return;
418
+ }
419
+
420
+ this.setOption("disabled", false);
421
+ this.formDisabledCallback(false);
422
+
423
+ if (value === this.getOption("values.on")) {
424
+ toggleOn.call(this);
425
+ return;
426
+ }
427
+
428
+ toggleOff.call(this);
429
429
  }
430
430
 
431
431
  /**
@@ -433,36 +433,36 @@ function validateAndSetValue() {
433
433
  * @return {initEventHandler}
434
434
  */
435
435
  function initEventHandler() {
436
- const self = this;
437
-
438
- let lastValue = self.value;
439
- self[internalSymbol].attachObserver(
440
- new Observer(function () {
441
- if (isObject(this) && this instanceof ProxyObserver) {
442
- const n = this.getSubject()?.options?.value;
443
- if (lastValue !== n) {
444
- lastValue = n;
445
- validateAndSetValue.call(self);
446
- }
447
- }
448
- }),
449
- );
450
-
451
- self.addEventListener("keyup", (event) => {
452
- if (event.keyCode === 32) {
453
- self.toggle();
454
- }
455
- });
456
-
457
- self.addEventListener("click", (event) => {
458
- self.toggle();
459
- });
460
-
461
- self.addEventListener("touch", (event) => {
462
- self.toggle();
463
- });
464
-
465
- return this;
436
+ const self = this;
437
+
438
+ let lastValue = self.value;
439
+ self[internalSymbol].attachObserver(
440
+ new Observer(function () {
441
+ if (isObject(this) && this instanceof ProxyObserver) {
442
+ const n = this.getSubject()?.options?.value;
443
+ if (lastValue !== n) {
444
+ lastValue = n;
445
+ validateAndSetValue.call(self);
446
+ }
447
+ }
448
+ }),
449
+ );
450
+
451
+ self.addEventListener("keyup", (event) => {
452
+ if (event.keyCode === 32) {
453
+ self.toggle();
454
+ }
455
+ });
456
+
457
+ self.addEventListener("click", (event) => {
458
+ self.toggle();
459
+ });
460
+
461
+ self.addEventListener("touch", (event) => {
462
+ self.toggle();
463
+ });
464
+
465
+ return this;
466
466
  }
467
467
 
468
468
  /**
@@ -470,8 +470,8 @@ function initEventHandler() {
470
470
  * @return {string}
471
471
  */
472
472
  function getTemplate() {
473
- // language=HTML
474
- return `
473
+ // language=HTML
474
+ return `
475
475
  <div data-monster-role="control" part="control" tabindex="0">
476
476
  <div class="switch" data-monster-role="switch"
477
477
  data-monster-attributes="data-monster-state path:value | call:state-callback">