blob: 644867965ed137d19dcd1baddb94de4aac3e2ed9 [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 'ngLodash',
7 'xos.helpers',
Matteo Scandolod44ceb42015-11-06 17:14:28 +01008 'ui.router',
Matteo Scandolobbcc01b2015-11-04 16:30:43 +01009 'xos.xos'
Matteo Scandolo15d692c2015-10-28 12:00:07 +010010])
Matteo Scandolod44ceb42015-11-06 17:14:28 +010011.config(($stateProvider, $urlRouterProvider) => {
Matteo Scandolo7db08432015-11-06 18:49:33 +010012
Matteo Scandolod44ceb42015-11-06 17:14:28 +010013 $stateProvider
14 .state('list', {
15 url: '/',
Matteo Scandolo15d692c2015-10-28 12:00:07 +010016 template: '<content-provider-list></content-provider-list>',
17 })
Matteo Scandolod44ceb42015-11-06 17:14:28 +010018 .state('details', {
Matteo Scandolo7db08432015-11-06 18:49:33 +010019 url: '/contentProvider/:id',
Matteo Scandolo15d692c2015-10-28 12:00:07 +010020 template: '<content-provider-detail></content-provider-detail>'
21 })
Matteo Scandolod44ceb42015-11-06 17:14:28 +010022 .state('cdn', {
23 url: '/contentProvider/:id/cdn_prefix',
Matteo Scandolo15d692c2015-10-28 12:00:07 +010024 template: '<content-provider-cdn></content-provider-cdn>'
25 })
Matteo Scandolod44ceb42015-11-06 17:14:28 +010026 .state('server', {
27 url: '/contentProvider/:id/origin_server',
Matteo Scandolo15d692c2015-10-28 12:00:07 +010028 template: '<content-provider-server></content-provider-server>'
29 })
Matteo Scandolod44ceb42015-11-06 17:14:28 +010030 .state('users', {
31 url: '/contentProvider/:id/users',
Matteo Scandolo15d692c2015-10-28 12:00:07 +010032 template: '<content-provider-users></content-provider-users>'
Matteo Scandolod44ceb42015-11-06 17:14:28 +010033 });
Matteo Scandolo15d692c2015-10-28 12:00:07 +010034})
35.config(function($httpProvider){
Matteo Scandolo15d692c2015-10-28 12:00:07 +010036 // add X-CSRFToken header for update, create, delete (!GET)
37 $httpProvider.interceptors.push('SetCSRFToken');
Matteo Scandolobbcc01b2015-11-04 16:30:43 +010038 $httpProvider.interceptors.push('NoHyperlinks');
Matteo Scandolo15d692c2015-10-28 12:00:07 +010039})
40.service('ContentProvider', function($resource){
41 return $resource('/hpcapi/contentproviders/:id/', {id: '@id'}, {
42 'update': {method: 'PUT'}
43 });
44})
45.service('ServiceProvider', function($resource){
46 return $resource('/hpcapi/serviceproviders/:id/', {id: '@id'});
47})
48.service('CdnPrefix', function($resource){
49 return $resource('/hpcapi/cdnprefixs/:id/', {id: '@id'});
50})
51.service('OriginServer', function($resource){
52 return $resource('/hpcapi/originservers/:id/', {id: '@id'});
53})
54.service('User', function($resource){
55 return $resource('/xos/users/:id/', {id: '@id'});
56})
57.directive('cpActions', function(ContentProvider, $location){
58 return {
59 restrict: 'E',
60 scope: {
61 id: '=id',
62 },
63 bindToController: true,
64 controllerAs: 'vm',
Matteo Scandolobbcc01b2015-11-04 16:30:43 +010065 templateUrl: 'templates/cp_actions.html',
Matteo Scandolo15d692c2015-10-28 12:00:07 +010066 controller: function(){
67 this.deleteCp = function(id){
68 ContentProvider.delete({id: id}).$promise
69 .then(function(){
70 $location.url('/');
71 });
72 };
73 }
74 };
75})
76.directive('contentProviderList', function(ContentProvider, lodash){
77 return {
78 restrict: 'E',
79 controllerAs: 'vm',
80 scope: {},
Matteo Scandolobbcc01b2015-11-04 16:30:43 +010081 templateUrl: 'templates/cp_list.html',
Matteo Scandolo15d692c2015-10-28 12:00:07 +010082 controller: function(){
Matteo Scandolo775d3242016-03-24 16:12:18 -070083 ['Name', 'Description', 'Status', 'Actions']
84 this.tableConfig = {
85 columns: [
86 {
87 label: 'Name',
88 field: 'humanReadableName'
89 },
90 {
91 label: 'Description',
92 field: 'description'
93 },
94 {
95 label: 'Status',
96 field: 'enabled'
97 }
98 ],
99 enableActions: true
100 };
101
Matteo Scandolo7db08432015-11-06 18:49:33 +0100102 var self = this;
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100103
104 ContentProvider.query().$promise
105 .then(function(cp){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100106 self.contentProviderList = cp;
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100107 })
108 .catch(function(e){
109 throw new Error(e);
110 });
111
112 this.deleteCp = function(id){
113 ContentProvider.delete({id: id}).$promise
114 .then(function(){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100115 lodash.remove(self.contentProviderList, {id: id});
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100116 });
117 };
118 }
119 };
120})
Matteo Scandolo7db08432015-11-06 18:49:33 +0100121.directive('contentProviderDetail', function(ContentProvider, ServiceProvider, $stateParams, $location){
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100122 return {
123 restrict: 'E',
124 controllerAs: 'vm',
125 scope: {},
Matteo Scandolobbcc01b2015-11-04 16:30:43 +0100126 templateUrl: 'templates/cp_detail.html',
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100127 controller: function(){
128 this.pageName = 'detail';
Matteo Scandolo7db08432015-11-06 18:49:33 +0100129 var self = this;
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100130
Matteo Scandolo7db08432015-11-06 18:49:33 +0100131 if($stateParams.id){
132 ContentProvider.get({id: $stateParams.id}).$promise
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100133 .then(function(cp){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100134 self.cp = cp;
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100135 }).catch(function(e){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100136 self.result = {
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100137 status: 0,
138 msg: e.data.detail
139 };
140 });
141 }
142 else{
Matteo Scandolo7db08432015-11-06 18:49:33 +0100143 self.cp = new ContentProvider();
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100144 }
145
146 ServiceProvider.query().$promise
147 .then(function(sp){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100148 self.sp = sp;
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100149 });
150
151 this.saveContentProvider = function(cp){
152 var p, isNew = false;
153
154 if(cp.id){
155 p = cp.$update();
156 }
157 else{
158 isNew = true;
159 cp.name = cp.humanReadableName;
160 p = cp.$save();
161 }
162
163 p.then(function(res){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100164 self.result = {
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100165 status: 1,
166 msg: 'Content Provider Saved'
167 };
168 if(isNew){
169 $location.url('contentProvider/' + res.id + '/');
170 }
171 })
172 .catch(function(e){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100173 self.result = {
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100174 status: 0,
175 msg: e.data.detail
176 };
177 });
178 };
179 }
180 };
181})
Matteo Scandolo7db08432015-11-06 18:49:33 +0100182.directive('contentProviderCdn', function($stateParams, CdnPrefix, ContentProvider, lodash){
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100183 return{
184 restrict: 'E',
185 controllerAs: 'vm',
186 scope: {},
Matteo Scandolobbcc01b2015-11-04 16:30:43 +0100187 templateUrl: 'templates/cp_cdn_prefix.html',
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100188 controller: function(){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100189 var self = this;
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100190
191 this.pageName = 'cdn';
192
Matteo Scandolo7db08432015-11-06 18:49:33 +0100193 if($stateParams.id){
194 ContentProvider.get({id: $stateParams.id}).$promise
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100195 .then(function(cp){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100196 self.cp = cp;
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100197 }).catch(function(e){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100198 self.result = {
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100199 status: 0,
200 msg: e.data.detail
201 };
202 });
203 }
204
205 CdnPrefix.query().$promise
206 .then(function(prf){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100207 self.prf = prf;
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100208 // set the active CdnPrefix for this contentProvider
Matteo Scandolo7db08432015-11-06 18:49:33 +0100209 self.cp_prf = lodash.where(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 Scandolo7db08432015-11-06 18:49:33 +0100237 lodash.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 Scandolo7db08432015-11-06 18:49:33 +0100249.directive('contentProviderServer', function($stateParams, OriginServer, ContentProvider, lodash){
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 Scandolo15d692c2015-10-28 12:00:07 +0100255 controller: function(){
256 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 Scandolo7db08432015-11-06 18:49:33 +0100303 lodash.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 Scandolo7db08432015-11-06 18:49:33 +0100315.directive('contentProviderUsers', function($stateParams, ContentProvider, User, lodash){
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 Scandolo15d692c2015-10-28 12:00:07 +0100321 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++){
350 ids[i] = lodash.find(list, {id: ids[i]});
351 }
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 Scandolo7db08432015-11-06 18:49:33 +0100360 lodash.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
366 cp.users = lodash.pluck(cp.users, 'id');
367
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});