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