comand-component-library 4.0.74 → 4.0.75

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,49 +1,54 @@
1
- import {openFancyBox} from "../components/CmdFancyBox.vue"
1
+ import {openFancyBox} from "comand-component-library"
2
+
2
3
  function addClickHandlerToFancyboxLinks(fancyboxLinks) {
3
4
  for(let i = 0; i < fancyboxLinks.length; i++) {
4
- fancyboxLinks[i].addEventListener("click", (event) => {
5
- event.preventDefault()
6
- openFancyBox({url: fancyboxLinks[i].href})
7
- })
5
+ if (!fancyboxLinks[i].dataset.cmdFancyBoxHandlerRegistered) {
6
+ fancyboxLinks[i].addEventListener("click", (event) => {
7
+ event.preventDefault()
8
+ openFancyBox({url: fancyboxLinks[i].href})
9
+ })
10
+ fancyboxLinks[i].dataset.cmdFancyBoxHandlerRegistered = "true"
11
+ }
8
12
  }
9
13
  }
10
14
 
11
- export default {
12
- // el = real dom-element
13
- mounted(el) {
14
- // get initially loaded elements
15
- const fancyboxLinks = el.querySelectorAll(".fancybox")
16
- addClickHandlerToFancyboxLinks(fancyboxLinks)
17
-
18
- // get elements added later to dom (and watched by mutationObserver)
19
- const observer = new MutationObserver((mutationList) => {
20
- const targetElements = []
15
+ const observer = new MutationObserver(onDomChange)
21
16
 
22
- for(let i = 0; i < mutationList.length; i++) {
23
- if (mutationList[i].type === "attributes") {
24
- if(mutationList[i].target.classList.contains("fancybox")) {
25
- targetElements.push(mutationList[i].target)
26
- }
27
- } else if (mutationList[i].type === "childList") {
28
- let childNodes = Array.from(mutationList[i].addedNodes).filter((node) => {
29
- if(node.classList) {
30
- return node.classList.contains("fancybox")
31
- }
32
- return false
33
- })
34
- if(childNodes.length) {
35
- targetElements.push(...childNodes)
36
- }
17
+ function onDomChange(mutationList) {
18
+ const targetElements = []
19
+ for(let i = 0; i < mutationList.length; i++) {
20
+ if (mutationList[i].type === "attributes") {
21
+ if(mutationList[i].target.classList.contains("fancybox")) {
22
+ targetElements.push(mutationList[i].target)
23
+ }
24
+ } else if (mutationList[i].type === "childList") {
25
+ let childNodes = Array.from(mutationList[i].addedNodes).filter((node) => {
26
+ if(node.classList) {
27
+ return node.classList.contains("fancybox")
37
28
  }
29
+ return false
30
+ });
31
+ if(childNodes.length) {
32
+ targetElements.push(...childNodes)
38
33
  }
39
- addClickHandlerToFancyboxLinks(targetElements)
40
- })
41
-
42
- observer.observe(el, {
43
- subtree: true, // observe nested elements
44
- childList: true,
45
- attributeFilter: ["class"], // set filter
46
- characterData: false // observe text changes
47
- })
34
+ }
35
+ }
36
+ if (targetElements.length > 0) {
37
+ addClickHandlerToFancyboxLinks(targetElements)
48
38
  }
49
- }
39
+ }
40
+
41
+ export default el => {
42
+ // get initially loaded elements
43
+ const fancyboxLinks = el.querySelectorAll(".fancybox")
44
+ addClickHandlerToFancyboxLinks(fancyboxLinks)
45
+
46
+ observer?.disconnect()
47
+ observer.observe(el, {
48
+ subtree: true, // observe nested elements
49
+ childList: true,
50
+ attributes: true,
51
+ attributeFilter: ["class"], // set filter
52
+ characterData: false
53
+ })
54
+ }