Allow only one bookmark to be added for multiple fast starring
[chromium-blink-merge.git] / chrome / browser / resources / chromeos / chromevox / common / media_widget.js
blob7b6dd05ed893f453f0c42c4e04b399195868d5bb
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 goog.provide('cvox.ChromeVoxHTMLMediaWidget');
7 /**
8 * @fileoverview Gives the user spoken feedback as they interact with the HTML5
9 * media widgets (<video> and <audio>) + makes the widget keyboard accessible.
13 /**
14 * A class containing the information needed to speak
15 * a media element to the user.
17 * @constructor
18 * @param {Element} mediaElem The media widget element.
19 * @param {cvox.TtsInterface} tts The TTS object from ChromeVox.
21 cvox.ChromeVoxHTMLMediaWidget = function(mediaElem, tts){
22 var self = this;
23 this.mediaElem_ = mediaElem;
24 this.mediaTts_ = tts;
26 this.keyListener_ = function(evt) {
27 self.eventHandler_(evt);
29 this.blurListener_ = function(evt) {
30 self.shutdown();
33 this.mediaElem_.addEventListener('keydown', this.keyListener_, false);
34 this.mediaElem_.addEventListener('keyup', this.keyListener_, false);
35 this.mediaElem_.addEventListener('blur', this.blurListener_, false);
38 /**
39 * Removes the key listeners for the media widget.
41 cvox.ChromeVoxHTMLMediaWidget.prototype.shutdown = function() {
42 this.mediaElem_.removeEventListener('blur', this.blurListener_, false);
43 this.mediaElem_.removeEventListener('keydown', this.keyListener_, false);
44 this.mediaElem_.removeEventListener('keyup', this.keyListener_, false);
47 cvox.ChromeVoxHTMLMediaWidget.prototype.jumpToTime_ = function(targetTime) {
48 if (targetTime < 0) {
49 targetTime = 0;
51 if (targetTime > this.mediaElem_.duration) {
52 targetTime = this.mediaElem_.duration;
54 this.mediaElem_.currentTime = targetTime;
57 cvox.ChromeVoxHTMLMediaWidget.prototype.setVolume_ = function(targetVolume) {
58 if (targetVolume < 0) {
59 targetVolume = 0;
61 if (targetVolume > 1.0) {
62 targetVolume = 1.0;
64 this.mediaElem_.volume = targetVolume;
67 /**
68 * Adds basic keyboard handlers to the media widget.
70 cvox.ChromeVoxHTMLMediaWidget.prototype.eventHandler_ = function(evt) {
71 if (evt.type == 'keydown') {
72 // Space/Enter for play/pause toggle.
73 if ((evt.keyCode == 13) || (evt.keyCode == 32)) {
74 if (this.mediaElem_.paused){
75 this.mediaElem_.play();
76 } else {
77 this.mediaElem_.pause();
79 } else if (evt.keyCode == 39) { // Right - FF
80 this.jumpToTime_(
81 this.mediaElem_.currentTime + (this.mediaElem_.duration/10));
82 } else if (evt.keyCode == 37) { // Left - REW
83 this.jumpToTime_(
84 this.mediaElem_.currentTime - (this.mediaElem_.duration/10));
85 } else if (evt.keyCode == 38) { // Up - Vol. Up
86 this.setVolume_(this.mediaElem_.volume + .1);
87 } else if (evt.keyCode == 40) { // Down - Vol. Down
88 this.setVolume_(this.mediaElem_.volume - .1);