blob: e9142bb1aab903b2f56199d028a56cde64e36f25 [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 Scandolo54bc5f72016-05-18 14:06:45 -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 Scandolo54bc5f72016-05-18 14:06:45 -0700186 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,
Matteo Scandolo54bc5f72016-05-18 14:06:45 -0700198 msg: e.data ? e.data.detail : ''
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100199 };
200 });
201 }
202
203 CdnPrefix.query().$promise
204 .then(function(prf){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100205 self.prf = prf;
Matteo Scandolo54bc5f72016-05-18 14:06:45 -0700206 // console.log(prf, $stateParams.id);
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100207 // set the active CdnPrefix for this contentProvider
Matteo Scandolo54bc5f72016-05-18 14:06:45 -0700208 self.cp_prf = [];
209 self.cp_prf.push(_.find(prf, {contentProvider: parseInt($stateParams.id)}));
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100210 }).catch(function(e){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100211 self.result = {
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100212 status: 0,
213 msg: e.data.detail
214 };
215 });
216
217 this.addPrefix = function(prf){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100218 prf.contentProvider = $stateParams.id;
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100219
220 var item = new CdnPrefix(prf);
221
222 item.$save()
223 .then(function(res){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100224 self.cp_prf.push(res);
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100225 })
226 .catch(function(e){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100227 self.result = {
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100228 status: 0,
229 msg: e.data.detail
230 };
231 });
232 };
233
234 this.removePrefix = function(item){
235 item.$delete()
236 .then(function(){
Matteo Scandolo40bea632016-05-17 16:51:29 -0700237 _.remove(self.cp_prf, item);
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100238 })
239 .catch(function(e){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100240 self.result = {
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100241 status: 0,
242 msg: e.data.detail
243 };
244 });
245 };
246 }
247 };
248})
Matteo Scandolo54bc5f72016-05-18 14:06:45 -0700249.directive('contentProviderServer', function($stateParams, OriginServer, ContentProvider){
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100250 return{
251 restrict: 'E',
252 controllerAs: 'vm',
253 scope: {},
Matteo Scandolobbcc01b2015-11-04 16:30:43 +0100254 templateUrl: 'templates/cp_origin_server.html',
Matteo Scandolo54bc5f72016-05-18 14:06:45 -0700255 controller: function(_){
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100256 this.pageName = 'server';
Matteo Scandolo7db08432015-11-06 18:49:33 +0100257 this.protocols = {'http': 'HTTP', 'rtmp': 'RTMP', 'rtp': 'RTP', 'shout': 'SHOUTcast'};
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100258
Matteo Scandolo7db08432015-11-06 18:49:33 +0100259 var self = this;
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100260
Matteo Scandolo7db08432015-11-06 18:49:33 +0100261 if($stateParams.id){
262 ContentProvider.get({id: $stateParams.id}).$promise
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100263 .then(function(cp){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100264 self.cp = cp;
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100265 }).catch(function(e){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100266 self.result = {
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100267 status: 0,
268 msg: e.data.detail
269 };
270 });
271 }
272
Matteo Scandolo7db08432015-11-06 18:49:33 +0100273 OriginServer.query({contentProvider: $stateParams.id}).$promise
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100274 .then(function(cp_os){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100275 self.cp_os = cp_os;
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100276 }).catch(function(e){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100277 self.result = {
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100278 status: 0,
279 msg: e.data.detail
280 };
281 });
282
283 this.addOrigin = function(os){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100284 os.contentProvider = $stateParams.id;
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100285
286 var item = new OriginServer(os);
287
288 item.$save()
289 .then(function(res){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100290 self.cp_os.push(res);
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100291 })
292 .catch(function(e){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100293 self.result = {
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100294 status: 0,
295 msg: e.data.detail
296 };
297 });
298 };
299
300 this.removeOrigin = function(item){
301 item.$delete()
302 .then(function(){
Matteo Scandolo40bea632016-05-17 16:51:29 -0700303 _.remove(self.cp_os, item);
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100304 })
305 .catch(function(e){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100306 self.result = {
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100307 status: 0,
308 msg: e.data.detail
309 };
310 });
311 };
312 }
313 };
314})
Matteo Scandolo54bc5f72016-05-18 14:06:45 -0700315.directive('contentProviderUsers', function($stateParams, ContentProvider, User){
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100316 return{
317 restrict: 'E',
318 controllerAs: 'vm',
319 scope: {},
Matteo Scandolobbcc01b2015-11-04 16:30:43 +0100320 templateUrl: 'templates/cp_user.html',
Matteo Scandolo54bc5f72016-05-18 14:06:45 -0700321 controller: function(_){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100322 var self = this;
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100323
324 this.pageName = 'user';
325
326 this.cp_users = [];
327
Matteo Scandolo7db08432015-11-06 18:49:33 +0100328 if($stateParams.id){
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100329 User.query().$promise
330 .then(function(users){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100331 self.users = users;
332 return ContentProvider.get({id: $stateParams.id}).$promise;
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100333 })
334 .then(function(res){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100335 res.users = self.populateUser(res.users, self.users);
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100336 return res;
337 })
338 .then(function(cp){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100339 self.cp = cp;
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100340 }).catch(function(e){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100341 self.result = {
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100342 status: 0,
343 msg: e.data.detail
344 };
345 });
346 }
347
348 this.populateUser = function(ids, list){
349 for(var i = 0; i < ids.length; i++){
Matteo Scandolo40bea632016-05-17 16:51:29 -0700350 ids[i] = _.find(list, {id: ids[i]});
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100351 }
352 return ids;
353 };
354
355 this.addUserToCp = function(user){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100356 self.cp.users.push(user);
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100357 };
358
359 this.removeUserFromCp = function(user){
Matteo Scandolo40bea632016-05-17 16:51:29 -0700360 _.remove(self.cp.users, user);
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100361 };
362
363 this.saveContentProvider = function(cp){
364
365 // flatten the user to id of array
Matteo Scandolo54bc5f72016-05-18 14:06:45 -0700366 cp.users = _.map(cp.users, 'id');
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100367
368 cp.$update()
369 .then(function(res){
370
Matteo Scandolo7db08432015-11-06 18:49:33 +0100371 self.cp.users = self.populateUser(res.users, self.users);
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100372
Matteo Scandolo7db08432015-11-06 18:49:33 +0100373 self.result = {
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100374 status: 1,
375 msg: 'Content Provider Saved'
376 };
377
378 })
379 .catch(function(e){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100380 self.result = {
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100381 status: 0,
382 msg: e.data.detail
383 };
384 });
385 };
386 }
387 };
388});