forked from noprotocol/angular-html5
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathangular-html5.js
More file actions
197 lines (192 loc) · 5.06 KB
/
angular-html5.js
File metadata and controls
197 lines (192 loc) · 5.06 KB
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
/**
* Add support for html5 tags & attributes in html4 browsers.
*
* Supported:
* <input placeholder="..." />
* <video />
*/
angular.module('html5', []);
/**
* Very simple placeholder support.
*/
angular.module('html5').directive('placeholder', function ($timeout) {
'use strict';
var input = document.createElement('input');
if ('placeholder' in input) {
return {}; // Has native placeholder support
}
return {
require: 'ngModel',
link: function (scope, el, attrs, ngModel) {
if (attrs.type === 'password') {
return;
}
if (el.val() === '') {
el.val(attrs.placeholder);
}
ngModel.$parsers.push(function(value) {
if (value === attrs.placeholder) {
return '';
}
return value;
});
$timeout(function(){
el.val(attrs.placeholder).focus(function(){
if (el.val() === attrs.placeholder) {
el.val('');
}
}).blur(function(){
if (el.val() === '') {
el.val(attrs.placeholder);
}
});
});
}
};
});
/**
* <video> tag for mp4 playback using video.js (tested with v4.1)
*
* To start/stop the video from the controller use the h5-play attribute, which works similar to ng-show.
* All video.js events are available through "on-*" attributes. Example: <video on-ended="trackView('watched video', $event)" />
*/
//document.createElement('video'); // for IE
angular.module('html5').directive('video', function($parse, $log) {
'use strict';
if (!videojs) {
$log.warn('video.js not loaded');
return {};
}
// @link https://github.com/videojs/video.js/blob/master/docs/api.md#event-types
var events = [
'loadstart',
'loadedmetadata',
'loadeddata',
'loadedalldata',
'play',
'pause',
'timeupdate',
'ended',
'durationchange',
'progress',
'resize',
'volumechange',
'error',
'fullscreenchange'
];
var scopeConfig = {
src: '@',
h5Src: '@',
poster: '@',
h5Poster: '@',
h5Play: '='
};
angular.forEach(events, function (event) {
var name = 'on' + event.charAt(0).toUpperCase() + event.slice(1);
scopeConfig[name] = '&';
});
return {
restrict: 'E',
scope: scopeConfig,
link: function ($scope, el, attrs) {
// Check if the installed flash version is compatible with video.js (requires optional dependancy SWFObject)
if (swfobject) {
var version = swfobject.getFlashPlayerVersion();
if (version.major < 11) {
el.after('<div class="error-message">Sorry, no compatible source and playback technology were found for this video.<br />Try using another browser like <a href="http://bit.ly/ccMUEC">Chrome</a> or download the latest <a href="http://adobe.ly/mwfN1">Adobe Flash Player</a>.</div>');
return;
}
}
// Add video-js classes
if (!attrs['class']) { // Only apply default skin if no class is defined.
el.addClass('vjs-default-skin');
}
var correctAutoplay = angular.isDefined(attrs.autoplay);
el.addClass('video-js');
var options = {};
if (navigator.userAgent.toLowerCase().indexOf('firefox') > -1) {
options.techOrder = ["flash", "html5"]; // use flash fallback in firefox for mp4 playback.
}
var safeApply = function () {
if (!$scope.$root.$$phase) {
$scope.$apply();
}
};
$scope.$watch('h5Poster', function (url) {
if (!url) {
return;
}
attrs.$set('poster', url);
});
var player = false;
videojs(el[0], options, function () {
player = this;
if ($scope.$$destroyed) { // video element is no longer needed?
player = false;
player.dispose();
return;
}
// Update the placeholder image (poster)
attrs.$observe('poster', function (url) {
if (url) {
el.children('.vjs-poster img').attr('src', '');
player.poster(url);
}
});
// Bind events to "on-*" attributes
angular.forEach(events, function (event) {
var method = attrs.$normalize('on-' + event);
if (attrs[method]) {
player.on(event, function (e) {
$scope.$root.$event = e;
$scope.$eval(method + '()');
$scope.$root.$event = null;
});
}
});
// Update the value bound to h5Play (Write binding).
if (attrs.h5Play) {
player.on('play', function () {
$scope.h5Play = true;
safeApply();
});
player.on('pause', function () {
$scope.h5Play = false;
safeApply();
});
}
// Update the src
angular.forEach(['src', 'h5Src'], function (expression) {
$scope.$watch(expression, function (url) {
if (angular.isDefined(url)) {
player.src(url);
if ($scope.h5Play) {
player.play();
}
}
});
});
// Start/Stop the video with the h5-play value (Read binding)
$scope.$watch('h5Play', function (value) {
var shouldPlay = !!value;
if (player.paused() == shouldPlay) {
if (shouldPlay) {
player.play();
} else if (correctAutoplay) {
correctAutoplay = false; // Prevent a pause-play loop.
} else {
player.pause();
}
}
});
safeApply();
});
// Clean up player
$scope.$on('$destroy', function () {
if (player) {
player.dispose();
}
});
}
};
});