PluginProbe
Sight – Professional Image Gallery and Portfolio / 1.0.4
Sight – Professional Image Gallery and Portfolio v1.0.4
trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8
sight / render / js / _initVideoBackground.js

_initVideoBackground.js in Sight – Professional Image Gallery and Portfolio 1.0.4, at render/js/_initVideoBackground.js

509 lines 12.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /** ----------------------------------------------------------------------------
2 * Video Background */
3
4 import {
5 $,
6 $window,
7 $doc,
8 $body
9 } from './utility';
10
11 var sightVideoPortfolio = {};
12
13 ( function() {
14 var $this;
15 var YTdeferredDone = false;
16 var initAPI = false;
17 var process = false;
18 var contex = [];
19 var players = [];
20 var attrs = [];
21
22 // Create deferred object
23 var YTdeferred = $.Deferred();
24
25 if ( typeof window.onYouTubePlayerAPIReady !== 'undefined' ) {
26 if ( typeof window.sightYTAPIReady === 'undefined' ) {
27 window.sightYTAPIReady = [];
28 }
29 window.sightYTAPIReady.push( window.onYouTubePlayerAPIReady );
30 }
31
32 window.onYouTubePlayerAPIReady = function() {
33 if ( typeof window.sightYTAPIReady !== 'undefined' ) {
34 if ( window.sightYTAPIReady.length ) {
35 window.sightYTAPIReady.pop()();
36 }
37 }
38
39 // Resolve when youtube callback is called
40 // passing YT as a parameter.
41 YTdeferred.resolve( window.YT );
42 };
43
44 // Whenever youtube callback was called = deferred resolved
45 // your custom function will be executed with YT as an argument.
46 YTdeferred.done( function( YT ) {
47 YTdeferredDone = true;
48 } );
49
50 // Embedding youtube iframe api.
51 function embedYoutubeAPI() {
52 if ( 'function' === typeof( window.onYTReady ) ) {
53 return;
54 }
55
56 var tag = document.createElement( 'script' );
57 tag.src = 'https://www.youtube.com/iframe_api';
58
59 var firstScriptTag = document.getElementsByTagName( 'script' )[ 0 ];
60 firstScriptTag.parentNode.insertBefore( tag, firstScriptTag );
61 }
62
63 $.fn.contexObject = function( id, type ) {
64 if ( 'string' === typeof id ) {
65 id = `[data-id="${id}"]`;
66 } else {
67 id = this;
68 }
69
70 if ( 'wrap' === type ) {
71 return $( id ).closest( '.sight-portfolio-video-wrap' );
72 } else if ( 'container' === type ) {
73 return $( id ).closest( '.sight-portfolio-video-container' );
74 } else if ( 'inner' === type ) {
75 return $( id ).closest( '.sight-portfolio-video-wrap' ).find( '.sight-portfolio-video-inner' );
76 } else {
77 return $( id );
78 }
79 };
80
81 // Object Video Portfolio.
82 sightVideoPortfolio = {
83
84 /** Initialize */
85 init: function( e ) {
86 if ( $( 'body' ).hasClass( 'wp-admin' ) ) {
87 return;
88 }
89
90 $this = sightVideoPortfolio;
91
92 // Init events.
93 $this.events( e );
94 },
95
96 // Video rescale.
97 rescaleVideoBackground: function() {
98 $( '.sight-portfolio-video-init' ).each( function() {
99 let w = $( this ).parent().width();
100 let h = $( this ).parent().height();
101
102 var hideControl = 400;
103
104 let id = $( this ).attr( 'data-id' );
105
106 if ( w / h > 16 / 9 ) {
107 if ( 'youtube' === $( this ).parent().data( 'video-mode' ) ) {
108 players[ id ].setSize( w, w / 16 * 9 + hideControl );
109 } else {
110 players[ id ].width( w );
111 players[ id ].height( w / 16 * 9 + hideControl );
112 }
113 } else {
114 if ( 'youtube' === $( this ).parent().data( 'video-mode' ) ) {
115 players[ id ].setSize( h / 9 * 16, h + hideControl );
116 } else {
117 players[ id ].width( h / 9 * 16 );
118 players[ id ].height( h + hideControl );
119 }
120 }
121 } );
122 },
123
124 // Init video background.
125 initVideoBackground: function( mode, object ) {
126
127 $( '.sight-portfolio-video-inner' ).each( function() {
128
129 // The mode.
130 if ( !$( this ).parent().is( `[data-video-mode="${mode}"]` ) ) {
131 return;
132 }
133
134 // The state.
135 var isInit = $( this ).hasClass( 'sight-portfolio-video-init' );
136
137 var id = null;
138
139 // Generate unique ID.
140 if ( !isInit ) {
141 id = Math.random().toString( 36 ).substr( 2, 9 );
142 } else {
143 id = $( this ).attr( 'data-id' );
144 }
145
146 // Create contex.
147 contex[ id ] = this;
148
149 // The monitor.
150 var isInView = $( contex[ id ] ).isInViewport();
151
152 // The actived.
153 var isActive = $( contex[ id ] ).hasClass( 'active' );
154
155 // Get video attrs.
156 var youtubeID = $( contex[ id ] ).parent().data( 'youtube-id' );
157 var videoType = $( contex[ id ] ).parent().data( 'video-type' );
158 var videoStart = $( contex[ id ] ).parent().data( 'video-start' );
159 var videoEnd = $( contex[ id ] ).parent().data( 'video-end' );
160
161 // Initialization.
162 if ( isInView && !isInit ) {
163
164 // Add init class.
165 $( contex[ id ] ).addClass( 'sight-portfolio-video-init' );
166
167 // Add unique ID.
168 $( contex[ id ] ).attr( 'data-id', id );
169
170 // Check video mode.
171 if ( 'youtube' === mode ) {
172 // Check video id.
173 if ( typeof youtubeID === 'undefined' || !youtubeID ) {
174 return;
175 }
176
177 // Video attrs.
178 attrs[ id ] = {
179 'videoId': youtubeID,
180 'startSeconds': videoStart,
181 'endSeconds': videoEnd,
182 'suggestedQuality': 'hd720'
183 };
184
185 // Creating a player.
186 players[ id ] = new YT.Player( contex[ id ], {
187 playerVars: {
188 autoplay: 0,
189 autohide: 1,
190 modestbranding: 1,
191 rel: 0,
192 showinfo: 0,
193 controls: 0,
194 disablekb: 1,
195 enablejsapi: 0,
196 iv_load_policy: 3,
197 playsinline: 1,
198 loop: 1
199 },
200 events: {
201 'onReady': function() {
202 players[ id ].cueVideoById( attrs[ id ] );
203 players[ id ].mute();
204
205 if ( 'always' === videoType ) {
206 $this.playVideo( id );
207 }
208 },
209 'onStateChange': function( e ) {
210 if ( e.data === 1 ) {
211 $( this ).contexObject( id ).closest( '.sight-portfolio-video-wrap' ).addClass( 'sight-portfolio-video-bg-init' );
212 $( this ).contexObject( id ).addClass( 'active' );
213 } else if ( e.data === 0 ) {
214 players[ id ].seekTo( attrs[ id ].startSeconds );
215 }
216 }
217 }
218 } );
219 } else {
220 // Creating a player.
221 players[ id ] = $( contex[ id ] );
222
223 // Ready play.
224 players[ id ].on( 'canplay', function() {
225 if ( true !== players[ id ].start ) {
226 players[ id ].start = true;
227
228 this.currentTime = videoStart ? videoStart : 0;
229
230 if ( 'always' === videoType ) {
231 $this.playVideo( id );
232 }
233 }
234 } );
235
236 // Play.
237 players[ id ].on( 'play', function() {
238 players[ id ].parents( '.sight-portfolio-video-wrap' ).addClass( 'sight-portfolio-video-bg-init' );
239 players[ id ].addClass( 'active' );
240 } );
241
242 // Ended.
243 players[ id ].on( 'timeupdate', function() {
244 if ( videoEnd && this.currentTime >= videoEnd ) {
245 players[ id ].trigger( 'pause' );
246
247 this.currentTime = videoStart;
248
249 players[ id ].trigger( 'play' );
250 }
251 } );
252
253 players[ id ].trigger( 'load' );
254 }
255
256 $this.rescaleVideoBackground();
257 }
258
259 // Pause and play.
260 if ( 'always' === videoType && isActive && isInit && ! $this.getVideoUpause( id ) ) {
261
262 if ( isInView ) {
263 $this.playVideo( id );
264 }
265
266 if ( ! isInView ) {
267 $this.pauseVideo( id );
268 }
269 }
270 } );
271 },
272
273 // Construct video background.
274 constructVideoBackground: function( object ) {
275 if ( $( 'body' ).hasClass( 'wp-admin' ) ) {
276 return;
277 }
278
279 if ( process ) {
280 return;
281 }
282
283 process = true;
284
285 // Smart init API.
286 if ( !initAPI ) {
287 let elements = $( '[data-video-mode="youtube"][data-youtube-id]' );
288
289 if ( elements.length ) {
290 embedYoutubeAPI();
291
292 initAPI = true;
293 }
294 }
295
296 if ( !initAPI ) {
297 process = false;
298 }
299
300 // Play Video.
301 $this.initVideoBackground( 'local', object );
302
303 if ( initAPI && YTdeferredDone ) {
304 $this.initVideoBackground( 'youtube', object );
305 }
306
307 process = false;
308 },
309
310 // Get video type.
311 getVideoType: function( id ) {
312 return $( this ).contexObject( id, 'container' ).data( 'video-type' );
313 },
314
315 // Get video mode.
316 getVideoMode: function( id ) {
317 return $( this ).contexObject( id, 'container' ).data( 'video-mode' );
318 },
319
320 // Get video state.
321 getVideoState: function( id ) {
322 return players[ id ].videoState;
323 },
324
325 // Get video upause.
326 getVideoUpause: function( id ) {
327 return players[ id ].videoUpause ? players[ id ].videoUpause : false;
328 },
329
330 // Get video volume.
331 getVideoVolume: function( id ) {
332 return players[ id ].videoVolume ? players[ id ].videoVolume : 'mute';
333 },
334
335 // Change video upause.
336 changeVideoUpause: function( id, val ) {
337 players[ id ].videoUpause = val;
338 },
339
340 // Play video.
341 playVideo: function( id ) {
342 if ( 'youtube' === $this.getVideoMode( id ) ) {
343 players[ id ].playVideo();
344 } else {
345 players[ id ].trigger( 'play' );
346 }
347
348 // Change control.
349 let control = $( this ).contexObject( id, 'wrap' ).find( '.sight-portfolio-player-state' );
350
351 $( control ).removeClass( 'sight-portfolio-player-pause' );
352 $( control ).addClass( 'sight-portfolio-player-play' );
353
354 // Set state.
355 players[ id ].videoState = 'play';
356 },
357
358 // Pause video.
359 pauseVideo: function( id ) {
360 if ( 'youtube' === $this.getVideoMode( id ) ) {
361 players[ id ].pauseVideo();
362 } else {
363 players[ id ].trigger( 'pause' );
364 }
365
366 // Change control.
367 let control = $( this ).contexObject( id, 'wrap' ).find( '.sight-portfolio-player-state' );
368
369 $( control ).removeClass( 'sight-portfolio-player-play' );
370 $( control ).addClass( 'sight-portfolio-player-pause' );
371
372 // Set state.
373 players[ id ].videoState = 'pause';
374 },
375
376 // Unmute video.
377 unmuteVideo: function( id ) {
378 if ( 'youtube' === $this.getVideoMode( id ) ) {
379 players[ id ].unMute();
380 } else {
381 players[ id ].prop( 'muted', false );
382 }
383
384 // Change control.
385 let control = $( this ).contexObject( id, 'wrap' ).find( '.sight-portfolio-player-volume' );
386
387 $( control ).removeClass( 'sight-portfolio-player-mute' );
388 $( control ).addClass( 'sight-portfolio-player-unmute' );
389
390 // Set state.
391 players[ id ].videoVolume = 'unmute';
392 },
393
394 // Mute video.
395 muteVideo: function( id ) {
396 if ( 'youtube' === $this.getVideoMode( id ) ) {
397 players[ id ].mute();
398 } else {
399 players[ id ].prop( 'muted', true );
400 }
401
402 // Change control.
403 let control = $( this ).contexObject( id, 'wrap' ).find( '.sight-portfolio-player-volume' );
404
405 $( control ).removeClass( 'sight-portfolio-player-unmute' );
406 $( control ).addClass( 'sight-portfolio-player-mute' );
407
408 // Set state.
409 players[ id ].videoVolume = 'muted';
410 },
411
412 // Toogle video state.
413 toogleVideoState: function( id ) {
414 if ( 'play' === $this.getVideoState( id ) ) {
415 $this.pauseVideo( id );
416 } else {
417 $this.playVideo( id );
418 }
419 },
420
421 // Toogle video volume.
422 toogleVideoVolume: function( id ) {
423 if ( 'unmute' === $this.getVideoVolume( id ) ) {
424 $this.muteVideo( id );
425 } else {
426 $this.unmuteVideo( id );
427 }
428 },
429
430 /** Events */
431 events: function( e ) {
432 // State Control.
433 $doc.on( 'click', '.sight-portfolio-player-state', function() {
434 let id = $( this ).contexObject( false, 'inner' ).attr( 'data-id' );
435
436 $this.toogleVideoState( id );
437
438 if ( 'play' === $this.getVideoState( id ) ) {
439 $this.changeVideoUpause( id, false );
440 } else {
441 $this.changeVideoUpause( id, true );
442 }
443 } );
444
445 // Stop Control.
446 $doc.on( 'click', '.sight-portfolio-player-stop', function() {
447 let id = $( this ).contexObject( false, 'inner' ).attr( 'data-id' );
448
449 if ( 'play' === $this.getVideoState( id ) ) {
450 $this.changeVideoUpause( id, true );
451 }
452
453 $this.pauseVideo( id );
454 } );
455
456 // Volume Control.
457 $doc.on( 'click', '.sight-portfolio-player-volume', function() {
458 let id = $( this ).contexObject( false, 'inner' ).attr( 'data-id' );
459
460 $this.toogleVideoVolume( id );
461 } );
462
463 // Mouseover.
464 $doc.on( 'mouseover', '.sight-portfolio-entry__thumbnail', function() {
465 let id = $( this ).contexObject( false, 'inner' ).attr( 'data-id' );
466
467 if ( 'hover' === $this.getVideoType( id ) ) {
468 $this.playVideo( id );
469 }
470 } );
471
472 // Mouseout.
473 $doc.on( 'mouseout', '.sight-portfolio-entry__thumbnail', function() {
474 let id = $( this ).contexObject( false, 'inner' ).attr( 'data-id' );
475
476 if ( 'hover' === $this.getVideoType( id ) ) {
477 $this.pauseVideo( id );
478 }
479 } );
480
481 // Document scroll.
482 $window.on( 'load scroll resize scrollstop', function() {
483 $this.constructVideoBackground();
484 } );
485
486 // Document ready.
487 $doc.ready( function() {
488 $this.constructVideoBackground();
489 } );
490
491 // Post load.
492 $body.on( 'post-load', function() {
493 $this.constructVideoBackground();
494 } );
495
496 // Document resize.
497 $window.on( 'resize', function() {
498 $this.rescaleVideoBackground();
499 } );
500
501 // Init.
502 $this.constructVideoBackground();
503 }
504 };
505 } )();
506
507 // Initialize.
508 sightVideoPortfolio.init();
509