blob: efb46bc3f264cd9bb447fd1043cdc34e0c9b09cc [file] [log] [blame]
Matteo Scandolobbcc01b2015-11-04 16:30:43 +01001'use strict';
2
3angular.module('xos.contentProvider', [
Matteo Scandolo15d692c2015-10-28 12:00:07 +01004 'ngResource',
Matteo Scandolo15d692c2015-10-28 12:00:07 +01005 'ngCookies',
Matteo Scandolobbcc01b2015-11-04 16:30:43 +01006 'xos.helpers',
Matteo Scandolo40bea632016-05-17 16:51:29 -07007 'ui.router'
Matteo Scandolo15d692c2015-10-28 12:00:07 +01008])
Matteo Scandolo40bea632016-05-17 16:51:29 -07009.config(($stateProvider) => {
Matteo Scandolo7db08432015-11-06 18:49:33 +010010
Matteo Scandolod44ceb42015-11-06 17:14:28 +010011 $stateProvider
12 .state('list', {
13 url: '/',
Matteo Scandolo15d692c2015-10-28 12:00:07 +010014 template: '<content-provider-list></content-provider-list>',
15 })
Matteo Scandolod44ceb42015-11-06 17:14:28 +010016 .state('details', {
Matteo Scandolo7db08432015-11-06 18:49:33 +010017 url: '/contentProvider/:id',
Matteo Scandolo15d692c2015-10-28 12:00:07 +010018 template: '<content-provider-detail></content-provider-detail>'
19 })
Matteo Scandolod44ceb42015-11-06 17:14:28 +010020 .state('cdn', {
21 url: '/contentProvider/:id/cdn_prefix',
Matteo Scandolo15d692c2015-10-28 12:00:07 +010022 template: '<content-provider-cdn></content-provider-cdn>'
23 })
Matteo Scandolod44ceb42015-11-06 17:14:28 +010024 .state('server', {
25 url: '/contentProvider/:id/origin_server',
Matteo Scandolo15d692c2015-10-28 12:00:07 +010026 template: '<content-provider-server></content-provider-server>'
27 })
Matteo Scandolod44ceb42015-11-06 17:14:28 +010028 .state('users', {
29 url: '/contentProvider/:id/users',
Matteo Scandolo15d692c2015-10-28 12:00:07 +010030 template: '<content-provider-users></content-provider-users>'
Matteo Scandolod44ceb42015-11-06 17:14:28 +010031 });
Matteo Scandolo15d692c2015-10-28 12:00:07 +010032})
33.config(function($httpProvider){
Matteo Scandolo15d692c2015-10-28 12:00:07 +010034 // add X-CSRFToken header for update, create, delete (!GET)
35 $httpProvider.interceptors.push('SetCSRFToken');
Matteo Scandolobbcc01b2015-11-04 16:30:43 +010036 $httpProvider.interceptors.push('NoHyperlinks');
Matteo Scandolo15d692c2015-10-28 12:00:07 +010037})
38.service('ContentProvider', function($resource){
39 return $resource('/hpcapi/contentproviders/:id/', {id: '@id'}, {
40 'update': {method: 'PUT'}
41 });
42})
43.service('ServiceProvider', function($resource){
44 return $resource('/hpcapi/serviceproviders/:id/', {id: '@id'});
45})
46.service('CdnPrefix', function($resource){
47 return $resource('/hpcapi/cdnprefixs/:id/', {id: '@id'});
48})
49.service('OriginServer', function($resource){
50 return $resource('/hpcapi/originservers/:id/', {id: '@id'});
51})
52.service('User', function($resource){
53 return $resource('/xos/users/:id/', {id: '@id'});
54})
55.directive('cpActions', function(ContentProvider, $location){
56 return {
57 restrict: 'E',
58 scope: {
59 id: '=id',
60 },
61 bindToController: true,
62 controllerAs: 'vm',
Matteo Scandolobbcc01b2015-11-04 16:30:43 +010063 templateUrl: 'templates/cp_actions.html',
Matteo Scandolo15d692c2015-10-28 12:00:07 +010064 controller: function(){
65 this.deleteCp = function(id){
66 ContentProvider.delete({id: id}).$promise
67 .then(function(){
68 $location.url('/');
69 });
70 };
71 }
72 };
73})
Matteo Scandolo40bea632016-05-17 16:51:29 -070074.directive('contentProviderList', function(ContentProvider, _){
Matteo Scandolo15d692c2015-10-28 12:00:07 +010075 return {
76 restrict: 'E',
77 controllerAs: 'vm',
78 scope: {},
Matteo Scandolobbcc01b2015-11-04 16:30:43 +010079 templateUrl: 'templates/cp_list.html',
Matteo Scandolo15d692c2015-10-28 12:00:07 +010080 controller: function(){
Matteo Scandolo775d3242016-03-24 16:12:18 -070081 ['Name', 'Description', 'Status', 'Actions']
82 this.tableConfig = {
83 columns: [
84 {
85 label: 'Name',
86 field: 'humanReadableName'
87 },
88 {
89 label: 'Description',
90 field: 'description'
91 },
92 {
93 label: 'Status',
94 field: 'enabled'
95 }
96 ],
97 enableActions: true
98 };
99
Matteo Scandolo7db08432015-11-06 18:49:33 +0100100 var self = this;
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100101
102 ContentProvider.query().$promise
103 .then(function(cp){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100104 self.contentProviderList = cp;
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100105 })
106 .catch(function(e){
107 throw new Error(e);
108 });
109
110 this.deleteCp = function(id){
111 ContentProvider.delete({id: id}).$promise
112 .then(function(){
Matteo Scandolo40bea632016-05-17 16:51:29 -0700113 _.remove(self.contentProviderList, {id: id});
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100114 });
115 };
116 }
117 };
118})
Matteo Scandolo7db08432015-11-06 18:49:33 +0100119.directive('contentProviderDetail', function(ContentProvider, ServiceProvider, $stateParams, $location){
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100120 return {
121 restrict: 'E',
122 controllerAs: 'vm',
123 scope: {},
Matteo Scandolobbcc01b2015-11-04 16:30:43 +0100124 templateUrl: 'templates/cp_detail.html',
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100125 controller: function(){
126 this.pageName = 'detail';
Matteo Scandolo7db08432015-11-06 18:49:33 +0100127 var self = this;
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100128
Matteo Scandolo7db08432015-11-06 18:49:33 +0100129 if($stateParams.id){
130 ContentProvider.get({id: $stateParams.id}).$promise
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100131 .then(function(cp){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100132 self.cp = cp;
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100133 }).catch(function(e){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100134 self.result = {
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100135 status: 0,
136 msg: e.data.detail
137 };
138 });
139 }
140 else{
Matteo Scandolo7db08432015-11-06 18:49:33 +0100141 self.cp = new ContentProvider();
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100142 }
143
144 ServiceProvider.query().$promise
145 .then(function(sp){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100146 self.sp = sp;
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100147 });
148
149 this.saveContentProvider = function(cp){
150 var p, isNew = false;
151
152 if(cp.id){
153 p = cp.$update();
154 }
155 else{
156 isNew = true;
157 cp.name = cp.humanReadableName;
158 p = cp.$save();
159 }
160
161 p.then(function(res){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100162 self.result = {
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100163 status: 1,
164 msg: 'Content Provider Saved'
165 };
166 if(isNew){
167 $location.url('contentProvider/' + res.id + '/');
168 }
169 })
170 .catch(function(e){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100171 self.result = {
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100172 status: 0,
173 msg: e.data.detail
174 };
175 });
176 };
177 }
178 };
179})
Matteo Scandolo40bea632016-05-17 16:51:29 -0700180.directive('contentProviderCdn', function($stateParams, CdnPrefix, ContentProvider, _){
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100181 return{
182 restrict: 'E',
183 controllerAs: 'vm',
184 scope: {},
Matteo Scandolobbcc01b2015-11-04 16:30:43 +0100185 templateUrl: 'templates/cp_cdn_prefix.html',
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100186 controller: function(){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100187 var self = this;
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100188
189 this.pageName = 'cdn';
190
Matteo Scandolo7db08432015-11-06 18:49:33 +0100191 if($stateParams.id){
192 ContentProvider.get({id: $stateParams.id}).$promise
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100193 .then(function(cp){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100194 self.cp = cp;
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100195 }).catch(function(e){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100196 self.result = {
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100197 status: 0,
198 msg: e.data.detail
199 };
200 });
201 }
202
203 CdnPrefix.query().$promise
204 .then(function(prf){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100205 self.prf = prf;
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100206 // set the active CdnPrefix for this contentProvider
Matteo Scandolo40bea632016-05-17 16:51:29 -0700207 self.cp_prf = _.where(prf, {contentProvider: parseInt($stateParams.id)});
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100208 }).catch(function(e){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100209 self.result = {
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100210 status: 0,
211 msg: e.data.detail
212 };
213 });
214
215 this.addPrefix = function(prf){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100216 prf.contentProvider = $stateParams.id;
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100217
218 var item = new CdnPrefix(prf);
219
220 item.$save()
221 .then(function(res){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100222 self.cp_prf.push(res);
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100223 })
224 .catch(function(e){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100225 self.result = {
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100226 status: 0,
227 msg: e.data.detail
228 };
229 });
230 };
231
232 this.removePrefix = function(item){
233 item.$delete()
234 .then(function(){
Matteo Scandolo40bea632016-05-17 16:51:29 -0700235 _.remove(self.cp_prf, item);
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100236 })
237 .catch(function(e){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100238 self.result = {
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100239 status: 0,
240 msg: e.data.detail
241 };
242 });
243 };
244 }
245 };
246})
Matteo Scandolo40bea632016-05-17 16:51:29 -0700247.directive('contentProviderServer', function($stateParams, OriginServer, ContentProvider, _){
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100248 return{
249 restrict: 'E',
250 controllerAs: 'vm',
251 scope: {},
Matteo Scandolobbcc01b2015-11-04 16:30:43 +0100252 templateUrl: 'templates/cp_origin_server.html',
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100253 controller: function(){
254 this.pageName = 'server';
Matteo Scandolo7db08432015-11-06 18:49:33 +0100255 this.protocols = {'http': 'HTTP', 'rtmp': 'RTMP', 'rtp': 'RTP', 'shout': 'SHOUTcast'};
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100256
Matteo Scandolo7db08432015-11-06 18:49:33 +0100257 var self = this;
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100258
Matteo Scandolo7db08432015-11-06 18:49:33 +0100259 if($stateParams.id){
260 ContentProvider.get({id: $stateParams.id}).$promise
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100261 .then(function(cp){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100262 self.cp = cp;
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100263 }).catch(function(e){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100264 self.result = {
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100265 status: 0,
266 msg: e.data.detail
267 };
268 });
269 }
270
Matteo Scandolo7db08432015-11-06 18:49:33 +0100271 OriginServer.query({contentProvider: $stateParams.id}).$promise
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100272 .then(function(cp_os){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100273 self.cp_os = cp_os;
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100274 }).catch(function(e){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100275 self.result = {
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100276 status: 0,
277 msg: e.data.detail
278 };
279 });
280
281 this.addOrigin = function(os){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100282 os.contentProvider = $stateParams.id;
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100283
284 var item = new OriginServer(os);
285
286 item.$save()
287 .then(function(res){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100288 self.cp_os.push(res);
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100289 })
290 .catch(function(e){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100291 self.result = {
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100292 status: 0,
293 msg: e.data.detail
294 };
295 });
296 };
297
298 this.removeOrigin = function(item){
299 item.$delete()
300 .then(function(){
Matteo Scandolo40bea632016-05-17 16:51:29 -0700301 _.remove(self.cp_os, item);
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100302 })
303 .catch(function(e){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100304 self.result = {
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100305 status: 0,
306 msg: e.data.detail
307 };
308 });
309 };
310 }
311 };
312})
Matteo Scandolo40bea632016-05-17 16:51:29 -0700313.directive('contentProviderUsers', function($stateParams, ContentProvider, User, _){
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100314 return{
315 restrict: 'E',
316 controllerAs: 'vm',
317 scope: {},
Matteo Scandolobbcc01b2015-11-04 16:30:43 +0100318 templateUrl: 'templates/cp_user.html',
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100319 controller: function(){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100320 var self = this;
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100321
322 this.pageName = 'user';
323
324 this.cp_users = [];
325
Matteo Scandolo7db08432015-11-06 18:49:33 +0100326 if($stateParams.id){
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100327 User.query().$promise
328 .then(function(users){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100329 self.users = users;
330 return ContentProvider.get({id: $stateParams.id}).$promise;
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100331 })
332 .then(function(res){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100333 res.users = self.populateUser(res.users, self.users);
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100334 return res;
335 })
336 .then(function(cp){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100337 self.cp = cp;
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100338 }).catch(function(e){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100339 self.result = {
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100340 status: 0,
341 msg: e.data.detail
342 };
343 });
344 }
345
346 this.populateUser = function(ids, list){
347 for(var i = 0; i < ids.length; i++){
Matteo Scandolo40bea632016-05-17 16:51:29 -0700348 ids[i] = _.find(list, {id: ids[i]});
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100349 }
350 return ids;
351 };
352
353 this.addUserToCp = function(user){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100354 self.cp.users.push(user);
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100355 };
356
357 this.removeUserFromCp = function(user){
Matteo Scandolo40bea632016-05-17 16:51:29 -0700358 _.remove(self.cp.users, user);
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100359 };
360
361 this.saveContentProvider = function(cp){
362
363 // flatten the user to id of array
Matteo Scandolo40bea632016-05-17 16:51:29 -0700364 cp.users = _.pluck(cp.users, 'id');
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100365
366 cp.$update()
367 .then(function(res){
368
Matteo Scandolo7db08432015-11-06 18:49:33 +0100369 self.cp.users = self.populateUser(res.users, self.users);
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100370
Matteo Scandolo7db08432015-11-06 18:49:33 +0100371 self.result = {
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100372 status: 1,
373 msg: 'Content Provider Saved'
374 };
375
376 })
377 .catch(function(e){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100378 self.result = {
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100379 status: 0,
380 msg: e.data.detail
381 };
382 });
383 };
384 }
385 };
386});