19496cb53a00da026209ed1fd14ccb6bc70a7b36
[yaffs-website] / node_modules / video.js / es5 / control-bar / progress-control / mouse-time-display.js
1 'use strict';
2
3 exports.__esModule = true;
4
5 var _component = require('../../component.js');
6
7 var _component2 = _interopRequireDefault(_component);
8
9 var _dom = require('../../utils/dom.js');
10
11 var Dom = _interopRequireWildcard(_dom);
12
13 var _fn = require('../../utils/fn.js');
14
15 var Fn = _interopRequireWildcard(_fn);
16
17 var _formatTime = require('../../utils/format-time.js');
18
19 var _formatTime2 = _interopRequireDefault(_formatTime);
20
21 var _computedStyle = require('../../utils/computed-style.js');
22
23 var _computedStyle2 = _interopRequireDefault(_computedStyle);
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 mouse-time-display.js
35                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 */
36
37
38 /**
39  * The Mouse Time Display component shows the time you will seek to
40  * when hovering over the progress bar
41  *
42  * @extends Component
43  */
44 var MouseTimeDisplay = function (_Component) {
45   _inherits(MouseTimeDisplay, _Component);
46
47   /**
48    * Creates an instance of this class.
49    *
50    * @param {Player} player
51    *        The `Player` that this class should be attached to.
52    *
53    * @param {Object} [options]
54    *        The key/value store of player options.
55    */
56   function MouseTimeDisplay(player, options) {
57     _classCallCheck(this, MouseTimeDisplay);
58
59     var _this = _possibleConstructorReturn(this, _Component.call(this, player, options));
60
61     if (options.playerOptions && options.playerOptions.controlBar && options.playerOptions.controlBar.progressControl && options.playerOptions.controlBar.progressControl.keepTooltipsInside) {
62       _this.keepTooltipsInside = options.playerOptions.controlBar.progressControl.keepTooltipsInside;
63     }
64
65     if (_this.keepTooltipsInside) {
66       _this.tooltip = Dom.createEl('div', { className: 'vjs-time-tooltip' });
67       _this.el().appendChild(_this.tooltip);
68       _this.addClass('vjs-keep-tooltips-inside');
69     }
70
71     _this.update(0, 0);
72
73     player.on('ready', function () {
74       _this.on(player.controlBar.progressControl.el(), 'mousemove', Fn.throttle(Fn.bind(_this, _this.handleMouseMove), 25));
75     });
76     return _this;
77   }
78
79   /**
80    * Create the `Component`'s DOM element
81    *
82    * @return {Element}
83    *         The element that was created.
84    */
85
86
87   MouseTimeDisplay.prototype.createEl = function createEl() {
88     return _Component.prototype.createEl.call(this, 'div', {
89       className: 'vjs-mouse-display'
90     });
91   };
92
93   /**
94    * Handle the mouse move event on the `MouseTimeDisplay`.
95    *
96    * @param {EventTarget~Event} event
97    *        The `mousemove` event that caused this to event to run.
98    *
99    * @listen mousemove
100    */
101
102
103   MouseTimeDisplay.prototype.handleMouseMove = function handleMouseMove(event) {
104     var duration = this.player_.duration();
105     var newTime = this.calculateDistance(event) * duration;
106     var position = event.pageX - Dom.findElPosition(this.el().parentNode).left;
107
108     this.update(newTime, position);
109   };
110
111   /**
112    * Update the time and posistion of the `MouseTimeDisplay`.
113    *
114    * @param {number} newTime
115    *        Time to change the `MouseTimeDisplay` to.
116    *
117    * @param {nubmer} position
118    *        Postion from the left of the in pixels.
119    */
120
121
122   MouseTimeDisplay.prototype.update = function update(newTime, position) {
123     var time = (0, _formatTime2['default'])(newTime, this.player_.duration());
124
125     this.el().style.left = position + 'px';
126     this.el().setAttribute('data-current-time', time);
127
128     if (this.keepTooltipsInside) {
129       var clampedPosition = this.clampPosition_(position);
130       var difference = position - clampedPosition + 1;
131       var tooltipWidth = parseFloat((0, _computedStyle2['default'])(this.tooltip, 'width'));
132       var tooltipWidthHalf = tooltipWidth / 2;
133
134       this.tooltip.innerHTML = time;
135       this.tooltip.style.right = '-' + (tooltipWidthHalf - difference) + 'px';
136     }
137   };
138
139   /**
140    * Get the mouse pointers x coordinate in pixels.
141    *
142    * @param {EventTarget~Event} [event]
143    *        The `mousemove` event that was passed to this function by
144    *        {@link MouseTimeDisplay#handleMouseMove}
145    *
146    * @return {number}
147    *         THe x position in pixels of the mouse pointer.
148    */
149
150
151   MouseTimeDisplay.prototype.calculateDistance = function calculateDistance(event) {
152     return Dom.getPointerPosition(this.el().parentNode, event).x;
153   };
154
155   /**
156    * This takes in a horizontal position for the bar and returns a clamped position.
157    * Clamped position means that it will keep the position greater than half the width
158    * of the tooltip and smaller than the player width minus half the width o the tooltip.
159    * It will only clamp the position if `keepTooltipsInside` option is set.
160    *
161    * @param {number} position
162    *        The position the bar wants to be
163    *
164    * @return {number}
165    *         The (potentially) new clamped position.
166    *
167    * @private
168    */
169
170
171   MouseTimeDisplay.prototype.clampPosition_ = function clampPosition_(position) {
172     if (!this.keepTooltipsInside) {
173       return position;
174     }
175
176     var playerWidth = parseFloat((0, _computedStyle2['default'])(this.player().el(), 'width'));
177     var tooltipWidth = parseFloat((0, _computedStyle2['default'])(this.tooltip, 'width'));
178     var tooltipWidthHalf = tooltipWidth / 2;
179     var actualPosition = position;
180
181     if (position < tooltipWidthHalf) {
182       actualPosition = Math.ceil(tooltipWidthHalf);
183     } else if (position > playerWidth - tooltipWidthHalf) {
184       actualPosition = Math.floor(playerWidth - tooltipWidthHalf);
185     }
186
187     return actualPosition;
188   };
189
190   return MouseTimeDisplay;
191 }(_component2['default']);
192
193 _component2['default'].registerComponent('MouseTimeDisplay', MouseTimeDisplay);
194 exports['default'] = MouseTimeDisplay;