@schukai/monster 3.78.0 → 3.80.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.
@@ -10,25 +10,28 @@
10
10
  * For more information about purchasing a commercial license, please contact schukai GmbH.
11
11
  */
12
12
 
13
- import {instanceSymbol} from "../../constants.mjs";
14
- import {addAttributeToken, removeAttributeToken} from "../../dom/attributes.mjs";
13
+ import { instanceSymbol } from "../../constants.mjs";
15
14
  import {
16
- ATTRIBUTE_ERRORMESSAGE,
17
- ATTRIBUTE_ROLE,
15
+ addAttributeToken,
16
+ removeAttributeToken,
17
+ } from "../../dom/attributes.mjs";
18
+ import {
19
+ ATTRIBUTE_ERRORMESSAGE,
20
+ ATTRIBUTE_ROLE,
18
21
  } from "../../dom/constants.mjs";
19
- import {CustomElement, getSlottedElements} from "../../dom/customelement.mjs";
22
+ import { CustomElement, getSlottedElements } from "../../dom/customelement.mjs";
20
23
  import {
21
- assembleMethodSymbol,
22
- registerCustomElement,
24
+ assembleMethodSymbol,
25
+ registerCustomElement,
23
26
  } from "../../dom/customelement.mjs";
24
- import {getDocument} from "../../dom/util.mjs";
25
- import {STYLE_DISPLAY_MODE_BLOCK} from "../form/constants.mjs";
26
- import {CopyStyleSheet} from "./stylesheet/copy.mjs";
27
- import {fireCustomEvent} from "../../dom/events.mjs";
27
+ import { getDocument } from "../../dom/util.mjs";
28
+ import { STYLE_DISPLAY_MODE_BLOCK } from "../form/constants.mjs";
29
+ import { CopyStyleSheet } from "./stylesheet/copy.mjs";
30
+ import { fireCustomEvent } from "../../dom/events.mjs";
28
31
  import { positionPopper } from "../form/util/floating-ui.mjs";
29
32
  import { DeadMansSwitch } from "../../util/deadmansswitch.mjs";
30
33
 
31
- export {Copy};
34
+ export { Copy };
32
35
 
33
36
  /**
34
37
  * @private
@@ -36,7 +39,6 @@ export {Copy};
36
39
  */
37
40
  const timerCallbackSymbol = Symbol("timerCallback");
38
41
 
39
-
40
42
  /**
41
43
  * @private
42
44
  * @type {symbol}
@@ -81,246 +83,242 @@ const resizeObserverSymbol = Symbol("resizeObserver");
81
83
  * @summary A beautiful Copy that can make your life easier and also looks good.
82
84
  */
83
85
  class Copy extends CustomElement {
84
- /**
85
- * This method is called by the `instanceof` operator.
86
- * @returns {symbol}
87
- */
88
- static get [instanceSymbol]() {
89
- return Symbol.for("@schukai/monster/components/content/copy@@instance");
90
- }
91
-
92
- /**
93
- *
94
- * @return {Components.Content.Copy
95
- */
96
- [assembleMethodSymbol]() {
97
- super[assembleMethodSymbol]();
98
- initControlReferences.call(this);
99
- initEventHandler.call(this);
100
- return this;
101
- }
102
-
103
-
104
- /**
105
- * This method is called when the element is connected to the dom.
106
- *
107
- * @return {void}
108
- */
109
- connectedCallback() {
110
- super.connectedCallback();
111
-
112
- const document = getDocument();
113
-
114
- for (const [, type] of Object.entries(["click", "touch"])) {
115
- // close on outside ui-events
116
- document.addEventListener(type, this[closeEventHandler]);
117
- }
118
-
119
- updatePopper.call(this);
120
- attachResizeObserver.call(this);
121
- }
122
-
123
-
124
- /**
125
- * This method is called when the element is disconnected from the dom.
126
- *
127
- * @return {void}
128
- */
129
- disconnectedCallback() {
130
- super.disconnectedCallback();
131
-
132
- // close on outside ui-events
133
- for (const [, type] of Object.entries(["click", "touch"])) {
134
- document.removeEventListener(type, this[closeEventHandler]);
135
- }
136
-
137
- disconnectResizeObserver.call(this);
138
- }
139
-
140
- /**
141
- * To set the options via the HTML Tag, the attribute `data-monster-options` must be used.
142
- * @see {@link https://monsterjs.org/en/doc/#configurate-a-monster-control}
143
- *
144
- * The individual configuration values can be found in the table.
145
- *
146
- * @property {Object} templates Template definitions
147
- * @property {string} templates.main Main template
148
- * @property {Object} actions Callbacks
149
- * @property {string} actions.click="throw Error" Callback when clicked
150
- * @property {Object} features Features
151
- * @property {boolean} features.stripTags=true Strip tags from the copied text
152
- * @property {boolean} features.preventOpenEventSent=false Prevent open event from being sent
153
- * @property {Object} popper Popper configuration
154
- * @property {string} popper.placement="top" Popper placement
155
- * @property {string[]} popper.middleware=["autoPlacement", "shift", "offset:15", "arrow"] Popper middleware
156
- * @property {boolean} disabled=false Disabled state
157
- */
158
- get defaults() {
159
- return Object.assign({}, super.defaults, {
160
- templates: {
161
- main: getTemplate(),
162
- },
163
- disabled: false,
164
- features: {
165
- stripTags: true,
166
- preventOpenEventSent: false,
167
- },
168
- popper: {
169
- placement: "top",
170
- middleware: ["autoPlacement", "shift", "offset:15", "arrow"],
171
- },
172
- });
173
- }
174
-
175
- /**
176
- * @return {string}
177
- */
178
- static getTag() {
179
- return "monster-copy";
180
- }
181
-
182
- /**
183
- * @return {CSSStyleSheet[]}
184
- */
185
- static getCSSStyleSheet() {
186
- return [CopyStyleSheet];
187
- }
188
-
189
- /**
190
- * With this method, you can show the popper.
191
- *
192
- * @return {Copy}
193
- */
194
- showDialog() {
195
- show.call(this);
196
- return this;
197
- }
198
-
199
- /**
200
- * With this method, you can hide the popper.
201
- *
202
- * @return {Copy}
203
- */
204
- hideDialog() {
205
- hide.call(this);
206
- return this;
207
- }
208
-
209
- /**
210
- * With this method, you can toggle the popper.
211
- *
212
- * @return {Copy}
213
- */
214
- toggleDialog() {
215
- if (this[popperElementSymbol].style.display === STYLE_DISPLAY_MODE_BLOCK) {
216
- this.hideDialog();
217
- } else {
218
- this.showDialog();
219
- }
220
- return this;
221
- }
222
-
223
-
86
+ /**
87
+ * This method is called by the `instanceof` operator.
88
+ * @returns {symbol}
89
+ */
90
+ static get [instanceSymbol]() {
91
+ return Symbol.for("@schukai/monster/components/content/copy@@instance");
92
+ }
93
+
94
+ /**
95
+ *
96
+ * @return {Components.Content.Copy
97
+ */
98
+ [assembleMethodSymbol]() {
99
+ super[assembleMethodSymbol]();
100
+ initControlReferences.call(this);
101
+ initEventHandler.call(this);
102
+ return this;
103
+ }
104
+
105
+ /**
106
+ * This method is called when the element is connected to the dom.
107
+ *
108
+ * @return {void}
109
+ */
110
+ connectedCallback() {
111
+ super.connectedCallback();
112
+
113
+ const document = getDocument();
114
+
115
+ for (const [, type] of Object.entries(["click", "touch"])) {
116
+ // close on outside ui-events
117
+ document.addEventListener(type, this[closeEventHandler]);
118
+ }
119
+
120
+ updatePopper.call(this);
121
+ attachResizeObserver.call(this);
122
+ }
123
+
124
+ /**
125
+ * This method is called when the element is disconnected from the dom.
126
+ *
127
+ * @return {void}
128
+ */
129
+ disconnectedCallback() {
130
+ super.disconnectedCallback();
131
+
132
+ // close on outside ui-events
133
+ for (const [, type] of Object.entries(["click", "touch"])) {
134
+ document.removeEventListener(type, this[closeEventHandler]);
135
+ }
136
+
137
+ disconnectResizeObserver.call(this);
138
+ }
139
+
140
+ /**
141
+ * To set the options via the HTML Tag, the attribute `data-monster-options` must be used.
142
+ * @see {@link https://monsterjs.org/en/doc/#configurate-a-monster-control}
143
+ *
144
+ * The individual configuration values can be found in the table.
145
+ *
146
+ * @property {Object} templates Template definitions
147
+ * @property {string} templates.main Main template
148
+ * @property {Object} actions Callbacks
149
+ * @property {string} actions.click="throw Error" Callback when clicked
150
+ * @property {Object} features Features
151
+ * @property {boolean} features.stripTags=true Strip tags from the copied text
152
+ * @property {boolean} features.preventOpenEventSent=false Prevent open event from being sent
153
+ * @property {Object} popper Popper configuration
154
+ * @property {string} popper.placement="top" Popper placement
155
+ * @property {string[]} popper.middleware=["autoPlacement", "shift", "offset:15", "arrow"] Popper middleware
156
+ * @property {boolean} disabled=false Disabled state
157
+ */
158
+ get defaults() {
159
+ return Object.assign({}, super.defaults, {
160
+ templates: {
161
+ main: getTemplate(),
162
+ },
163
+ disabled: false,
164
+ features: {
165
+ stripTags: true,
166
+ preventOpenEventSent: false,
167
+ },
168
+ popper: {
169
+ placement: "top",
170
+ middleware: ["autoPlacement", "shift", "offset:15", "arrow"],
171
+ },
172
+ });
173
+ }
174
+
175
+ /**
176
+ * @return {string}
177
+ */
178
+ static getTag() {
179
+ return "monster-copy";
180
+ }
181
+
182
+ /**
183
+ * @return {CSSStyleSheet[]}
184
+ */
185
+ static getCSSStyleSheet() {
186
+ return [CopyStyleSheet];
187
+ }
188
+
189
+ /**
190
+ * With this method, you can show the popper.
191
+ *
192
+ * @return {Copy}
193
+ */
194
+ showDialog() {
195
+ show.call(this);
196
+ return this;
197
+ }
198
+
199
+ /**
200
+ * With this method, you can hide the popper.
201
+ *
202
+ * @return {Copy}
203
+ */
204
+ hideDialog() {
205
+ hide.call(this);
206
+ return this;
207
+ }
208
+
209
+ /**
210
+ * With this method, you can toggle the popper.
211
+ *
212
+ * @return {Copy}
213
+ */
214
+ toggleDialog() {
215
+ if (this[popperElementSymbol].style.display === STYLE_DISPLAY_MODE_BLOCK) {
216
+ this.hideDialog();
217
+ } else {
218
+ this.showDialog();
219
+ }
220
+ return this;
221
+ }
224
222
  }
225
223
 
226
224
  /**
227
225
  * @private
228
226
  */
229
227
  function attachResizeObserver() {
230
- // against flickering
231
- this[resizeObserverSymbol] = new ResizeObserver((entries) => {
232
- if (this[timerCallbackSymbol] instanceof DeadMansSwitch) {
233
- try {
234
- this[timerCallbackSymbol].touch();
235
- return;
236
- } catch (e) {
237
- delete this[timerCallbackSymbol];
238
- }
239
- }
240
-
241
- this[timerCallbackSymbol] = new DeadMansSwitch(200, () => {
242
- updatePopper.call(this);
243
- });
244
- });
245
-
246
- this[resizeObserverSymbol].observe(this.parentElement);
228
+ // against flickering
229
+ this[resizeObserverSymbol] = new ResizeObserver((entries) => {
230
+ if (this[timerCallbackSymbol] instanceof DeadMansSwitch) {
231
+ try {
232
+ this[timerCallbackSymbol].touch();
233
+ return;
234
+ } catch (e) {
235
+ delete this[timerCallbackSymbol];
236
+ }
237
+ }
238
+
239
+ this[timerCallbackSymbol] = new DeadMansSwitch(200, () => {
240
+ updatePopper.call(this);
241
+ });
242
+ });
243
+
244
+ this[resizeObserverSymbol].observe(this.parentElement);
247
245
  }
248
246
 
249
247
  function disconnectResizeObserver() {
250
- if (this[resizeObserverSymbol] instanceof ResizeObserver) {
251
- this[resizeObserverSymbol].disconnect();
252
- }
248
+ if (this[resizeObserverSymbol] instanceof ResizeObserver) {
249
+ this[resizeObserverSymbol].disconnect();
250
+ }
253
251
  }
254
252
 
255
253
  /**
256
254
  * @private
257
255
  */
258
256
  function hide() {
259
- const self = this;
257
+ const self = this;
260
258
 
261
- fireCustomEvent(self, "monster-popper-hide", {
262
- self,
263
- });
259
+ fireCustomEvent(self, "monster-popper-hide", {
260
+ self,
261
+ });
264
262
 
265
- self[popperElementSymbol].style.display = "none";
266
- removeAttributeToken(self[controlElementSymbol], "class", "open");
263
+ self[popperElementSymbol].style.display = "none";
264
+ removeAttributeToken(self[controlElementSymbol], "class", "open");
267
265
 
268
- setTimeout(() => {
269
- fireCustomEvent(self, "monster-popper-hidden", {
270
- self,
271
- });
272
- }, 0);
266
+ setTimeout(() => {
267
+ fireCustomEvent(self, "monster-popper-hidden", {
268
+ self,
269
+ });
270
+ }, 0);
273
271
  }
274
272
 
275
273
  /**
276
274
  * @private
277
275
  */
278
276
  function show() {
279
- const self = this;
277
+ const self = this;
280
278
 
281
- if (self.getOption("disabled", false) === true) {
282
- return;
283
- }
279
+ if (self.getOption("disabled", false) === true) {
280
+ return;
281
+ }
284
282
 
285
- if (self[popperElementSymbol].style.display === STYLE_DISPLAY_MODE_BLOCK) {
286
- return;
287
- }
283
+ if (self[popperElementSymbol].style.display === STYLE_DISPLAY_MODE_BLOCK) {
284
+ return;
285
+ }
288
286
 
289
- fireCustomEvent(self, "monster-popper-open", {
290
- self,
291
- });
287
+ fireCustomEvent(self, "monster-popper-open", {
288
+ self,
289
+ });
292
290
 
293
- self[popperElementSymbol].style.visibility = "hidden";
294
- self[popperElementSymbol].style.display = STYLE_DISPLAY_MODE_BLOCK;
291
+ self[popperElementSymbol].style.visibility = "hidden";
292
+ self[popperElementSymbol].style.display = STYLE_DISPLAY_MODE_BLOCK;
295
293
 
296
- addAttributeToken(self[controlElementSymbol], "class", "open");
297
- updatePopper.call(self);
294
+ addAttributeToken(self[controlElementSymbol], "class", "open");
295
+ updatePopper.call(self);
298
296
 
299
- setTimeout(() => {
300
- fireCustomEvent(self, "monster-popper-opened", {
301
- self,
302
- });
303
- }, 0);
297
+ setTimeout(() => {
298
+ fireCustomEvent(self, "monster-popper-opened", {
299
+ self,
300
+ });
301
+ }, 0);
304
302
  }
305
303
 
306
304
  /**
307
305
  * @private
308
306
  */
309
307
  function updatePopper() {
310
- if (this[popperElementSymbol].style.display !== STYLE_DISPLAY_MODE_BLOCK) {
311
- return;
312
- }
313
-
314
- if (this.getOption("disabled", false) === true) {
315
- return;
316
- }
317
-
318
- positionPopper.call(
319
- this,
320
- this[controlElementSymbol],
321
- this[popperElementSymbol],
322
- this.getOption("popper", {}),
323
- );
308
+ if (this[popperElementSymbol].style.display !== STYLE_DISPLAY_MODE_BLOCK) {
309
+ return;
310
+ }
311
+
312
+ if (this.getOption("disabled", false) === true) {
313
+ return;
314
+ }
315
+
316
+ positionPopper.call(
317
+ this,
318
+ this[controlElementSymbol],
319
+ this[popperElementSymbol],
320
+ this.getOption("popper", {}),
321
+ );
324
322
  }
325
323
 
326
324
  /**
@@ -331,105 +329,110 @@ function updatePopper() {
331
329
  * @fires monster-copy-error
332
330
  */
333
331
  function initEventHandler() {
334
- const self = this;
335
-
336
- this[closeEventHandler] = (event) => {
337
- const path = event.composedPath();
338
-
339
- for (const [, element] of Object.entries(path)) {
340
- if (element === this) {
341
- return;
342
- }
343
- }
344
- hide.call(this);
345
- };
346
-
347
- const type = "click";
348
-
349
- this[controlElementSymbol].addEventListener("mouseenter", (event) => {
350
- if (this.getOption("features.preventOpenEventSent") === true) {
351
- event.preventDefault();
352
- }
353
- this.showDialog();
354
- });
355
-
356
- this[controlElementSymbol].addEventListener("focus", (event) => {
357
- if (this.getOption("features.preventOpenEventSent") === true) {
358
- event.preventDefault();
359
- }
360
- this.showDialog();
361
- });
362
- this[controlElementSymbol].addEventListener("blur", (event) => {
363
- if (this.getOption("features.preventOpenEventSent") === true) {
364
- event.preventDefault();
365
- }
366
- this.hideDialog();
367
- });
368
-
369
- this[copyButtonElementSymbol].addEventListener(type, function (event) {
370
-
371
- fireCustomEvent(self, "monster-copy-clicked", {
372
- element: self,
373
- });
374
-
375
- const nodes = getSlottedElements.call(self, ":scope");
376
-
377
- let text = "";
378
- for (const node of nodes) {
379
- if (self.getOption("features.stripTags")) {
380
- text += node.textContent;
381
- } else {
382
- text += node.outerHTML;
383
- }
384
- }
385
-
386
- navigator.clipboard.writeText(text).then(function () {
387
-
388
- self[copyButtonElementSymbol].querySelector("use").setAttribute("href", "#copy-success");
389
- setTimeout(() => {
390
- self[copyButtonElementSymbol].querySelector("use").setAttribute("href", "#copy");
391
- }, 2000);
392
-
393
- fireCustomEvent(self, "monster-copy-success", {
394
- element: self,
395
- });
396
-
397
- }).catch(function (e) {
398
-
399
- self[copyButtonElementSymbol].querySelector("use").setAttribute("href", "#copy-error");
400
- setTimeout(() => {
401
- self[copyButtonElementSymbol].querySelector("use").setAttribute("href", "#copy");
402
- }, 2000);
403
-
404
- fireCustomEvent(self, "monster-copy-error", {
405
- element: self,
406
- });
407
-
408
- addAttributeToken(self, ATTRIBUTE_ERRORMESSAGE, "" + e);
409
- })
410
- });
411
-
412
- return this;
332
+ const self = this;
333
+
334
+ this[closeEventHandler] = (event) => {
335
+ const path = event.composedPath();
336
+
337
+ for (const [, element] of Object.entries(path)) {
338
+ if (element === this) {
339
+ return;
340
+ }
341
+ }
342
+ hide.call(this);
343
+ };
344
+
345
+ const type = "click";
346
+
347
+ this[controlElementSymbol].addEventListener("mouseenter", (event) => {
348
+ if (this.getOption("features.preventOpenEventSent") === true) {
349
+ event.preventDefault();
350
+ }
351
+ this.showDialog();
352
+ });
353
+
354
+ this[controlElementSymbol].addEventListener("focus", (event) => {
355
+ if (this.getOption("features.preventOpenEventSent") === true) {
356
+ event.preventDefault();
357
+ }
358
+ this.showDialog();
359
+ });
360
+ this[controlElementSymbol].addEventListener("blur", (event) => {
361
+ if (this.getOption("features.preventOpenEventSent") === true) {
362
+ event.preventDefault();
363
+ }
364
+ this.hideDialog();
365
+ });
366
+
367
+ this[copyButtonElementSymbol].addEventListener(type, function (event) {
368
+ fireCustomEvent(self, "monster-copy-clicked", {
369
+ element: self,
370
+ });
371
+
372
+ const nodes = getSlottedElements.call(self, ":scope");
373
+
374
+ let text = "";
375
+ for (const node of nodes) {
376
+ if (self.getOption("features.stripTags")) {
377
+ text += node.textContent;
378
+ } else {
379
+ text += node.outerHTML;
380
+ }
381
+ }
382
+
383
+ navigator.clipboard
384
+ .writeText(text)
385
+ .then(function () {
386
+ self[copyButtonElementSymbol]
387
+ .querySelector("use")
388
+ .setAttribute("href", "#copy-success");
389
+ setTimeout(() => {
390
+ self[copyButtonElementSymbol]
391
+ .querySelector("use")
392
+ .setAttribute("href", "#copy");
393
+ }, 2000);
394
+
395
+ fireCustomEvent(self, "monster-copy-success", {
396
+ element: self,
397
+ });
398
+ })
399
+ .catch(function (e) {
400
+ self[copyButtonElementSymbol]
401
+ .querySelector("use")
402
+ .setAttribute("href", "#copy-error");
403
+ setTimeout(() => {
404
+ self[copyButtonElementSymbol]
405
+ .querySelector("use")
406
+ .setAttribute("href", "#copy");
407
+ }, 2000);
408
+
409
+ fireCustomEvent(self, "monster-copy-error", {
410
+ element: self,
411
+ });
412
+
413
+ addAttributeToken(self, ATTRIBUTE_ERRORMESSAGE, "" + e);
414
+ });
415
+ });
416
+
417
+ return this;
413
418
  }
414
419
 
415
-
416
420
  /**
417
421
  * @private
418
422
  * @return {void}
419
423
  */
420
424
  function initControlReferences() {
421
- this[controlElementSymbol] = this.shadowRoot.querySelector(
422
- `[${ATTRIBUTE_ROLE}="control"]`,
423
- );
424
-
425
- this[copyButtonElementSymbol] = this.shadowRoot.querySelector(
426
- `[${ATTRIBUTE_ROLE}="button"]`,
427
- );
428
-
429
- this[popperElementSymbol] = this.shadowRoot.querySelector(
430
- `[${ATTRIBUTE_ROLE}="popper"]`,
431
- );
425
+ this[controlElementSymbol] = this.shadowRoot.querySelector(
426
+ `[${ATTRIBUTE_ROLE}="control"]`,
427
+ );
428
+
429
+ this[copyButtonElementSymbol] = this.shadowRoot.querySelector(
430
+ `[${ATTRIBUTE_ROLE}="button"]`,
431
+ );
432
432
 
433
+ this[popperElementSymbol] = this.shadowRoot.querySelector(
434
+ `[${ATTRIBUTE_ROLE}="popper"]`,
435
+ );
433
436
  }
434
437
 
435
438
  /**
@@ -437,8 +440,8 @@ function initControlReferences() {
437
440
  * @return {string}
438
441
  */
439
442
  function getTemplate() {
440
- // language=HTML
441
- return `
443
+ // language=HTML
444
+ return `
442
445
  <div data-monster-role="control" part="control">
443
446
  <slot></slot>
444
447
  <div data-monster-role="popper" part="popper" tabindex="-1" class="monster-color-primary-1">
@@ -471,5 +474,4 @@ function getTemplate() {
471
474
  </div>`;
472
475
  }
473
476
 
474
-
475
477
  registerCustomElement(Copy);