1
1
//@ts -nocheck
2
2
3
- import { FC , useState , useRef } from "react" ;
3
+ import { FC , useState , useRef , useEffect } from "react" ;
4
4
import { observer } from "mobx-react-lite" ;
5
5
import "react-responsive-carousel/lib/styles/carousel.min.css" ; // requires a loader
6
6
import { Carousel } from 'react-responsive-carousel' ;
@@ -50,14 +50,18 @@ export const CarouselDVC: FC<CarouselDVCProps> = observer(({
50
50
var videoListRefs = useRef ( [ ] ) ;
51
51
var dataMediaList : any [ ] | undefined = undefined ;
52
52
const [ isAutoPlay , setIsAutoPlay ] = useState ( true ) ;
53
+ const [ isPlayingVideo , setIsPlayingVideo ] = useState ( false ) ;
53
54
54
55
// Parse media json list
55
56
if ( typeof data_media_list_json == 'string' ) {
56
57
dataMediaList = JSON . parse ( data_media_list_json ) ;
57
58
}
58
59
59
60
const handleVideoEnded = ( videoElement ) => {
60
- if ( carouselRef . current != null ) {
61
+ // More than one element
62
+ if ( carouselRef . current != null && carouselRef . current . itemsRef . length > 1 && isPlayingVideo ) {
63
+ setIsPlayingVideo ( false ) ;
64
+
61
65
// Reset video after ended
62
66
$ ( videoElement . target ) . get ( 0 ) . pause ( ) ;
63
67
$ ( videoElement . target ) . get ( 0 ) . currentTime = 0 ;
@@ -67,9 +71,42 @@ export const CarouselDVC: FC<CarouselDVCProps> = observer(({
67
71
// Next slide
68
72
$ ( "#arrow-next" ) . trigger ( "click" ) ;
69
73
74
+ } else if ( carouselRef . current != null && carouselRef . current . itemsRef . length == 1 && isPlayingVideo ) {
75
+ // Exactly one element --> then repeat if it's a video
76
+
77
+ // Reset video after ended
78
+ $ ( videoElement . target ) . get ( 0 ) . pause ( ) ;
79
+ $ ( videoElement . target ) . get ( 0 ) . currentTime = 0 ;
80
+
81
+ // Replay
82
+ $ ( videoElement . target ) . get ( 0 ) . play ( ) ;
70
83
}
71
84
}
72
85
86
+ const handleLoadedData = ( videoElement ) => {
87
+ if ( carouselRef . current != null && videoElement . target . id === "video-0" ) {
88
+ const videoList = $ ( ".selected" ) . find ( "video" ) ;
89
+
90
+ for ( var video of videoList ) {
91
+ $ ( video ) . get ( 0 ) . play ( ) ;
92
+ }
93
+ setIsPlayingVideo ( true ) ;
94
+ }
95
+ }
96
+
97
+ useEffect ( ( ) => {
98
+ if ( carouselRef . current != null ) {
99
+ const videoList = $ ( ".selected" ) . find ( "video" ) ;
100
+
101
+ for ( var video of videoList ) {
102
+ if ( video . id === "video-0" )
103
+ $ ( video ) . get ( 0 ) . play ( ) ;
104
+ }
105
+
106
+ setIsPlayingVideo ( true ) ;
107
+ }
108
+ } , carouselRef ) ;
109
+
73
110
const defaultOnChange = ( index : number , item : React . ReactNode ) => {
74
111
// Check if sibling element are video ?
75
112
@@ -112,16 +149,27 @@ export const CarouselDVC: FC<CarouselDVCProps> = observer(({
112
149
setIsAutoPlay ( false ) ;
113
150
const currentVideo = videoListRefs . current . filter ( video => video . id === itemVideo [ 0 ] . props . id ) ;
114
151
115
- // Get video item and then play
116
- $ ( currentVideo ) . get ( 0 ) . play ( ) ;
152
+ // TODO : If first element is a video ==>
153
+ // Play the other video (because first element of a carousel has two videos and catch the event of only one)
154
+ if ( itemVideo [ 0 ] . props . id === "video-0" ) {
155
+ const videoList = $ ( ".selected" ) . find ( "video" ) ;
156
+
157
+ for ( var video of videoList ) {
158
+ $ ( video ) . get ( 0 ) . play ( ) ;
159
+ } ;
160
+ } else {
161
+ // Get video item and then play
162
+ $ ( currentVideo ) . get ( 0 ) . play ( ) ;
163
+ }
164
+
165
+ setIsPlayingVideo ( true ) ;
117
166
118
167
} else {
119
168
setIsAutoPlay ( true ) ;
120
169
}
121
170
122
171
123
172
}
124
-
125
173
return (
126
174
< Carousel
127
175
ref = { carouselRef }
@@ -165,8 +213,10 @@ export const CarouselDVC: FC<CarouselDVCProps> = observer(({
165
213
className = "diapo-container"
166
214
src = { media . url }
167
215
type = { media . mime_type }
168
- preload = "none"
169
- muted = "muted" />
216
+ preload = "auto"
217
+ muted = "muted"
218
+ onLoadeddata = { handleLoadedData }
219
+ />
170
220
}
171
221
</ div > ) ;
172
222
} )
0 commit comments