1+ window . imagePreviewLightbox = function ( ) {
2+ return {
3+ isOpen : false ,
4+ currentIndex : 0 ,
5+ images : [ ] ,
6+ currentImage : {
7+ url : '' ,
8+ title : '' ,
9+ link : '' ,
10+ filename : ''
11+ } ,
12+
13+ getDisplayName ( ) {
14+ return this . currentImage . title || this . currentImage . filename || '' ;
15+ } ,
16+
17+ init ( ) {
18+ const self = this ;
19+ document . addEventListener ( 'click' , function ( e ) {
20+ if ( e . target . classList . contains ( 'image-preview-trigger' ) ) {
21+ e . preventDefault ( ) ;
22+ e . stopPropagation ( ) ;
23+ self . openFromTable ( e . target ) ;
24+ }
25+ } , true ) ;
26+ document . addEventListener ( 'keydown' , ( e ) => {
27+ if ( this . isOpen ) {
28+ if ( e . key === 'ArrowLeft' ) {
29+ e . preventDefault ( ) ;
30+ this . previous ( ) ;
31+ } else if ( e . key === 'ArrowRight' ) {
32+ e . preventDefault ( ) ;
33+ this . next ( ) ;
34+ }
35+ }
36+ } ) ;
37+ } ,
38+
39+ openFromTable ( imageElement ) {
40+ const allRows = document . querySelectorAll ( 'tbody tr' ) ;
41+ this . images = [ ] ;
42+ let clickedIndex = 0 ;
43+
44+ const imageGrid = [ ] ;
45+ let maxColumns = 0 ;
46+
47+ allRows . forEach ( ( row ) => {
48+ const rowImages = row . querySelectorAll ( '.fi-ta-image .image-preview-trigger' ) ;
49+ const rowImageArray = Array . from ( rowImages ) ;
50+ imageGrid . push ( rowImageArray ) ;
51+ maxColumns = Math . max ( maxColumns , rowImageArray . length ) ;
52+ } ) ;
53+
54+ let clickedRowIndex = - 1 ;
55+ let clickedColIndex = - 1 ;
56+
57+ imageGrid . forEach ( ( rowImages , rowIndex ) => {
58+ rowImages . forEach ( ( img , colIndex ) => {
59+ if ( img === imageElement ) {
60+ clickedRowIndex = rowIndex ;
61+ clickedColIndex = colIndex ;
62+ }
63+ } ) ;
64+ } ) ;
65+
66+ imageGrid . forEach ( ( rowImages , rowIndex ) => {
67+ rowImages . forEach ( ( img , colIndex ) => {
68+ this . addImageToCollection ( img , imageElement , ( ) => {
69+ if ( rowIndex === clickedRowIndex && colIndex === clickedColIndex ) {
70+ clickedIndex = this . images . length - 1 ;
71+ }
72+ } ) ;
73+ } ) ;
74+ } ) ;
75+
76+ if ( this . images . length > 0 ) {
77+ this . currentIndex = clickedIndex ;
78+ this . updateCurrentImage ( ) ;
79+ this . open ( ) ;
80+ }
81+ } ,
82+
83+ addImageToCollection ( img , imageElement , onMatch ) {
84+ const configData = img . dataset . lightboxConfig ;
85+
86+ if ( configData ) {
87+ try {
88+ const lightboxData = JSON . parse ( configData ) ;
89+ const matchingImageData = lightboxData . find ( data =>
90+ img . src . includes ( data . url ) || data . url . includes ( img . src ) || data . url === img . src
91+ ) ;
92+
93+ if ( matchingImageData ) {
94+ this . images . push ( {
95+ url : img . src ,
96+ title : matchingImageData . title || '' ,
97+ link : matchingImageData . link || '' ,
98+ filename : img . alt || ''
99+ } ) ;
100+
101+ if ( img === imageElement ) {
102+ onMatch ( ) ;
103+ }
104+ return ;
105+ }
106+ } catch ( e ) {
107+ console . error ( 'Error parsing lightbox config:' , e ) ;
108+ }
109+ }
110+
111+ const imageData = {
112+ url : img . src ,
113+ title : '' ,
114+ link : '' ,
115+ filename : img . alt || ''
116+ } ;
117+
118+ this . images . push ( imageData ) ;
119+
120+ if ( img === imageElement ) {
121+ onMatch ( ) ;
122+ }
123+ } ,
124+
125+ open ( ) {
126+ this . isOpen = true ;
127+ document . body . style . overflow = 'hidden' ;
128+ } ,
129+
130+ close ( ) {
131+ this . isOpen = false ;
132+ document . body . style . overflow = '' ;
133+ } ,
134+
135+ next ( ) {
136+ if ( this . images . length > 0 ) {
137+ this . currentIndex = ( this . currentIndex + 1 ) % this . images . length ;
138+ this . updateCurrentImage ( ) ;
139+ }
140+ } ,
141+
142+ previous ( ) {
143+ if ( this . images . length > 0 ) {
144+ this . currentIndex = ( this . currentIndex - 1 + this . images . length ) % this . images . length ;
145+ this . updateCurrentImage ( ) ;
146+ }
147+ } ,
148+
149+ updateCurrentImage ( ) {
150+ if ( this . images [ this . currentIndex ] ) {
151+ this . currentImage = this . images [ this . currentIndex ] ;
152+ }
153+ }
154+ } ;
155+ } ;
0 commit comments