| 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 |
|
| 203 |
players[ id ].cueVideoById( attrs[ id ] ); |
| 204 |
players[ id ].mute(); |
| 205 |
|
| 206 |
if ( 'always' === videoType ) { |
| 207 |
$this.playVideo( id ); |
| 208 |
} |
| 209 |
}, |
| 210 |
'onStateChange': function( e ) { |
| 211 |
if ( e.data === 1 ) { |
| 212 |
$( this ).contexObject( id ).closest( '.sight-portfolio-video-wrap' ).addClass( 'sight-portfolio-video-bg-init' ); |
| 213 |
$( this ).contexObject( id ).addClass( 'active' ); |
| 214 |
} else if ( e.data === 0 ) { |
| 215 |
players[ id ].seekTo( attrs[ id ].startSeconds ); |
| 216 |
} else if ( e.data === 5 ) { |
| 217 |
players[ id ].videoReady = true; |
| 218 |
} |
| 219 |
} |
| 220 |
} |
| 221 |
} ); |
| 222 |
} else { |
| 223 |
// Creating a player. |
| 224 |
players[ id ] = $( contex[ id ] ); |
| 225 |
|
| 226 |
// Ready play. |
| 227 |
players[ id ].on( 'canplay', function() { |
| 228 |
players[ id ].videoReady = true; |
| 229 |
|
| 230 |
if ( true !== players[ id ].start ) { |
| 231 |
players[ id ].start = true; |
| 232 |
|
| 233 |
this.currentTime = videoStart ? videoStart : 0; |
| 234 |
|
| 235 |
if ( 'always' === videoType ) { |
| 236 |
$this.playVideo( id ); |
| 237 |
} |
| 238 |
} |
| 239 |
} ); |
| 240 |
|
| 241 |
// Play. |
| 242 |
players[ id ].on( 'play', function() { |
| 243 |
players[ id ].parents( '.sight-portfolio-video-wrap' ).addClass( 'sight-portfolio-video-bg-init' ); |
| 244 |
players[ id ].addClass( 'active' ); |
| 245 |
} ); |
| 246 |
|
| 247 |
// Ended. |
| 248 |
players[ id ].on( 'timeupdate', function() { |
| 249 |
if ( videoEnd && this.currentTime >= videoEnd ) { |
| 250 |
players[ id ].trigger( 'pause' ); |
| 251 |
|
| 252 |
this.currentTime = videoStart; |
| 253 |
|
| 254 |
players[ id ].trigger( 'play' ); |
| 255 |
} |
| 256 |
} ); |
| 257 |
|
| 258 |
players[ id ].trigger( 'load' ); |
| 259 |
} |
| 260 |
|
| 261 |
$this.rescaleVideoBackground(); |
| 262 |
} |
| 263 |
|
| 264 |
// Pause and play. |
| 265 |
if ( 'always' === videoType && isActive && isInit && ! $this.getVideoUpause( id ) ) { |
| 266 |
|
| 267 |
if ( isInView ) { |
| 268 |
$this.playVideo( id ); |
| 269 |
} |
| 270 |
|
| 271 |
if ( ! isInView ) { |
| 272 |
$this.pauseVideo( id ); |
| 273 |
} |
| 274 |
} |
| 275 |
} ); |
| 276 |
}, |
| 277 |
|
| 278 |
// Construct video background. |
| 279 |
constructVideoBackground: function( object ) { |
| 280 |
if ( $( 'body' ).hasClass( 'wp-admin' ) ) { |
| 281 |
return; |
| 282 |
} |
| 283 |
|
| 284 |
if ( process ) { |
| 285 |
return; |
| 286 |
} |
| 287 |
|
| 288 |
process = true; |
| 289 |
|
| 290 |
// Smart init API. |
| 291 |
if ( !initAPI ) { |
| 292 |
let elements = $( '[data-video-mode="youtube"][data-youtube-id]' ); |
| 293 |
|
| 294 |
if ( elements.length ) { |
| 295 |
embedYoutubeAPI(); |
| 296 |
|
| 297 |
initAPI = true; |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
if ( !initAPI ) { |
| 302 |
process = false; |
| 303 |
} |
| 304 |
|
| 305 |
// Play Video. |
| 306 |
$this.initVideoBackground( 'local', object ); |
| 307 |
|
| 308 |
if ( initAPI && YTdeferredDone ) { |
| 309 |
$this.initVideoBackground( 'youtube', object ); |
| 310 |
} |
| 311 |
|
| 312 |
process = false; |
| 313 |
}, |
| 314 |
|
| 315 |
// Get video type. |
| 316 |
getVideoType: function( id ) { |
| 317 |
return $( this ).contexObject( id, 'container' ).data( 'video-type' ); |
| 318 |
}, |
| 319 |
|
| 320 |
// Get video mode. |
| 321 |
getVideoMode: function( id ) { |
| 322 |
return $( this ).contexObject( id, 'container' ).data( 'video-mode' ); |
| 323 |
}, |
| 324 |
|
| 325 |
// Get video state. |
| 326 |
getVideoState: function( id ) { |
| 327 |
return players[ id ].videoState; |
| 328 |
}, |
| 329 |
|
| 330 |
// Get video ready. |
| 331 |
getVideoReady: function( id ) { |
| 332 |
return players[ id ].videoReady ? players[ id ].videoReady : false; |
| 333 |
}, |
| 334 |
|
| 335 |
// Get video upause. |
| 336 |
getVideoUpause: function( id ) { |
| 337 |
return players[ id ].videoUpause ? players[ id ].videoUpause : false; |
| 338 |
}, |
| 339 |
|
| 340 |
// Get video volume. |
| 341 |
getVideoVolume: function( id ) { |
| 342 |
return players[ id ].videoVolume ? players[ id ].videoVolume : 'mute'; |
| 343 |
}, |
| 344 |
|
| 345 |
// Change video upause. |
| 346 |
changeVideoUpause: function( id, val ) { |
| 347 |
players[ id ].videoUpause = val; |
| 348 |
}, |
| 349 |
|
| 350 |
// Play video. |
| 351 |
playVideo: function( id ) { |
| 352 |
if ( 'play' === players[ id ].videoState ) { |
| 353 |
return; |
| 354 |
} |
| 355 |
|
| 356 |
if ( ! players[ id ].videoReady ) { |
| 357 |
return setTimeout(function(){ |
| 358 |
$this.playVideo( id ); |
| 359 |
}, 100); |
| 360 |
} |
| 361 |
|
| 362 |
if ( 'youtube' === $this.getVideoMode( id ) ) { |
| 363 |
players[ id ].playVideo(); |
| 364 |
} else { |
| 365 |
players[ id ].trigger( 'play' ); |
| 366 |
} |
| 367 |
|
| 368 |
// Change control. |
| 369 |
let control = $( this ).contexObject( id, 'wrap' ).find( '.sight-portfolio-player-state' ); |
| 370 |
|
| 371 |
$( control ).removeClass( 'sight-portfolio-player-pause' ); |
| 372 |
$( control ).addClass( 'sight-portfolio-player-play' ); |
| 373 |
|
| 374 |
// Set state. |
| 375 |
players[ id ].videoState = 'play'; |
| 376 |
}, |
| 377 |
|
| 378 |
// Pause video. |
| 379 |
pauseVideo: function( id ) { |
| 380 |
if ( 'pause' === players[ id ].videoState ) { |
| 381 |
return; |
| 382 |
} |
| 383 |
|
| 384 |
if ( ! players[ id ].videoReady ) { |
| 385 |
return; |
| 386 |
} |
| 387 |
|
| 388 |
if ( 'youtube' === $this.getVideoMode( id ) ) { |
| 389 |
players[ id ].pauseVideo(); |
| 390 |
} else { |
| 391 |
players[ id ].trigger( 'pause' ); |
| 392 |
} |
| 393 |
|
| 394 |
// Change control. |
| 395 |
let control = $( this ).contexObject( id, 'wrap' ).find( '.sight-portfolio-player-state' ); |
| 396 |
|
| 397 |
$( control ).removeClass( 'sight-portfolio-player-play' ); |
| 398 |
$( control ).addClass( 'sight-portfolio-player-pause' ); |
| 399 |
|
| 400 |
// Set state. |
| 401 |
players[ id ].videoState = 'pause'; |
| 402 |
}, |
| 403 |
|
| 404 |
// Unmute video. |
| 405 |
unmuteVideo: function( id ) { |
| 406 |
if ( ! players[ id ].videoReady ) { |
| 407 |
return; |
| 408 |
} |
| 409 |
|
| 410 |
if ( 'youtube' === $this.getVideoMode( id ) ) { |
| 411 |
players[ id ].unMute(); |
| 412 |
} else { |
| 413 |
players[ id ].prop( 'muted', false ); |
| 414 |
} |
| 415 |
|
| 416 |
// Change control. |
| 417 |
let control = $( this ).contexObject( id, 'wrap' ).find( '.sight-portfolio-player-volume' ); |
| 418 |
|
| 419 |
$( control ).removeClass( 'sight-portfolio-player-mute' ); |
| 420 |
$( control ).addClass( 'sight-portfolio-player-unmute' ); |
| 421 |
|
| 422 |
// Set state. |
| 423 |
players[ id ].videoVolume = 'unmute'; |
| 424 |
}, |
| 425 |
|
| 426 |
// Mute video. |
| 427 |
muteVideo: function( id ) { |
| 428 |
if ( ! players[ id ].videoReady ) { |
| 429 |
return; |
| 430 |
} |
| 431 |
|
| 432 |
if ( 'youtube' === $this.getVideoMode( id ) ) { |
| 433 |
players[ id ].mute(); |
| 434 |
} else { |
| 435 |
players[ id ].prop( 'muted', true ); |
| 436 |
} |
| 437 |
|
| 438 |
// Change control. |
| 439 |
let control = $( this ).contexObject( id, 'wrap' ).find( '.sight-portfolio-player-volume' ); |
| 440 |
|
| 441 |
$( control ).removeClass( 'sight-portfolio-player-unmute' ); |
| 442 |
$( control ).addClass( 'sight-portfolio-player-mute' ); |
| 443 |
|
| 444 |
// Set state. |
| 445 |
players[ id ].videoVolume = 'muted'; |
| 446 |
}, |
| 447 |
|
| 448 |
// Toogle video state. |
| 449 |
toogleVideoState: function( id ) { |
| 450 |
if ( 'play' === $this.getVideoState( id ) ) { |
| 451 |
$this.pauseVideo( id ); |
| 452 |
} else { |
| 453 |
$this.playVideo( id ); |
| 454 |
} |
| 455 |
}, |
| 456 |
|
| 457 |
// Toogle video volume. |
| 458 |
toogleVideoVolume: function( id ) { |
| 459 |
if ( 'unmute' === $this.getVideoVolume( id ) ) { |
| 460 |
$this.muteVideo( id ); |
| 461 |
} else { |
| 462 |
$this.unmuteVideo( id ); |
| 463 |
} |
| 464 |
}, |
| 465 |
|
| 466 |
/** Events */ |
| 467 |
events: function( e ) { |
| 468 |
// State Control. |
| 469 |
$doc.on( 'click', '.sight-portfolio-player-state', function() { |
| 470 |
let id = $( this ).contexObject( false, 'inner' ).attr( 'data-id' ); |
| 471 |
|
| 472 |
$this.toogleVideoState( id ); |
| 473 |
|
| 474 |
if ( 'play' === $this.getVideoState( id ) ) { |
| 475 |
$this.changeVideoUpause( id, false ); |
| 476 |
} else { |
| 477 |
$this.changeVideoUpause( id, true ); |
| 478 |
} |
| 479 |
} ); |
| 480 |
|
| 481 |
// Stop Control. |
| 482 |
$doc.on( 'click', '.sight-portfolio-player-stop', function() { |
| 483 |
let id = $( this ).contexObject( false, 'inner' ).attr( 'data-id' ); |
| 484 |
|
| 485 |
if ( 'play' === $this.getVideoState( id ) ) { |
| 486 |
$this.changeVideoUpause( id, true ); |
| 487 |
} |
| 488 |
|
| 489 |
$this.pauseVideo( id ); |
| 490 |
} ); |
| 491 |
|
| 492 |
// Volume Control. |
| 493 |
$doc.on( 'click', '.sight-portfolio-player-volume', function() { |
| 494 |
let id = $( this ).contexObject( false, 'inner' ).attr( 'data-id' ); |
| 495 |
|
| 496 |
$this.toogleVideoVolume( id ); |
| 497 |
} ); |
| 498 |
|
| 499 |
// Mouseover. |
| 500 |
$doc.on( 'mouseover mousemove', '.sight-portfolio-entry__thumbnail', function() { |
| 501 |
let id = $( this ).contexObject( false, 'inner' ).attr( 'data-id' ); |
| 502 |
|
| 503 |
if ( 'hover' === $this.getVideoType( id ) ) { |
| 504 |
$this.playVideo( id ); |
| 505 |
} |
| 506 |
} ); |
| 507 |
|
| 508 |
// Mouseout. |
| 509 |
$doc.on( 'mouseout', '.sight-portfolio-entry__thumbnail', function() { |
| 510 |
let id = $( this ).contexObject( false, 'inner' ).attr( 'data-id' ); |
| 511 |
|
| 512 |
if ( 'hover' === $this.getVideoType( id ) ) { |
| 513 |
$this.pauseVideo( id ); |
| 514 |
} |
| 515 |
} ); |
| 516 |
|
| 517 |
// Document scroll. |
| 518 |
$window.on( 'load scroll resize scrollstop', function() { |
| 519 |
$this.constructVideoBackground(); |
| 520 |
} ); |
| 521 |
|
| 522 |
// Document ready. |
| 523 |
$doc.ready( function() { |
| 524 |
$this.constructVideoBackground(); |
| 525 |
} ); |
| 526 |
|
| 527 |
// Post load. |
| 528 |
$body.on( 'post-load', function() { |
| 529 |
$this.constructVideoBackground(); |
| 530 |
} ); |
| 531 |
|
| 532 |
// Document resize. |
| 533 |
$window.on( 'resize', function() { |
| 534 |
$this.rescaleVideoBackground(); |
| 535 |
} ); |
| 536 |
|
| 537 |
// Init. |
| 538 |
$this.constructVideoBackground(); |
| 539 |
} |
| 540 |
}; |
| 541 |
} )(); |
| 542 |
|
| 543 |
// Initialize. |
| 544 |
sightVideoPortfolio.init(); |
| 545 |
|