-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrubandselect.js
354 lines (302 loc) · 13.6 KB
/
scrubandselect.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/** @license websight v1.0.0 http://websight.nl
* Eric Bus
* License: MIT
*/
/**
* @ngdoc directive
* @name nl.websight.videogular.plugins.scrubandselect.directive:vgScrubAndSelectBar
* @restrict E
* @description
* This directive acts as a container and you will need other directives to control the media.
* Inside this directive you can add other directives like vg-play-pause-button and vg-scrub-bar.
*
* <pre>
* <videogular vg-theme="config.theme.url">
* <vg-media vg-src="sources"></vg-media>
*
* <vg-controls vg-autohide='config.autohide' vg-autohide-time='config.autohideTime'></vg-controls>
* </videogular>
* </pre>
*
* @param {boolean=false} vgAutohide Boolean variable or value to activate autohide.
* @param {number=2000} vgAutohideTime Number variable or value that represents the time in milliseconds that will wait vgControls until it hides.
*
*
*/
"use strict";
angular.module("nl.websight.videogular.plugins.scrubandselect", [])
.run(
["$templateCache", function ($templateCache) {
$templateCache.put("vg-templates/vg-scrub-and-select-bar",
'<div role="slider" aria-valuemax="{{ariaTime(API.totalTime)}}" aria-valuenow="{{ariaTime(API.currentTime)}}" aria-valuemin="0" aria-label="Time scrub bar" tabindex="0" ng-transclude ng-keydown="onScrubBarKeyDown($event)"></div>');
}]
)
.directive("vgScrubAndSelectBar",
["VG_STATES", "VG_UTILS", '$window', function (VG_STATES, VG_UTILS, $window) {
return {
restrict: "E",
require: "^videogular",
transclude: true,
scope: {
vgReady: '&',
vgSelectionChange: '&'
},
templateUrl: function (elem, attrs) {
return attrs.vgTemplate || 'vg-templates/vg-scrub-and-select-bar';
},
link: function (scope, elem, attr, API) {
var isSeeking = false;
var mouseDown = false;
var hasDragged = false;
var isPlaying = false;
var dragStartX = 0;
var isPlayingWhenSeeking = false;
var touchStartX = 0;
var END = 35;
var HOME = 36;
var LEFT = 37;
var RIGHT = 39;
var SPACE = 32;
var NUM_PERCENT = 5;
var barLeft;
var selectionElem;
var optionsElem;
function onResize()
{
barLeft = angular.element(elem[0]).offset().left;
selectionElem = angular.element(elem[0].getElementsByTagName("vg-scrub-bar-selection"));
optionsElem = angular.element(elem[0].getElementsByTagName("vg-scrub-bar-selection-options"));
}
scope.API = API;
scope.ariaTime = function (time) {
return Math.round(time / 1000);
};
scope.selection = null;
scope.$watch(attr.initialSelection, function(newValue, oldValue) {
if (newValue !== oldValue) {
scope.setSelection(newValue);
}
});
angular.element($window).bind('resize', function() {
onResize();
})
onResize();
scope.onScrubBarTouchStart = function onScrubBarTouchStart($event) {
var event = $event.originalEvent || $event;
var touches = event.touches;
var touchX;
if (VG_UTILS.isiOSDevice()) {
touchStartX = (touches[0].clientX - event.layerX) * -1;
}
else {
touchStartX = event.layerX;
}
touchX = touches[0].clientX + touchStartX - touches[0].target.offsetLeft;
isSeeking = true;
if (isPlaying) isPlayingWhenSeeking = true;
API.pause();
API.seekTime(touchX * API.mediaElement[0].duration / elem[0].scrollWidth);
scope.$apply();
};
scope.onScrubBarTouchEnd = function onScrubBarTouchEnd($event) {
var event = $event.originalEvent || $event;
if (isPlayingWhenSeeking) {
isPlayingWhenSeeking = false;
API.play();
}
isSeeking = false;
scope.$apply();
};
scope.onScrubBarTouchMove = function onScrubBarTouchMove($event) {
var event = $event.originalEvent || $event;
var touches = event.touches;
var touchX;
if (isSeeking) {
touchX = touches[0].clientX + touchStartX - touches[0].target.offsetLeft;
API.seekTime(touchX * API.mediaElement[0].duration / elem[0].scrollWidth);
}
scope.$apply();
};
scope.onScrubBarTouchLeave = function onScrubBarTouchLeave(event) {
isSeeking = false;
scope.$apply();
};
scope.onScrubBarMouseDown = function onScrubBarMouseDown(event) {
mouseDown = true;
hasDragged = false;
dragStartX = event.pageX - barLeft;
// Initial bar position
selectionElem.css('width', 0).css('left', ((dragStartX / elem[0].scrollWidth) * 100) + '%');
// Initialize selection information
scope.selection = { start: (event.pageX - barLeft) * API.mediaElement[0].duration / elem[0].scrollWidth, duration: 0 }
scope.vgSelectionChange({ $selection: scope.selection });
scope.$apply();
};
scope.onScrubBarMouseUp = function onScrubBarMouseUp(event) {
mouseDown = false;
if (hasDragged)
{
// End of drag
hasDragged = false;
}
else
{
API.seekTime((event.pageX - barLeft) * API.mediaElement[0].duration / elem[0].scrollWidth);
optionsElem.hide();
scope.selection = null;
scope.vgSelectionChange({ $selection: scope.selection });
}
scope.$apply();
};
scope.onScrubBarMouseMove = function onScrubBarMouseMove(event) {
if (mouseDown)
{
var realX = event.pageX - barLeft;
if (Math.abs(realX - dragStartX) >= 3)
{
hasDragged = true;
optionsElem.show();
var direction = (realX < dragStartX) ? 'left' : 'right';
if (direction === 'left')
{
selectionElem.css('left', ((realX / elem[0].scrollWidth) * 100) + '%');
selectionElem.css('width', (dragStartX - realX) + 'px');
// Update selection information
scope.selection.start = ((event.pageX - barLeft) * API.mediaElement[0].duration / elem[0].scrollWidth);
scope.selection.duration = ((dragStartX - realX) * API.mediaElement[0].duration / elem[0].scrollWidth);
scope.vgSelectionChange({ $selection: scope.selection });
}
else
{
selectionElem.css('width', (realX - dragStartX) + 'px');
// Update selection information
scope.selection.duration = ((realX - dragStartX) * API.mediaElement[0].duration / elem[0].scrollWidth);
scope.vgSelectionChange({ $selection: scope.selection });
}
}
}
scope.$apply();
};
scope.clearSelection = function()
{
// Clear scope selection
scope.selection = null;
// Reset selection element
selectionElem.css('width', 0);
// Hide popup
optionsElem.hide();
};
scope.setSelection = function(selection)
{
// Check if media is loaded
if (!API.mediaElement[0].duration)
{
return false;
}
// Calculate number of pixels per second
var pixelsPerSecond = elem[0].scrollWidth / API.mediaElement[0].duration;
// Set selection element
selectionElem.css({ left: (selection.start * pixelsPerSecond) + 'px', width : (selection.duration * pixelsPerSecond) + 'px' });
// Update scope selection
scope.selection = selection;
// Show popup
optionsElem.show();
};
scope.onScrubBarMouseLeave = function onScrubBarMouseLeave(event) {
mouseDown = hasDragged = false;
scope.$apply();
};
/*
scope.onScrubBarKeyDown = function onScrubBarKeyDown(event) {
var currentPercent = (API.currentTime / API.totalTime) * 100;
if (event.which === LEFT || event.keyCode === LEFT) {
API.seekTime(currentPercent - NUM_PERCENT, true);
event.preventDefault();
}
else if (event.which === RIGHT || event.keyCode === RIGHT) {
API.seekTime(currentPercent + NUM_PERCENT, true);
event.preventDefault();
}
else if (event.which === HOME || event.keyCode === HOME) {
API.seekTime(0, true);
event.preventDefault();
}
else if (event.which === END || event.keyCode === END) {
API.seekTime(100, true);
event.preventDefault();
}
else if (event.which === SPACE || event.keyCode === SPACE) {
API.playPause();
}
};
*/
// Disable normal dragging
scope.onScrubBarDragStart = function onScrubBarDragStart(event) {
event.preventDefault();
return false;
};
scope.setState = function setState(newState) {
if (!isSeeking) {
switch (newState) {
case VG_STATES.PLAY:
isPlaying = true;
break;
case VG_STATES.PAUSE:
isPlaying = false;
break;
case VG_STATES.STOP:
isPlaying = false;
break;
}
}
};
scope.$watch(
function () {
return API.currentState;
},
function (newVal, oldVal) {
if (newVal != oldVal) {
scope.setState(newVal);
}
}
);
scope.$watch(
function () {
return API.sources;
},
function (newVal, oldVal) {
if (newVal != oldVal) {
scope.clearSelection();
}
}
);
// Touch move is really buggy in Chrome for Android, maybe we could use mouse move that works ok
if (VG_UTILS.isMobileDevice()) {
elem.bind("touchstart", scope.onScrubBarTouchStart);
elem.bind("touchend", scope.onScrubBarTouchEnd);
elem.bind("touchmove", scope.onScrubBarTouchMove);
elem.bind("touchleave", scope.onScrubBarTouchLeave);
}
else {
elem.bind("mousedown", scope.onScrubBarMouseDown);
elem.bind("mouseup", scope.onScrubBarMouseUp);
elem.bind("dragsart", scope.onScrubBarDragStart);
elem.bind("mousemove", scope.onScrubBarMouseMove);
elem.bind("mouseleave", scope.onScrubBarMouseLeave);
}
},
controller: ['$scope', function($scope) {
this.setSelection = function(selection)
{
$scope.setSelection(selection);
};
this.clearSelection = function()
{
$scope.clearSelection();
};
$scope.vgReady({$API: this});
}],
controllerAs: "API"
}
}]
);