1545263f8002d807d0299a6f9069070f3e16ca36
[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 $(document).ready(function() {
16         loadedTabs = [];
17         
18         $('.nav-tabs a').click(function (e) {
19                 e.preventDefault();
20                 $(this).tab('show');
21                 id = $(this).attr('href').substr(1);
22                 if (!(id in loadedTabs)) {
23                         switch(id) {
24                                 case 'users':
25                                         //loadUsers();
26                                         loadedTabs[id] = true;
27                                 break;
28                         }
29                 }
30                 
31         });
32
33     $('button#deleteusers').click(function() {
34         spin_all();
35         $('input:checkbox.user').each(function (index) {
36             if(this.checked){
37                 var record_id = this.id;
38                 var user_email = this.dataset.email; 
39                 console.log(this);
40                 // Delete in SFA Registry
41                 $.post("/delete/user/",{'filters':{'user_hrn':record_id}}, function(data) {
42                     if(data.success){
43                         $.post("/local_user/delete/",{'email':user_email}, function(data) {
44                             console.log(data);
45                             if (data == true){
46                                 mysliceAlert('Success: user deleted','success', true);
47                             }else{
48                                 mysliceAlert('Local DB Error for: '+record_id,'warning', true);
49                             }
50                             $('tr[id="'+record_id+'"]').fadeOut("slow");
51                             $('tr[id="'+record_id+'"]').remove();
52                         });
53                         //$.post("/delete/local:user/",{'filters':{'user_hrn':this.id}}, function(data) {
54                     }else{
55                         mysliceAlert('Rest Error for: '+data.error,'warning', true);
56                         //alert("Rest Error for "+record_id+": "+data.error);
57                     }   
58                     unspin_all();
59                 });
60             } 
61         });
62     });
63
64     /* TODO: factorize into functions */
65     $('button#deleteslices').click(function() {
66         spin_all();
67         var flag = false;
68         $('input:checkbox.slice').each(function (index) {
69             if(this.checked){
70                 var record_id = this.id;
71                 $('#'+record_id+'-loading').spin();
72                 $.post("/delete/slice/",{'filters':{'slice_hrn':this.id}}, function(data) {
73                     if(data.success){
74                         localStorage.clear();
75                         $('tr[id="'+record_id+'"]').fadeOut("slow");
76                         $('tr[id="'+record_id+'"]').remove();
77                         mysliceAlert('Success: slice deleted','success', true);
78                     }else{
79                         mysliceAlert('Rest Error for: '+data.error,'warning', true);
80                         //alert("Rest Error for "+record_id+": "+data.error);
81                     }
82                     unspin_all();
83                 });
84             }
85         });
86     });
87     $('button#renewslices').click(function() {
88         spin_all();
89         var now = new Date();
90         /* In Javascript getMonth() gives month[0] = january, month[1] = february, and so on...  */
91         var month = now.getMonth()+2;
92         var one_month_later = now.getFullYear()+"-"+month+"-"+now.getDate()+" "+now.getHours()+":"+now.getMinutes()+":"+now.getSeconds();
93         $('input:checkbox.slice').each(function (index) {
94             if(this.checked){
95                 var record_id = this.id;
96                 $('#'+record_id+'-loading').spin();
97                 $.post("/update/slice/",{'filters':{'slice_hrn':this.id},'params':{'expires':one_month_later}}, function(data) {
98                     if(data.success){
99                         // TODO: highlight row after success
100                         //$('tr[id="'+record_id+'"]').highlight();
101                         mysliceAlert('Success: slice renewed','success', true);
102                     }else{
103                         mysliceAlert('Rest Error for: '+data.error,'warning', true);
104                         //alert("Rest Error for "+record_id+": "+data.error);
105                     }
106                     unspin_all();
107                 });
108                 
109             }
110         });
111         // TODO: refresh table
112         //window.location="/portal/institution#slices";
113     });
114     $('button#deleteprojects').click(function() {
115         spin_all();
116         var flag = false;
117         $('input:checkbox.project').each(function (index) {
118             if(this.checked){
119                 var record_id = this.id;
120                 $('#'+record_id+'-loading').spin();
121                 console.log(record_id);
122                 $.post("/delete/myslice:authority/",{'filters':{'authority_hrn':this.id}}, function(data) {
123                     if(data.success){
124                         localStorage.clear();
125                         $('tr[id="'+record_id+'"]').fadeOut("slow");
126                         $('tr[id="'+record_id+'"]').remove();
127                         mysliceAlert('Success: project deleted','success', true);
128                     }else{
129                         mysliceAlert('Rest Error for: '+data.error,'warning', true);
130                         //alert("Rest Error for "+record_id+": "+data.error);
131                     }
132                     unspin_all();
133                 });
134             }
135         });
136     });
137
138     $('button#createslice').click(function() {
139         window.location="/portal/slice_request/";
140     });
141
142     $('button#createproject').click(function() {
143         window.location="/portal/project_request/";
144     });
145     $('button#slicerequestbtn').click(function() {
146         /*
147         window.location="/portal/slice_request/";
148         */
149     });
150 });
151
152 /*function loadUsers() {
153         $('div#users table').load('/table/user/',
154                 {
155                         'fields'  : [ 'user_hrn', 'user_first_name', 'user_last_name', 'user_email', 'user_phone' ],
156                         'filters' : { 'parent_authority' : $('div#users').data('authority') },
157                         'options' : [ 'checkbox' ]
158                 }
159         );
160 }*/