Source: ui/volume_bar.js

  1. /**
  2. * @license
  3. * Copyright 2016 Google Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. goog.provide('shaka.ui.VolumeBar');
  18. goog.require('shaka.ui.Element');
  19. goog.require('shaka.ui.Locales');
  20. goog.require('shaka.ui.Localization');
  21. /**
  22. * @extends {shaka.ui.Element}
  23. * @final
  24. * @export
  25. */
  26. shaka.ui.VolumeBar = class extends shaka.ui.Element {
  27. /**
  28. * @param {!HTMLElement} parent
  29. * @param {!shaka.ui.Controls} controls
  30. */
  31. constructor(parent, controls) {
  32. super(parent, controls);
  33. // This container is to support IE 11. See detailed notes in
  34. // less/range_elements.less for a complete explanation.
  35. // TODO: Factor this into a range-element component.
  36. /** @private {!HTMLElement} */
  37. this.container_ = shaka.ui.Utils.createHTMLElement('div');
  38. this.container_.classList.add('shaka-volume-bar-container');
  39. this.bar_ =
  40. /** @type {!HTMLInputElement} */ (document.createElement('input'));
  41. this.bar_.classList.add('shaka-volume-bar');
  42. this.bar_.setAttribute('type', 'range');
  43. // NOTE: step=any causes keyboard nav problems on IE 11.
  44. this.bar_.setAttribute('step', 'any');
  45. this.bar_.setAttribute('min', '0');
  46. this.bar_.setAttribute('max', '1');
  47. this.bar_.setAttribute('value', '0');
  48. this.container_.appendChild(this.bar_);
  49. this.parent.appendChild(this.container_);
  50. this.updateAriaLabel_();
  51. this.eventManager.listen(this.video, 'volumechange', () => {
  52. this.onVolumeStateChange_();
  53. });
  54. this.eventManager.listen(this.bar_, 'input', () => {
  55. this.onVolumeInput_();
  56. });
  57. this.eventManager.listen(
  58. this.localization, shaka.ui.Localization.LOCALE_UPDATED, () => {
  59. this.updateAriaLabel_();
  60. });
  61. this.eventManager.listen(
  62. this.localization, shaka.ui.Localization.LOCALE_CHANGED, () => {
  63. this.updateAriaLabel_();
  64. });
  65. // Initialize volume display with a fake event.
  66. this.onVolumeStateChange_();
  67. }
  68. /**
  69. * @private
  70. */
  71. onVolumeStateChange_() {
  72. if (this.video.muted) {
  73. this.bar_.value = 0;
  74. } else {
  75. this.bar_.value = this.video.volume;
  76. }
  77. // TODO: Can we do this with LESS?
  78. let gradient = ['to right'];
  79. gradient.push(shaka.ui.Constants.VOLUME_BAR_VOLUME_LEVEL_COLOR +
  80. (this.bar_.value * 100) + '%');
  81. gradient.push(shaka.ui.Constants.VOLUME_BAR_BASE_COLOR +
  82. (this.bar_.value * 100) + '%');
  83. gradient.push(shaka.ui.Constants.VOLUME_BAR_BASE_COLOR + '100%');
  84. this.container_.style.background =
  85. 'linear-gradient(' + gradient.join(',') + ')';
  86. }
  87. /**
  88. * @private
  89. */
  90. onVolumeInput_() {
  91. this.video.volume = parseFloat(this.bar_.value);
  92. if (this.video.volume == 0) {
  93. this.video.muted = true;
  94. } else {
  95. this.video.muted = false;
  96. }
  97. }
  98. /**
  99. * @private
  100. */
  101. updateAriaLabel_() {
  102. this.bar_.setAttribute(shaka.ui.Constants.ARIA_LABEL,
  103. this.localization.resolve(shaka.ui.Locales.Ids.ARIA_LABEL_VOLUME));
  104. }
  105. };
  106. /**
  107. * @implements {shaka.extern.IUIElement.Factory}
  108. * @final
  109. */
  110. shaka.ui.VolumeBar.Factory = class {
  111. /** @override */
  112. create(rootElement, controls) {
  113. return new shaka.ui.VolumeBar(rootElement, controls);
  114. }
  115. };
  116. shaka.ui.Controls.registerElement('volume', new shaka.ui.VolumeBar.Factory());