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 | 54bc5f7 | 2016-05-18 14:06:45 -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 | 54bc5f7 | 2016-05-18 14:06:45 -0700 | [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, |
Matteo Scandolo | 54bc5f7 | 2016-05-18 14:06:45 -0700 | [diff] [blame] | 198 | msg: e.data ? e.data.detail : '' |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 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 | 54bc5f7 | 2016-05-18 14:06:45 -0700 | [diff] [blame] | 206 | // console.log(prf, $stateParams.id); |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 207 | // set the active CdnPrefix for this contentProvider |
Matteo Scandolo | 54bc5f7 | 2016-05-18 14:06:45 -0700 | [diff] [blame] | 208 | self.cp_prf = []; |
| 209 | self.cp_prf.push(_.find(prf, {contentProvider: parseInt($stateParams.id)})); |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 210 | }).catch(function(e){ |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 211 | self.result = { |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 212 | status: 0, |
| 213 | msg: e.data.detail |
| 214 | }; |
| 215 | }); |
| 216 | |
| 217 | this.addPrefix = function(prf){ |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 218 | prf.contentProvider = $stateParams.id; |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 219 | |
| 220 | var item = new CdnPrefix(prf); |
| 221 | |
| 222 | item.$save() |
| 223 | .then(function(res){ |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 224 | self.cp_prf.push(res); |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 225 | }) |
| 226 | .catch(function(e){ |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 227 | self.result = { |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 228 | status: 0, |
| 229 | msg: e.data.detail |
| 230 | }; |
| 231 | }); |
| 232 | }; |
| 233 | |
| 234 | this.removePrefix = function(item){ |
| 235 | item.$delete() |
| 236 | .then(function(){ |
Matteo Scandolo | 40bea63 | 2016-05-17 16:51:29 -0700 | [diff] [blame] | 237 | _.remove(self.cp_prf, item); |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 238 | }) |
| 239 | .catch(function(e){ |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 240 | self.result = { |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 241 | status: 0, |
| 242 | msg: e.data.detail |
| 243 | }; |
| 244 | }); |
| 245 | }; |
| 246 | } |
| 247 | }; |
| 248 | }) |
Matteo Scandolo | 54bc5f7 | 2016-05-18 14:06:45 -0700 | [diff] [blame] | 249 | .directive('contentProviderServer', function($stateParams, OriginServer, ContentProvider){ |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 250 | return{ |
| 251 | restrict: 'E', |
| 252 | controllerAs: 'vm', |
| 253 | scope: {}, |
Matteo Scandolo | bbcc01b | 2015-11-04 16:30:43 +0100 | [diff] [blame] | 254 | templateUrl: 'templates/cp_origin_server.html', |
Matteo Scandolo | 54bc5f7 | 2016-05-18 14:06:45 -0700 | [diff] [blame] | 255 | controller: function(_){ |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 256 | this.pageName = 'server'; |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 257 | this.protocols = {'http': 'HTTP', 'rtmp': 'RTMP', 'rtp': 'RTP', 'shout': 'SHOUTcast'}; |
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 | var self = this; |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 260 | |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 261 | if($stateParams.id){ |
| 262 | ContentProvider.get({id: $stateParams.id}).$promise |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 263 | .then(function(cp){ |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 264 | self.cp = cp; |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 265 | }).catch(function(e){ |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 266 | self.result = { |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 267 | status: 0, |
| 268 | msg: e.data.detail |
| 269 | }; |
| 270 | }); |
| 271 | } |
| 272 | |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 273 | OriginServer.query({contentProvider: $stateParams.id}).$promise |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 274 | .then(function(cp_os){ |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 275 | self.cp_os = cp_os; |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 276 | }).catch(function(e){ |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 277 | self.result = { |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 278 | status: 0, |
| 279 | msg: e.data.detail |
| 280 | }; |
| 281 | }); |
| 282 | |
| 283 | this.addOrigin = function(os){ |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 284 | os.contentProvider = $stateParams.id; |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 285 | |
| 286 | var item = new OriginServer(os); |
| 287 | |
| 288 | item.$save() |
| 289 | .then(function(res){ |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 290 | self.cp_os.push(res); |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 291 | }) |
| 292 | .catch(function(e){ |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 293 | self.result = { |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 294 | status: 0, |
| 295 | msg: e.data.detail |
| 296 | }; |
| 297 | }); |
| 298 | }; |
| 299 | |
| 300 | this.removeOrigin = function(item){ |
| 301 | item.$delete() |
| 302 | .then(function(){ |
Matteo Scandolo | 40bea63 | 2016-05-17 16:51:29 -0700 | [diff] [blame] | 303 | _.remove(self.cp_os, item); |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 304 | }) |
| 305 | .catch(function(e){ |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 306 | self.result = { |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 307 | status: 0, |
| 308 | msg: e.data.detail |
| 309 | }; |
| 310 | }); |
| 311 | }; |
| 312 | } |
| 313 | }; |
| 314 | }) |
Matteo Scandolo | 54bc5f7 | 2016-05-18 14:06:45 -0700 | [diff] [blame] | 315 | .directive('contentProviderUsers', function($stateParams, ContentProvider, User){ |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 316 | return{ |
| 317 | restrict: 'E', |
| 318 | controllerAs: 'vm', |
| 319 | scope: {}, |
Matteo Scandolo | bbcc01b | 2015-11-04 16:30:43 +0100 | [diff] [blame] | 320 | templateUrl: 'templates/cp_user.html', |
Matteo Scandolo | 54bc5f7 | 2016-05-18 14:06:45 -0700 | [diff] [blame] | 321 | controller: function(_){ |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 322 | var self = this; |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 323 | |
| 324 | this.pageName = 'user'; |
| 325 | |
| 326 | this.cp_users = []; |
| 327 | |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 328 | if($stateParams.id){ |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 329 | User.query().$promise |
| 330 | .then(function(users){ |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 331 | self.users = users; |
| 332 | return ContentProvider.get({id: $stateParams.id}).$promise; |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 333 | }) |
| 334 | .then(function(res){ |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 335 | res.users = self.populateUser(res.users, self.users); |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 336 | return res; |
| 337 | }) |
| 338 | .then(function(cp){ |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 339 | self.cp = cp; |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 340 | }).catch(function(e){ |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 341 | self.result = { |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 342 | 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 Scandolo | 40bea63 | 2016-05-17 16:51:29 -0700 | [diff] [blame] | 350 | ids[i] = _.find(list, {id: ids[i]}); |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 351 | } |
| 352 | return ids; |
| 353 | }; |
| 354 | |
| 355 | this.addUserToCp = function(user){ |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 356 | self.cp.users.push(user); |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 357 | }; |
| 358 | |
| 359 | this.removeUserFromCp = function(user){ |
Matteo Scandolo | 40bea63 | 2016-05-17 16:51:29 -0700 | [diff] [blame] | 360 | _.remove(self.cp.users, user); |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 361 | }; |
| 362 | |
| 363 | this.saveContentProvider = function(cp){ |
| 364 | |
| 365 | // flatten the user to id of array |
Matteo Scandolo | 54bc5f7 | 2016-05-18 14:06:45 -0700 | [diff] [blame] | 366 | cp.users = _.map(cp.users, 'id'); |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 367 | |
| 368 | cp.$update() |
| 369 | .then(function(res){ |
| 370 | |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 371 | self.cp.users = self.populateUser(res.users, self.users); |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 372 | |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 373 | self.result = { |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 374 | status: 1, |
| 375 | msg: 'Content Provider Saved' |
| 376 | }; |
| 377 | |
| 378 | }) |
| 379 | .catch(function(e){ |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 380 | self.result = { |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 381 | status: 0, |
| 382 | msg: e.data.detail |
| 383 | }; |
| 384 | }); |
| 385 | }; |
| 386 | } |
| 387 | }; |
| 388 | }); |