Multiple Messages supported when sending Renew to several AMs
[unfold.git] / portal / static / js / institution.js
1 function unspin_all(){
2     $('input:checkbox').each(function (index) {
3         if(this.checked){
4             this.nextElementSibling.style.display = "none";
5         }
6     });
7 }
8 function spin_all(){
9     $('input:checkbox').each(function (index) {
10         if(this.checked){
11             this.nextElementSibling.style.display = "inline";
12         }
13     });
14 }
15 function post_renew(obj){
16     var record_id = obj.id;
17     $.post("/sfa/Renew/",{'hrn':record_id,'type':'slice'}, function(data) {
18         txt = '';
19         errors = '';
20         $.each(data, function (index, val) {
21             console.log(index, val);
22             if (index != 'columns' && !('error' in val)){
23                 if('code' in val && val['code']['geni_code']==0){
24                     if('output' in val && val['output']!=''){
25                         txt += index+': '+val['output']+'<br>';
26                     }
27                     if('value' in val && val['value']!=''){
28                         if(typeof val['value'] == 'string' || val['value'] instanceof String){
29                             txt += index+': '+val['value']+'<br>';
30                         }else{
31                             txt += index+': expiration = '+val['value'][0]['geni_expires']+'<br>';
32                         }
33                     }
34                 }else{
35                     if('output' in val && val['output']!=''){
36                         errors += index+': '+val['output']+'<br>';
37                     }
38                     if('value' in val && val['value']!=''){
39                         errors += index+': '+val['value']+'<br>';
40                     }
41                 }
42             }
43             if('error' in val){
44                 errors += index+': '+val['error_msg']+'<br>';
45             } 
46         });
47         console.log(txt);
48         console.log(record_id);
49         if(txt != ''){
50             mysliceAlert('Success: '+record_id+'<br>'+txt,'success', false, record_id);
51         }
52         if(errors != ''){
53             mysliceAlert('Warning: '+record_id+'<br>'+errors,'warning', false, record_id);
54         }
55         unspin_all();
56     });
57 }
58 $(document).ready(function() {
59         loadedTabs = [];
60         
61         $('.nav-tabs a').click(function (e) {
62                 e.preventDefault();
63                 $(this).tab('show');
64                 id = $(this).attr('href').substr(1);
65                 if (!(id in loadedTabs)) {
66                         switch(id) {
67                                 case 'users':
68                                         //loadUsers();
69                                         loadedTabs[id] = true;
70                                 break;
71                         }
72                 }
73                 
74         });
75
76     $('button#deleteusers').click(function() {
77         spin_all();
78         $('input:checkbox.user').each(function (index) {
79             if(this.checked){
80                 var record_id = this.id;
81                 var user_email = this.dataset.email; 
82                 console.log(this);
83                 // Delete in SFA Registry
84                 $.post("/delete/user/",{'filters':{'user_hrn':record_id}}, function(data) {
85                     if(data.success){
86                         $.post("/local_user/delete/",{'email':user_email}, function(data) {
87                             console.log(data);
88                             if (data == true){
89                                 mysliceAlert('Success: user deleted','success', true);
90                             }else{
91                                 mysliceAlert('Local DB Error for: '+record_id,'warning', true);
92                             }
93                             $('tr[id="'+record_id+'"]').fadeOut("slow");
94                             $('tr[id="'+record_id+'"]').remove();
95                         });
96                         //$.post("/delete/local:user/",{'filters':{'user_hrn':this.id}}, function(data) {
97                     }else{
98                         mysliceAlert('Rest Error for: '+data.error,'warning', true);
99                         //alert("Rest Error for "+record_id+": "+data.error);
100                     }   
101                     unspin_all();
102                 });
103             } 
104         });
105     });
106
107     /* TODO: factorize into functions */
108     $('button#deleteslices').click(function() {
109         spin_all();
110         var flag = false;
111         $('input:checkbox.slice').each(function (index) {
112             if(this.checked){
113                 var record_id = this.id;
114                 $('#'+record_id+'-loading').spin();
115                 $.post("/delete/slice/",{'filters':{'slice_hrn':this.id}}, function(data) {
116                     if(data.success){
117                         localStorage.clear();
118                         $('tr[id="'+record_id+'"]').fadeOut("slow");
119                         $('tr[id="'+record_id+'"]').remove();
120                         mysliceAlert('Success: slice deleted','success', true);
121                     }else{
122                         mysliceAlert('Rest Error for: '+data.error,'warning', true);
123                         //alert("Rest Error for "+record_id+": "+data.error);
124                     }
125                     unspin_all();
126                 });
127             }
128         });
129     });
130     $('button#renewslices').click(function() {
131         var record_id;
132         spin_all();
133         var now = new Date();
134         /* In Javascript getMonth() gives month[0] = january, month[1] = february, and so on...  */
135         var month = now.getMonth()+2;
136         var one_month_later = now.getFullYear()+"-"+month+"-"+now.getDate()+" "+now.getHours()+":"+now.getMinutes()+":"+now.getSeconds();
137         $('input:checkbox.slice').each(function (index) {
138             if(this.checked){
139                 console.log(this.id);
140                 record_id = $(this).attr('id');
141                 $('#'+this.id+'-loading').spin();
142                 // /sfa/Renew?hrn=onelab.upmc.projectx.slicex&type=slice
143                 post_renew(this);
144             }
145         });
146         // TODO: refresh table
147         //window.location="/portal/institution#slices";
148     });
149     $('button#deleteprojects').click(function() {
150         spin_all();
151         var flag = false;
152         $('input:checkbox.project').each(function (index) {
153             if(this.checked){
154                 var record_id = this.id;
155                 $('#'+record_id+'-loading').spin();
156                 console.log(record_id);
157                 $.post("/delete/myslice:authority/",{'filters':{'authority_hrn':this.id}}, function(data) {
158                     if(data.success){
159                         localStorage.clear();
160                         $('tr[id="'+record_id+'"]').fadeOut("slow");
161                         $('tr[id="'+record_id+'"]').remove();
162                         mysliceAlert('Success: project deleted','success', true);
163                     }else{
164                         mysliceAlert('Rest Error for: '+data.error,'warning', true);
165                         //alert("Rest Error for "+record_id+": "+data.error);
166                     }
167                     unspin_all();
168                 });
169             }
170         });
171     });
172
173     $('button#createslice').click(function() {
174         window.location="/portal/slice_request/";
175     });
176
177     $('button#createproject').click(function() {
178         window.location="/portal/project_request/";
179     });
180     $('button#slicerequestbtn').click(function() {
181         /*
182         window.location="/portal/slice_request/";
183         */
184     });
185 });
186
187 /*function loadUsers() {
188         $('div#users table').load('/table/user/',
189                 {
190                         'fields'  : [ 'user_hrn', 'user_first_name', 'user_last_name', 'user_email', 'user_phone' ],
191                         'filters' : { 'parent_authority' : $('div#users').data('authority') },
192                         'options' : [ 'checkbox' ]
193                 }
194         );
195 }*/