institution style
[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     /* TODO: factorize into functions */
20     $('button#deleteusers').click(function() {
21         $('input:checkbox.user').each(function (index) {
22             if(this.checked){
23                 var record_id = this.id;
24                 $.post("/delete/user/",{'filters':{'user_hrn':this.id}}, function(data) {
25                     if(data.success){
26                         $('tr[id="'+record_id+'"]').fadeOut("slow");
27                         $('tr[id="'+record_id+'"]').remove();
28                     }else{
29                         alert("Rest Error for "+record_id+": "+data.error);
30                     }
31                 });
32                 
33             }
34         });
35     });
36     $('button#deleteslices').click(function() {
37         $('input:checkbox.slice').each(function (index) {
38             if(this.checked){
39                 var record_id = this.id;
40                 $.post("/delete/slice/",{'filters':{'slice_hrn':this.id}}, function(data) {
41                     if(data.success){
42                         $('tr[id="'+record_id+'"]').fadeOut("slow");
43                         $('tr[id="'+record_id+'"]').remove();
44                     }else{
45                         alert("Rest Error for "+record_id+": "+data.error);
46                     }
47                 });
48                 
49             }
50         });
51     });
52     $('button#renewslices').click(function() {
53         var now = new Date();
54         /* In Javascript getMonth() gives month[0] = january, month[1] = february, and so on...  */
55         var month = now.getMonth()+2;
56         var one_month_later = now.getFullYear()+"-"+month+"-"+now.getDate()+" "+now.getHours()+":"+now.getMinutes()+":"+now.getSeconds();
57         console.log(one_month_later);
58         $('input:checkbox.slice').each(function (index) {
59             if(this.checked){
60                 var record_id = this.id;
61                 $.post("/update/slice/",{'filters':{'slice_hrn':this.id},'params':{'expires':one_month_later}}, function(data) {
62                     if(data.success){
63                         // TODO: highlight row after success
64                         //$('tr[id="'+record_id+'"]').highlight();
65                     }else{
66                         alert("Rest Error for "+record_id+": "+data.error);
67                     }
68                 });
69                 
70             }
71         });
72         // TODO: refresh table
73         //window.location="/portal/institution#slices";
74     });
75
76     $('button#createslice').click(function() {
77         window.location="/portal/slice_request/";
78     });
79     $('button#slicerequestbtn').click(function() {
80         /*
81         window.location="/portal/slice_request/";
82         */
83     });
84 });
85
86 function loadUsers() {
87         $('div#users table').load('/table/user/',
88                 {
89                         'fields'  : [ 'user_hrn', 'user_first_name', 'user_last_name', 'user_email', 'user_phone' ],
90                         'filters' : { 'parent_authority' : $('div#users').data('authority') },
91                         'options' : [ 'checkbox' ]
92                 }
93         );
94 }