Matteo Scandolo | 82697c0 | 2015-11-04 16:30:43 +0100 | [diff] [blame] | 1 | 'use strict'; |
| 2 | |
| 3 | angular.module('xos.contentProvider', [ |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 4 | 'ngResource', |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 5 | 'ngCookies', |
Matteo Scandolo | 82697c0 | 2015-11-04 16:30:43 +0100 | [diff] [blame] | 6 | 'ngLodash', |
| 7 | 'xos.helpers', |
Matteo Scandolo | c5ee7b5 | 2015-11-06 17:14:28 +0100 | [diff] [blame] | 8 | 'ui.router', |
Matteo Scandolo | 82697c0 | 2015-11-04 16:30:43 +0100 | [diff] [blame] | 9 | 'xos.xos' |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 10 | ]) |
Matteo Scandolo | c5ee7b5 | 2015-11-06 17:14:28 +0100 | [diff] [blame] | 11 | .config(($stateProvider, $urlRouterProvider) => { |
Matteo Scandolo | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 12 | |
Matteo Scandolo | c5ee7b5 | 2015-11-06 17:14:28 +0100 | [diff] [blame] | 13 | $stateProvider |
| 14 | .state('list', { |
| 15 | url: '/', |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 16 | template: '<content-provider-list></content-provider-list>', |
| 17 | }) |
Matteo Scandolo | c5ee7b5 | 2015-11-06 17:14:28 +0100 | [diff] [blame] | 18 | .state('details', { |
Matteo Scandolo | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 19 | url: '/contentProvider/:id', |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 20 | template: '<content-provider-detail></content-provider-detail>' |
| 21 | }) |
Matteo Scandolo | c5ee7b5 | 2015-11-06 17:14:28 +0100 | [diff] [blame] | 22 | .state('cdn', { |
| 23 | url: '/contentProvider/:id/cdn_prefix', |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 24 | template: '<content-provider-cdn></content-provider-cdn>' |
| 25 | }) |
Matteo Scandolo | c5ee7b5 | 2015-11-06 17:14:28 +0100 | [diff] [blame] | 26 | .state('server', { |
| 27 | url: '/contentProvider/:id/origin_server', |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 28 | template: '<content-provider-server></content-provider-server>' |
| 29 | }) |
Matteo Scandolo | c5ee7b5 | 2015-11-06 17:14:28 +0100 | [diff] [blame] | 30 | .state('users', { |
| 31 | url: '/contentProvider/:id/users', |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 32 | template: '<content-provider-users></content-provider-users>' |
Matteo Scandolo | c5ee7b5 | 2015-11-06 17:14:28 +0100 | [diff] [blame] | 33 | }); |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 34 | }) |
| 35 | .config(function($httpProvider){ |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 36 | // add X-CSRFToken header for update, create, delete (!GET) |
| 37 | $httpProvider.interceptors.push('SetCSRFToken'); |
Matteo Scandolo | 82697c0 | 2015-11-04 16:30:43 +0100 | [diff] [blame] | 38 | $httpProvider.interceptors.push('NoHyperlinks'); |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 39 | }) |
| 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 Scandolo | 82697c0 | 2015-11-04 16:30:43 +0100 | [diff] [blame] | 65 | templateUrl: 'templates/cp_actions.html', |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 66 | 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 Scandolo | 82697c0 | 2015-11-04 16:30:43 +0100 | [diff] [blame] | 81 | templateUrl: 'templates/cp_list.html', |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 82 | controller: function(){ |
Matteo Scandolo | c443c2d | 2016-03-24 16:12:18 -0700 | [diff] [blame] | 83 | ['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 Scandolo | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 102 | var self = this; |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 103 | |
| 104 | ContentProvider.query().$promise |
| 105 | .then(function(cp){ |
Matteo Scandolo | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 106 | self.contentProviderList = cp; |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 107 | }) |
| 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 Scandolo | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 115 | lodash.remove(self.contentProviderList, {id: id}); |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 116 | }); |
| 117 | }; |
| 118 | } |
| 119 | }; |
| 120 | }) |
Matteo Scandolo | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 121 | .directive('contentProviderDetail', function(ContentProvider, ServiceProvider, $stateParams, $location){ |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 122 | return { |
| 123 | restrict: 'E', |
| 124 | controllerAs: 'vm', |
| 125 | scope: {}, |
Matteo Scandolo | 82697c0 | 2015-11-04 16:30:43 +0100 | [diff] [blame] | 126 | templateUrl: 'templates/cp_detail.html', |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 127 | controller: function(){ |
| 128 | this.pageName = 'detail'; |
Matteo Scandolo | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 129 | var self = this; |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 130 | |
Matteo Scandolo | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 131 | if($stateParams.id){ |
| 132 | ContentProvider.get({id: $stateParams.id}).$promise |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 133 | .then(function(cp){ |
Matteo Scandolo | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 134 | self.cp = cp; |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 135 | }).catch(function(e){ |
Matteo Scandolo | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 136 | self.result = { |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 137 | status: 0, |
| 138 | msg: e.data.detail |
| 139 | }; |
| 140 | }); |
| 141 | } |
| 142 | else{ |
Matteo Scandolo | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 143 | self.cp = new ContentProvider(); |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | ServiceProvider.query().$promise |
| 147 | .then(function(sp){ |
Matteo Scandolo | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 148 | self.sp = sp; |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 149 | }); |
| 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 Scandolo | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 164 | self.result = { |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 165 | status: 1, |
| 166 | msg: 'Content Provider Saved' |
| 167 | }; |
| 168 | if(isNew){ |
| 169 | $location.url('contentProvider/' + res.id + '/'); |
| 170 | } |
| 171 | }) |
| 172 | .catch(function(e){ |
Matteo Scandolo | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 173 | self.result = { |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 174 | status: 0, |
| 175 | msg: e.data.detail |
| 176 | }; |
| 177 | }); |
| 178 | }; |
| 179 | } |
| 180 | }; |
| 181 | }) |
Matteo Scandolo | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 182 | .directive('contentProviderCdn', function($stateParams, CdnPrefix, ContentProvider, lodash){ |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 183 | return{ |
| 184 | restrict: 'E', |
| 185 | controllerAs: 'vm', |
| 186 | scope: {}, |
Matteo Scandolo | 82697c0 | 2015-11-04 16:30:43 +0100 | [diff] [blame] | 187 | templateUrl: 'templates/cp_cdn_prefix.html', |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 188 | controller: function(){ |
Matteo Scandolo | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 189 | var self = this; |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 190 | |
| 191 | this.pageName = 'cdn'; |
| 192 | |
Matteo Scandolo | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 193 | if($stateParams.id){ |
| 194 | ContentProvider.get({id: $stateParams.id}).$promise |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 195 | .then(function(cp){ |
Matteo Scandolo | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 196 | self.cp = cp; |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 197 | }).catch(function(e){ |
Matteo Scandolo | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 198 | self.result = { |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 199 | status: 0, |
| 200 | msg: e.data.detail |
| 201 | }; |
| 202 | }); |
| 203 | } |
| 204 | |
| 205 | CdnPrefix.query().$promise |
| 206 | .then(function(prf){ |
Matteo Scandolo | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 207 | self.prf = prf; |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 208 | // set the active CdnPrefix for this contentProvider |
Matteo Scandolo | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 209 | self.cp_prf = lodash.where(prf, {contentProvider: parseInt($stateParams.id)}); |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 210 | }).catch(function(e){ |
Matteo Scandolo | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 211 | self.result = { |
Matteo Scandolo | f5cbbb1 | 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 | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 218 | prf.contentProvider = $stateParams.id; |
Matteo Scandolo | f5cbbb1 | 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 | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 224 | self.cp_prf.push(res); |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 225 | }) |
| 226 | .catch(function(e){ |
Matteo Scandolo | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 227 | self.result = { |
Matteo Scandolo | f5cbbb1 | 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 | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 237 | lodash.remove(self.cp_prf, item); |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 238 | }) |
| 239 | .catch(function(e){ |
Matteo Scandolo | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 240 | self.result = { |
Matteo Scandolo | f5cbbb1 | 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 | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 249 | .directive('contentProviderServer', function($stateParams, OriginServer, ContentProvider, lodash){ |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 250 | return{ |
| 251 | restrict: 'E', |
| 252 | controllerAs: 'vm', |
| 253 | scope: {}, |
Matteo Scandolo | 82697c0 | 2015-11-04 16:30:43 +0100 | [diff] [blame] | 254 | templateUrl: 'templates/cp_origin_server.html', |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 255 | controller: function(){ |
| 256 | this.pageName = 'server'; |
Matteo Scandolo | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 257 | this.protocols = {'http': 'HTTP', 'rtmp': 'RTMP', 'rtp': 'RTP', 'shout': 'SHOUTcast'}; |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 258 | |
Matteo Scandolo | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 259 | var self = this; |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 260 | |
Matteo Scandolo | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 261 | if($stateParams.id){ |
| 262 | ContentProvider.get({id: $stateParams.id}).$promise |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 263 | .then(function(cp){ |
Matteo Scandolo | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 264 | self.cp = cp; |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 265 | }).catch(function(e){ |
Matteo Scandolo | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 266 | self.result = { |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 267 | status: 0, |
| 268 | msg: e.data.detail |
| 269 | }; |
| 270 | }); |
| 271 | } |
| 272 | |
Matteo Scandolo | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 273 | OriginServer.query({contentProvider: $stateParams.id}).$promise |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 274 | .then(function(cp_os){ |
Matteo Scandolo | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 275 | self.cp_os = cp_os; |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 276 | }).catch(function(e){ |
Matteo Scandolo | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 277 | self.result = { |
Matteo Scandolo | f5cbbb1 | 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 | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 284 | os.contentProvider = $stateParams.id; |
Matteo Scandolo | f5cbbb1 | 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 | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 290 | self.cp_os.push(res); |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 291 | }) |
| 292 | .catch(function(e){ |
Matteo Scandolo | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 293 | self.result = { |
Matteo Scandolo | f5cbbb1 | 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 | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 303 | lodash.remove(self.cp_os, item); |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 304 | }) |
| 305 | .catch(function(e){ |
Matteo Scandolo | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 306 | self.result = { |
Matteo Scandolo | f5cbbb1 | 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 | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 315 | .directive('contentProviderUsers', function($stateParams, ContentProvider, User, lodash){ |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 316 | return{ |
| 317 | restrict: 'E', |
| 318 | controllerAs: 'vm', |
| 319 | scope: {}, |
Matteo Scandolo | 82697c0 | 2015-11-04 16:30:43 +0100 | [diff] [blame] | 320 | templateUrl: 'templates/cp_user.html', |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 321 | controller: function(){ |
Matteo Scandolo | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 322 | var self = this; |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 323 | |
| 324 | this.pageName = 'user'; |
| 325 | |
| 326 | this.cp_users = []; |
| 327 | |
Matteo Scandolo | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 328 | if($stateParams.id){ |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 329 | User.query().$promise |
| 330 | .then(function(users){ |
Matteo Scandolo | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 331 | self.users = users; |
| 332 | return ContentProvider.get({id: $stateParams.id}).$promise; |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 333 | }) |
| 334 | .then(function(res){ |
Matteo Scandolo | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 335 | res.users = self.populateUser(res.users, self.users); |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 336 | return res; |
| 337 | }) |
| 338 | .then(function(cp){ |
Matteo Scandolo | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 339 | self.cp = cp; |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 340 | }).catch(function(e){ |
Matteo Scandolo | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 341 | self.result = { |
Matteo Scandolo | f5cbbb1 | 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++){ |
| 350 | ids[i] = lodash.find(list, {id: ids[i]}); |
| 351 | } |
| 352 | return ids; |
| 353 | }; |
| 354 | |
| 355 | this.addUserToCp = function(user){ |
Matteo Scandolo | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 356 | self.cp.users.push(user); |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 357 | }; |
| 358 | |
| 359 | this.removeUserFromCp = function(user){ |
Matteo Scandolo | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 360 | lodash.remove(self.cp.users, user); |
Matteo Scandolo | f5cbbb1 | 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 |
| 366 | cp.users = lodash.pluck(cp.users, 'id'); |
| 367 | |
| 368 | cp.$update() |
| 369 | .then(function(res){ |
| 370 | |
Matteo Scandolo | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 371 | self.cp.users = self.populateUser(res.users, self.users); |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 372 | |
Matteo Scandolo | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 373 | self.result = { |
Matteo Scandolo | f5cbbb1 | 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 | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 380 | self.result = { |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 381 | status: 0, |
| 382 | msg: e.data.detail |
| 383 | }; |
| 384 | }); |
| 385 | }; |
| 386 | } |
| 387 | }; |
| 388 | }); |