707a27afaf95cce5c3260d570cc412ea5a1855ad
[yaffs-website] / poster-image.js
1 'use strict';
2
3 exports.__esModule = true;
4
5 var _clickableComponent = require('./clickable-component.js');
6
7 var _clickableComponent2 = _interopRequireDefault(_clickableComponent);
8
9 var _component = require('./component.js');
10
11 var _component2 = _interopRequireDefault(_component);
12
13 var _fn = require('./utils/fn.js');
14
15 var Fn = _interopRequireWildcard(_fn);
16
17 var _dom = require('./utils/dom.js');
18
19 var Dom = _interopRequireWildcard(_dom);
20
21 var _browser = require('./utils/browser.js');
22
23 var browser = _interopRequireWildcard(_browser);
24
25 function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj['default'] = obj; return newObj; } }
26
27 function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
28
29 function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
30
31 function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
32
33 function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } /**
34                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 * @file poster-image.js
35                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 */
36
37
38 /**
39  * A `ClickableComponent` that handles showing the poster image for the player.
40  *
41  * @extends ClickableComponent
42  */
43 var PosterImage = function (_ClickableComponent) {
44   _inherits(PosterImage, _ClickableComponent);
45
46   /**
47    * Create an instance of this class.
48    *
49    * @param {Player} player
50    *        The `Player` that this class should attach to.
51    *
52    * @param {Object} [options]
53    *        The key/value store of player options.
54    */
55   function PosterImage(player, options) {
56     _classCallCheck(this, PosterImage);
57
58     var _this = _possibleConstructorReturn(this, _ClickableComponent.call(this, player, options));
59
60     _this.update();
61     player.on('posterchange', Fn.bind(_this, _this.update));
62     return _this;
63   }
64
65   /**
66    * Clean up and dispose of the `PosterImage`.
67    */
68
69
70   PosterImage.prototype.dispose = function dispose() {
71     this.player().off('posterchange', this.update);
72     _ClickableComponent.prototype.dispose.call(this);
73   };
74
75   /**
76    * Create the `PosterImage`s DOM element.
77    *
78    * @return {Element}
79    *         The element that gets created.
80    */
81
82
83   PosterImage.prototype.createEl = function createEl() {
84     var el = Dom.createEl('div', {
85       className: 'vjs-poster',
86
87       // Don't want poster to be tabbable.
88       tabIndex: -1
89     });
90
91     // To ensure the poster image resizes while maintaining its original aspect
92     // ratio, use a div with `background-size` when available. For browsers that
93     // do not support `background-size` (e.g. IE8), fall back on using a regular
94     // img element.
95     if (!browser.BACKGROUND_SIZE_SUPPORTED) {
96       this.fallbackImg_ = Dom.createEl('img');
97       el.appendChild(this.fallbackImg_);
98     }
99
100     return el;
101   };
102
103   /**
104    * An {@link EventTarget~EventListener} for {@link Player#posterchange} events.
105    *
106    * @listens Player#posterchange
107    *
108    * @param {EventTarget~Event} [event]
109    *        The `Player#posterchange` event that triggered this function.
110    */
111
112
113   PosterImage.prototype.update = function update(event) {
114     var url = this.player().poster();
115
116     this.setSrc(url);
117
118     // If there's no poster source we should display:none on this component
119     // so it's not still clickable or right-clickable
120     if (url) {
121       this.show();
122     } else {
123       this.hide();
124     }
125   };
126
127   /**
128    * Set the source of the `PosterImage` depending on the display method.
129    *
130    * @param {string} url
131    *        The URL to the source for the `PosterImage`.
132    */
133
134
135   PosterImage.prototype.setSrc = function setSrc(url) {
136     if (this.fallbackImg_) {
137       this.fallbackImg_.src = url;
138     } else {
139       var backgroundImage = '';
140
141       // Any falsey values should stay as an empty string, otherwise
142       // this will throw an extra error
143       if (url) {
144         backgroundImage = 'url("' + url + '")';
145       }
146
147       this.el_.style.backgroundImage = backgroundImage;
148     }
149   };
150
151   /**
152    * An {@link EventTarget~EventListener} for clicks on the `PosterImage`. See
153    * {@link ClickableComponent#handleClick} for instances where this will be triggered.
154    *
155    * @listens tap
156    * @listens click
157    * @listens keydown
158    *
159    * @param {EventTarget~Event} event
160    +        The `click`, `tap` or `keydown` event that caused this function to be called.
161    */
162
163
164   PosterImage.prototype.handleClick = function handleClick(event) {
165     // We don't want a click to trigger playback when controls are disabled
166     if (!this.player_.controls()) {
167       return;
168     }
169
170     if (this.player_.paused()) {
171       this.player_.play();
172     } else {
173       this.player_.pause();
174     }
175   };
176
177   return PosterImage;
178 }(_clickableComponent2['default']);
179
180 _component2['default'].registerComponent('PosterImage', PosterImage);
181 exports['default'] = PosterImage;