forked from lopezs/ableplayer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreference.js
More file actions
335 lines (292 loc) · 10.1 KB
/
preference.js
File metadata and controls
335 lines (292 loc) · 10.1 KB
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
(function ($) {
AblePlayer.prototype.setCookie = function(cookieValue) {
$.cookie.json = true;
if ($.isFunction($.cookie)) {
// set cookie that expires in 90 days
$.cookie('Able-Player', cookieValue, 90);
}
};
AblePlayer.prototype.getCookie = function() {
var defaultCookie = {
preferences: {}
};
$.cookie.json = true;
if ($.isFunction($.cookie)) {
var cookie;
try {
cookie = $.cookie('Able-Player');
}
catch (err) {
// Original cookie can't be parsed; update to default.
this.setCookie(defaultCookie);
cookie = defaultCookie;
}
if (cookie) {
return cookie;
}
else {
return defaultCookie;
}
}
};
AblePlayer.prototype.getAvailablePreferences = function() {
// Return the list of currently available preferences.
var prefs = [];
// modifier keys preferences apply to both audio and video
prefs.push({
'name': 'prefAltKey', // use alt key with shortcuts
'label': this.tt.prefAltKey,
'default': 1
});
prefs.push({
'name': 'prefCtrlKey', // use ctrl key with shortcuts
'label': this.tt.prefCtrlKey,
'default': 1
});
prefs.push({
'name': 'prefShiftKey',
'label': this.tt.prefShiftKey,
'default': 0
});
if (this.mediaType === 'video') { // features prefs apply only to video
prefs.push({
'name': 'prefCaptions', // closed captions default state
'label': this.tt.prefCaptions,
'default': 1 // on because many users can benefit
});
prefs.push({
'name': 'prefSignLanguage', // use sign language if available
'label': this.tt.prefSignLanguage,
'default': 1 // on because in rare cases that it's actually available, users should be exposed to it
});
prefs.push({
'name': 'prefDesc', // audio description default state
'label': this.tt.prefDesc,
'default': 0 // off because users who don't need it might find it distracting
});
prefs.push({
'name': 'prefClosedDesc', // use closed description if available
'label': this.tt.prefClosedDesc,
'default': 0 // off because experimental
});
prefs.push({
'name': 'prefDescPause', // automatically pause when closed description starts
'label': this.tt.prefDescPause,
'default': 0 // off because it burdens user with restarting after every pause
});
prefs.push({
'name': 'prefVisibleDesc', // visibly show closed description (if avilable and used)
'label': this.tt.prefVisibleDesc,
'default': 1 // on because sighted users probably want to see this cool feature in action
});
prefs.push({
'name': 'prefTranscript', // transcript default state
'label': this.tt.prefTranscript,
'default': 0 // off because turning it on has a certain WOW factor
});
prefs.push({
'name': 'prefHighlight', // highlight transcript as media plays
'label': this.tt.prefHighlight,
'default': 1 // on because many users can benefit
});
prefs.push({
'name': 'prefTabbable', // tab-enable transcript
'label': this.tt.prefTabbable,
'default': 0 // off because if users don't need it, it impedes tabbing elsewhere on the page
});
}
else {
prefs.push({
'name': 'prefTranscript', // transcript default state
'label': this.tt.prefTranscript,
'default': 0 // off because turning it on has a certain WOW factor
});
prefs.push({
'name': 'prefHighlight', // highlight transcript as media plays
'label': this.tt.prefHighlight,
'default': 1 // on because many users can benefit
});
prefs.push({
'name': 'prefTabbable', // tab-enable transcript
'label': this.tt.prefTabbable,
'default': 0 // off because if users don't need it, it impedes tabbing elsewhere on the page
});
}
return prefs;
};
// Loads current/default preferences from cookie into the AblePlayer object.
AblePlayer.prototype.loadCurrentPreferences = function () {
var available = this.getAvailablePreferences();
var cookie = this.getCookie();
// Copy current cookie values into this object, and fill in any default values.
for (var ii = 0; ii < available.length; ii++) {
var prefName = available[ii]['name'];
var defaultValue = available[ii]['default'];
if (cookie.preferences[prefName] !== undefined) {
this[prefName] = cookie.preferences[prefName];
}
else {
cookie.preferences[prefName] = defaultValue;
this[prefName] = defaultValue;
}
}
// Save since we may have added default values.
this.setCookie(cookie);
};
// Creates the preferences form and injects it.
AblePlayer.prototype.injectPrefsForm = function () {
var prefsDiv, introText, prefsIntro,
featuresFieldset, featuresLegend,
keysFieldset, keysLegend,
i, thisPref, thisDiv, thisId, thisLabel, thisCheckbox,
thisObj, available;
thisObj = this;
available = this.getAvailablePreferences();
// outer container, will be assigned role="dialog"
prefsDiv = $('<div>',{
'class': 'able-prefs-form'
});
introText = '<p>' + this.tt.prefIntro + '</p>\n';
prefsIntro = $('<p>',{
html: introText
});
featuresFieldset = $('<fieldset>');
featuresLegend = $('<legend>' + this.tt.prefFeatures + '</legend>');
featuresFieldset.append(featuresLegend);
keysFieldset = $('<fieldset>');
keysLegend = $('<legend>' + this.tt.prefKeys + '</legend>');
keysFieldset.append(keysLegend);
for (i=0; i<available.length; i++) {
thisPref = available[i]['name'];
thisDiv = $('<div>');
thisId = this.mediaId + '_' + thisPref;
thisLabel = $('<label for="' + thisId + '"> ' + available[i]['label'] + '</label>');
thisCheckbox = $('<input>',{
type: 'checkbox',
name: thisPref,
id: thisId,
value: 'true'
});
thisDiv.append(thisCheckbox).append(thisLabel);
// check current active value for this preference
if (this[thisPref] === 1) {
thisCheckbox.prop('checked',true);
}
// TODO: We need to indicate this in the prefs structure itself.
if (i === 0 || i === 1 || i === 2) { // this is a key preference
keysFieldset.append(thisDiv);
}
else { // this is a feature preference
featuresFieldset.append(thisDiv);
}
}
// Now assemble all the parts
prefsDiv
.append(prefsIntro)
.append(keysFieldset)
.append(featuresFieldset);
// must be appended to the BODY!
// otherwise when aria-hidden="true" is applied to all background content
// that will include an ancestor of the dialog,
// which will render the dialog unreadable by screen readers
// this.$ableDiv.append(prefsDiv);
$('body').append(prefsDiv);
var dialog = new AccessibleDialog(prefsDiv, 'dialog', thisObj.tt.prefTitle, prefsIntro, thisObj.tt.closeButtonLabel, '32em');
// Add save and cancel buttons.
prefsDiv.append('<hr>');
var saveButton = $('<button class="modal-button">' + this.tt.save + '</button>');
var cancelButton = $('<button class="modal-button">' + this.tt.cancel + '</button>');
saveButton.click(function () {
dialog.hide();
thisObj.savePrefsFromForm();
});
cancelButton.click(function () {
dialog.hide();
});
prefsDiv.append(saveButton);
prefsDiv.append(cancelButton);
this.prefsDialog = dialog;
};
// Return a prefs object constructed from the form.
AblePlayer.prototype.savePrefsFromForm = function () {
// called when user saves the Preferences form
// update cookie with new value
var numChanges;
numChanges = 0;
var cookie = this.getCookie();
var available = this.getAvailablePreferences();
for (var ii = 0; ii < available.length; ii++) {
var prefName = available[ii]['name'];
if ($('input[name="' + prefName + '"]').is(':checked')) {
cookie.preferences[prefName] = 1;
if (this[prefName] === 1) {
// nothing has changed
}
else {
// user has just turned this pref on
this[prefName] = 1;
numChanges++;
}
}
else { // thisPref is not checked
cookie.preferences[prefName] = 0;
if (this[prefName] === 1) {
// user has just turned this pref off
this[prefName] = 0;
numChanges++;
}
else {
// nothing has chaged
}
}
}
if (numChanges > 0) {
this.setCookie(cookie);
this.showAlert(this.tt.prefSuccess);
}
else {
this.showAlert(this.tt.prefNoChange);
}
this.updatePrefs();
}
// Updates player based on current prefs. Safe to call multiple times.
AblePlayer.prototype.updatePrefs = function () {
var modHelp;
// modifier keys (update help text)
if (this.prefAltKey === 1) {
modHelp = 'Alt + ';
}
else {
modHelp = '';
}
if (this.prefCtrlKey === 1) {
modHelp += 'Control + ';
}
if (this.prefShiftKey === 1) {
modHelp += 'Shift + ';
}
$('.able-help-modifiers').text(modHelp);
// tabbable transcript
if (this.prefTabbable === 1) {
$('.able-transcript span.able-transcript-seekpoint').attr('tabindex','0');
}
else {
$('.able-transcript span.able-transcript-seekpoint').removeAttr('tabindex');
}
this.updateCaption();
this.updateDescription();
};
AblePlayer.prototype.usingModifierKeys = function(e) {
// return true if user is holding down required modifier keys
if ((this.prefAltKey === 1) && !e.altKey) {
return false;
}
if ((this.prefCtrlKey === 1) && !e.ctrlKey) {
return false;
}
if ((this.prefShiftKey === 1) && !e.shiftKey) {
return false;
}
return true;
};
})(jQuery);