Bug 8283 - Replace usage of YUI Cookie utility with jQuery Cookie plugin
[srvgit] / koha-tmpl / intranet-tmpl / prog / en / js / pages / batchMod.js
1 // Set expiration date for cookies
2     var date = new Date();
3     date.setTime(date.getTime()+(365*24*60*60*1000));
4     var expiration = date.toGMTString();
5
6
7 function hideColumns(){
8   valCookie = $.cookie("showColumns");
9   if(valCookie){
10     valCookie = valCookie.split("/");
11     $("#showall").removeAttr("checked").parent().removeClass("selected");
12     for( i=0; i<valCookie.length; i++ ){
13       if(valCookie[i] !== ''){
14         index = valCookie[i] - 2;
15         $("#itemst td:nth-child("+valCookie[i]+"),#itemst th:nth-child("+valCookie[i]+")").toggle();
16         $("#checkheader"+index).removeAttr("checked").parent().removeClass("selected");
17       }
18     }
19   }
20 }
21
22 function hideColumn(num) {
23   $("#hideall,#showall").removeAttr("checked").parent().removeClass("selected");
24   valCookie = $.cookie("showColumns");
25   // set the index of the table column to hide
26   $("#"+num).parent().removeClass("selected");
27   var hide = Number(num.replace("checkheader","")) + 2;
28   // hide header and cells matching the index
29   $("#itemst td:nth-child("+hide+"),#itemst th:nth-child("+hide+")").toggle();
30   // set or modify cookie with the hidden column's index
31   if(valCookie){
32     valCookie = valCookie.split("/");
33     var found = false;
34     for( $i=0; $i<valCookie.length; $i++ ){
35         if (hide == valCookie[i]) {
36             found = true;
37             break;
38         }
39     }
40     if( !found ){
41         valCookie.push(hide);
42         var cookieString = valCookie.join("/");
43         $.cookie("showColumns", cookieString, { expires : date });
44     }
45   } else {
46         $.cookie("showColumns", hide, { expires : date });
47   }
48 }
49
50 // Array Remove - By John Resig (MIT Licensed)
51 // http://ejohn.org/blog/javascript-array-remove/
52 Array.prototype.remove = function(from, to) {
53   var rest = this.slice((to || from) + 1 || this.length);
54   this.length = from < 0 ? this.length + from : from;
55   return this.push.apply(this, rest);
56 };
57
58 function showColumn(num){
59   $("#hideall").removeAttr("checked").parent().removeClass("selected");
60   $("#"+num).parent().addClass("selected");
61   valCookie = $.cookie("showColumns");
62   // set the index of the table column to hide
63   show = Number(num.replace("checkheader","")) + 2;
64   // hide header and cells matching the index
65   $("#itemst td:nth-child("+show+"),#itemst th:nth-child("+show+")").toggle();
66   // set or modify cookie with the hidden column's index
67   if(valCookie){
68     valCookie = valCookie.split("/");
69     var found = false;
70     for( i=0; i<valCookie.length; i++ ){
71         if (show == valCookie[i]) {
72           valCookie.remove(i);
73           found = true;
74         }
75     }
76     if( found ){
77         var cookieString = valCookie.join("/");
78         $.cookie("showColumns", cookieString, { expires : date });
79     }
80   }
81 }
82 function showAllColumns(){
83     $("#selections").checkCheckboxes();
84     $("#selections span").addClass("selected");
85     $("#itemst td:nth-child(2),#itemst tr th:nth-child(2)").nextAll().show();
86     $.cookie("showColumns",null);
87     $("#hideall").removeAttr("checked").parent().removeClass("selected");
88 }
89 function hideAllColumns(){
90     $("#selections").unCheckCheckboxes();
91     $("#selections span").removeClass("selected");
92     $("#itemst td:nth-child(2),#itemst th:nth-child(2)").nextAll().hide();
93     $("#hideall").attr("checked","checked").parent().addClass("selected");
94     var cookieString = allColumns.join("/");
95     $.cookie("showColumns", cookieString, { expires : date });
96 }
97
98   $(document).ready(function() {
99     hideColumns();
100     $("#itemst").tablesorter({
101       widgets : ['zebra'],
102       headers: {0:{sorter: false}}
103     });
104     $("#selectallbutton").click(function(){
105       $("#itemst").checkCheckboxes();
106       return false;
107     });
108     $("#clearallbutton").click(function(){
109       $("#itemst").unCheckCheckboxes();
110       return false;
111     });
112     $("#selections input").change(function(e){
113       var num = $(this).attr("id");
114       if(num == 'showall'){
115         showAllColumns();
116         e.stopPropagation();
117       } else if(num == 'hideall'){
118         hideAllColumns();
119         e.stopPropagation();
120       } else {
121         if($(this).attr("checked")){
122           showColumn(num);
123         } else {
124           hideColumn(num);
125         }
126       }
127     });
128   });