@@ -8,9 +8,15 @@ import Model from 'flarum/common/Model';
88import { Carousel , Fancybox } from '@fancyapps/ui' ;
99
1010app . initializers . add ( 'flectar/flarum-fancybox' , ( ) => {
11+ const isExcerptEnabled = ( ) => {
12+ const val = app . forum . attribute ( 'flectar-fancybox.excerpt_enabled' ) ;
13+ return val === '1' || val === true || val === 1 ;
14+ } ;
15+
1116 Discussion . prototype . excerpt = Model . attribute < string > ( 'excerpt' ) ;
1217
1318 extend ( DiscussionListItem . prototype , 'infoItems' , function ( items ) {
19+ if ( ! isExcerptEnabled ( ) ) return ;
1420 const excerpt = this . attrs . discussion . excerpt ( ) ;
1521 if ( excerpt ) {
1622 items . remove ( 'excerpt' ) ;
@@ -20,79 +26,30 @@ app.initializers.add('flectar/flarum-fancybox', () => {
2026 } ) ;
2127
2228 extend ( DiscussionListItem . prototype , 'oncreate' , function ( ) {
23- const excerptBody = this . $ ( '.item-excerpt' ) ;
24- if ( excerptBody . length == 0 ) return ;
25-
26- excerptBody
27- . children ( '.fancybox-gallery:not(.fancybox-ready)' )
28- . addClass ( 'fancybox-ready' )
29- . each ( ( _ , gallery ) => {
30- new Carousel ( gallery , {
31- Dots : false ,
32- infinite : false ,
33- dragFree : false ,
34- preload : 0 ,
35- } ) ;
36- } ) ;
37-
38- excerptBody
39- . find ( 'a[data-fancybox]:not(.fancybox-ready)' )
40- . addClass ( 'fancybox-ready' )
41- . each ( ( _ , el ) => {
42- const link = $ ( el ) ;
43- let isDragging = false ;
44- let startX : number , startY : number ;
45-
46- link
47- . on ( 'mousedown' , ( e ) => {
48- isDragging = false ;
49- startX = e . clientX ;
50- startY = e . clientY ;
51- } )
52- . on ( 'mousemove' , ( e ) => {
53- if ( Math . abs ( e . clientX - startX ) > 5 || Math . abs ( e . clientY - startY ) > 5 ) {
54- isDragging = true ;
55- }
56- } )
57- . on ( 'click' , ( e ) => {
58- e . preventDefault ( ) ;
59- if ( isDragging ) return ;
60-
61- const groupName = link . attr ( 'data-fancybox' ) ;
62- const carouselEl = link . closest ( '.fancybox-gallery' ) ;
63- const group = ( carouselEl . length > 0 ? carouselEl : excerptBody ) . find ( `a[data-fancybox="${ groupName } "]` ) . toArray ( ) ;
64- const startIndex = group . indexOf ( el ) ;
65-
66- Fancybox . fromNodes ( group , {
67- Carousel : {
68- infinite : false ,
69- preload : 0 ,
70- } ,
71- Toolbar : {
72- display : {
73- left : [ 'infobar' ] ,
74- middle : [ 'rotateCCW' , 'rotateCW' , 'flipX' , 'flipY' ] ,
75- right : [ 'slideshow' , 'fullscreen' , 'close' ] ,
76- } ,
77- } ,
78- Images : {
79- initialSize : 'fit' as 'fit' ,
80- } ,
81- dragToClose : true ,
82- Hash : false ,
83- startIndex,
84- } ) ;
85- } ) ;
86- } ) ;
29+ if ( ! isExcerptEnabled ( ) ) return ;
30+ initFancybox ( this . $ ( '.item-excerpt' ) ) ;
8731 } ) ;
8832
8933 extend ( CommentPost . prototype , 'refreshContent' , function ( ) {
9034 if ( this . isEditing ( ) ) return ;
35+ initFancybox ( this . $ ( '.Post-body' ) ) ;
36+ } ) ;
37+
38+ function initFancybox ( container ) {
39+ if ( ! container || container . length === 0 ) return ;
9140
92- const postBody = this . $ ( '.Post-body' ) ;
93- if ( postBody . length == 0 ) return ;
41+ container
42+ . find ( 'img' )
43+ . not ( '.fancybox-gallery img, a > img' )
44+ . each ( function ( ) {
45+ const $img = $ ( this ) ;
46+ const src = $img . attr ( 'src' ) ;
47+ if ( src ) {
48+ $img . wrap ( `<a data-fancybox="single" href="${ src } "></a>` ) ;
49+ }
50+ } ) ;
9451
95- postBody
52+ container
9653 . children ( '.fancybox-gallery:not(.fancybox-ready)' )
9754 . addClass ( 'fancybox-ready' )
9855 . each ( ( _ , gallery ) => {
@@ -104,7 +61,7 @@ app.initializers.add('flectar/flarum-fancybox', () => {
10461 } ) ;
10562 } ) ;
10663
107- postBody
64+ container
10865 . find ( 'a[data-fancybox]:not(.fancybox-ready)' )
10966 . addClass ( 'fancybox-ready' )
11067 . each ( ( _ , el ) => {
@@ -126,9 +83,17 @@ app.initializers.add('flectar/flarum-fancybox', () => {
12683 . on ( 'click' , ( e ) => {
12784 e . preventDefault ( ) ;
12885 if ( isDragging ) return ;
86+
12987 const groupName = link . attr ( 'data-fancybox' ) ;
13088 const carouselEl = link . closest ( '.fancybox-gallery' ) ;
131- const group = ( carouselEl . length > 0 ? carouselEl : postBody ) . find ( `a[data-fancybox="${ groupName } "]` ) . toArray ( ) ;
89+ let group ;
90+
91+ if ( carouselEl . length > 0 ) {
92+ group = carouselEl . find ( `a[data-fancybox="${ groupName } "]` ) . toArray ( ) ;
93+ } else {
94+ group = container . find ( `a[data-fancybox="${ groupName } "]` ) . toArray ( ) ;
95+ }
96+
13297 const startIndex = group . indexOf ( el ) ;
13398
13499 Fancybox . fromNodes ( group , {
@@ -152,5 +117,5 @@ app.initializers.add('flectar/flarum-fancybox', () => {
152117 } ) ;
153118 } ) ;
154119 } ) ;
155- } ) ;
120+ }
156121} ) ;
0 commit comments