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