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 | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 83 | var self = this; |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 84 | |
| 85 | ContentProvider.query().$promise |
| 86 | .then(function(cp){ |
Matteo Scandolo | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 87 | self.contentProviderList = cp; |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 88 | }) |
| 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 Scandolo | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 96 | lodash.remove(self.contentProviderList, {id: id}); |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 97 | }); |
| 98 | }; |
| 99 | } |
| 100 | }; |
| 101 | }) |
Matteo Scandolo | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 102 | .directive('contentProviderDetail', function(ContentProvider, ServiceProvider, $stateParams, $location){ |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 103 | return { |
| 104 | restrict: 'E', |
| 105 | controllerAs: 'vm', |
| 106 | scope: {}, |
Matteo Scandolo | 82697c0 | 2015-11-04 16:30:43 +0100 | [diff] [blame] | 107 | templateUrl: 'templates/cp_detail.html', |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 108 | controller: function(){ |
| 109 | this.pageName = 'detail'; |
Matteo Scandolo | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 110 | var self = this; |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 111 | |
Matteo Scandolo | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 112 | if($stateParams.id){ |
| 113 | ContentProvider.get({id: $stateParams.id}).$promise |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 114 | .then(function(cp){ |
Matteo Scandolo | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 115 | self.cp = cp; |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 116 | }).catch(function(e){ |
Matteo Scandolo | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 117 | self.result = { |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 118 | status: 0, |
| 119 | msg: e.data.detail |
| 120 | }; |
| 121 | }); |
| 122 | } |
| 123 | else{ |
Matteo Scandolo | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 124 | self.cp = new ContentProvider(); |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | ServiceProvider.query().$promise |
| 128 | .then(function(sp){ |
Matteo Scandolo | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 129 | self.sp = sp; |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 130 | }); |
| 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 Scandolo | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 145 | self.result = { |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 146 | status: 1, |
| 147 | msg: 'Content Provider Saved' |
| 148 | }; |
| 149 | if(isNew){ |
| 150 | $location.url('contentProvider/' + res.id + '/'); |
| 151 | } |
| 152 | }) |
| 153 | .catch(function(e){ |
Matteo Scandolo | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 154 | self.result = { |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 155 | status: 0, |
| 156 | msg: e.data.detail |
| 157 | }; |
| 158 | }); |
| 159 | }; |
| 160 | } |
| 161 | }; |
| 162 | }) |
Matteo Scandolo | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 163 | .directive('contentProviderCdn', function($stateParams, CdnPrefix, ContentProvider, lodash){ |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 164 | return{ |
| 165 | restrict: 'E', |
| 166 | controllerAs: 'vm', |
| 167 | scope: {}, |
Matteo Scandolo | 82697c0 | 2015-11-04 16:30:43 +0100 | [diff] [blame] | 168 | templateUrl: 'templates/cp_cdn_prefix.html', |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 169 | controller: function(){ |
Matteo Scandolo | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 170 | var self = this; |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 171 | |
| 172 | this.pageName = 'cdn'; |
| 173 | |
Matteo Scandolo | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 174 | if($stateParams.id){ |
| 175 | ContentProvider.get({id: $stateParams.id}).$promise |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 176 | .then(function(cp){ |
Matteo Scandolo | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 177 | self.cp = cp; |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 178 | }).catch(function(e){ |
Matteo Scandolo | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 179 | self.result = { |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 180 | status: 0, |
| 181 | msg: e.data.detail |
| 182 | }; |
| 183 | }); |
| 184 | } |
| 185 | |
| 186 | CdnPrefix.query().$promise |
| 187 | .then(function(prf){ |
Matteo Scandolo | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 188 | self.prf = prf; |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 189 | // set the active CdnPrefix for this contentProvider |
Matteo Scandolo | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 190 | self.cp_prf = lodash.where(prf, {contentProvider: parseInt($stateParams.id)}); |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 191 | }).catch(function(e){ |
Matteo Scandolo | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 192 | self.result = { |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 193 | status: 0, |
| 194 | msg: e.data.detail |
| 195 | }; |
| 196 | }); |
| 197 | |
| 198 | this.addPrefix = function(prf){ |
Matteo Scandolo | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 199 | prf.contentProvider = $stateParams.id; |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 200 | |
| 201 | var item = new CdnPrefix(prf); |
| 202 | |
| 203 | item.$save() |
| 204 | .then(function(res){ |
Matteo Scandolo | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 205 | self.cp_prf.push(res); |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 206 | }) |
| 207 | .catch(function(e){ |
Matteo Scandolo | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 208 | self.result = { |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 209 | status: 0, |
| 210 | msg: e.data.detail |
| 211 | }; |
| 212 | }); |
| 213 | }; |
| 214 | |
| 215 | this.removePrefix = function(item){ |
| 216 | item.$delete() |
| 217 | .then(function(){ |
Matteo Scandolo | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 218 | lodash.remove(self.cp_prf, item); |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 219 | }) |
| 220 | .catch(function(e){ |
Matteo Scandolo | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 221 | self.result = { |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 222 | status: 0, |
| 223 | msg: e.data.detail |
| 224 | }; |
| 225 | }); |
| 226 | }; |
| 227 | } |
| 228 | }; |
| 229 | }) |
Matteo Scandolo | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 230 | .directive('contentProviderServer', function($stateParams, OriginServer, ContentProvider, lodash){ |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 231 | return{ |
| 232 | restrict: 'E', |
| 233 | controllerAs: 'vm', |
| 234 | scope: {}, |
Matteo Scandolo | 82697c0 | 2015-11-04 16:30:43 +0100 | [diff] [blame] | 235 | templateUrl: 'templates/cp_origin_server.html', |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 236 | controller: function(){ |
| 237 | this.pageName = 'server'; |
Matteo Scandolo | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 238 | this.protocols = {'http': 'HTTP', 'rtmp': 'RTMP', 'rtp': 'RTP', 'shout': 'SHOUTcast'}; |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 239 | |
Matteo Scandolo | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 240 | var self = this; |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 241 | |
Matteo Scandolo | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 242 | if($stateParams.id){ |
| 243 | ContentProvider.get({id: $stateParams.id}).$promise |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 244 | .then(function(cp){ |
Matteo Scandolo | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 245 | self.cp = cp; |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 246 | }).catch(function(e){ |
Matteo Scandolo | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 247 | self.result = { |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 248 | status: 0, |
| 249 | msg: e.data.detail |
| 250 | }; |
| 251 | }); |
| 252 | } |
| 253 | |
Matteo Scandolo | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 254 | OriginServer.query({contentProvider: $stateParams.id}).$promise |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 255 | .then(function(cp_os){ |
Matteo Scandolo | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 256 | self.cp_os = cp_os; |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 257 | }).catch(function(e){ |
Matteo Scandolo | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 258 | self.result = { |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 259 | status: 0, |
| 260 | msg: e.data.detail |
| 261 | }; |
| 262 | }); |
| 263 | |
| 264 | this.addOrigin = function(os){ |
Matteo Scandolo | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 265 | os.contentProvider = $stateParams.id; |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 266 | |
| 267 | var item = new OriginServer(os); |
| 268 | |
| 269 | item.$save() |
| 270 | .then(function(res){ |
Matteo Scandolo | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 271 | self.cp_os.push(res); |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 272 | }) |
| 273 | .catch(function(e){ |
Matteo Scandolo | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 274 | self.result = { |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 275 | status: 0, |
| 276 | msg: e.data.detail |
| 277 | }; |
| 278 | }); |
| 279 | }; |
| 280 | |
| 281 | this.removeOrigin = function(item){ |
| 282 | item.$delete() |
| 283 | .then(function(){ |
Matteo Scandolo | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 284 | lodash.remove(self.cp_os, item); |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 285 | }) |
| 286 | .catch(function(e){ |
Matteo Scandolo | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 287 | self.result = { |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 288 | status: 0, |
| 289 | msg: e.data.detail |
| 290 | }; |
| 291 | }); |
| 292 | }; |
| 293 | } |
| 294 | }; |
| 295 | }) |
Matteo Scandolo | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 296 | .directive('contentProviderUsers', function($stateParams, ContentProvider, User, lodash){ |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 297 | return{ |
| 298 | restrict: 'E', |
| 299 | controllerAs: 'vm', |
| 300 | scope: {}, |
Matteo Scandolo | 82697c0 | 2015-11-04 16:30:43 +0100 | [diff] [blame] | 301 | templateUrl: 'templates/cp_user.html', |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 302 | controller: function(){ |
Matteo Scandolo | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 303 | var self = this; |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 304 | |
| 305 | this.pageName = 'user'; |
| 306 | |
| 307 | this.cp_users = []; |
| 308 | |
Matteo Scandolo | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 309 | if($stateParams.id){ |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 310 | User.query().$promise |
| 311 | .then(function(users){ |
Matteo Scandolo | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 312 | self.users = users; |
| 313 | return ContentProvider.get({id: $stateParams.id}).$promise; |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 314 | }) |
| 315 | .then(function(res){ |
Matteo Scandolo | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 316 | res.users = self.populateUser(res.users, self.users); |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 317 | return res; |
| 318 | }) |
| 319 | .then(function(cp){ |
Matteo Scandolo | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 320 | self.cp = cp; |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 321 | }).catch(function(e){ |
Matteo Scandolo | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 322 | self.result = { |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 323 | 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 Scandolo | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 337 | self.cp.users.push(user); |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 338 | }; |
| 339 | |
| 340 | this.removeUserFromCp = function(user){ |
Matteo Scandolo | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 341 | lodash.remove(self.cp.users, user); |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 342 | }; |
| 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 Scandolo | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 352 | self.cp.users = self.populateUser(res.users, self.users); |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 353 | |
Matteo Scandolo | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 354 | self.result = { |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 355 | status: 1, |
| 356 | msg: 'Content Provider Saved' |
| 357 | }; |
| 358 | |
| 359 | }) |
| 360 | .catch(function(e){ |
Matteo Scandolo | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 361 | self.result = { |
Matteo Scandolo | f5cbbb1 | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 362 | status: 0, |
| 363 | msg: e.data.detail |
| 364 | }; |
| 365 | }); |
| 366 | }; |
| 367 | } |
| 368 | }; |
| 369 | }); |