@schukai/monster 3.68.4 → 3.69.0

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