Matteo Scandolo | d2044a4 | 2017-08-07 16:08:28 -0700 | [diff] [blame^] | 1 | |
| 2 | /* |
| 3 | * Copyright 2017-present Open Networking Foundation |
| 4 | |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | |
Matteo Scandolo | bbcc01b | 2015-11-04 16:30:43 +0100 | [diff] [blame] | 19 | 'use strict'; |
| 20 | |
| 21 | angular.module('xos.contentProvider', [ |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 22 | 'ngResource', |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 23 | 'ngCookies', |
Matteo Scandolo | bbcc01b | 2015-11-04 16:30:43 +0100 | [diff] [blame] | 24 | 'xos.helpers', |
Matteo Scandolo | 40bea63 | 2016-05-17 16:51:29 -0700 | [diff] [blame] | 25 | 'ui.router' |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 26 | ]) |
Matteo Scandolo | 40bea63 | 2016-05-17 16:51:29 -0700 | [diff] [blame] | 27 | .config(($stateProvider) => { |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 28 | |
Matteo Scandolo | d44ceb4 | 2015-11-06 17:14:28 +0100 | [diff] [blame] | 29 | $stateProvider |
| 30 | .state('list', { |
| 31 | url: '/', |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 32 | template: '<content-provider-list></content-provider-list>', |
| 33 | }) |
Matteo Scandolo | d44ceb4 | 2015-11-06 17:14:28 +0100 | [diff] [blame] | 34 | .state('details', { |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 35 | url: '/contentProvider/:id', |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 36 | template: '<content-provider-detail></content-provider-detail>' |
| 37 | }) |
Matteo Scandolo | d44ceb4 | 2015-11-06 17:14:28 +0100 | [diff] [blame] | 38 | .state('cdn', { |
| 39 | url: '/contentProvider/:id/cdn_prefix', |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 40 | template: '<content-provider-cdn></content-provider-cdn>' |
| 41 | }) |
Matteo Scandolo | d44ceb4 | 2015-11-06 17:14:28 +0100 | [diff] [blame] | 42 | .state('server', { |
| 43 | url: '/contentProvider/:id/origin_server', |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 44 | template: '<content-provider-server></content-provider-server>' |
| 45 | }) |
Matteo Scandolo | d44ceb4 | 2015-11-06 17:14:28 +0100 | [diff] [blame] | 46 | .state('users', { |
| 47 | url: '/contentProvider/:id/users', |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 48 | template: '<content-provider-users></content-provider-users>' |
Matteo Scandolo | d44ceb4 | 2015-11-06 17:14:28 +0100 | [diff] [blame] | 49 | }); |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 50 | }) |
| 51 | .config(function($httpProvider){ |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 52 | // add X-CSRFToken header for update, create, delete (!GET) |
| 53 | $httpProvider.interceptors.push('SetCSRFToken'); |
Matteo Scandolo | bbcc01b | 2015-11-04 16:30:43 +0100 | [diff] [blame] | 54 | $httpProvider.interceptors.push('NoHyperlinks'); |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 55 | }) |
| 56 | .service('ContentProvider', function($resource){ |
| 57 | return $resource('/hpcapi/contentproviders/:id/', {id: '@id'}, { |
| 58 | 'update': {method: 'PUT'} |
| 59 | }); |
| 60 | }) |
| 61 | .service('ServiceProvider', function($resource){ |
| 62 | return $resource('/hpcapi/serviceproviders/:id/', {id: '@id'}); |
| 63 | }) |
| 64 | .service('CdnPrefix', function($resource){ |
| 65 | return $resource('/hpcapi/cdnprefixs/:id/', {id: '@id'}); |
| 66 | }) |
| 67 | .service('OriginServer', function($resource){ |
| 68 | return $resource('/hpcapi/originservers/:id/', {id: '@id'}); |
| 69 | }) |
| 70 | .service('User', function($resource){ |
| 71 | return $resource('/xos/users/:id/', {id: '@id'}); |
| 72 | }) |
| 73 | .directive('cpActions', function(ContentProvider, $location){ |
| 74 | return { |
| 75 | restrict: 'E', |
| 76 | scope: { |
| 77 | id: '=id', |
| 78 | }, |
| 79 | bindToController: true, |
| 80 | controllerAs: 'vm', |
Matteo Scandolo | bbcc01b | 2015-11-04 16:30:43 +0100 | [diff] [blame] | 81 | templateUrl: 'templates/cp_actions.html', |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 82 | controller: function(){ |
| 83 | this.deleteCp = function(id){ |
| 84 | ContentProvider.delete({id: id}).$promise |
| 85 | .then(function(){ |
| 86 | $location.url('/'); |
| 87 | }); |
| 88 | }; |
| 89 | } |
| 90 | }; |
| 91 | }) |
Matteo Scandolo | 40bea63 | 2016-05-17 16:51:29 -0700 | [diff] [blame] | 92 | .directive('contentProviderList', function(ContentProvider, _){ |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 93 | return { |
| 94 | restrict: 'E', |
| 95 | controllerAs: 'vm', |
| 96 | scope: {}, |
Matteo Scandolo | bbcc01b | 2015-11-04 16:30:43 +0100 | [diff] [blame] | 97 | templateUrl: 'templates/cp_list.html', |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 98 | controller: function(){ |
Matteo Scandolo | 775d324 | 2016-03-24 16:12:18 -0700 | [diff] [blame] | 99 | ['Name', 'Description', 'Status', 'Actions'] |
| 100 | this.tableConfig = { |
| 101 | columns: [ |
| 102 | { |
| 103 | label: 'Name', |
| 104 | field: 'humanReadableName' |
| 105 | }, |
| 106 | { |
| 107 | label: 'Description', |
| 108 | field: 'description' |
| 109 | }, |
| 110 | { |
| 111 | label: 'Status', |
| 112 | field: 'enabled' |
| 113 | } |
| 114 | ], |
| 115 | enableActions: true |
| 116 | }; |
| 117 | |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 118 | var self = this; |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 119 | |
| 120 | ContentProvider.query().$promise |
| 121 | .then(function(cp){ |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 122 | self.contentProviderList = cp; |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 123 | }) |
| 124 | .catch(function(e){ |
| 125 | throw new Error(e); |
| 126 | }); |
| 127 | |
| 128 | this.deleteCp = function(id){ |
| 129 | ContentProvider.delete({id: id}).$promise |
| 130 | .then(function(){ |
Matteo Scandolo | 40bea63 | 2016-05-17 16:51:29 -0700 | [diff] [blame] | 131 | _.remove(self.contentProviderList, {id: id}); |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 132 | }); |
| 133 | }; |
| 134 | } |
| 135 | }; |
| 136 | }) |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 137 | .directive('contentProviderDetail', function(ContentProvider, ServiceProvider, $stateParams, $location){ |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 138 | return { |
| 139 | restrict: 'E', |
| 140 | controllerAs: 'vm', |
| 141 | scope: {}, |
Matteo Scandolo | bbcc01b | 2015-11-04 16:30:43 +0100 | [diff] [blame] | 142 | templateUrl: 'templates/cp_detail.html', |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 143 | controller: function(){ |
| 144 | this.pageName = 'detail'; |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 145 | var self = this; |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 146 | |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 147 | if($stateParams.id){ |
| 148 | ContentProvider.get({id: $stateParams.id}).$promise |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 149 | .then(function(cp){ |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 150 | self.cp = cp; |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 151 | }).catch(function(e){ |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 152 | self.result = { |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 153 | status: 0, |
| 154 | msg: e.data.detail |
| 155 | }; |
| 156 | }); |
| 157 | } |
| 158 | else{ |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 159 | self.cp = new ContentProvider(); |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | ServiceProvider.query().$promise |
| 163 | .then(function(sp){ |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 164 | self.sp = sp; |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 165 | }); |
| 166 | |
| 167 | this.saveContentProvider = function(cp){ |
| 168 | var p, isNew = false; |
| 169 | |
| 170 | if(cp.id){ |
| 171 | p = cp.$update(); |
| 172 | } |
| 173 | else{ |
| 174 | isNew = true; |
| 175 | cp.name = cp.humanReadableName; |
| 176 | p = cp.$save(); |
| 177 | } |
| 178 | |
| 179 | p.then(function(res){ |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 180 | self.result = { |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 181 | status: 1, |
| 182 | msg: 'Content Provider Saved' |
| 183 | }; |
| 184 | if(isNew){ |
| 185 | $location.url('contentProvider/' + res.id + '/'); |
| 186 | } |
| 187 | }) |
| 188 | .catch(function(e){ |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 189 | self.result = { |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 190 | status: 0, |
| 191 | msg: e.data.detail |
| 192 | }; |
| 193 | }); |
| 194 | }; |
| 195 | } |
| 196 | }; |
| 197 | }) |
Matteo Scandolo | 54bc5f7 | 2016-05-18 14:06:45 -0700 | [diff] [blame] | 198 | .directive('contentProviderCdn', function($stateParams, CdnPrefix, ContentProvider){ |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 199 | return{ |
| 200 | restrict: 'E', |
| 201 | controllerAs: 'vm', |
| 202 | scope: {}, |
Matteo Scandolo | bbcc01b | 2015-11-04 16:30:43 +0100 | [diff] [blame] | 203 | templateUrl: 'templates/cp_cdn_prefix.html', |
Matteo Scandolo | 54bc5f7 | 2016-05-18 14:06:45 -0700 | [diff] [blame] | 204 | controller: function(_){ |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 205 | var self = this; |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 206 | |
| 207 | this.pageName = 'cdn'; |
| 208 | |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 209 | if($stateParams.id){ |
| 210 | ContentProvider.get({id: $stateParams.id}).$promise |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 211 | .then(function(cp){ |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 212 | self.cp = cp; |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 213 | }).catch(function(e){ |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 214 | self.result = { |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 215 | status: 0, |
Matteo Scandolo | 54bc5f7 | 2016-05-18 14:06:45 -0700 | [diff] [blame] | 216 | msg: e.data ? e.data.detail : '' |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 217 | }; |
| 218 | }); |
| 219 | } |
| 220 | |
| 221 | CdnPrefix.query().$promise |
| 222 | .then(function(prf){ |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 223 | self.prf = prf; |
Matteo Scandolo | 54bc5f7 | 2016-05-18 14:06:45 -0700 | [diff] [blame] | 224 | // console.log(prf, $stateParams.id); |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 225 | // set the active CdnPrefix for this contentProvider |
Matteo Scandolo | 54bc5f7 | 2016-05-18 14:06:45 -0700 | [diff] [blame] | 226 | self.cp_prf = []; |
| 227 | self.cp_prf.push(_.find(prf, {contentProvider: parseInt($stateParams.id)})); |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 228 | }).catch(function(e){ |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 229 | self.result = { |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 230 | status: 0, |
| 231 | msg: e.data.detail |
| 232 | }; |
| 233 | }); |
| 234 | |
| 235 | this.addPrefix = function(prf){ |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 236 | prf.contentProvider = $stateParams.id; |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 237 | |
| 238 | var item = new CdnPrefix(prf); |
| 239 | |
| 240 | item.$save() |
| 241 | .then(function(res){ |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 242 | self.cp_prf.push(res); |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 243 | }) |
| 244 | .catch(function(e){ |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 245 | self.result = { |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 246 | status: 0, |
| 247 | msg: e.data.detail |
| 248 | }; |
| 249 | }); |
| 250 | }; |
| 251 | |
| 252 | this.removePrefix = function(item){ |
| 253 | item.$delete() |
| 254 | .then(function(){ |
Matteo Scandolo | 40bea63 | 2016-05-17 16:51:29 -0700 | [diff] [blame] | 255 | _.remove(self.cp_prf, item); |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 256 | }) |
| 257 | .catch(function(e){ |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 258 | self.result = { |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 259 | status: 0, |
| 260 | msg: e.data.detail |
| 261 | }; |
| 262 | }); |
| 263 | }; |
| 264 | } |
| 265 | }; |
| 266 | }) |
Matteo Scandolo | 54bc5f7 | 2016-05-18 14:06:45 -0700 | [diff] [blame] | 267 | .directive('contentProviderServer', function($stateParams, OriginServer, ContentProvider){ |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 268 | return{ |
| 269 | restrict: 'E', |
| 270 | controllerAs: 'vm', |
| 271 | scope: {}, |
Matteo Scandolo | bbcc01b | 2015-11-04 16:30:43 +0100 | [diff] [blame] | 272 | templateUrl: 'templates/cp_origin_server.html', |
Matteo Scandolo | 54bc5f7 | 2016-05-18 14:06:45 -0700 | [diff] [blame] | 273 | controller: function(_){ |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 274 | this.pageName = 'server'; |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 275 | this.protocols = {'http': 'HTTP', 'rtmp': 'RTMP', 'rtp': 'RTP', 'shout': 'SHOUTcast'}; |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 276 | |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 277 | var self = this; |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 278 | |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 279 | if($stateParams.id){ |
| 280 | ContentProvider.get({id: $stateParams.id}).$promise |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 281 | .then(function(cp){ |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 282 | self.cp = cp; |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 283 | }).catch(function(e){ |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 284 | self.result = { |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 285 | status: 0, |
| 286 | msg: e.data.detail |
| 287 | }; |
| 288 | }); |
| 289 | } |
| 290 | |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 291 | OriginServer.query({contentProvider: $stateParams.id}).$promise |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 292 | .then(function(cp_os){ |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 293 | self.cp_os = cp_os; |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 294 | }).catch(function(e){ |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 295 | self.result = { |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 296 | status: 0, |
| 297 | msg: e.data.detail |
| 298 | }; |
| 299 | }); |
| 300 | |
| 301 | this.addOrigin = function(os){ |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 302 | os.contentProvider = $stateParams.id; |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 303 | |
| 304 | var item = new OriginServer(os); |
| 305 | |
| 306 | item.$save() |
| 307 | .then(function(res){ |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 308 | self.cp_os.push(res); |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 309 | }) |
| 310 | .catch(function(e){ |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 311 | self.result = { |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 312 | status: 0, |
| 313 | msg: e.data.detail |
| 314 | }; |
| 315 | }); |
| 316 | }; |
| 317 | |
| 318 | this.removeOrigin = function(item){ |
| 319 | item.$delete() |
| 320 | .then(function(){ |
Matteo Scandolo | 40bea63 | 2016-05-17 16:51:29 -0700 | [diff] [blame] | 321 | _.remove(self.cp_os, item); |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 322 | }) |
| 323 | .catch(function(e){ |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 324 | self.result = { |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 325 | status: 0, |
| 326 | msg: e.data.detail |
| 327 | }; |
| 328 | }); |
| 329 | }; |
| 330 | } |
| 331 | }; |
| 332 | }) |
Matteo Scandolo | 54bc5f7 | 2016-05-18 14:06:45 -0700 | [diff] [blame] | 333 | .directive('contentProviderUsers', function($stateParams, ContentProvider, User){ |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 334 | return{ |
| 335 | restrict: 'E', |
| 336 | controllerAs: 'vm', |
| 337 | scope: {}, |
Matteo Scandolo | bbcc01b | 2015-11-04 16:30:43 +0100 | [diff] [blame] | 338 | templateUrl: 'templates/cp_user.html', |
Matteo Scandolo | 54bc5f7 | 2016-05-18 14:06:45 -0700 | [diff] [blame] | 339 | controller: function(_){ |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 340 | var self = this; |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 341 | |
| 342 | this.pageName = 'user'; |
| 343 | |
| 344 | this.cp_users = []; |
| 345 | |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 346 | if($stateParams.id){ |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 347 | User.query().$promise |
| 348 | .then(function(users){ |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 349 | self.users = users; |
| 350 | return ContentProvider.get({id: $stateParams.id}).$promise; |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 351 | }) |
| 352 | .then(function(res){ |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 353 | res.users = self.populateUser(res.users, self.users); |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 354 | return res; |
| 355 | }) |
| 356 | .then(function(cp){ |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 357 | self.cp = cp; |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 358 | }).catch(function(e){ |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 359 | self.result = { |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 360 | status: 0, |
| 361 | msg: e.data.detail |
| 362 | }; |
| 363 | }); |
| 364 | } |
| 365 | |
| 366 | this.populateUser = function(ids, list){ |
| 367 | for(var i = 0; i < ids.length; i++){ |
Matteo Scandolo | 40bea63 | 2016-05-17 16:51:29 -0700 | [diff] [blame] | 368 | ids[i] = _.find(list, {id: ids[i]}); |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 369 | } |
| 370 | return ids; |
| 371 | }; |
| 372 | |
| 373 | this.addUserToCp = function(user){ |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 374 | self.cp.users.push(user); |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 375 | }; |
| 376 | |
| 377 | this.removeUserFromCp = function(user){ |
Matteo Scandolo | 40bea63 | 2016-05-17 16:51:29 -0700 | [diff] [blame] | 378 | _.remove(self.cp.users, user); |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 379 | }; |
| 380 | |
| 381 | this.saveContentProvider = function(cp){ |
| 382 | |
| 383 | // flatten the user to id of array |
Matteo Scandolo | 54bc5f7 | 2016-05-18 14:06:45 -0700 | [diff] [blame] | 384 | cp.users = _.map(cp.users, 'id'); |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 385 | |
| 386 | cp.$update() |
| 387 | .then(function(res){ |
| 388 | |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 389 | self.cp.users = self.populateUser(res.users, self.users); |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 390 | |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 391 | self.result = { |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 392 | status: 1, |
| 393 | msg: 'Content Provider Saved' |
| 394 | }; |
| 395 | |
| 396 | }) |
| 397 | .catch(function(e){ |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 398 | self.result = { |
Matteo Scandolo | 15d692c | 2015-10-28 12:00:07 +0100 | [diff] [blame] | 399 | status: 0, |
| 400 | msg: e.data.detail |
| 401 | }; |
| 402 | }); |
| 403 | }; |
| 404 | } |
| 405 | }; |
| 406 | }); |