-
Notifications
You must be signed in to change notification settings - Fork 237
Expand file tree
/
Copy pathpreference.js
More file actions
1099 lines (1032 loc) · 39.6 KB
/
preference.js
File metadata and controls
1099 lines (1032 loc) · 39.6 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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* global Cookies */
import $ from 'jquery';
import AccessibleDialog from './dialog';
function addPreferenceFunctions(AblePlayer) {
AblePlayer.prototype.setPrefs = function(preferences) {
if ( typeof Cookies !== 'undefined' ) {
Cookies.set('Able-Player', JSON.stringify(preferences), {
expires: 90,
sameSite: 'strict'
});
} else {
localStorage.setItem( 'Able-Player', JSON.stringify( preferences ) );
}
};
AblePlayer.prototype.getPref = function() {
var defaultPrefs = {
preferences: {},
sign: {},
transcript: {},
voices: []
};
var preferences;
try {
if ( typeof Cookies !== 'undefined' ) {
preferences = JSON.parse( Cookies.get('Able-Player') );
} else {
preferences = JSON.parse( localStorage.getItem('Able-Player') );
}
}
catch (err) {
// Original preferences can't be parsed; update to default
this.setPrefs( defaultPrefs );
preferences = defaultPrefs;
}
return (preferences) ? preferences : defaultPrefs;
};
AblePlayer.prototype.updatePreferences = function( setting ) {
// useful for settings updated independently of Preferences dialog
// e.g., prefAutoScrollTranscript, which is updated in control.js > handleTranscriptLockToggle()
// setting is any supported preference name (e.g., "prefCaptions")
// OR 'transcript' or 'sign' (not user-defined preferences, used to save position of draggable windows)
var preferences, $window, windowPos, available, i, prefName, voiceLangFound, newVoice;
preferences = this.getPref();
if (setting === 'transcript' || setting === 'sign') {
if (setting === 'transcript') {
$window = this.$transcriptArea;
windowPos = $window.position();
if (typeof preferences.transcript === 'undefined') {
preferences.transcript = {};
}
preferences.transcript['position'] = $window.css('position'); // either 'relative' or 'absolute'
preferences.transcript['zindex'] = $window.css('z-index');
preferences.transcript['top'] = windowPos.top;
preferences.transcript['left'] = windowPos.left;
preferences.transcript['width'] = $window.width();
preferences.transcript['height'] = $window.height();
} else if (setting === 'sign') {
$window = this.$signWindow;
windowPos = $window.position();
if (typeof preferences.sign === 'undefined') {
preferences.sign = {};
}
preferences.sign['position'] = $window.css('position'); // either 'relative' or 'absolute'
preferences.sign['zindex'] = $window.css('z-index');
preferences.sign['top'] = windowPos.top;
preferences.sign['left'] = windowPos.left;
preferences.sign['width'] = $window.width();
preferences.sign['height'] = $window.height();
}
} else if (setting === 'voice') {
if (typeof preferences.voices === 'undefined') {
preferences.voices = [];
}
// replace preferred voice for this lang in preferences.voices array, if one exists
// otherwise, add it to the array
voiceLangFound = false;
for (var v=0; v < preferences.voices.length; v++) {
if (preferences.voices[v].lang === this.prefDescVoiceLang) {
voiceLangFound = true;
preferences.voices[v].name = this.prefDescVoice;
}
}
if (!voiceLangFound) {
// no voice has been saved yet for this language. Add it to array.
newVoice = {'name':this.prefDescVoice, 'lang':this.prefDescVoiceLang};
preferences.voices.push(newVoice);
}
} else {
available = this.getAvailablePreferences();
// Rebuild preferences with current preferences values,
// replacing the one value that's been changed
for (i = 0; i < available.length; i++) {
prefName = available[i]['name'];
if (prefName == setting) {
// this is the one that requires an update
preferences.preferences[prefName] = this[prefName];
}
}
}
// Save updated preferences
this.setPrefs(preferences);
};
AblePlayer.prototype.getPreferencesGroups = function() {
// return array of groups in the order in which they will appear
// in the Preferences popup menu
// Human-readable label for each group is defined in translation table
if (this.usingYouTubeCaptions) {
// no transcript is possible
return ['captions','descriptions','keyboard'];
} else if (this.usingVimeoCaptions) {
// users cannot control caption appearance
// and no transcript is possible
return ['descriptions','keyboard'];
} else {
return ['captions','descriptions','keyboard','transcript'];
}
}
AblePlayer.prototype.getAvailablePreferences = function() {
// Return the list of currently available preferences.
// Preferences with no 'label' are set within player, not shown in Prefs dialog
var prefs = [];
// Modifier keys preferences
prefs.push({
'name': 'prefAltKey', // use alt key with shortcuts
'label': this.translate( 'prefAltKey', 'Alt' ),
'group': 'keyboard',
'default': 1
});
prefs.push({
'name': 'prefCtrlKey', // use ctrl key with shortcuts
'label': this.translate( 'prefCtrlKey', 'Control' ),
'group': 'keyboard',
'default': 1
});
prefs.push({
'name': 'prefShiftKey',
'label': this.translate( 'prefShiftKey', 'Shift' ),
'group': 'keyboard',
'default': 0
});
prefs.push({
'name': 'prefNoKeyShortcuts',
'label': this.translate( 'prefNoKeyShortcuts', 'Disable Keyboard Shortcuts' ),
'group': 'keyboard',
'default': 0
});
// Transcript preferences
prefs.push({
'name': 'prefTranscript', // transcript default state
'label': null,
'group': 'transcript',
'default': 0 // off because turning it on has a certain WOW factor
});
prefs.push({
'name': 'prefHighlight', // highlight transcript as media plays
'label': this.translate( 'prefHighlight', 'Highlight transcript as media plays' ),
'group': 'transcript',
'default': 1 // on because many users can benefit
});
prefs.push({
'name': 'prefAutoScrollTranscript',
'label': null,
'group': 'transcript',
'default': 1
});
prefs.push({
'name': 'prefTabbable', // tab-enable transcript
'label': this.translate( 'prefTabbable', 'Keyboard-enable transcript' ),
'group': 'transcript',
'default': 0 // off because if users don't need it, it impedes tabbing elsewhere on the page
});
// Caption preferences
prefs.push({
'name': 'prefCaptions', // closed captions default state
'label': null,
'group': 'captions',
'default': this.defaultStateCaptions
});
if (!this.usingYouTubeCaptions) {
/* // not supported yet
prefs.push({
'name': 'prefCaptionsStyle',
'label': this.translate( 'prefCaptionsStyle', 'Style' ),
'group': 'captions',
'default': this.translate( 'captionsStylePopOn', 'Pop-on' )
});
*/
// captions are always positioned above the player for audio
if (this.mediaType === 'video') {
prefs.push({
'name': 'prefCaptionsPosition',
'label': this.translate( 'prefCaptionsPosition', 'Position' ),
'group': 'captions',
'default': this.defaultCaptionsPosition
});
}
prefs.push({
'name': 'prefCaptionsFont',
'label': this.translate( 'prefCaptionsFont', 'Font' ),
'group': 'captions',
'default': 'sans-serif'
});
}
// This is the one option that is supported by YouTube IFrame API
prefs.push({
'name': 'prefCaptionsSize',
'label': this.translate( 'prefCaptionsSize', 'Font size' ),
'group': 'captions',
'default': '100%'
});
if (!this.usingYouTubeCaptions) {
prefs.push({
'name': 'prefCaptionsColor',
'label': this.translate( 'prefCaptionsColor', 'Text Color' ),
'group': 'captions',
'default': 'white'
});
prefs.push({
'name': 'prefCaptionsBGColor',
'label': this.translate( 'prefCaptionsBGColor', 'Background' ),
'group': 'captions',
'default': 'black'
});
prefs.push({
'name': 'prefCaptionsOpacity',
'label': this.translate( 'prefCaptionsOpacity', 'Opacity' ),
'group': 'captions',
'default': '100%'
});
}
if (this.mediaType === 'video') {
// Description preferences
prefs.push({
'name': 'prefDesc', // audio description default state
'label': null,
'group': 'descriptions',
'default': this.defaultStateDescriptions
});
prefs.push({
'name': 'prefDescMethod', // audio description default format (if both 'video' and 'text' are available)
'label': null,
'group': 'descriptions',
'default': 'video' // video (an alternative described version) always wins
});
prefs.push({
'name': 'prefDescVoice',
'label': this.translate( 'prefDescVoice', 'Voice' ),
'group': 'descriptions',
'default': null // will be set later, in injectPrefsForm()
});
prefs.push({
'name': 'prefDescPitch',
'label': this.translate( 'prefDescPitch', 'Pitch' ),
'group': 'descriptions',
'default': 1 // 0 to 2
});
prefs.push({
'name': 'prefDescRate',
'label': this.translate( 'prefDescRate', 'Rate' ),
'group': 'descriptions',
'default': 1 // 0.1 to 10 (1 is normal speech; 2 is fast but decipherable; >2 is super fast)
});
prefs.push({
'name': 'prefDescVolume',
'label': this.translate( 'volume', 'Volume' ),
'group': 'descriptions',
'default': 1 // 0 to 1
});
// Don't enable pause option if video described files in use.
if ( this.descMethod !== 'video' ) {
prefs.push({
'name': 'prefDescPause', // automatically pause when closed description starts
'label': this.translate( 'prefDescPause', 'Automatically pause video when description starts' ),
'group': 'descriptions',
'default': this.defaultDescPause
});
}
prefs.push({
'name': 'prefDescVisible', // visibly show closed description (if avilable and used)
'label': this.translate( 'prefDescVisible', 'Make description visible' ),
'group': 'descriptions',
'default': 0 // off as of 4.3.16, to avoid overloading the player with visible features
});
}
// Preferences without a category (not shown in Preferences dialogs)
prefs.push({
'name': 'prefSign', // open sign language window by default if avilable
'label': null,
'group': null,
'default': 0 // off because clicking an icon to see the sign window has a powerful impact
});
return prefs;
};
AblePlayer.prototype.loadCurrentPreferences = function () {
// Load current/default preferences into the AblePlayer object.
var available = this.getAvailablePreferences();
var preferences = this.getPref();
// Copy current preferences 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 (preferences.preferences[prefName] !== undefined) {
this[prefName] = preferences.preferences[prefName];
} else {
preferences.preferences[prefName] = defaultValue;
this[prefName] = defaultValue;
}
}
// Also load array of preferred voices from preferences
if (typeof preferences.voices !== 'undefined') {
this.prefVoices = preferences.voices;
}
this.setPrefs(preferences);
};
AblePlayer.prototype.injectPrefsForm = function (form) {
// Creates a preferences form and injects it.
// form is one of the supported forms (groups) defined in getPreferencesGroups()
var thisObj, available,
$prefsDiv, formTitle, introText, $prefsIntro,$prefsIntroP2,p3Text,$prefsIntroP3,i, j,
$fieldset, fieldsetClass, fieldsetId, $legend, legendId, thisPref, $thisDiv, thisClass,
thisId, $thisLabel, $thisField, options,$thisOption,optionValue,optionLang,optionText,
changedPref,changedSpan,changedText, currentDescState, prefDescVoice, $kbHeading,$kbList,
kbLabels,keys,kbListText,$kbListItem, dialog,$saveButton,$cancelButton,$buttonContainer;
thisObj = this;
available = this.getAvailablePreferences();
// outer container, will be assigned role="dialog"
$prefsDiv = $('<div>',{
'class': 'able-prefs-form '
});
var customClass = 'able-prefs-form-' + form;
$prefsDiv.addClass(customClass);
// add titles and intros
if (form == 'captions') {
formTitle = this.translate( 'prefTitleCaptions', 'Captions Preferences' );
} else if (form == 'descriptions') {
formTitle = this.translate( 'prefTitleDescriptions', 'Audio Description Preferences' );
$prefsIntro = $('<p>',{
text: this.translate( 'prefIntroDescription1', 'This media player supports audio description in two ways: ' )
});
var $prefsIntroUL = $('<ul>');
var $prefsIntroLI1 = $('<li>',{
text: this.translate( 'prefDescFormatOption1', 'alternative described version of video' )
});
var $prefsIntroLI2 = $('<li>',{
text: this.translate( 'prefDescFormatOption2', 'text-based description, announced by screen reader' )
});
$prefsIntroUL.append($prefsIntroLI1,$prefsIntroLI2);
if (this.hasOpenDesc && this.hasClosedDesc) {
currentDescState = this.translate( 'prefIntroDescription2', 'The current video has ' ) + ' ';
currentDescState += '<strong>' + this.translate( 'prefDescFormatOption1b', 'an alternative described version' ) + '</strong>';
currentDescState += ' <em>' + this.translate( 'and', 'and' ) + '</em> <strong>' + this.translate( 'prefDescFormatOption2b', 'text-based description, announced by screen reader' ) + '</strong>.';
} else if (this.hasOpenDesc) {
currentDescState = this.translate( 'prefIntroDescription2', 'The current video has ' );
currentDescState += ' <strong>' + this.translate( 'prefDescFormatOption1b', 'an alternative described version' ) + '</strong>.';
} else if (this.hasClosedDesc) {
currentDescState = this.translate( 'prefIntroDescription2', 'The current video has ' );
currentDescState += ' <strong>' + this.translate( 'prefDescFormatOption2b', 'text-based description, announced by screen reader' ) + '</strong>.';
} else {
currentDescState = this.translate( 'prefIntroDescriptionNone', 'The current video has no audio description in either format.' );
}
$prefsIntroP2 = $('<p>',{
html: currentDescState
});
p3Text = this.translate( 'prefIntroDescription3', 'Use the following form to set your preferences related to text-based audio description.' );
if (this.hasOpenDesc || this.hasClosedDesc) {
p3Text += ' ' + this.translate( 'prefIntroDescription4', 'After you save your settings, audio description can be toggled on/off using the Description button.' );
}
$prefsIntroP3 = $('<p>',{
text: p3Text
});
$prefsDiv.append( $prefsIntro, $prefsIntroUL, $prefsIntroP2, $prefsIntroP3 );
} else if (form == 'keyboard') {
formTitle = this.translate( 'prefTitleKeyboard', 'Keyboard Preferences' );
introText = this.translate( 'prefIntroKeyboard1', 'The media player on this web page can be operated from anywhere on the page using keyboard shortcuts (see below for a list).' );
introText += ' ' + this.translate( 'prefIntroKeyboard2', 'Modifier keys (Shift, Alt, and Control) can be assigned below.' );
introText += ' ' + this.translate( 'prefIntroKeyboard3', 'NOTE: Some key combinations might conflict with keys used by your browser and/or other software applications. Try various combinations of modifier keys to find one that works for you.' );
$prefsIntro = $('<p>',{
text: introText
});
$prefsDiv.append($prefsIntro);
} else if (form == 'transcript') {
formTitle = this.translate( 'prefTitleTranscript', 'Transcript Preferences' );
}
$fieldset = $('<div>').attr('role','group');
fieldsetClass = 'able-prefs-' + form;
fieldsetId = this.mediaId + '-prefs-' + form;
legendId = fieldsetId + '-legend';
$fieldset.addClass(fieldsetClass).attr('id',fieldsetId);
if (form === 'keyboard') {
$legend = $('<h2>' + this.translate( 'prefHeadingKeyboard1', 'Modifier keys used for shortcuts' ) + '</h2>');
$legend.attr('id',legendId);
$fieldset.attr('aria-labelledby',legendId);
$fieldset.append($legend);
} else if (form === 'descriptions') {
$legend = $('<h2>' + this.translate( 'prefHeadingTextDescription', 'Text-based audio description' ) + '</h2>');
$legend.attr('id',legendId);
$fieldset.attr('aria-labelledby',legendId);
$fieldset.append($legend);
}
for (i=0; i<available.length; i++) {
// only include prefs on the current form if they have a label
if ((available[i]['group'] == form) && available[i]['label']) {
thisPref = available[i]['name'];
thisClass = 'able-' + thisPref;
thisId = this.mediaId + '_' + thisPref;
$thisDiv = $('<div>').addClass(thisClass);
if (form === 'captions') {
$thisLabel = $('<label for="' + thisId + '"> ' + available[i]['label'] + '</label>');
$thisField = $('<select>',{
name: thisPref,
id: thisId,
});
if (thisPref !== 'prefCaptions' && thisPref !== 'prefCaptionsStyle') {
// add a change handler that updates the style of the sample caption text
$thisField.on( 'change', function() {
changedPref = $(this).attr('name');
thisObj.stylizeCaptions(thisObj.$sampleCapsDiv,changedPref);
});
}
options = this.getCaptionsOptions(thisPref);
for (j=0; j < options.length; j++) {
if (thisPref === 'prefCaptionsPosition') {
optionValue = options[j];
if (optionValue === 'overlay') {
optionText = this.translate( 'captionsPositionOverlay', 'Overlay' );
} else if (optionValue === 'below') {
optionValue = options[j];
optionText = this.translate( 'captionsPositionBelow', 'Below video' );
}
} else if (thisPref === 'prefCaptionsFont' || thisPref === 'prefCaptionsColor' || thisPref === 'prefCaptionsBGColor') {
optionValue = options[j][0];
optionText = options[j][1];
} else if (thisPref === 'prefCaptionsOpacity') {
optionValue = options[j];
optionText = options[j];
optionText += (optionValue === '0%') ? ' (' + this.translate( 'transparent', 'transparent' ) + ')' : ' (' + this.translate( 'solid', 'solid' ) + ')';
} else {
optionValue = options[j];
optionText = options[j];
}
$thisOption = $('<option>',{
value: optionValue,
text: optionText
});
if (this[thisPref] === optionValue) {
$thisOption.prop('selected',true);
}
$thisField.append($thisOption);
}
$thisDiv.append($thisLabel,$thisField);
} else if (form === 'descriptions') {
$thisLabel = $('<label for="' + thisId + '"> ' + available[i]['label'] + '</label>');
if (thisPref === 'prefDescPause' || thisPref === 'prefDescVisible') {
// these preferences are checkboxes
$thisDiv.addClass('able-prefs-checkbox');
$thisField = $('<input>',{
type: 'checkbox',
name: thisPref,
id: thisId,
value: 'true'
});
// check current active value for this preference
if (this[thisPref] === 1) {
$thisField.prop('checked',true);
}
$thisDiv.append($thisField,$thisLabel);
} else if (this.synth) {
// Only show these options if browser supports speech synthesis
$thisDiv.addClass('able-prefs-select');
$thisField = $('<select>',{
name: thisPref,
id: thisId,
});
if (thisPref === 'prefDescVoice' && this.descVoices.length) {
prefDescVoice = this.getPrefDescVoice();
for (j=0; j < this.descVoices.length; j++) {
optionValue = this.descVoices[j].name;
optionLang = this.descVoices[j].lang.substring(0,2).toLowerCase();
optionText = optionValue + ' (' + this.descVoices[j].lang + ')';
$thisOption = $('<option>',{
'value': optionValue,
'data-lang': optionLang,
text: optionText
});
if (prefDescVoice === optionValue) {
$thisOption.prop('selected',true);
}
$thisField.append($thisOption);
}
this.$voiceSelectField = $thisField;
} else {
if (thisPref == 'prefDescPitch') { // 0 to 2
options = [0,0.5,1,1.5,2];
} else if (thisPref == 'prefDescRate') { // 0.1 to 10
// Tests with a variety of voices on MacOS and Windows
// yielded the following choices that seem reasonable for audio description:
// 0.5 - too slow (exclude this)
// 0.7 - casual
// 0.8 - add this
// 0.9 - add this
// 1 - normal
// 1.1 - add this
// 1.2 - add this
// 1.5 - quick
// 2 - speedy
// 2.5 - fleet
// 3 - fast! (some voices don't get any faster than this
// Note: if these values are modified, must also modfiy them
// in makePrefsValueReadable()
options = [0.7,0.8,0.9,1,1.1,1.2,1.5,2,2.5,3];
} else if (thisPref == 'prefDescVolume') { // 0 (mute) to 1
options = [0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1];
}
if (typeof options !== 'undefined') {
for (j=0; j < options.length; j++) {
optionValue = options[j];
optionText = this.makePrefsValueReadable(thisPref,optionValue);
$thisOption = $('<option>',{
value: optionValue,
text: optionText
});
if (this[thisPref] == optionValue) {
$thisOption.prop('selected',true);
}
$thisField.append($thisOption);
$thisDiv.append($thisLabel,$thisField);
}
}
}
// add a change handler that announces the sample description text
$thisField.on('change',function() {
thisObj.announceDescriptionText('sample',thisObj.currentSampleText);
});
$thisDiv.append($thisLabel,$thisField);
}
} else { // all other fields are checkboxes
$thisLabel = $('<label for="' + thisId + '"> ' + available[i]['label'] + '</label>');
$thisField = $('<input>',{
type: 'checkbox',
name: thisPref,
id: thisId,
value: 'true'
});
// check current active value for this preference
if (this[thisPref] === 1) {
$thisField.prop('checked',true);
}
if (form === 'keyboard') {
// add a change handler that updates the list of current keyboard shortcuts
$thisField.on('change',function() {
changedPref = $(this).attr('name');
if (changedPref === 'prefAltKey') {
changedSpan = '.able-modkey-alt';
changedText = thisObj.tt.prefAltKey + ' + ';
} else if (changedPref === 'prefCtrlKey') {
changedSpan = '.able-modkey-ctrl';
changedText = thisObj.tt.prefCtrlKey + ' + ';
} else if (changedPref === 'prefShiftKey') {
changedSpan = '.able-modkey-shift';
changedText = thisObj.tt.prefShiftKey + ' + ';
}
if ( changedPref !== 'prefNoKeyShortcuts' ) {
if ($(this).is(':checked')) {
$(changedSpan).text(changedText);
} else {
$(changedSpan).text('');
}
} else {
if ($(this).is(':checked')) {
$('.able-modkey-item').addClass('hidden');
} else {
$('.able-modkey-item').removeClass('hidden');
}
}
});
}
$thisDiv.append($thisField,$thisLabel);
}
if (thisPref === 'prefDescVoice' && !this.descVoices.length) {
// No voices are available (e.g., in Safari 15.4 on Mac OS)
} else {
$fieldset.append($thisDiv);
}
}
}
$prefsDiv.append($fieldset);
if (form === 'captions') {
// add a sample closed caption div to prefs dialog
// do not show this for YouTube captions, since it's not an accurate reflection
if (!this.usingYouTubeCaptions) {
this.$sampleCapsDiv = $('<div>',{
'class': 'able-captions-sample'
}).text( this.translate( 'sampleCaptionText', 'Sample caption text' ) );
$prefsDiv.append(this.$sampleCapsDiv);
this.stylizeCaptions(this.$sampleCapsDiv);
}
} else if (form === 'descriptions') {
if (this.synth) {
// add a div with sample audio description text
this.$sampleDescDiv = $('<div>',{
'class': 'able-desc-sample'
}).text( this.translate( 'sampleDescriptionText', 'Adjust settings to hear this sample text.' ) );
$prefsDiv.append(this.$sampleDescDiv);
this.currentSampleText = this.translate( 'sampleDescriptionText', 'Adjust settings to hear this sample text.' );
}
} else if (form === 'keyboard') {
let shortcutClass = (this.prefNoKeyShortcuts === 1 ) ? 'able-modkey-item hidden' : 'able-modkey-item';
// add a current list of keyboard shortcuts
$kbHeading = $('<h2>',{
text: this.translate( 'prefHeadingKeyboard2', 'Current keyboard shortcuts' )
});
$kbList = $('<ul>');
// create arrays of kbLabels and keys
kbLabels = [];
keys = [];
for (i=0; i<this.controls.length; i++) {
if (this.controls[i] === 'play') {
kbLabels.push( this.translate( 'play', 'Play' ) + '/' + this.translate( 'pause', 'Pause' ) );
keys.push('p</span> <em>' + this.translate( 'or', 'or' ) + '</em> <span class="able-help-modifiers"> ' + this.translate( 'spacebar', 'spacebar' ));
} else if (this.controls[i] === 'restart') {
kbLabels.push(this.translate( 'restart', 'Restart' ));
keys.push('s');
} else if (this.controls[i] === 'previous') {
kbLabels.push( this.translate( 'prevTrack', 'Previous track' ) );
keys.push('b'); // b = back
} else if (this.controls[i] === 'next') {
kbLabels.push( this.translate( 'nextTrack', 'Next track' ) );
keys.push('n');
} else if (this.controls[i] === 'rewind') {
kbLabels.push(this.translate( 'rewind', 'Rewind' ));
keys.push('r');
} else if (this.controls[i] === 'forward') {
kbLabels.push(this.translate( 'forward', 'Forward' ));
keys.push('f');
} else if (this.controls[i] === 'volume') {
kbLabels.push(this.translate( 'volume', 'Volume' ));
keys.push('v</span> <em>' + this.translate( 'or', 'or' ) + '</em> <span class="able-modkey">1-9');
// mute toggle
kbLabels.push(this.translate( 'mute', 'Mute' ) + '/' + this.translate( 'unmute', 'Unmute' ));
keys.push('m');
} else if (this.controls[i] === 'captions') {
if (this.captions.length > 1) {
// caption button launches a Captions popup menu
kbLabels.push(this.translate( 'captions', 'Captions' ));
} else {
// there is only one caption track
// therefore caption button is a toggle
if (this.captionsOn) {
kbLabels.push(this.translate( 'hideCaptions', 'Hide captions' ));
} else {
kbLabels.push(this.translate( 'showCaptions', 'Show captions' ));
}
}
keys.push('c');
} else if (this.controls[i] === 'descriptions') {
if (this.descOn) {
kbLabels.push(this.translate( 'turnOffDescriptions', 'Turn off descriptions' ));
} else {
kbLabels.push(this.translate( 'turnOnDescriptions', 'Turn on descriptions' ));
}
keys.push('d');
} else if (this.controls[i] === 'prefs') {
kbLabels.push(this.translate( 'preferences', 'Preferences' ));
keys.push('e');
}
}
for (i=0; i<keys.length; i++) {
// alt
kbListText = '<span class="able-modkey-alt">';
if (this.prefAltKey === 1) {
kbListText += this.translate( 'prefAltKey', 'Alt' ) + ' + ';
}
kbListText += '</span>';
// ctrl
kbListText += '<span class="able-modkey-ctrl">';
if (this.prefCtrlKey === 1) {
kbListText += this.translate( 'prefCtrlKey', 'Control' ) + ' + ';
}
kbListText += '</span>';
// shift
kbListText += '<span class="able-modkey-shift">';
if (this.prefShiftKey === 1) {
kbListText += this.translate( 'prefShiftKey', 'Shift' ) + ' + ';
}
kbListText += '</span>';
kbListText += '<span class="able-modkey">' + keys[i] + '</span>';
kbListText += ' = ' + kbLabels[i];
$kbListItem = $('<li>',{
'class': shortcutClass,
html: kbListText,
});
$kbList.append($kbListItem);
}
// add Escape key
kbListText = '<span class="able-modkey">' + this.translate( 'escapeKey', 'Escape' ) + '</span>';
kbListText += ' = ' + this.translate( 'escapeKeyFunction', 'Close current dialog or popup menu' );
$kbListItem = $('<li>',{
html: kbListText
});
$kbList.append($kbListItem);
// put it all together
$prefsDiv.append($kbHeading,$kbList);
}
// $prefsDiv (dialog) must be appended to the BODY!
$('body').append($prefsDiv);
dialog = new AccessibleDialog(
$prefsDiv,
this.$prefsButton,
formTitle,
thisObj.tt.closeButtonLabel
);
// Add save and cancel buttons.
$buttonContainer = $( '<div class="able-prefs-buttons"></div>' );
$saveButton = $('<button class="modal-button">' + this.translate( 'save', 'Save' ) + '</button>');
$cancelButton = $('<button class="modal-button">' + this.translate( 'cancel', 'Cancel' ) + '</button>');
$saveButton.on( 'click', function () {
dialog.hide();
thisObj.savePrefsFromForm();
});
$cancelButton.on( 'click', function () {
dialog.hide();
thisObj.resetPrefsForm();
});
$buttonContainer.append( $saveButton,$cancelButton );
$prefsDiv.append($buttonContainer);
// Associate the dialog's H1 as aria-labelledby for groups of fields
// (alternative to fieldset and legend)
if (form === 'captions' || form === 'transcript') {
$fieldset.attr('aria-labelledby',dialog.titleH1.attr('id'));
}
// add global reference for future control
if (form === 'captions') {
this.captionPrefsDialog = dialog;
} else if (form === 'descriptions') {
this.descPrefsDialog = dialog;
} else if (form === 'keyboard') {
this.keyboardPrefsDialog = dialog;
} else if (form === 'transcript') {
this.transcriptPrefsDialog = dialog;
}
// Add click handler for dialog close button
// (button is added in dialog.js)
$('div.able-prefs-form button.modalCloseButton').on( 'click', function() {
thisObj.resetPrefsForm();
})
// Add handler for escape key
$('div.able-prefs-form').on( 'keydown', function(e) {
if (e.key === 'Escape') {
thisObj.resetPrefsForm();
}
});
};
AblePlayer.prototype.getPrefDescVoice = function () {
// return user's preferred voice for the current language from preferences.voices
var lang, preferences, i;
if (this.selectedDescriptions) {
lang = this.selectedDescriptions.language;
} else if (this.captionLang) {
lang = this.captionLang;
} else {
lang = this.lang;
}
preferences = this.getPref();
if (preferences.voices) {
for (i=0; i < preferences.voices.length; i++) {
if (preferences.voices[i].lang === lang) {
return preferences.voices[i].name;
}
}
}
return null; // user has no saved preference
}
AblePlayer.prototype.rebuildDescPrefsForm = function () {
// Called if this.descVoices changes, which may happen if:
// getBrowserVoices() succeeds after an earlier failure
// user changes language of captions/subtitles and descVoices changes to match the new language
var i, optionValue, optionText, $thisOption;
this.$voiceSelectField = $('#' + this.mediaId + '_prefDescVoice');
this.$voiceSelectField.empty();
for (i=0; i < this.descVoices.length; i++) {
optionValue = this.descVoices[i].name;
optionText = optionValue + ' (' + this.descVoices[i].lang + ')';
$thisOption = $('<option>',{
'value': optionValue,
'data-lang': this.descVoices[i].lang.substring(0,2).toLowerCase(),
text: optionText
});
if (this.prefDescVoice == optionValue) {
$thisOption.prop('selected',true);
}
this.$voiceSelectField.append($thisOption);
}
};
AblePlayer.prototype.makePrefsValueReadable = function(pref,value) {
// The values for pitch, rate, and volume (web speech API)
// are strange and inconsistent between variables
// this function returns text that is more readable than the values themselves
if (pref === 'prefDescPitch') {
if (value === 0) {
return this.translate( 'prefDescPitch1', 'Very low' );
} else if (value === 0.5) {
return this.translate( 'prefDescPitch2', 'Low' );
} else if (value === 1) {
return this.translate( 'prefDescPitch3', 'Default' );
} else if (value === 1.5) {
return this.translate( 'prefDescPitch4', 'High' );
} else if (value === 2) {
return this.translate( 'prefDescPitch5', 'Very high' );
}
} else if (pref === 'prefDescRate') {
// default in the API is 0.1 to 10, where 1 is normal speaking voice
// our custom range offers several rates close to 1
// plus a couple of crazy fast ones for sport
// Our more readable options (1-10) or mapped here to API values
if (value === 0.7) {
return 1;
} else if (value === 0.8) {
return 2;
} else if (value === 0.9) {
return 3;
} else if (value === 1) {
return 4;
} else if (value === 1.1) {
return 5;
} else if (value === 1.2) {
return 6;
} else if (value === 1.5) {
return 7;
} else if (value === 2) {
return 8;
} else if (value === 2.5) {
return 9;
} else if (value === 3) {
return 10;
}
} else if (pref === 'prefDescVolume') {
// values range from 0.1 to 1.0
return value * 10;
}
return value;
};
AblePlayer.prototype.resetPrefsForm = function () {
// Reset preferences form with default values from preferences
// Called when:
// User clicks cancel or close button in Prefs Dialog
// User presses Escape to close Prefs dialog
// User clicks Save in Prefs dialog, & there's more than one player on page
var preferences, available, i, prefName;
preferences = this.getPref();
available = this.getAvailablePreferences();
for (i=0; i<available.length; i++) {
prefName = available[i]['name'];
if ((prefName.indexOf('Captions') !== -1) && (prefName !== 'prefCaptions')) {
// this is a caption-related select box
$('select[name="' + prefName + '"]').val(preferences.preferences[prefName]);
} else { // all others are checkboxes
if (this[prefName] === 1) {
$('input[name="' + prefName + '"]').prop('checked',true);
} else {
$('input[name="' + prefName + '"]').prop('checked',false);
}
}
}
// also restore style of sample caption div
this.stylizeCaptions(this.$sampleCapsDiv);
};
AblePlayer.prototype.savePrefsFromForm = function () {
// Return a prefs object constructed from the form.
// called when user saves the Preferences form
// update preferences with new value
var preferences, available, prefName, prefId,
voiceSelectId, newVoice, numChanges, voiceLangFound,
numCapChanges, capSizeChanged, capSizeValue, newValue;
numChanges = 0;
numCapChanges = 0; // changes to caption-style-related preferences
capSizeChanged = false;
preferences = this.getPref();
available = this.getAvailablePreferences();
for (var i=0; i < available.length; i++) {
// only prefs with labels are used in the Prefs form
if (available[i]['label']) {
prefName = available[i]['name'];
prefId = this.mediaId + '_' + prefName;
if (prefName === 'prefDescVoice') {
if (typeof preferences.voices === 'undefined') {
preferences.voices = [];
}
voiceSelectId = this.mediaId + '_prefDescVoice';
this.prefDescVoice = $('select#' + voiceSelectId).find(':selected').val();
this.prefDescVoiceLang = $('select#' + voiceSelectId).find(':selected').attr('data-lang');
// replace preferred voice for this lang in preferences.voices array, if one exists
// otherwise, add it to the array
voiceLangFound = false;
for (var v=0; v < preferences.voices.length; v++) {
if (preferences.voices[v].lang === this.prefDescVoiceLang) {
voiceLangFound = true;
preferences.voices[v].name = this.prefDescVoice;
}
}
if (!voiceLangFound) {
// no voice has been saved yet for this language. Add it to array.
newVoice = {'name':this.prefDescVoice, 'lang':this.prefDescVoiceLang};
preferences.voices.push(newVoice);
}
numChanges++;
} else if (prefName == 'prefDescMethod') {
// As of v4.0.10, prefDescMethod is no longer a choice
// this.prefDescMethod = $('input[name="' + prefName + '"]:checked').val();
this.prefDescMethod = 'video';
if (this.prefDescMethod !== preferences.preferences['prefDescMethod']) { // user's preference has changed
preferences.preferences['prefDescMethod'] = this.prefDescMethod;
numChanges++;
}
} else if ((prefName.indexOf('Captions') !== -1) && (prefName !== 'prefCaptions')) {
// this is one of the caption-related select fields
newValue = $('select[id="' + prefId + '"]').val();
if (preferences.preferences[prefName] !== newValue) { // user changed setting
preferences.preferences[prefName] = newValue;
// also update global var for this pref (for caption fields, not done elsewhere)
this[prefName] = newValue;
numChanges++;
numCapChanges++;
}
if (prefName === 'prefCaptionsSize') {
capSizeChanged = true;
capSizeValue = newValue;
}
} else if ((prefName.indexOf('Desc') !== -1) && (prefName !== 'prefDescPause') && prefName !== 'prefDescVisible') {
// this is one of the description-related select fields
newValue = $('select[id="' + prefId + '"]').val();
if (preferences.preferences[prefName] !== newValue) { // user changed setting
preferences.preferences[prefName] = newValue;
// also update global var for this pref
this[prefName] = newValue;
numChanges++;
}
} else { // all other fields are checkboxes
if ($('input[id="' + prefId + '"]').is(':checked')) {
preferences.preferences[prefName] = 1;
if (this[prefName] === 1) {