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