-
Notifications
You must be signed in to change notification settings - Fork 6
/
videojs.autoplay-toggle.js
211 lines (188 loc) · 6.66 KB
/
videojs.autoplay-toggle.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
'use strict';
(function (window, document, videojs) {
/**
* Cookie access functions.
* From: https://developer.mozilla.org/en-US/docs/Web/API/document.cookie
*/
var cookies = {
getItem: function (sKey) {
if (!sKey) { return null; }
return decodeURIComponent(
document.cookie.replace(
new RegExp("(?:(?:^|.*;)\\s*" + encodeURIComponent(sKey).replace(
/[\-\.\+\*]/g, "\\$&") + "\\s*\\=\\s*([^;]*).*$)|^.*$"), "$1")
) || null;
},
setItem: function (sKey, sValue, vEnd, sPath, sDomain, bSecure) {
if (!sKey || /^(?:expires|max\-age|path|domain|secure)$/i.test(sKey)) { return false; }
var sExpires = "";
if (vEnd) {
switch (vEnd.constructor) {
case Number:
sExpires = vEnd === Infinity ? "; expires=Fri, 31 Dec 9999 23:59:59 GMT" : "; max-age=" + vEnd;
break;
case String:
sExpires = "; expires=" + vEnd;
break;
case Date:
sExpires = "; expires=" + vEnd.toUTCString();
break;
}
}
document.cookie =
encodeURIComponent(sKey) + "=" + encodeURIComponent(sValue)
+ sExpires
+ (sDomain ? "; domain=" + sDomain : "")
+ (sPath ? "; path=" + sPath : "")
+ (bSecure ? "; secure" : "");
return true;
},
removeItem: function(sKey, sPath, sDomain) {
if (!this.hasItem(sKey)) {
return false;
}
document.cookie = encodeURIComponent(sKey) + "=;" + " expires=Thu, 01 Jan 1970 00:00:00 GMT" + (sDomain ? "; domain=" + sDomain : "") + (sPath ? "; path=" + sPath : "");
return true;
},
hasItem: function (sKey) {
if (!sKey) { return false; }
return (new RegExp("(?:^|;\\s*)" + encodeURIComponent(sKey).replace(
/[\-\.\+\*]/g, "\\$&") + "\\s*\\=")).test(document.cookie);
},
},
/**
* Local storage functionality.
*/
localStorage = {
available: function() {
try {
window.localStorage.setItem('fishingForLocalStorage', 'itsHere');
window.localStorage.removeItem('fishingForLocalStorage');
return true;
} catch(e) {
return false;
}
},
getItem: function(key) {
return window.localStorage.getItem(key);
},
setItem: function(key, value) {
return window.localStorage.setItem(key, value);
},
removeItem: function(key) {
return window.localStorage.removeItem(key);
}
},
/**
* Storage chooser, will use localstorage if available, otherwise use cookies.
*/
storage = {
getItem: function (key) {
return localStorage.available() ? localStorage.getItem(key) : cookies.getItem(key);
},
setItem: function (key, value) {
localStorage.available() ? localStorage.setItem(key, value) : cookies.setItem(key, value, Infinity, '/');
return value;
},
removeItem: function (key) {
localStorage.available() ? localStorage.removeItem(key) : cookies.removeItem(key);
}
},
/**
* Object extend function.
*/
extend = function(obj) {
var arg, i, k;
for (i = 1; i < arguments.length; i++) {
arg = arguments[i];
for (k in arg) {
if (arg.hasOwnProperty(k)) {
obj[k] = arg[k];
}
}
}
return obj;
},
/**
* Default settings for this plugin.
*/
defaults = {
namespace: 'autoplay-toggle', // namespace for cookie/localstorage
},
/**
* Autoplay toggle plugin setup.
*/
autoplayToggle = function (options) {
var player = this,
settings = extend({}, defaults, options || {}),
key = settings.namespace + '-autoplay';
// add new button to player
var autoplayBtn = document.createElement('div');
autoplayBtn.className = 'vjs-autoplay-toggle-button vjs-menu-button vjs-control';
autoplayBtn.innerHTML =
'<div>'
+ '<span class="vjs-control-text">'
+ 'Autoplay:<br>'
+ '<span class="autoplay-toggle autoplay-toggle-active autoplay-on">On</span>'
+ ' / '
+ '<span class="autoplay-toggle autoplay-off">Off</span>'
+ '</span>'
+ '</div>';
player.controlBar.el().appendChild(autoplayBtn);
// retrieve autoplay from storage and highlight the correct toggle option in *all* video players
var autoplayToggleButton = function (activate) {
// set cookie once
activate ? storage.removeItem(key) : storage.setItem(key, 'no');
// get all videos and toggle all their autoplays
var videos = document.querySelectorAll('.video-js');
for (var i = 0; i < videos.length; i++) {
// check that this video has a toggle button
var toggleBtnSelector = videos[i].querySelectorAll('.vjs-autoplay-toggle-button');
if (toggleBtnSelector.length > 0) {
var toggleBtn = toggleBtnSelector[0],
toggleOn = toggleBtn.querySelectorAll('.autoplay-on')[0],
toggleOff = toggleBtn.querySelectorAll('.autoplay-off')[0];
if (activate) {
// toggle this on
toggleOn.className = 'autoplay-toggle autoplay-toggle-active autoplay-on';
toggleOff.className = 'autoplay-toggle autoplay-off';
} else {
// toggle this off
toggleOn.className = 'autoplay-toggle autoplay-on';
toggleOff.className = 'autoplay-toggle autoplay-toggle-active autoplay-off';
}
}
}
};
var turnOn = !storage.getItem(key);
// change player behavior based on toggle
if (player.autoplay() && !turnOn) {
// this could be autoplaying, make sure to stop it and ensure player's autoplay is false
player.autoplay(false);
player.pause();
} else if (player.autoplay() && turnOn) {
// we want this to autoplay
player.play();
}
// initialize autoplay toggle
autoplayToggleButton(turnOn);
// set up toggle click
autoplayBtn.onclick = function () {
// check if key in storage and do the opposite of that to toggle
var toggle = !!storage.getItem(key);
autoplayToggleButton(toggle);
};
// return player to allow this plugin to be chained
return player;
};
// set this thing up as a vjs plugin
videojs.plugin('autoplayToggle', autoplayToggle);
// alternative function for retrieving autoplay value from storage for situations where other plugins
// are interfering with this plugin
videojs.autoplaySettingFromStorage = function (options) {
var settings = extend({}, defaults, options || {}),
key = settings.namespace + '-autoplay';
// negate what's in storage since only "don't autoplay" is stored
return !storage.getItem(key);
};
})(window, document, videojs);