blob: 6f93c0ac3ce433fa0af9387c6a56e3ab2b19d0d1 [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 Scandolo7db08432015-11-06 18:49:33 +010083 var self = this;
Matteo Scandolo15d692c2015-10-28 12:00:07 +010084
85 ContentProvider.query().$promise
86 .then(function(cp){
Matteo Scandolo7db08432015-11-06 18:49:33 +010087 self.contentProviderList = cp;
Matteo Scandolo15d692c2015-10-28 12:00:07 +010088 })
89 .catch(function(e){
90 throw new Error(e);
91 });
92
93 this.deleteCp = function(id){
94 ContentProvider.delete({id: id}).$promise
95 .then(function(){
Matteo Scandolo7db08432015-11-06 18:49:33 +010096 lodash.remove(self.contentProviderList, {id: id});
Matteo Scandolo15d692c2015-10-28 12:00:07 +010097 });
98 };
99 }
100 };
101})
Matteo Scandolo7db08432015-11-06 18:49:33 +0100102.directive('contentProviderDetail', function(ContentProvider, ServiceProvider, $stateParams, $location){
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100103 return {
104 restrict: 'E',
105 controllerAs: 'vm',
106 scope: {},
Matteo Scandolobbcc01b2015-11-04 16:30:43 +0100107 templateUrl: 'templates/cp_detail.html',
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100108 controller: function(){
109 this.pageName = 'detail';
Matteo Scandolo7db08432015-11-06 18:49:33 +0100110 var self = this;
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100111
Matteo Scandolo7db08432015-11-06 18:49:33 +0100112 if($stateParams.id){
113 ContentProvider.get({id: $stateParams.id}).$promise
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100114 .then(function(cp){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100115 self.cp = cp;
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100116 }).catch(function(e){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100117 self.result = {
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100118 status: 0,
119 msg: e.data.detail
120 };
121 });
122 }
123 else{
Matteo Scandolo7db08432015-11-06 18:49:33 +0100124 self.cp = new ContentProvider();
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100125 }
126
127 ServiceProvider.query().$promise
128 .then(function(sp){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100129 self.sp = sp;
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100130 });
131
132 this.saveContentProvider = function(cp){
133 var p, isNew = false;
134
135 if(cp.id){
136 p = cp.$update();
137 }
138 else{
139 isNew = true;
140 cp.name = cp.humanReadableName;
141 p = cp.$save();
142 }
143
144 p.then(function(res){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100145 self.result = {
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100146 status: 1,
147 msg: 'Content Provider Saved'
148 };
149 if(isNew){
150 $location.url('contentProvider/' + res.id + '/');
151 }
152 })
153 .catch(function(e){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100154 self.result = {
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100155 status: 0,
156 msg: e.data.detail
157 };
158 });
159 };
160 }
161 };
162})
Matteo Scandolo7db08432015-11-06 18:49:33 +0100163.directive('contentProviderCdn', function($stateParams, CdnPrefix, ContentProvider, lodash){
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100164 return{
165 restrict: 'E',
166 controllerAs: 'vm',
167 scope: {},
Matteo Scandolobbcc01b2015-11-04 16:30:43 +0100168 templateUrl: 'templates/cp_cdn_prefix.html',
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100169 controller: function(){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100170 var self = this;
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100171
172 this.pageName = 'cdn';
173
Matteo Scandolo7db08432015-11-06 18:49:33 +0100174 if($stateParams.id){
175 ContentProvider.get({id: $stateParams.id}).$promise
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100176 .then(function(cp){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100177 self.cp = cp;
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100178 }).catch(function(e){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100179 self.result = {
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100180 status: 0,
181 msg: e.data.detail
182 };
183 });
184 }
185
186 CdnPrefix.query().$promise
187 .then(function(prf){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100188 self.prf = prf;
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100189 // set the active CdnPrefix for this contentProvider
Matteo Scandolo7db08432015-11-06 18:49:33 +0100190 self.cp_prf = lodash.where(prf, {contentProvider: parseInt($stateParams.id)});
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100191 }).catch(function(e){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100192 self.result = {
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100193 status: 0,
194 msg: e.data.detail
195 };
196 });
197
198 this.addPrefix = function(prf){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100199 prf.contentProvider = $stateParams.id;
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100200
201 var item = new CdnPrefix(prf);
202
203 item.$save()
204 .then(function(res){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100205 self.cp_prf.push(res);
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100206 })
207 .catch(function(e){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100208 self.result = {
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100209 status: 0,
210 msg: e.data.detail
211 };
212 });
213 };
214
215 this.removePrefix = function(item){
216 item.$delete()
217 .then(function(){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100218 lodash.remove(self.cp_prf, item);
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100219 })
220 .catch(function(e){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100221 self.result = {
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100222 status: 0,
223 msg: e.data.detail
224 };
225 });
226 };
227 }
228 };
229})
Matteo Scandolo7db08432015-11-06 18:49:33 +0100230.directive('contentProviderServer', function($stateParams, OriginServer, ContentProvider, lodash){
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100231 return{
232 restrict: 'E',
233 controllerAs: 'vm',
234 scope: {},
Matteo Scandolobbcc01b2015-11-04 16:30:43 +0100235 templateUrl: 'templates/cp_origin_server.html',
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100236 controller: function(){
237 this.pageName = 'server';
Matteo Scandolo7db08432015-11-06 18:49:33 +0100238 this.protocols = {'http': 'HTTP', 'rtmp': 'RTMP', 'rtp': 'RTP', 'shout': 'SHOUTcast'};
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100239
Matteo Scandolo7db08432015-11-06 18:49:33 +0100240 var self = this;
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100241
Matteo Scandolo7db08432015-11-06 18:49:33 +0100242 if($stateParams.id){
243 ContentProvider.get({id: $stateParams.id}).$promise
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100244 .then(function(cp){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100245 self.cp = cp;
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100246 }).catch(function(e){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100247 self.result = {
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100248 status: 0,
249 msg: e.data.detail
250 };
251 });
252 }
253
Matteo Scandolo7db08432015-11-06 18:49:33 +0100254 OriginServer.query({contentProvider: $stateParams.id}).$promise
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100255 .then(function(cp_os){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100256 self.cp_os = cp_os;
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100257 }).catch(function(e){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100258 self.result = {
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100259 status: 0,
260 msg: e.data.detail
261 };
262 });
263
264 this.addOrigin = function(os){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100265 os.contentProvider = $stateParams.id;
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100266
267 var item = new OriginServer(os);
268
269 item.$save()
270 .then(function(res){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100271 self.cp_os.push(res);
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100272 })
273 .catch(function(e){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100274 self.result = {
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100275 status: 0,
276 msg: e.data.detail
277 };
278 });
279 };
280
281 this.removeOrigin = function(item){
282 item.$delete()
283 .then(function(){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100284 lodash.remove(self.cp_os, item);
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100285 })
286 .catch(function(e){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100287 self.result = {
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100288 status: 0,
289 msg: e.data.detail
290 };
291 });
292 };
293 }
294 };
295})
Matteo Scandolo7db08432015-11-06 18:49:33 +0100296.directive('contentProviderUsers', function($stateParams, ContentProvider, User, lodash){
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100297 return{
298 restrict: 'E',
299 controllerAs: 'vm',
300 scope: {},
Matteo Scandolobbcc01b2015-11-04 16:30:43 +0100301 templateUrl: 'templates/cp_user.html',
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100302 controller: function(){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100303 var self = this;
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100304
305 this.pageName = 'user';
306
307 this.cp_users = [];
308
Matteo Scandolo7db08432015-11-06 18:49:33 +0100309 if($stateParams.id){
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100310 User.query().$promise
311 .then(function(users){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100312 self.users = users;
313 return ContentProvider.get({id: $stateParams.id}).$promise;
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100314 })
315 .then(function(res){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100316 res.users = self.populateUser(res.users, self.users);
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100317 return res;
318 })
319 .then(function(cp){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100320 self.cp = cp;
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100321 }).catch(function(e){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100322 self.result = {
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100323 status: 0,
324 msg: e.data.detail
325 };
326 });
327 }
328
329 this.populateUser = function(ids, list){
330 for(var i = 0; i < ids.length; i++){
331 ids[i] = lodash.find(list, {id: ids[i]});
332 }
333 return ids;
334 };
335
336 this.addUserToCp = function(user){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100337 self.cp.users.push(user);
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100338 };
339
340 this.removeUserFromCp = function(user){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100341 lodash.remove(self.cp.users, user);
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100342 };
343
344 this.saveContentProvider = function(cp){
345
346 // flatten the user to id of array
347 cp.users = lodash.pluck(cp.users, 'id');
348
349 cp.$update()
350 .then(function(res){
351
Matteo Scandolo7db08432015-11-06 18:49:33 +0100352 self.cp.users = self.populateUser(res.users, self.users);
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100353
Matteo Scandolo7db08432015-11-06 18:49:33 +0100354 self.result = {
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100355 status: 1,
356 msg: 'Content Provider Saved'
357 };
358
359 })
360 .catch(function(e){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100361 self.result = {
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100362 status: 0,
363 msg: e.data.detail
364 };
365 });
366 };
367 }
368 };
369});