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