91bad4d7e634b15f2dd65991366acd805195e436
[unimgr.git] / dlux / cpeui / cpeui-module / src / main / resources / cpeui / admin.controller.js
1 define([ 'app/cpeui/cpeui.module' ], function(cpeui) {
2
3   cpeui.register.controller('AdminPageCtrl',
4       function($scope, CpeuiSvc, CpeuiDialogs, $mdDialog, $mdMedia) {
5
6         // Tenants
7
8         $scope.selectedTenant = {};
9
10         $scope.updateTenantView = function() {
11           CpeuiSvc.getTenantList(function(tenant_list) {
12             $scope.tenantArray = tenant_list;
13           });
14         }
15
16         $scope.AddTenant = function() {
17           CpeuiSvc.addTenant($scope.tenantId, function() {
18               $scope.updateTenantView();
19           });
20         };
21
22         $scope.tenantDialog = new CpeuiDialogs.Dialog('AddTenant', {},
23             function(obj) {
24               CpeuiSvc.addTenant(obj.id, function() {
25                  $scope.updateTenantView();
26               });
27             });
28
29         $scope.OpenTenantPortal = function(tenant) {
30           $scope.currentTenent = tenant;
31           window.location = "#cpeui/tenant/" + tenant.name +"/";
32         };
33
34         $scope.DeleteTenant = function(tenantID) {
35           CpeuiDialogs.confirm(function() {
36             CpeuiSvc.deleteTenant(tenantID, function() {
37                 $scope.updateTenantView();
38             });
39           });
40         };
41
42         // Profiles
43         $scope.profiles = [];
44         $scope.updateProfilesView = function() {
45           CpeuiSvc.getProfiles(function(profiles) {
46             $scope.profiles = profiles;
47           });
48         };
49
50         var profileDialogController = function($scope, $mdDialog) {
51
52             $scope.getDefualtCbs = function(cir) {
53                 return Math.round(cir * 0.0125);
54             }
55
56             $scope.done = function() {
57                 if ($scope.obj.default_cbs) {
58                     $scope.obj.cbs = $scope.getDefualtCbs($scope.obj.cir);
59                 }
60                 if ($scope.projectForm.$valid) {
61                   $scope.callback($scope.obj);
62                   $mdDialog.hide();
63                 }
64               };
65           };
66
67         $scope.addProfile = new CpeuiDialogs.Dialog('AddProfile', {}, function(obj) {
68             CpeuiSvc.addProfile(obj['bw-profile'], obj.cir, obj.cbs, function() {
69               $scope.updateProfilesView();
70             });
71         }, profileDialogController);
72
73         $scope.editProfile = function(profileName, cbs, cir) {
74             new CpeuiDialogs.Dialog('AddProfile', {}, function(obj) {
75                 CpeuiSvc.editProfile(obj['bw-profile'], obj.cir, obj.cbs, function() {
76                   $scope.updateProfilesView();
77                 });
78             }, profileDialogController).show(null,{edit:true, profileName:profileName, cbs:cbs, cir:cir});
79         };
80
81         $scope.deleteProfile = function(profileName) {
82           CpeuiDialogs.confirm(function() {
83             CpeuiSvc.deleteProfile(profileName, function() {
84               $scope.updateProfilesView();
85             });
86           });
87         };
88
89         // CEs
90         $scope.updateCesView = function() {
91           CpeuiSvc.getCes(function(ces) {
92             $scope.ces = ces;
93           });
94         };
95
96         $scope.cesDialog = new CpeuiDialogs.Dialog('AddCE', {}, function(obj) {
97           CpeuiSvc.addCe(obj.id, obj.name, function() {
98             $scope.updateCesView();
99           });
100         });
101
102         $scope.deleteCe = function(tenantid, ceid) {
103           CpeuiDialogs.confirm(function() {
104             CpeuiSvc.removeCe(ceid, function() {
105               $scope.updateCesView();
106             });
107           });
108         };
109
110         $scope.assignCpeToTenant = function(cpeId) {
111           CpeuiDialogs.customConfirm("Are You Sure?",
112               "Are you sure you want to override all this CPE's unis tenants?",
113               function() {
114                 for (var i = 0; i < $scope.unis.length; ++i) {
115                   if ($scope.unis[i].device == cpeId) {
116                     CpeuiSvc.updateUni($scope.unis[i]['uni-id'],
117                         $scope.selectedTenant[cpeId]);
118                   }
119                 }
120               }, function() {
121                 $scope.selectedTenant[cpeId] = undefined;
122               });
123         }
124
125         $scope.assignNetworkToTenant = function(svc) {
126           CpeuiDialogs.customConfirm("Are You Sure?",
127               "Are you sure you want to assign service "+ svc['svc-id'] +" to tenant " + $scope.selectedTenant[svc['svc-id']] +"?",
128               function() {
129                 CpeuiSvc.addTenantToService(svc['svc-id'], $scope.selectedTenant[svc['svc-id']], function(){
130                   svc['tenant-id'] = $scope.selectedTenant[svc['svc-id']];
131                 },function(){
132                   $scope.selectedTenant[svc['svc-id']] = undefined;
133                 });
134               }, function() {
135                 $scope.selectedTenant[svc['svc-id']] = undefined;
136               });
137         };
138
139         function updateCpeTenants(unis) {
140           // update tenant cpe tenant column
141           var hasMultipleTenants = [];
142           var device2tenant = {};
143           for (var i = 0; i < $scope.unis.length; ++i) {
144             var tenant = $scope.unis[i]["tenant-id"];
145
146             if (hasMultipleTenants.indexOf($scope.unis[i].device) != -1) {
147               continue;
148             }
149             if (device2tenant[$scope.unis[i].device] == undefined) {
150               if (tenant) {
151                 device2tenant[$scope.unis[i].device] = tenant;
152               } else {
153                 device2tenant[$scope.unis[i].device] = ""; // none
154               }
155             } else if (device2tenant[$scope.unis[i].device] != tenant) {
156               if ((device2tenant[$scope.unis[i].device] != "") || (tenant)) {
157                 device2tenant[$scope.unis[i].device] = true; // multiple
158                 hasMultipleTenants.push($scope.unis[i].device);
159               }
160             }
161           }
162           var devices = Object.keys(device2tenant);
163           for (var i = 0; i < devices.length; ++i) {
164             if (device2tenant[devices[i]] == true) {
165               $scope.selectedTenant[devices[i]] = undefined;
166             } else {
167               $scope.selectedTenant[devices[i]] = device2tenant[devices[i]];
168             }
169           }
170         }
171
172         // UNIs
173         $scope.updateUniView = function() {
174           CpeuiSvc.getUnis(function(unis) {
175                 $scope.unis = unis;                
176                 updateCpeTenants(unis);
177               });
178         };
179
180         $scope.linkUniDialog = new CpeuiDialogs.Dialog('LinkUni', {}, function(
181             obj) {
182           CpeuiSvc.updateUni(obj.id, obj.tenant, function() {
183             $scope.updateUniView();
184           });
185         });
186
187         $scope.deleteUni = function(id) {
188           CpeuiDialogs.confirm(function() {
189             CpeuiSvc.removeUni(id, function() {
190               $scope.updateUniView();
191             });
192           });
193         };
194
195         $scope.addCEName = function(ce){
196           ce._naming = true;
197           var input = $('#INPUT_' +ce['dev-id']);          
198           // hack to focus input after show is complete
199           setTimeout(function(){input.focus();},20);          
200           input.parent().on('blur',function(){
201             setTimeout(function(){
202               ce._naming = false;
203               delete ce._new_name;
204             },20);
205           });
206           
207           input.bind("keyup", function (eventSubmit) {
208             if(eventSubmit.which === 13) {            
209               $('#OK_' +ce['dev-id']).click();
210             } else if(eventSubmit.which === 27) {              
211               input.parent().blur();
212             }
213           });
214         }
215         
216         $scope.renameCE = function(ce){
217           CpeuiSvc.addCeName(ce, ce._new_name, function(){
218             ce['device-name'] = ce._new_name;
219             });
220           ce._naming = false;
221         }
222         
223         $scope.services = [];
224         $scope.networkNames = {};
225         
226         $scope.updateNetworksView = function() {
227           CpeuiSvc.getAllServices(function(services) {
228             $scope.services = services;
229           });
230           CpeuiSvc.getNetworkNames(function(networks){        
231             networks.forEach(function(net){
232               $scope.networkNames[net.uuid] = net.name;
233             });
234           });
235         };
236         
237         // General
238         $scope.updateView = function() {
239           if ($scope.isTabSet('admin',4)){
240             $scope.updateNetworksView();
241           }
242           $scope.updateTenantView();
243           $scope.updateCesView();
244           $scope.updateUniView();
245           $scope.updateProfilesView()
246         };
247
248         $scope.updateView();
249       });
250 });