@@ -9,33 +9,47 @@ let Photo = {
9
9
/*
10
10
* Pre size items such that we can do the layouting while the images are loading
11
11
*/
12
- var sizer = $ ( '.grid-sizer' ) . width ( ) ;
13
- var gutter = $ ( '.gutter-sizer' ) . width ( ) ;
14
- $ ( 'figure.pswp-gallery__item > a > img' ) . each ( function ( index ) {
15
- var item = $ ( this ) ;
16
- var ratio = sizer / item . data ( 'width' ) ;
17
- var height = Math . round ( ratio * item . data ( 'height' ) ) ;
18
- if ( item . parent ( ) . parent ( ) . hasClass ( 'potw-thumb' ) ) {
19
- item . attr ( 'width' , 2 * sizer + gutter ) ;
20
- item . attr ( 'height' , 2 * height + gutter ) ;
21
- } else {
22
- item . attr ( 'width' , sizer ) ;
23
- item . attr ( 'height' , height ) ;
24
- }
25
- } ) ;
12
+ function getElementWidth ( selector ) {
13
+ return document . querySelector ( selector ) . offsetWidth ;
14
+ }
15
+
16
+ function updateImageSizes ( ) {
17
+ let sizer = getElementWidth ( '.grid-sizer' ) ;
18
+ let gutter = getElementWidth ( '.gutter-sizer' ) ;
19
+
20
+ document . querySelectorAll ( 'figure.pswp-gallery__item > a > img' ) . forEach ( function ( item ) {
21
+ let ratio = sizer / item . dataset . width ;
22
+ let height = Math . round ( ratio * item . dataset . height ) ;
26
23
24
+ if ( item . closest ( '.potw-thumb' ) ) {
25
+ item . setAttribute ( 'width' , 2 * sizer + gutter ) ;
26
+ item . setAttribute ( 'height' , 2 * height + gutter ) ;
27
+ } else {
28
+ item . setAttribute ( 'width' , sizer ) ;
29
+ item . setAttribute ( 'height' , height ) ;
30
+ }
31
+ } ) ;
32
+ }
33
+
34
+ updateImageSizes ( ) ;
27
35
let lazyLoadInstance = new LazyLoad ( {
28
36
elements_selector : '.lazy-load' ,
29
37
} ) ;
30
38
31
- $ ( '.pswp-gallery' ) . masonry ( {
39
+ let msnry = new Masonry ( '.pswp-gallery' , {
32
40
itemSelector : '.pswp-gallery__item' ,
33
41
columnWidth : '.grid-sizer' ,
34
42
percentPosition : true ,
35
43
gutter : '.gutter-sizer' ,
36
44
transitionDuration : 0 ,
37
45
resize : true ,
38
46
} ) ;
47
+
48
+ // Allow reflowing of the layout when changing the window aspect-ratio and/or size.
49
+ window . addEventListener ( 'resize' , function ( ) {
50
+ updateImageSizes ( ) ;
51
+ msnry . layout ( ) ;
52
+ } ) ;
39
53
} ,
40
54
initRemoveTag : element => {
41
55
element . addEventListener ( 'click' , event => {
0 commit comments