• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

silvermine / videojs-chromecast / 4766541855

pending completion
4766541855

push

github

GitHub
Merge pull request #158 from pbredenberg/pb/ci-migrate-to-github-actions

0 of 171 branches covered (0.0%)

Branch coverage included in aggregate %.

14 of 356 relevant lines covered (3.93%)

0.2 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

6.0
/src/js/components/ChromecastButton.js
1
module.exports = function(videojs) {
1✔
2

3
   /**
4
    * Registers the ChromecastButton Component with Video.js. Calls
5
    * {@link http://docs.videojs.com/Component.html#.registerComponent}, which will add a
6
    * component called `chromecastButton` to the list of globally registered Video.js
7
    * components. The `chromecastButton` is added to the player's control bar UI
8
    * automatically once {@link module:enableChromecast} has been called. If you would
9
    * like to specify the order of the buttons that appear in the control bar, including
10
    * this button, you can do so in the options that you pass to the `videojs` function
11
    * when creating a player:
12
    *
13
    * ```
14
    * videojs('playerID', {
15
    *    controlBar: {
16
    *       children: [
17
    *          'playToggle',
18
    *          'progressControl',
19
    *          'volumePanel',
20
    *          'fullscreenToggle',
21
    *          'chromecastButton',
22
    *       ],
23
    *    }
24
    * });
25
    * ```
26
    *
27
    * @param videojs {object} A reference to {@link http://docs.videojs.com/module-videojs.html|Video.js}
28
    * @see http://docs.videojs.com/module-videojs.html#~registerPlugin
29
    */
30

31
   /**
32
    * The Video.js Button class is the base class for UI button components.
33
    *
34
    * @external Button
35
    * @see {@link http://docs.videojs.com/Button.html|Button}
36
    */
37
   const ButtonComponent = videojs.getComponent('Button');
1✔
38

39
   /**
40
    * The ChromecastButton module contains both the ChromecastButton class definition and
41
    * the function used to register the button as a Video.js Component.
42
    * @module ChromecastButton
43
    */
44

45

46
   /** @lends ChromecastButton.prototype **/
47
   class ChromecastButton extends ButtonComponent {
48

49
      /**
50
       * This class is a button component designed to be displayed in the
51
       * player UI's control bar. It opens the Chromecast menu when clicked.
52
       *
53
       * @constructs
54
       * @extends external:Button
55
       * @param player {Player} the video.js player instance
56
       */
57
      constructor(player, options) {
58
         super(player, options);
×
59

60
         player.on('chromecastConnected', this._onChromecastConnected.bind(this));
×
61
         player.on('chromecastDisconnected', this._onChromecastDisconnected.bind(this));
×
62
         player.on('chromecastDevicesAvailable', this._onChromecastDevicesAvailable.bind(this));
×
63
         player.on('chromecastDevicesUnavailable', this._onChromecastDevicesUnavailable.bind(this));
×
64

65
         // Use the initial state of `hasAvailableDevices` to call the corresponding event
66
         // handlers because the corresponding events may have already been emitted before
67
         // binding the listeners above.
68
         if (player.chromecastSessionManager && player.chromecastSessionManager.hasAvailableDevices()) {
×
69
            this._onChromecastDevicesAvailable();
×
70
         } else {
71
            this._onChromecastDevicesUnavailable();
×
72
         }
73

74
         if (options.addCastLabelToButton) {
×
75
            this.el().classList.add('vjs-chromecast-button-lg');
×
76

77
            this._labelEl = document.createElement('span');
×
78
            this._labelEl.classList.add('vjs-chromecast-button-label');
×
79
            this._updateCastLabelText();
×
80

81
            this.el().appendChild(this._labelEl);
×
82
         } else {
83
            this.controlText('Open Chromecast menu');
×
84
         }
85
      }
86

87
      /**
88
       * Overrides Button#buildCSSClass to return the classes used on the button element.
89
       *
90
       * @param el {DOMElement}
91
       * @see {@link http://docs.videojs.com/Button.html#buildCSSClass|Button#buildCSSClass}
92
       */
93
      buildCSSClass() {
94
         return 'vjs-chromecast-button ' +
×
95
            (this._isChromecastConnected ? 'vjs-chromecast-casting-state ' : '') +
×
96
            (this.options_.addCastLabelToButton ? 'vjs-chromecast-button-lg ' : '') +
×
97
            ButtonComponent.prototype.buildCSSClass();
98
      }
99

100
      /**
101
       * Overrides Button#handleClick to handle button click events. Chromecast
102
       * functionality is handled outside of this class, which should be limited
103
       * to UI related logic.  This function simply triggers an event on the player.
104
       *
105
       * @fires ChromecastButton#chromecastRequested
106
       * @param el {DOMElement}
107
       * @see {@link http://docs.videojs.com/Button.html#handleClick|Button#handleClick}
108
       */
109
      handleClick() {
110
         this.player().trigger('chromecastRequested');
×
111
      }
112

113
      /**
114
       * Handles `chromecastConnected` player events.
115
       *
116
       * @private
117
       */
118
      _onChromecastConnected() {
119
         this._isChromecastConnected = true;
×
120
         this._reloadCSSClasses();
×
121
         this._updateCastLabelText();
×
122
      }
123

124
      /**
125
       * Handles `chromecastDisconnected` player events.
126
       *
127
       * @private
128
       */
129
      _onChromecastDisconnected() {
130
         this._isChromecastConnected = false;
×
131
         this._reloadCSSClasses();
×
132
         this._updateCastLabelText();
×
133
      }
134

135
      /**
136
       * Handles `chromecastDevicesAvailable` player events.
137
       *
138
       * @private
139
       */
140
      _onChromecastDevicesAvailable() {
141
         this.show();
×
142
      }
143

144
      /**
145
       * Handles `chromecastDevicesUnavailable` player events.
146
       *
147
       * @private
148
       */
149
      _onChromecastDevicesUnavailable() {
150
         this.hide();
×
151
      }
152

153
      /**
154
       * Re-calculates which CSS classes the button needs and sets them on the buttons'
155
       * DOMElement.
156
       *
157
       * @private
158
       */
159
      _reloadCSSClasses() {
160
         if (!this.el_) {
×
161
            return;
×
162
         }
163
         this.el_.className = this.buildCSSClass();
×
164
      }
165

166
      /**
167
       * Updates the optional cast label text based on whether the chromecast is connected
168
       * or disconnected.
169
       *
170
       * @private
171
       */
172
      _updateCastLabelText() {
173
         if (!this._labelEl) {
×
174
            return;
×
175
         }
176
         this._labelEl.textContent = this._isChromecastConnected ? this.localize('Disconnect Cast') : this.localize('Cast');
×
177
      }
178
   }
179

180
   videojs.registerComponent('chromecastButton', ChromecastButton);
1✔
181
};
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc