Bug 19490: Add a 'Holds' column to the items batchmodification tool
[koha.git] / koha-tmpl / intranet-tmpl / prog / 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").prop("checked", false).parent().removeClass("selected");
12     for( i=0; i<valCookie.length; i++ ){
13       if(valCookie[i] !== ''){
14         index = valCookie[i] - 3;
15         $("#itemst td:nth-child("+valCookie[i]+"),#itemst th:nth-child("+valCookie[i]+")").toggle();
16         $("#checkheader"+index).prop("checked", false).parent().removeClass("selected");
17       }
18     }
19   }
20 }
21
22 function hideColumn(num) {
23   $("#hideall,#showall").prop("checked", false).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","")) + 3;
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, path: '/' });
44     }
45   } else {
46         $.cookie("showColumns", hide, { expires : date, path: '/' });
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").prop("checked", false).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","")) + 3;
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, path: '/' });
79     }
80   }
81 }
82 function showAllColumns(){
83     $("#selections").checkCheckboxes();
84     $("#selections span").addClass("selected");
85     $("#itemst td:nth-child(3),#itemst tr th:nth-child(3)").nextAll().show();
86     $.removeCookie("showColumns", { path: '/' });
87     $("#hideall").prop("checked", false).parent().removeClass("selected");
88 }
89 function hideAllColumns(){
90     $("#selections").unCheckCheckboxes();
91     $("#selections span").removeClass("selected");
92     $("#itemst td:nth-child(3),#itemst th:nth-child(3)").nextAll().hide();
93     $("#hideall").prop("checked", true).parent().addClass("selected");
94     var cookieString = allColumns.join("/");
95     $.cookie("showColumns", cookieString, { expires : date, path: '/' });
96 }
97
98   $(document).ready(function() {
99     hideColumns();
100     $("#itemst").dataTable($.extend(true, {}, dataTablesDefaults, {
101         "sDom": 't',
102         "aoColumnDefs": [
103             { "aTargets": [ 0 ], "bSortable": false, "bSearchable": false },
104             { "sType": "anti-the", "aTargets" : [ "anti-the" ] }
105         ],
106         "bPaginate": false,
107     }));
108     $("#selectallbutton").click(function(){
109       $("#itemst").checkCheckboxes();
110       return false;
111     });
112     $("#clearallbutton").click(function(){
113       $("#itemst").unCheckCheckboxes();
114       return false;
115     });
116     $("#clearonloanbutton").click(function(){
117       $("#itemst input[name='itemnumber'][data-is-onloan='1']").each(function(){
118         $(this).prop('checked', false);
119       });
120       return false;
121     });
122     $("#selections input").change(function(e){
123       var num = $(this).attr("id");
124       if(num == 'showall'){
125         showAllColumns();
126         e.stopPropagation();
127       } else if(num == 'hideall'){
128         hideAllColumns();
129         e.stopPropagation();
130       } else {
131         if($(this).prop("checked")){
132           showColumn(num);
133         } else {
134           hideColumn(num);
135         }
136       }
137     });
138   });