Matteo Scandolo | bbcc01b | 2015-11-04 16:30:43 +0100 | [diff] [blame] | 1 | 'use strict'; |
| 2 | |
| 3 | angular.module('xos.contentProvider', [ |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 4 | 'ngResource', |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 5 | 'ngCookies', |
Matteo Scandolo | bbcc01b | 2015-11-04 16:30:43 +0100 | [diff] [blame] | 6 | 'xos.helpers', |
Matteo Scandolo | 40bea63 | 2016-05-17 16:51:29 -0700 | [diff] [blame] | 7 | 'ui.router' |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 8 | ]) |
Matteo Scandolo | 40bea63 | 2016-05-17 16:51:29 -0700 | [diff] [blame] | 9 | .config(($stateProvider) => { |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 10 | |
Matteo Scandolo | d44ceb4 | 2015-11-06 17:14:28 +0100 | [diff] [blame] | 11 | $stateProvider |
| 12 | .state('list', { |
| 13 | url: '/', |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 14 | template: '<content-provider-list></content-provider-list>', |
| 15 | }) |
Matteo Scandolo | d44ceb4 | 2015-11-06 17:14:28 +0100 | [diff] [blame] | 16 | .state('details', { |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 17 | url: '/contentProvider/:id', |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 18 | template: '<content-provider-detail></content-provider-detail>' |
| 19 | }) |
Matteo Scandolo | d44ceb4 | 2015-11-06 17:14:28 +0100 | [diff] [blame] | 20 | .state('cdn', { |
| 21 | url: '/contentProvider/:id/cdn_prefix', |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 22 | template: '<content-provider-cdn></content-provider-cdn>' |
| 23 | }) |
Matteo Scandolo | d44ceb4 | 2015-11-06 17:14:28 +0100 | [diff] [blame] | 24 | .state('server', { |
| 25 | url: '/contentProvider/:id/origin_server', |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 26 | template: '<content-provider-server></content-provider-server>' |
| 27 | }) |
Matteo Scandolo | d44ceb4 | 2015-11-06 17:14:28 +0100 | [diff] [blame] | 28 | .state('users', { |
| 29 | url: '/contentProvider/:id/users', |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 30 | template: '<content-provider-users></content-provider-users>' |
Matteo Scandolo | d44ceb4 | 2015-11-06 17:14:28 +0100 | [diff] [blame] | 31 | }); |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 32 | }) |
| 33 | .config(function($httpProvider){ |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 34 | // add X-CSRFToken header for update, create, delete (!GET) |
| 35 | $httpProvider.interceptors.push('SetCSRFToken'); |
Matteo Scandolo | bbcc01b | 2015-11-04 16:30:43 +0100 | [diff] [blame] | 36 | $httpProvider.interceptors.push('NoHyperlinks'); |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 37 | }) |
| 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 Scandolo | bbcc01b | 2015-11-04 16:30:43 +0100 | [diff] [blame] | 63 | templateUrl: 'templates/cp_actions.html', |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 64 | 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 Scandolo | 40bea63 | 2016-05-17 16:51:29 -0700 | [diff] [blame] | 74 | .directive('contentProviderList', function(ContentProvider, _){ |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 75 | return { |
| 76 | restrict: 'E', |
| 77 | controllerAs: 'vm', |
| 78 | scope: {}, |
Matteo Scandolo | bbcc01b | 2015-11-04 16:30:43 +0100 | [diff] [blame] | 79 | templateUrl: 'templates/cp_list.html', |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 80 | controller: function(){ |
Matteo Scandolo | 775d324 | 2016-03-24 16:12:18 -0700 | [diff] [blame] | 81 | ['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 Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 100 | var self = this; |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 101 | |
| 102 | ContentProvider.query().$promise |
| 103 | .then(function(cp){ |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 104 | self.contentProviderList = cp; |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 105 | }) |
| 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 Scandolo | 40bea63 | 2016-05-17 16:51:29 -0700 | [diff] [blame] | 113 | _.remove(self.contentProviderList, {id: id}); |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 114 | }); |
| 115 | }; |
| 116 | } |
| 117 | }; |
| 118 | }) |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 119 | .directive('contentProviderDetail', function(ContentProvider, ServiceProvider, $stateParams, $location){ |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 120 | return { |
| 121 | restrict: 'E', |
| 122 | controllerAs: 'vm', |
| 123 | scope: {}, |
Matteo Scandolo | bbcc01b | 2015-11-04 16:30:43 +0100 | [diff] [blame] | 124 | templateUrl: 'templates/cp_detail.html', |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 125 | controller: function(){ |
| 126 | this.pageName = 'detail'; |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 127 | var self = this; |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 128 | |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 129 | if($stateParams.id){ |
| 130 | ContentProvider.get({id: $stateParams.id}).$promise |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 131 | .then(function(cp){ |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 132 | self.cp = cp; |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 133 | }).catch(function(e){ |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 134 | self.result = { |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 135 | status: 0, |
| 136 | msg: e.data.detail |
| 137 | }; |
| 138 | }); |
| 139 | } |
| 140 | else{ |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 141 | self.cp = new ContentProvider(); |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | ServiceProvider.query().$promise |
| 145 | .then(function(sp){ |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 146 | self.sp = sp; |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 147 | }); |
| 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 Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 162 | self.result = { |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 163 | status: 1, |
| 164 | msg: 'Content Provider Saved' |
| 165 | }; |
| 166 | if(isNew){ |
| 167 | $location.url('contentProvider/' + res.id + '/'); |
| 168 | } |
| 169 | }) |
| 170 | .catch(function(e){ |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 171 | self.result = { |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 172 | status: 0, |
| 173 | msg: e.data.detail |
| 174 | }; |
| 175 | }); |
| 176 | }; |
| 177 | } |
| 178 | }; |
| 179 | }) |
Matteo Scandolo | 40bea63 | 2016-05-17 16:51:29 -0700 | [diff] [blame] | 180 | .directive('contentProviderCdn', function($stateParams, CdnPrefix, ContentProvider, _){ |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 181 | return{ |
| 182 | restrict: 'E', |
| 183 | controllerAs: 'vm', |
| 184 | scope: {}, |
Matteo Scandolo | bbcc01b | 2015-11-04 16:30:43 +0100 | [diff] [blame] | 185 | templateUrl: 'templates/cp_cdn_prefix.html', |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 186 | controller: function(){ |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 187 | var self = this; |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 188 | |
| 189 | this.pageName = 'cdn'; |
| 190 | |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 191 | if($stateParams.id){ |
| 192 | ContentProvider.get({id: $stateParams.id}).$promise |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 193 | .then(function(cp){ |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 194 | self.cp = cp; |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 195 | }).catch(function(e){ |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 196 | self.result = { |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 197 | status: 0, |
| 198 | msg: e.data.detail |
| 199 | }; |
| 200 | }); |
| 201 | } |
| 202 | |
| 203 | CdnPrefix.query().$promise |
| 204 | .then(function(prf){ |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 205 | self.prf = prf; |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 206 | // set the active CdnPrefix for this contentProvider |
Matteo Scandolo | 40bea63 | 2016-05-17 16:51:29 -0700 | [diff] [blame] | 207 | self.cp_prf = _.where(prf, {contentProvider: parseInt($stateParams.id)}); |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 208 | }).catch(function(e){ |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 209 | self.result = { |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 210 | status: 0, |
| 211 | msg: e.data.detail |
| 212 | }; |
| 213 | }); |
| 214 | |
| 215 | this.addPrefix = function(prf){ |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 216 | prf.contentProvider = $stateParams.id; |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 217 | |
| 218 | var item = new CdnPrefix(prf); |
| 219 | |
| 220 | item.$save() |
| 221 | .then(function(res){ |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 222 | self.cp_prf.push(res); |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 223 | }) |
| 224 | .catch(function(e){ |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 225 | self.result = { |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 226 | status: 0, |
| 227 | msg: e.data.detail |
| 228 | }; |
| 229 | }); |
| 230 | }; |
| 231 | |
| 232 | this.removePrefix = function(item){ |
| 233 | item.$delete() |
| 234 | .then(function(){ |
Matteo Scandolo | 40bea63 | 2016-05-17 16:51:29 -0700 | [diff] [blame] | 235 | _.remove(self.cp_prf, item); |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 236 | }) |
| 237 | .catch(function(e){ |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 238 | self.result = { |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 239 | status: 0, |
| 240 | msg: e.data.detail |
| 241 | }; |
| 242 | }); |
| 243 | }; |
| 244 | } |
| 245 | }; |
| 246 | }) |
Matteo Scandolo | 40bea63 | 2016-05-17 16:51:29 -0700 | [diff] [blame] | 247 | .directive('contentProviderServer', function($stateParams, OriginServer, ContentProvider, _){ |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 248 | return{ |
| 249 | restrict: 'E', |
| 250 | controllerAs: 'vm', |
| 251 | scope: {}, |
Matteo Scandolo | bbcc01b | 2015-11-04 16:30:43 +0100 | [diff] [blame] | 252 | templateUrl: 'templates/cp_origin_server.html', |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 253 | controller: function(){ |
| 254 | this.pageName = 'server'; |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 255 | this.protocols = {'http': 'HTTP', 'rtmp': 'RTMP', 'rtp': 'RTP', 'shout': 'SHOUTcast'}; |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 256 | |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 257 | var self = this; |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 258 | |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 259 | if($stateParams.id){ |
| 260 | ContentProvider.get({id: $stateParams.id}).$promise |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 261 | .then(function(cp){ |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 262 | self.cp = cp; |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 263 | }).catch(function(e){ |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 264 | self.result = { |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 265 | status: 0, |
| 266 | msg: e.data.detail |
| 267 | }; |
| 268 | }); |
| 269 | } |
| 270 | |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 271 | OriginServer.query({contentProvider: $stateParams.id}).$promise |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 272 | .then(function(cp_os){ |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 273 | self.cp_os = cp_os; |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 274 | }).catch(function(e){ |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 275 | self.result = { |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 276 | status: 0, |
| 277 | msg: e.data.detail |
| 278 | }; |
| 279 | }); |
| 280 | |
| 281 | this.addOrigin = function(os){ |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 282 | os.contentProvider = $stateParams.id; |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 283 | |
| 284 | var item = new OriginServer(os); |
| 285 | |
| 286 | item.$save() |
| 287 | .then(function(res){ |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 288 | self.cp_os.push(res); |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 289 | }) |
| 290 | .catch(function(e){ |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 291 | self.result = { |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 292 | status: 0, |
| 293 | msg: e.data.detail |
| 294 | }; |
| 295 | }); |
| 296 | }; |
| 297 | |
| 298 | this.removeOrigin = function(item){ |
| 299 | item.$delete() |
| 300 | .then(function(){ |
Matteo Scandolo | 40bea63 | 2016-05-17 16:51:29 -0700 | [diff] [blame] | 301 | _.remove(self.cp_os, item); |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 302 | }) |
| 303 | .catch(function(e){ |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 304 | self.result = { |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 305 | status: 0, |
| 306 | msg: e.data.detail |
| 307 | }; |
| 308 | }); |
| 309 | }; |
| 310 | } |
| 311 | }; |
| 312 | }) |
Matteo Scandolo | 40bea63 | 2016-05-17 16:51:29 -0700 | [diff] [blame] | 313 | .directive('contentProviderUsers', function($stateParams, ContentProvider, User, _){ |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 314 | return{ |
| 315 | restrict: 'E', |
| 316 | controllerAs: 'vm', |
| 317 | scope: {}, |
Matteo Scandolo | bbcc01b | 2015-11-04 16:30:43 +0100 | [diff] [blame] | 318 | templateUrl: 'templates/cp_user.html', |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 319 | controller: function(){ |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 320 | var self = this; |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 321 | |
| 322 | this.pageName = 'user'; |
| 323 | |
| 324 | this.cp_users = []; |
| 325 | |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 326 | if($stateParams.id){ |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 327 | User.query().$promise |
| 328 | .then(function(users){ |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 329 | self.users = users; |
| 330 | return ContentProvider.get({id: $stateParams.id}).$promise; |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 331 | }) |
| 332 | .then(function(res){ |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 333 | res.users = self.populateUser(res.users, self.users); |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 334 | return res; |
| 335 | }) |
| 336 | .then(function(cp){ |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 337 | self.cp = cp; |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 338 | }).catch(function(e){ |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 339 | self.result = { |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 340 | 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 Scandolo | 40bea63 | 2016-05-17 16:51:29 -0700 | [diff] [blame] | 348 | ids[i] = _.find(list, {id: ids[i]}); |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 349 | } |
| 350 | return ids; |
| 351 | }; |
| 352 | |
| 353 | this.addUserToCp = function(user){ |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 354 | self.cp.users.push(user); |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 355 | }; |
| 356 | |
| 357 | this.removeUserFromCp = function(user){ |
Matteo Scandolo | 40bea63 | 2016-05-17 16:51:29 -0700 | [diff] [blame] | 358 | _.remove(self.cp.users, user); |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 359 | }; |
| 360 | |
| 361 | this.saveContentProvider = function(cp){ |
| 362 | |
| 363 | // flatten the user to id of array |
Matteo Scandolo | 40bea63 | 2016-05-17 16:51:29 -0700 | [diff] [blame] | 364 | cp.users = _.pluck(cp.users, 'id'); |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 365 | |
| 366 | cp.$update() |
| 367 | .then(function(res){ |
| 368 | |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 369 | self.cp.users = self.populateUser(res.users, self.users); |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 370 | |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 371 | self.result = { |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 372 | status: 1, |
| 373 | msg: 'Content Provider Saved' |
| 374 | }; |
| 375 | |
| 376 | }) |
| 377 | .catch(function(e){ |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 378 | self.result = { |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 379 | status: 0, |
| 380 | msg: e.data.detail |
| 381 | }; |
| 382 | }); |
| 383 | }; |
| 384 | } |
| 385 | }; |
| 386 | }); |