-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathjwplayer.js
123 lines (97 loc) · 3.41 KB
/
jwplayer.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
/* global jwplayer */
/**
* Created by David Karchmer on 9/11/15.
*/
(function() {
'use strict';
angular
.module('ng-jwplayer', [])
.constant('jwplayer', jwplayer);
})();
/* global jwplayer */
(function () {
'use strict';
JWPlayerService.$inject = ["jwplayer"];
angular
.module('ng-jwplayer')
.service('jwplayerService', JWPlayerService);
/* @ngInject */
function JWPlayerService(jwplayer) {
this.myPlayer = {};
this.existJWPlayer = function(id) {
return (angular.isDefined(this.myPlayer) &&
angular.isDefined(this.myPlayer[id]) &&
this.myPlayer[id] !== null);
};
this.initJWPlayer = function(id) {
// Always delete the player, if it exists
this.cleanUp(id);
this.myPlayer[id] = jwplayer(id);
return this.myPlayer[id];
};
this.cleanUp = function(id) {
if (this.existJWPlayer(id)) {
this.myPlayer[id].remove();
this.myPlayer[id] = null;
}
};
}
})();
(function() {
'use strict';
JWPlayer.$inject = ["$compile", "$log", "$rootScope", "jwplayerService"];
angular
.module('ng-jwplayer')
.directive('jwplayer', JWPlayer);
/* @ngInject */
function JWPlayer($compile, $log, $rootScope, jwplayerService) {
var player;
var _renderJWPlayerElement = function(scope, element) {
var playerId = scope.playerId || 'myPlayer1';
var getTemplate = function (playerId) {
return '<div id="' + playerId + '"></div>';
};
element.html(getTemplate(playerId));
$compile(element.contents())(scope);
player = jwplayerService.initJWPlayer(playerId);
player.setup(scope.playerOptions);
player.on('ready', function() {
$rootScope.$broadcast('ng-jwplayer-ready', {
playerId: playerId
});
});
player.on('setupError', function() {
$rootScope.$broadcast('ng-jwplayer-setup-error', {
playerId: playerId
});
});
};
return {
restrict: 'EC',
scope: {
playerId: '@',
playerOptions: '='
},
link: function (scope, element, attrs) {
var playerId = scope.playerId || 'myPlayer1';
scope.$on('$destroy', function () {
$log.debug('jwplayer onDestroy: ' + playerId);
jwplayerService.cleanUp(playerId);
});
scope.$watch(function () {
return attrs.ngSrc;
}, function (value) {
$log.debug('ng-src(' + playerId + ') has changed: ' + value);
if (angular.isDefined(scope.playerOptions)) {
scope.playerOptions.file = value;
_renderJWPlayerElement(scope, element);
}
});
if (angular.isDefined(attrs.ngSrc) && angular.isDefined(scope.playerOptions)) {
scope.playerOptions.file = attrs.ngSrc;
_renderJWPlayerElement(scope, element);
}
}
};
}
})();