Bläddra i källkod

creating click handler AFTER next render tick

tags/0.9.0
J 6 år sedan
förälder
incheckning
c908874e8b
2 ändrade filer med 27 tillägg och 17 borttagningar
  1. 4
    1
      vue-theme/src/components/gallery.vue
  2. 23
    16
      vue-theme/src/pages/single.vue

+ 4
- 1
vue-theme/src/components/gallery.vue Visa fil

19
     },
19
     },
20
     data() { 
20
     data() { 
21
         return {
21
         return {
22
-            selected: 0
22
+            selected: -1
23
         }
23
         }
24
     },
24
     },
25
     methods: {
25
     methods: {
52
         }
52
         }
53
     },
53
     },
54
     mounted() {
54
     mounted() {
55
+        // Set the first selection
56
+        this.selected = this.fullscreengallery
57
+        
55
         window.addEventListener('keydown', this.interpretKeypress)
58
         window.addEventListener('keydown', this.interpretKeypress)
56
     },
59
     },
57
     destroyed() {
60
     destroyed() {

+ 23
- 16
vue-theme/src/pages/single.vue Visa fil

60
                 postsMap[post.slug] = post
60
                 postsMap[post.slug] = post
61
                 return postsMap
61
                 return postsMap
62
             }, {})
62
             }, {})
63
-        },
63
+        }
64
     },
64
     },
65
     methods: {
65
     methods: {
66
-        checkForImages(posts) {
67
-            const allBlocks = posts[this.$route.params.slug].blocks.map(block => {
66
+        pageBlocks(posts) {
67
+            return posts[this.$route.params.slug].blocks.map(block => {
68
                 if(block) return block
68
                 if(block) return block
69
             })
69
             })
70
-            allBlocks.forEach(block => {
70
+        },
71
+        checkForImages(posts) {
72
+            this.pageBlocks(posts).forEach(block => {
71
                 if(!block) return
73
                 if(!block) return
72
                 const doc = new DOMParser().parseFromString(block, 'text/html')
74
                 const doc = new DOMParser().parseFromString(block, 'text/html')
73
                 const gallery = doc.querySelectorAll('.blocks-gallery-item img')
75
                 const gallery = doc.querySelectorAll('.blocks-gallery-item img')
74
                 // add captions
76
                 // add captions
75
-                if(gallery) {
77
+                if(gallery.length) {
76
                     this.images = [].slice.call(gallery).map(
78
                     this.images = [].slice.call(gallery).map(
77
                         item => { return item.src }
79
                         item => { return item.src }
78
                     )
80
                     )
79
                 }
81
                 }
80
             })
82
             })
81
-        },
82
-        wrapImagesWithLink() {
83
-            const gallery = document.querySelectorAll('.blocks-gallery-item figure, figure.wp-block-image')
84
-            gallery.forEach((item, i) => {
85
-                item.addEventListener('click', () => {
86
-                    this.fullscreengallery = i
87
-                })
88
-            })
89
         }
83
         }
90
     },
84
     },
91
     watch: {
85
     watch: {
92
         posts(newVal, oldVal) { 
86
         posts(newVal, oldVal) { 
93
             // Loads images from the DOM
87
             // Loads images from the DOM
94
             this.checkForImages(newVal)
88
             this.checkForImages(newVal)
89
+            
90
+            // Gottas be on the next render tick
91
+            this.$nextTick(() => {
92
+                let gallery
93
+                this.pageBlocks(newVal).forEach(block => {
94
+                    if(!block) return
95
+                    gallery = document.querySelectorAll('.blocks-gallery-item figure')
96
+                })
97
+                
98
+                if(gallery.length < 1) return
99
+
100
+                gallery.forEach((item, i) => {
101
+                    item.addEventListener('mouseup', event => { this.fullscreengallery = i })
102
+                })
103
+            })
95
         }
104
         }
96
     },
105
     },
97
-    mounted() {
106
+    created() {
98
         // TODO: Only makes req if this hasn't been loaded yet
107
         // TODO: Only makes req if this hasn't been loaded yet
99
         let type = this.$route.params.type
108
         let type = this.$route.params.type
100
         type = type.charAt(0).toUpperCase() + type.slice(1)
109
         type = type.charAt(0).toUpperCase() + type.slice(1)
101
         this.$store.dispatch(`getAll${type}`)
110
         this.$store.dispatch(`getAll${type}`)
102
-
103
-        this.wrapImagesWithLink()
104
     }
111
     }
105
 }
112
 }
106
 </script>
113
 </script>

Laddar…
Avbryt
Spara