@schukai/monster 3.57.0 → 3.58.1

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