forked from fluid-project/videoPlayer
/
VideoPlayer_controllers.js
487 lines (445 loc) · 18.9 KB
/
VideoPlayer_controllers.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
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
/*
Copyright 2009 University of Toronto
Copyright 2011 Charly Molter
Copyright 2011-2012 OCAD University
Licensed under the Educational Community License (ECL), Version 2.0 or the New
BSD license. You may not use this file except in compliance with one these
Licenses.
You may obtain a copy of the ECL 2.0 License and BSD License at
https://github.com/fluid-project/infusion/raw/master/Infusion-LICENSE.txt
*/
/*global jQuery, window, fluid*/
// JSLint options
/*jslint white: true, funcinvoke: true, undef: true, newcap: true, nomen: true, regexp: true, bitwise: true, browser: true, forin: true, maxerr: 100, indent: 4 */
(function ($) {
/**
* controllers is a video controller containing a play button, a time scrubber,
* a volume controller, a button to put captions on/off
* , a button to put transcripts on/off
*
* @param {Object} container the container which this component is rooted
* @param {Object} options configuration options for the component
*/
//add all the modelChanged listener to the applier
// TODO: Privacy is inherited. Consider making this public
var bindControllerModel = function (that) {
that.applier.modelChanged.addListener("canPlay", function () {
that.locate("play").attr("disabled", !that.model.canPlay);
that.locate("fullscreen").attr("disabled", !that.model.canPlay);
});
};
fluid.defaults("fluid.videoPlayer.controllers", {
gradeNames: ["fluid.viewComponent", "autoInit"],
components: {
scrubber: {
type: "fluid.videoPlayer.controllers.scrubber",
container: "{controllers}.dom.scrubberContainer",
options: {
model: "{controllers}.model",
applier: "{controllers}.applier",
events: {
onScrub: "{controllers}.events.onScrub",
afterScrub: "{controllers}.events.afterScrub",
onStartScrub: "{controllers}.events.onStartScrub"
}
}
},
volumeControl: {
type: "fluid.videoPlayer.controllers.volumeControls",
container: "{controllers}.dom.volumeContainer",
options: {
model: "{controllers}.model",
applier: "{controllers}.applier"
}
},
captionControls: {
type: "fluid.videoPlayer.controllers.languageControls",
container: "{controllers}.dom.captionControlsContainer",
options: {
languages: "{controllers}.options.captions",
model: "{controllers}.model",
applier: "{controllers}.applier",
showHidePath: "displayCaptions",
currentLanguagePath: "currentTracks.captions",
selectors: {
button: ".flc-videoPlayer-captions-button",
menu: ".flc-videoPlayer-captions-languageMenu"
},
styles: {
button: "fl-videoPlayer-captions-button",
buttonWithShowing: "fl-videoPlayer-captions-button-on"
},
strings: {
showLanguage: "Show Captions",
hideLanguage: "Hide Captions",
press: "Captions",
release: "Captions"
}
}
},
transcriptControls: {
type: "fluid.videoPlayer.controllers.languageControls",
container: "{controllers}.dom.transcriptControlsContainer",
options: {
languages: "{controllers}.options.transcripts",
model: "{controllers}.model",
applier: "{controllers}.applier",
showHidePath: "displayTranscripts",
currentLanguagePath: "currentTracks.transcripts",
selectors: {
button: ".flc-videoPlayer-transcripts-button",
menu: ".flc-videoPlayer-transcripts-languageMenu"
},
styles: {
button: "fl-videoPlayer-transcripts-button",
buttonWithShowing: "fl-videoPlayer-transcripts-button-on"
},
strings: {
showLanguage: "Show Transcripts",
hideLanguage: "Hide Transcripts",
press: "Transcripts",
release: "Transcripts"
}
}
},
playButton: {
type: "fluid.toggleButton",
container: "{controllers}.container",
options: {
selectors: {
button: ".flc-videoPlayer-play"
},
styles: {
init: "fl-videoPlayer-play",
pressed: "fl-videoPlayer-playing"
},
// TODO: Strings should be moved out into a single top-level bundle (FLUID-4590)
strings: {
press: "Play",
release: "Pause"
},
model: "{controllers}.model",
modelPath: "play",
ownModel: false,
applier: "{controllers}.applier"
}
},
fullScreenButton: {
type: "fluid.toggleButton",
container: "{controllers}.container",
options: {
selectors: {
button: ".flc-videoPlayer-fullscreen"
},
styles: {
init: "fl-videoPlayer-fullscreen",
pressed: "fl-videoPlayer-fullscreen-on"
},
// TODO: Strings should be moved out into a single top-level bundle (FLUID-4590)
strings: {
press: "Full screen",
release: "Exit full screen mode"
},
model: "{controllers}.model",
modelPath: "fullscreen",
ownModel: false,
applier: "{controllers}.applier"
}
}
},
finalInitFunction: "fluid.videoPlayer.controllers.finalInit",
events: {
onControllersReady: null,
onStartTimeChange: null,
onTimeChange: null,
afterTimeChange: null,
onMarkupReady: null
},
selectors: {
play: ".flc-videoPlayer-play",
scrubberContainer: ".flc-videoPlayer-scrubberContainer",
volumeContainer: ".flc-videoPlayer-volumeContainer",
captionControlsContainer: ".flc-videoPlayer-captionControls-container",
transcriptControlsContainer: ".flc-videoPlayer-transcriptControls-container",
fullscreen: ".flc-videoPlayer-fullscreen"
},
styles: {
fullscreenOn: "fl-videoPlayer-state-fullscreenOn",
fullscreenOff: "fl-videoPlayer-state-fullscreenOff",
fullscreenIcon: "ui-icon-extlink",
captionIcon: "ui-icon-comment",
transcriptIcon: "ui-icon-comment"
}
});
fluid.videoPlayer.controllers.finalInit = function (that) {
bindControllerModel(that);
that.events.onControllersReady.fire(that);
};
/********************************************
* scrubber: a slider to follow the progress *
* of the video *
********************************************/
// TODO: Privacy is inherited. Consider making this public
//change the text of the selected time
var updateTime = function (that, element) {
var time = that.locate(element);
time.text(fluid.videoPlayer.formatTime(that.model[element]));
};
// TODO: Privacy is inherited. Consider making this public
var bindScrubberDOMEvents = function (that) {
// Bind the scrubbers slide event to change the video's time.
var scrubber = that.locate("scrubber");
scrubber.bind({
"slidestart": function (evt, ui) {
that.events.onStartScrub.fire(ui.value);
},
"slide": function (evt, ui) {
that.events.onScrub.fire(ui.value);
},
"slidestop": function (evt, ui) {
that.events.afterScrub.fire(ui.value);
}
});
};
// TODO: This function is inherited. Consider making this public
var bindScrubberModel = function (that) {
// Setup the scrubber when we know the duration of the video.
that.applier.modelChanged.addListener("startTime", that.updateMin);
that.applier.modelChanged.addListener("startTime", that.updateMax);
that.applier.modelChanged.addListener("totalTime", that.updateMax);
// Bind to the video's timeupdate event so we can programmatically update the slider.
that.applier.modelChanged.addListener("currentTime", that.updateCurrent);
that.applier.modelChanged.addListener("buffered", that.updateBuffered);
that.applier.modelChanged.addListener("canPlay", function () {
var scrubber = that.locate("scrubber");
if (that.model.canPlay === true) {
scrubber.slider("enable");
} else {
scrubber.slider("disable");
}
});
};
// TODO: Privacy is inherited. Consider making this public
var createScrubberMarkup = function (that) {
var scrubber = that.locate("scrubber");
scrubber.slider({
unittext: "seconds",
range: "min",
disabled: true
}).attr({
"role": "slider"
});
// TODO: This in inherited. Do we need to add aria to sliders ourselves?
that.locate("handle").attr({
"aria-label": that.options.strings.scrubber,
"aria-valuemin": 0,
"aria-valuemax": 0,
"aria-valuenow": 0,
"aria-valuetext": 0
});
return scrubber;
};
fluid.defaults("fluid.videoPlayer.controllers.scrubber", {
gradeNames: ["fluid.viewComponent", "autoInit"],
finalInitFunction: "fluid.videoPlayer.controllers.scrubber.finalInit",
postInitFunction: "fluid.videoPlayer.controllers.scrubber.postInit",
events: {
afterScrub: null,
onScrub: null,
onScrubberReady: null,
onStartScrub: null
},
selectors: {
totalTime: ".flc-videoPlayer-total",
currentTime: ".flc-videoPlayer-current",
scrubber: ".flc-videoPlayer-scrubber",
handle: ".ui-slider-handle"
},
// TODO: Strings should be moved out into a single top-level bundle (FLUID-4590)
strings: {
scrubber: "Time scrub"
}
});
fluid.videoPlayer.controllers.scrubber.postInit = function (that) {
// TODO: these methods should be public functions, since people might like to alter them
// (inherited code)
that.updateMin = function () {
var startTime = that.model.startTime || 0;
var scrubber = that.locate("scrubber");
scrubber.slider("option", "min", startTime + that.model.currentTime);
that.locate("handle").attr({
"aria-valuemin": startTime + that.model.currentTime
});
};
that.updateMax = function () {
updateTime(that, "totalTime");
var scrubber = that.locate("scrubber");
scrubber.slider("option", "max", that.model.totalTime);
that.locate("handle").attr({
"aria-valuemax": that.model.totalTime
});
};
that.updateCurrent = function () {
updateTime(that, "currentTime");
var scrubber = that.locate("scrubber");
scrubber.slider("value", that.model.currentTime);
that.locate("handle").attr({
"aria-valuenow": that.model.currentTime,
"aria-valuetext": fluid.videoPlayer.formatTime(that.model.currentTime) + " of " + fluid.videoPlayer.formatTime(that.model.totalTime)
});
};
that.updateBuffered = function () {
var startTime = that.model.startTime || 0;
var lastBufferedTime = that.model.buffered.end(that.model.buffered.length - 1);
var scrubber = that.locate("scrubber");
scrubber.slider("option", "max", lastBufferedTime);
that.locate("handle").attr({
"aria-valuemax": lastBufferedTime
});
};
};
fluid.videoPlayer.controllers.scrubber.finalInit = function (that) {
createScrubberMarkup(that);
bindScrubberDOMEvents(that);
bindScrubberModel(that);
that.events.onScrubberReady.fire();
};
/********************************************************
* Volume Control: a button that turns into a slider *
* To control the volume *
*********************************************************/
// TODO: Privacy is inherited. Consider making this public
var bindVolumeDOMEvents = function (that) {
// Bind the volume Control slide event to change the video's volume and its image.
that.locate("volumeControl").bind("slide", function (evt, ui) {
fluid.fireSourcedChange(that.applier, "volume", ui.value, "slider");
});
that.locate("volumeControl").bind("slidechange", function (evt, ui) {
fluid.fireSourcedChange(that.applier, "volume", ui.value, "slider");
});
};
// TODO: Privacy is inherited. Consider making this public
var bindVolumeModel = function (that) {
fluid.addSourceGuardedListener(that.applier.modelChanged,
"volume", "slider", that.updateVolume);
that.applier.modelChanged.addListener("canPlay", function () {
that.locate("mute").attr("disabled", !that.model.canPlay);
});
};
// TODO: Privacy is inherited. Consider making this public
var setUpVolumeControls = function (that) {
var volumeControl = that.locate("volumeControl");
volumeControl.addClass(that.options.styles.volumeControl);
volumeControl.slider({
orientation: "vertical",
range: "min",
min: that.model.minVolume,
max: that.model.maxVolume,
value: that.model.volume
});
// TODO: This in inherited. Do we need to add aria to sliders ourselves?
that.locate("handle").attr({
"aria-label": that.options.strings.volume,
"aria-valuemin": that.model.minVolume,
"aria-valuemax": that.model.maxVolume,
"aria-valuenow": that.model.volume,
"aria-valuetext": that.model.volume + "%",
"role": "slider"
});
fluid.tabindex(that.container, 0);
fluid.tabindex(that.locate("mute"), -1);
fluid.tabindex(volumeControl, -1);
fluid.tabindex(that.locate("handle"), -1);
fluid.activatable(that.container, function (evt) {
that.muteButton.events.onPress.fire();
});
// TODO: This will be converted to use the activatable plugin
// as part of FLUID-4552
that.container.keydown(function (evt) {
var volumeControl = that.locate("volumeControl");
var code = evt.which ? evt.which : evt.keyCode;
if ((code === $.ui.keyCode.UP) || (code === $.ui.keyCode.RIGHT)) {
volumeControl.slider("value", volumeControl.slider("value") + 1);
} else if ((code === $.ui.keyCode.DOWN) || (code === $.ui.keyCode.LEFT)) {
volumeControl.slider("value", volumeControl.slider("value") - 1);
} else {
return true;
}
return false;
});
};
fluid.defaults("fluid.videoPlayer.controllers.volumeControls", {
gradeNames: ["fluid.viewComponent", "autoInit"],
postInitFunction: "fluid.videoPlayer.controllers.volumeControls.postInit",
finalInitFunction: "fluid.videoPlayer.controllers.volumeControls.finalInit",
events: {
onReady: null
},
model: {
muted: false,
volume: 50,
minVolume: 0,
maxVolume: 100
},
selectors: {
mute: ".flc-videoPlayer-mute",
volumeControl: ".flc-videoPlayer-volumeControl",
handle: ".ui-slider-handle"
},
styles: {
mute: "fl-videoPlayer-mute",
volumeControl: "fl-videoPlayer-volumeControl",
buttonIcon: "ui-icon-signal"
},
// TODO: Strings should be moved out into a single top-level bundle (FLUID-4590)
strings: {
volume: "Volume"
},
components: {
muteButton: {
type: "fluid.toggleButton",
options: {
selectors: {
button: ".flc-videoPlayer-mute"
},
styles: {
init: "fl-videoPlayer-mute",
pressed: "fl-videoPlayer-muted"
},
// TODO: Strings should be moved out into a single top-level bundle (FLUID-4590)
strings: {
press: "Mute",
release: "Un-mute"
},
model: "{volumeControls}.model",
modelPath: "muted",
applier: "{volumeControls}.applier"
}
}
}
});
fluid.videoPlayer.controllers.volumeControls.postInit = function (that) {
that.options.components.muteButton.container = that.container;
that.showVolumeControl = function () {
that.locate("volumeControl").show();
};
that.hideVolumeControl = function () {
that.locate("volumeControl").hide();
};
that.updateVolume = function () {
var volume = that.model.volume;
var volumeControl = that.locate("volumeControl");
volumeControl.slider("value", volume);
that.locate("handle").attr({
"aria-valuenow": volume,
"aria-valuetext": Math.round(volume) + "%"
});
};
};
fluid.videoPlayer.controllers.volumeControls.finalInit = function (that) {
setUpVolumeControls(that);
bindVolumeDOMEvents(that);
bindVolumeModel(that);
that.events.onReady.fire(that);
};
})(jQuery);