e8d5121ca9dceac59d15aaeab2c8f26ede51a80d
[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                 $.post("/delete/user/",{'filters':{'user_hrn':this.id}}, function(data) {
24                     if(data.success){
25                         $('tr[id="'+record_id+'"]').fadeOut("slow");
26                         $('tr[id="'+record_id+'"]').remove();
27                         mysliceAlert('Success: user deleted','success', true);
28                     }else{
29                         mysliceAlert('Rest Error for: '+data.error,'warning', true);
30                         //alert("Rest Error for "+record_id+": "+data.error);
31                     }   
32                 });     
33             } 
34         });
35     });
36
37     /* TODO: factorize into functions */
38     $('button#deleteslices').click(function() {
39         var flag = false;
40         $('input:checkbox.slice').each(function (index) {
41             if(this.checked){
42                 var record_id = this.id;
43                 $.post("/delete/slice/",{'filters':{'slice_hrn':this.id}}, function(data) {
44                     if(data.success){
45                         localStorage.clear();
46                         $('tr[id="'+record_id+'"]').fadeOut("slow");
47                         $('tr[id="'+record_id+'"]').remove();
48                         mysliceAlert('Success: slice deleted','success', true);
49                     }else{
50                         mysliceAlert('Rest Error for: '+data.error,'warning', true);
51                         //alert("Rest Error for "+record_id+": "+data.error);
52                     }
53                 });
54             }
55         });
56     });
57     $('button#renewslices').click(function() {
58         var now = new Date();
59         /* In Javascript getMonth() gives month[0] = january, month[1] = february, and so on...  */
60         var month = now.getMonth()+2;
61         var one_month_later = now.getFullYear()+"-"+month+"-"+now.getDate()+" "+now.getHours()+":"+now.getMinutes()+":"+now.getSeconds();
62         $('input:checkbox.slice').each(function (index) {
63             if(this.checked){
64                 var record_id = this.id;
65                 $.post("/update/slice/",{'filters':{'slice_hrn':this.id},'params':{'expires':one_month_later}}, function(data) {
66                     if(data.success){
67                         // TODO: highlight row after success
68                         //$('tr[id="'+record_id+'"]').highlight();
69                         mysliceAlert('Success: slice renewed','success', true);
70                     }else{
71                         mysliceAlert('Rest Error for: '+data.error,'warning', true);
72                         //alert("Rest Error for "+record_id+": "+data.error);
73                     }
74                 });
75                 
76             }
77         });
78         // TODO: refresh table
79         //window.location="/portal/institution#slices";
80     });
81     $('button#deleteprojects').click(function() {
82         var flag = false;
83         $('input:checkbox.project').each(function (index) {
84             if(this.checked){
85                 var record_id = this.id;
86                 console.log(record_id);
87                 $.post("/delete/myslice:authority/",{'filters':{'authority_hrn':this.id}}, function(data) {
88                     if(data.success){
89                         localStorage.clear();
90                         $('tr[id="'+record_id+'"]').fadeOut("slow");
91                         $('tr[id="'+record_id+'"]').remove();
92                         mysliceAlert('Success: project deleted','success', true);
93                     }else{
94                         mysliceAlert('Rest Error for: '+data.error,'warning', true);
95                         //alert("Rest Error for "+record_id+": "+data.error);
96                     }
97                 });
98             }
99         });
100     });
101
102     $('button#createslice').click(function() {
103         window.location="/portal/slice_request/";
104     });
105
106     $('button#createproject').click(function() {
107         window.location="/portal/project_request/";
108     });
109     $('button#slicerequestbtn').click(function() {
110         /*
111         window.location="/portal/slice_request/";
112         */
113     });
114 });
115
116 /*function loadUsers() {
117         $('div#users table').load('/table/user/',
118                 {
119                         'fields'  : [ 'user_hrn', 'user_first_name', 'user_last_name', 'user_email', 'user_phone' ],
120                         'filters' : { 'parent_authority' : $('div#users').data('authority') },
121                         'options' : [ 'checkbox' ]
122                 }
123         );
124 }*/