11import app from 'flarum/forum/app' ;
2- import { extend } from 'flarum/common/extend' ;
2+ import { extend } from 'flarum/common/extend' ;
33import CommentPost from 'flarum/forum/components/CommentPost' ;
4+ import DiscussionListItem from 'flarum/forum/components/DiscussionListItem' ;
5+ import Discussion from 'flarum/common/models/Discussion' ;
6+ import Model from 'flarum/common/Model' ;
47
5- import { Carousel , Fancybox } from '@fancyapps/ui' ;
8+ import { Carousel , Fancybox } from '@fancyapps/ui' ;
69
710app . initializers . add ( 'flectar/flarum-fancybox' , ( ) => {
11+ Discussion . prototype . excerpt = Model . attribute < string > ( 'excerpt' ) ;
12+
13+ extend ( DiscussionListItem . prototype , 'infoItems' , function ( items ) {
14+ const excerpt = this . attrs . discussion . excerpt ( ) ;
15+ if ( excerpt ) {
16+ items . remove ( 'excerpt' ) ;
17+ // @ts -ignore
18+ items . add ( 'excerpt' , m . trust ( excerpt ) , - 100 ) ;
19+ }
20+ } ) ;
21+
22+ 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+ } ) ;
87+ } ) ;
88+
889 extend ( CommentPost . prototype , 'refreshContent' , function ( ) {
990 if ( this . isEditing ( ) ) return ;
1091
1192 const postBody = this . $ ( '.Post-body' ) ;
1293 if ( postBody . length == 0 ) return ;
1394
14- // Initialize Carousel for each gallery
15- postBody . children ( '.fancybox-gallery:not(.fancybox-ready)' ) // The galleries should be only at the first level of Post-body
16- . addClass ( 'fancybox-ready' )
17- . each ( ( _ , gallery ) => {
18- new Carousel ( gallery , {
19- Dots : false ,
20- infinite : false ,
21- dragFree : false ,
22- preload : 0 ,
23- } ) ;
95+ postBody
96+ . children ( '.fancybox-gallery:not(.fancybox-ready)' )
97+ . addClass ( 'fancybox-ready' )
98+ . each ( ( _ , gallery ) => {
99+ new Carousel ( gallery , {
100+ Dots : false ,
101+ infinite : false ,
102+ dragFree : false ,
103+ preload : 0 ,
24104 } ) ;
105+ } ) ;
25106
26- postBody . find ( 'a[data-fancybox]:not(.fancybox-ready)' )
27- . addClass ( 'fancybox-ready' )
28- . each ( ( _ , el ) => {
29- const link = $ ( el ) ;
30- let isDragging = false ;
31- let startX : number , startY : number ;
107+ postBody
108+ . find ( 'a[data-fancybox]:not(.fancybox-ready)' )
109+ . addClass ( 'fancybox-ready' )
110+ . each ( ( _ , el ) => {
111+ const link = $ ( el ) ;
112+ let isDragging = false ;
113+ let startX : number , startY : number ;
32114
33- link
34- . on ( 'mousedown' , ( e ) => {
35- isDragging = false ;
36- startX = e . clientX ;
37- startY = e . clientY ;
38- } )
39- . on ( 'mousemove' , ( e ) => {
40- if ( Math . abs ( e . clientX - startX ) > 5 || Math . abs ( e . clientY - startY ) > 5 ) {
41- isDragging = true ;
42- }
43- } )
44- . on ( 'click' , ( e ) => {
45- e . preventDefault ( ) ;
46- if ( isDragging ) return ;
47- const groupName = link . attr ( 'data-fancybox' ) ;
48- const carouselEl = link . closest ( '.fancybox-gallery' ) ;
49- const group = ( carouselEl . length > 0 ? carouselEl : postBody ) . find ( `a[data-fancybox="${ groupName } "]` ) . toArray ( ) ;
50- const startIndex = group . indexOf ( el ) ;
115+ link
116+ . on ( 'mousedown' , ( e ) => {
117+ isDragging = false ;
118+ startX = e . clientX ;
119+ startY = e . clientY ;
120+ } )
121+ . on ( 'mousemove' , ( e ) => {
122+ if ( Math . abs ( e . clientX - startX ) > 5 || Math . abs ( e . clientY - startY ) > 5 ) {
123+ isDragging = true ;
124+ }
125+ } )
126+ . on ( 'click' , ( e ) => {
127+ e . preventDefault ( ) ;
128+ if ( isDragging ) return ;
129+ const groupName = link . attr ( 'data-fancybox' ) ;
130+ const carouselEl = link . closest ( '.fancybox-gallery' ) ;
131+ const group = ( carouselEl . length > 0 ? carouselEl : postBody ) . find ( `a[data-fancybox="${ groupName } "]` ) . toArray ( ) ;
132+ const startIndex = group . indexOf ( el ) ;
51133
52- Fancybox . fromNodes ( group , {
53- Carousel : {
54- infinite : false ,
55- preload : 0 ,
56- } ,
57- Toolbar : {
58- display : {
59- left : [ 'infobar' ] ,
60- middle : [ 'rotateCCW' , 'rotateCW' , 'flipX' , 'flipY' ] ,
61- right : [ 'slideshow' , 'fullscreen' , 'close' ] ,
62- } ,
63- } ,
64- Images : {
65- initialSize : 'fit' as 'fit' ,
66- } ,
67- dragToClose : true ,
68- Hash : false ,
69- startIndex,
70- } ) ;
71- } ) ;
72- } ) ;
134+ Fancybox . fromNodes ( group , {
135+ Carousel : {
136+ infinite : false ,
137+ preload : 0 ,
138+ } ,
139+ Toolbar : {
140+ display : {
141+ left : [ 'infobar' ] ,
142+ middle : [ 'rotateCCW' , 'rotateCW' , 'flipX' , 'flipY' ] ,
143+ right : [ 'slideshow' , 'fullscreen' , 'close' ] ,
144+ } ,
145+ } ,
146+ Images : {
147+ initialSize : 'fit' as 'fit' ,
148+ } ,
149+ dragToClose : true ,
150+ Hash : false ,
151+ startIndex,
152+ } ) ;
153+ } ) ;
154+ } ) ;
73155 } ) ;
74- } ) ;
156+ } ) ;
0 commit comments