4d1426353fcf1d58e3065da622f78dfb599f6766
[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.updateView();
19           });
20         };
21
22         $scope.tenantDialog = new CpeuiDialogs.Dialog('AddTenant', {},
23             function(obj) {
24               CpeuiSvc.addTenant(obj.id, function() {
25                 $scope.updateView();
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.updateView();
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         $scope.addProfile = new CpeuiDialogs.Dialog('AddProfile', {}, function(obj) {
51           CpeuiSvc.addProfile(obj['bw-profile'], obj.cir, obj.cbs, function() {
52             $scope.updateProfilesView();
53           });
54         });
55
56         $scope.deleteProfile = function(profileName) {
57           CpeuiDialogs.confirm(function() {
58             CpeuiSvc.deleteProfile(profileName, function() {
59               $scope.updateProfilesView();
60             });
61           });
62         };
63
64         // CEs
65         $scope.updateCesView = function() {
66           CpeuiSvc.getCes(function(ces) {
67             $scope.ces = ces;
68           });
69         };
70
71         $scope.cesDialog = new CpeuiDialogs.Dialog('AddCE', {}, function(obj) {
72           CpeuiSvc.addCe(obj.id, obj.name, function() {
73             $scope.updateCesView();
74           });
75         });
76
77         $scope.deleteCe = function(tenantid, ceid) {
78           CpeuiDialogs.confirm(function() {
79             CpeuiSvc.removeCe(ceid, function() {
80               $scope.updateCesView();
81             });
82           });
83         };
84
85         $scope.assignCpeToTenant = function(cpeId) {
86           CpeuiDialogs.customConfirm("Are You Sure?",
87               "Are you sure you want to override all this CPE's unis tenants?",
88               function() {
89                 for (var i = 0; i < $scope.unis.length; ++i) {
90                   if ($scope.unis[i].device == cpeId) {
91                     CpeuiSvc.updateUni($scope.unis[i]['uni-id'],
92                         $scope.selectedTenant[cpeId]);
93                   }
94                 }
95               }, function() {
96                 $scope.selectedTenant[cpeId] = undefined;
97               });
98         }
99
100         $scope.assignNetworkToTenant = function(svc) {
101           CpeuiDialogs.customConfirm("Are You Sure?",
102               "Are you sure you want to assign service "+ svc['svc-id'] +" to tenant " + $scope.selectedTenant[svc['svc-id']] +"?",
103               function() {
104                 CpeuiSvc.addTenantToService(svc['svc-id'], $scope.selectedTenant[svc['svc-id']], function(){
105                   svc['tenant-id'] = $scope.selectedTenant[svc['svc-id']];
106                 },function(){
107                   $scope.selectedTenant[svc['svc-id']] = undefined;
108                 });
109               }, function() {
110                 $scope.selectedTenant[svc['svc-id']] = undefined;
111               });
112         };
113
114         function updateCpeTenants(unis) {
115           // update tenant cpe tenant column
116           var hasMultipleTenants = [];
117           var device2tenant = {};
118           for (var i = 0; i < $scope.unis.length; ++i) {
119             var tenant = $scope.unis[i]["tenant-id"];
120
121             if (hasMultipleTenants.indexOf($scope.unis[i].device) != -1) {
122               continue;
123             }
124             if (device2tenant[$scope.unis[i].device] == undefined) {
125               if (tenant) {
126                 device2tenant[$scope.unis[i].device] = tenant;
127               } else {
128                 device2tenant[$scope.unis[i].device] = ""; // none
129               }
130             } else if (device2tenant[$scope.unis[i].device] != tenant) {
131               if ((device2tenant[$scope.unis[i].device] != "") || (tenant)) {
132                 device2tenant[$scope.unis[i].device] = true; // multiple
133                 hasMultipleTenants.push($scope.unis[i].device);
134               }
135             }
136           }
137           var devices = Object.keys(device2tenant);
138           for (var i = 0; i < devices.length; ++i) {
139             if (device2tenant[devices[i]] == true) {
140               $scope.selectedTenant[devices[i]] = undefined;
141             } else {
142               $scope.selectedTenant[devices[i]] = device2tenant[devices[i]];
143             }
144           }
145         }
146
147         // UNIs
148         $scope.updateUniView = function() {
149           CpeuiSvc.getUnis(function(unis) {
150                 $scope.unis = unis;                
151                 updateCpeTenants(unis);
152               });
153         };
154
155         $scope.linkUniDialog = new CpeuiDialogs.Dialog('LinkUni', {}, function(
156             obj) {
157           CpeuiSvc.updateUni(obj.id, obj.tenant, function() {
158             $scope.updateUniView();
159           });
160         });
161
162         $scope.deleteUni = function(id) {
163           CpeuiDialogs.confirm(function() {
164             CpeuiSvc.removeUni(id, function() {
165               $scope.updateUniView();
166             });
167           });
168         };
169
170         $scope.addCEName = function(ce){
171           ce._naming = true;
172           var input = $('#INPUT_' +ce['dev-id']);          
173           // hack to focus input after show is complete
174           setTimeout(function(){input.focus();},20);          
175           input.parent().on('blur',function(){
176             setTimeout(function(){
177               ce._naming = false;
178               delete ce._new_name;
179             },20);
180           });
181           
182           input.bind("keyup", function (eventSubmit) {
183             if(eventSubmit.which === 13) {            
184               $('#OK_' +ce['dev-id']).click();
185             } else if(eventSubmit.which === 27) {              
186               input.parent().blur();
187             }
188           });
189         }
190         
191         $scope.renameCE = function(ce){
192           CpeuiSvc.addCeName(ce, ce._new_name, function(){
193             ce['device-name'] = ce._new_name;
194             });
195           ce._naming = false;
196         }
197         
198         $scope.services = [];
199         $scope.networkNames = {};
200         
201         $scope.updateNetworksView = function() {
202           CpeuiSvc.getAllServices(function(services) {
203             $scope.services = services;
204           });
205           CpeuiSvc.getNetworkNames(function(networks){        
206             networks.forEach(function(net){
207               $scope.networkNames[net.uuid] = net.name;
208             });
209           });
210         };
211         
212         // General
213         $scope.updateView = function() {
214           if ($scope.isTabSet('admin',4)){
215             $scope.updateNetworksView();
216           }
217           $scope.updateTenantView();
218           $scope.updateCesView();
219           $scope.updateUniView();
220           $scope.updateProfilesView()
221         };
222
223         $scope.updateView();
224       });
225 });