1+ window . mediaGallery = function ( { state, getLocale } ) {
2+ return {
3+ state : state || [ ] ,
4+ getLocale : getLocale ,
5+
6+ init ( ) {
7+ if ( ! Array . isArray ( this . state ) ) {
8+ this . state = [ ] ;
9+ }
10+
11+ this . lightboxOpen = false ;
12+ this . lightboxIndex = 0 ;
13+ this . lightboxImage = '' ;
14+ this . lightboxAlt = '' ;
15+ this . lightboxName = '' ;
16+ this . lightboxDescription = '' ;
17+
18+ document . addEventListener ( 'keydown' , ( e ) => {
19+ if ( this . lightboxOpen ) {
20+ if ( e . key === 'ArrowLeft' ) {
21+ e . preventDefault ( ) ;
22+ this . previousImage ( ) ;
23+ } else if ( e . key === 'ArrowRight' ) {
24+ e . preventDefault ( ) ;
25+ this . nextImage ( ) ;
26+ }
27+ }
28+ } ) ;
29+ } ,
30+
31+ getLocalizedName ( image ) {
32+ const currentLocale = this . getLocale ( ) ;
33+ if ( ! image . name || typeof image . name !== 'object' ) {
34+ return image . file_name || '' ;
35+ }
36+ return image . name [ currentLocale ] || image . name [ 'en' ] || image . file_name || '' ;
37+ } ,
38+
39+ getLocalizedDescription ( image ) {
40+ const currentLocale = this . getLocale ( ) ;
41+ if ( ! image . description || typeof image . description !== 'object' ) {
42+ return '' ;
43+ }
44+ return image . description [ currentLocale ] || image . description [ 'en' ] || '' ;
45+ } ,
46+
47+ openImageModal ( index ) {
48+ this . lightboxIndex = index ;
49+ const image = this . state [ index ] ;
50+ this . lightboxImage = image . url ;
51+ this . lightboxAlt = image . file_name ;
52+ this . lightboxName = this . getLocalizedName ( image ) ;
53+ this . lightboxDescription = this . getLocalizedDescription ( image ) ;
54+ this . lightboxOpen = true ;
55+ } ,
56+
57+ previousImage ( ) {
58+ this . lightboxIndex = ( this . lightboxIndex - 1 + this . state . length ) % this . state . length ;
59+ this . updateLightboxImage ( ) ;
60+ } ,
61+
62+ nextImage ( ) {
63+ this . lightboxIndex = ( this . lightboxIndex + 1 ) % this . state . length ;
64+ this . updateLightboxImage ( ) ;
65+ } ,
66+
67+ updateLightboxImage ( ) {
68+ const image = this . state [ this . lightboxIndex ] ;
69+ this . lightboxImage = image . url ;
70+ this . lightboxAlt = image . file_name ;
71+ this . lightboxName = this . getLocalizedName ( image ) ;
72+ this . lightboxDescription = this . getLocalizedDescription ( image ) ;
73+ } ,
74+
75+ handleSetCover ( image ) {
76+ if ( image . is_cover ) return ;
77+ this . $wire . mountFormComponentAction ( 'setCover' , { arguments : { uuid : image . uuid } } ) ;
78+ }
79+ } ;
80+ } ;
0 commit comments