forked from aullman/OpenTok-Angular
-
Notifications
You must be signed in to change notification settings - Fork 0
/
opentok-angular.js
194 lines (190 loc) · 6.87 KB
/
opentok-angular.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
/*!
* opentok-angular (https://github.com/aullman/OpenTok-Angular)
*
* Angular module for OpenTok
*
* @Author: Adam Ullman (http://github.com/aullman)
* @Copyright (c) 2014 Adam Ullman
* @License: Released under the MIT license (http://opensource.org/licenses/MIT)
**/
if (!window.OT) throw new Error('You must include the OT library before the OT_Angular library');
angular.module('opentok', [])
.factory('OT', function() {
return OT;
})
.factory('OTSession', ['OT', '$rootScope',
function(OT, $rootScope) {
var OTSession = {
streams: [],
connections: [],
publishers: [],
init: function(apiKey, sessionId, token, cb) {
this.session = OT.initSession(apiKey, sessionId);
OTSession.session.on({
sessionConnected: function() {
OTSession.publishers.forEach(function(publisher) {
OTSession.session.publish(publisher);
});
},
streamCreated: function(event) {
$rootScope.$apply(function() {
OTSession.streams.push(event.stream);
});
},
streamDestroyed: function(event) {
$rootScope.$apply(function() {
OTSession.streams.splice(OTSession.streams.indexOf(event.stream), 1);
});
},
sessionDisconnected: function() {
$rootScope.$apply(function() {
OTSession.streams.splice(0, OTSession.streams.length);
OTSession.connections.splice(0, OTSession.connections.length);
});
},
connectionCreated: function(event) {
$rootScope.$apply(function() {
OTSession.connections.push(event.connection);
});
},
connectionDestroyed: function(event) {
$rootScope.$apply(function() {
OTSession.connections.splice(OTSession.connections.indexOf(event.connection), 1);
});
}
});
this.session.connect(token, function(err) {
if (cb) cb(err, OTSession.session);
});
this.trigger('init');
}
};
OT.$.eventing(OTSession);
return OTSession;
}
])
.directive('otLayout', ['$window', '$parse', 'OT', 'OTSession',
function($window, $parse, OT, OTSession) {
return {
restrict: 'E',
link: function(scope, element, attrs) {
var props = $parse(attrs.props)();
var container = OT.initLayoutContainer(element[0], props);
var layout = function() {
container.layout();
scope.$emit('otLayoutComplete');
};
scope.$watch(function() {
return element.children().length;
}, layout);
$window.addEventListener('resize', layout);
scope.$on('otLayout', layout);
var listenForStreamChange = function listenForStreamChange() {
OTSession.session.on('streamPropertyChanged', function(event) {
if (event.changedProperty === 'videoDimensions') {
layout();
}
});
};
if (OTSession.session) listenForStreamChange();
else OTSession.on('init', listenForStreamChange);
}
};
}
])
.directive('otPublisher', ['OTSession',
function(OTSession) {
return {
restrict: 'E',
scope: {
props: '&'
},
link: function(scope, element, attrs) {
var props = scope.props() || {};
props.width = props.width ? props.width : angular.element(element).width();
props.height = props.height ? props.height : angular.element(element).height();
var oldChildren = angular.element(element).children();
scope.publisher = OT.initPublisher(attrs.apikey || OTSession.session.apiKey,
element[0], props, function(err) {
if (err) {
scope.$emit('otPublisherError', err, scope.publisher);
}
});
// Make transcluding work manually by putting the children back in there
angular.element(element).append(oldChildren);
scope.publisher.on({
accessDenied: function() {
scope.$emit('otAccessDenied');
},
accessDialogOpened: function() {
scope.$emit('otAccessDialogOpened');
},
accessDialogClosed: function() {
scope.$emit('otAccessDialogClosed');
},
accessAllowed: function() {
angular.element(element).addClass('allowed');
scope.$emit('otAccessAllowed');
},
loaded: function() {
scope.$emit('otLayout');
},
streamCreated: function() {
scope.$emit('otStreamCreated');
},
streamDestroyed: function() {
scope.$emit('otStreamDestroyed');
}
});
scope.$on('$destroy', function() {
if (OTSession.session) OTSession.session.unpublish(scope.publisher);
else scope.publisher.destroy();
OTSession.publishers = OTSession.publishers.filter(function(publisher) {
return publisher !== scope.publisher;
});
scope.publisher = null;
});
if (OTSession.session && (OTSession.session.connected ||
(OTSession.session.isConnected && OTSession.session.isConnected()))) {
OTSession.session.publish(scope.publisher, function(err) {
if (err) {
scope.$emit('otPublisherError', err, scope.publisher);
}
});
}
OTSession.publishers.push(scope.publisher);
}
};
}
])
.directive('otSubscriber', ['OTSession',
function(OTSession) {
return {
restrict: 'E',
scope: {
stream: '=',
props: '&'
},
link: function(scope, element) {
var stream = scope.stream,
props = scope.props() || {};
props.width = props.width ? props.width : angular.element(element).width();
props.height = props.height ? props.height : angular.element(element).height();
var oldChildren = angular.element(element).children();
var subscriber = OTSession.session.subscribe(stream, element[0], props, function(err) {
if (err) {
scope.$emit('otSubscriberError', err, subscriber);
}
});
subscriber.on('loaded', function() {
scope.$emit('otLayout');
});
// Make transcluding work manually by putting the children back in there
angular.element(element).append(oldChildren);
scope.$on('$destroy', function() {
OTSession.session.unsubscribe(subscriber);
});
}
};
}
]);