Passed
Push — develop ( c6dd2a...1f8334 )
by Paul
03:38
created

assets/js/youtube.js   A

Complexity

Total Complexity 16
Complexity/F 1.6

Size

Lines of Code 84
Function Count 10

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 0
nc 1
dl 0
loc 84
rs 10
c 0
b 0
f 0
wmc 16
mnd 2
bc 16
fnc 10
bpm 1.6
cpm 1.6
noi 4

6 Functions

Rating   Name   Duplication   Size   Complexity  
A Plugin.playerHasLoaded 0 8 1
A Plugin.onYouTubePlayerAPIReady 0 21 1
A Plugin.onReady 0 8 1
A Plugin.injectScript 0 8 1
A Plugin.init 0 8 2
A youtube.js ➔ Plugin 0 10 2
1
;(function( window, document, undefined ) {
2
	"use strict";
3
4
	var Plugin = function( videoEl, options )
5
	{
6
		this.video = videoEl;
7
		if( this.video ) {
8
			this.hasLoaded = typeof YT === undefined;
0 ignored issues
show
Bug introduced by
The variable YT seems to be never declared. If this is a global, consider adding a /** global: YT */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
9
			this.options = castor._extend( this.defaults, options );
0 ignored issues
show
Bug introduced by
The variable castor seems to be never declared. If this is a global, consider adding a /** global: castor */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
10
			this.player = null;
11
			this.init();
12
		}
13
	};
14
15
	Plugin.prototype =
16
	{
17
		defaults: {
18
			iframe: '.video-embed',
19
			poster: '.video-poster',
20
			spinner: '.video-spinner',
21
		},
22
23
		init: function()
24
		{
25
			if( this.hasLoaded === false ) {
26
				this.injectScript();
27
			}
28
			this.onYouTubePlayerAPIReady();
29
			this.video.querySelector( this.options.poster ).classList.add( 'hide' );
30
		},
31
32
		injectScript: function()
33
		{
34
			this.hasLoaded = true;
35
			var tag = document.createElement( 'script' );
36
			tag.src = "//www.youtube.com/player_api";
37
			var firstScriptTag = document.getElementsByTagName( 'script' )[0];
38
			firstScriptTag.parentNode.insertBefore( tag, firstScriptTag );
39
		},
40
41
		onReady: function()
42
		{
43
			var spinner = this.video.querySelector( this.options.spinner );
44
			this.player.playVideo();
45
			setTimeout( function() {
46
				spinner.classList.add( 'hide' );
47
			}, 1000 );
48
		},
49
50
		playerHasLoaded: function()
51
		{
52
			this.player = new YT.Player( this.video.querySelector( this.options.iframe ), {
0 ignored issues
show
Bug introduced by
The variable YT seems to be never declared. If this is a global, consider adding a /** global: YT */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
53
				events: {
54
					onReady: this.onReady.bind( this ),
55
				},
56
			});
57
		},
58
59
		onYouTubePlayerAPIReady: function()
60
		{
61
			var self = this;
62
			var youtubeApiReadyExists = typeof window.castor.YouTubeAPIReady === undefined;
63
			setTimeout( function() {
64
				if( typeof window.onYouTubePlayerAPIReady !== undefined ) {
65
					if( !youtubeApiReadyExists ) {
66
						window.castor.YouTubeAPIReady = [];
67
					}
68
					window.castor.YouTubeAPIReady.push( window.onYouTubePlayerAPIReady );
69
				}
70
				window.onYouTubePlayerAPIReady = function() {
71
					self.playerHasLoaded();
72
					if( youtubeApiReadyExists ) {
73
						if( window.castor.YouTubeAPIReady.length ) {
74
							window.castor.YouTubeAPIReady.pop()();
75
						}
76
					}
77
				}
78
			}, 2 );
79
		},
80
	};
81
82
	castor.YouTube = Plugin;
0 ignored issues
show
Bug introduced by
The variable castor seems to be never declared. If this is a global, consider adding a /** global: castor */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
83
84
})( window, document );
85