Bug 29407: Make the pickup locations dropdown JS reusable
[koha.git] / koha-tmpl / intranet-tmpl / prog / js / viewlog.js
1 var Sticky;
2
3 function tickAll(section){
4     $("input[type='checkbox'][name='" + section + "']").prop("checked", true);
5     $("#" + section.slice(0,-1) + "ALL").prop("checked", true);
6     $("input[type='checkbox'][name='" + section + "']").prop("disabled", true);
7     $("#" + section.slice(0,-1) + "ALL").prop("disabled", false);
8 }
9
10 function untickAll(section){
11     $("input[type='checkbox'][name='" + section + "']").prop("checked", false);
12     $("input[type='checkbox'][name='" + section + "']").prop("disabled", false);
13 }
14
15 function limitCheckboxes() {
16     var checkboxes = $(".compare");
17     var limit = 2;
18     var compare_link = '<a href="#" class="btn btn-link compare_link"><i class="fa fa-columns"></i> ' + __("View comparison") + '</a>';
19     checkboxes.each(function(){
20         $(this).on("change", function(){
21             var checked = [];
22             checkboxes.each(function () {
23                 if( $(this).prop("checked") ){
24                     checked.push( $(this).data("actionid") );
25                 }
26             });
27             if (checked.length > 0) {
28                 $("#select_none").removeClass("disabled");
29             } else {
30                 $("#select_none").addClass("disabled");
31                 $("#logst").DataTable().search("").draw();
32             }
33             if( checked.length == 1 ){
34                 $("#logst").DataTable().search($(this).data("filter")).draw();
35                 humanMsg.displayAlert( __("Showing results for %s").format( $(this).data("filter") ) );
36             }
37             if( checked.length == 2 ){
38                 $("#compare_info" + checked[0]).prepend( compare_link );
39                 $("#compare_info" + checked[1]).prepend( compare_link );
40                 $("button.compare_link").removeClass("disabled");
41             } else if (checked.length > limit) {
42                 humanMsg.displayAlert( __("You can select maximum of two checkboxes") );
43                 $(this).prop("checked", false );
44             } else if (checked.length < limit) {
45                 $("a.compare_link").remove();
46                 $("button.compare_link").addClass("disabled");
47             }
48         });
49     });
50 }
51
52 $(document).ready(function(){
53     limitCheckboxes();
54
55     if( $(".compare_info").length == 0 ){
56         /* Remove toolbar if there are no system preference
57            entries to compare */
58         $("#toolbar").remove();
59     }
60
61     if ($('#toolbar').length) {
62         Sticky = $("#toolbar");
63         Sticky.hcSticky({
64             stickTo: "main",
65             stickyClass: "floating"
66         });
67     }
68
69     if ( $('input[type="checkbox"][name="modules"]:checked').length == 0 ) {
70         tickAll('modules');
71     }
72     $("#moduleALL").change(function(){
73         if ( this.checked == true ){
74             tickAll('modules');
75         } else {
76             untickAll('modules');
77         }
78     });
79     $("input[type='checkbox'][name='modules']").change(function(){
80         if ( $("input[name='modules']:checked").length == $("input[name='modules']").length - 1 ){
81             tickAll('modules');
82         }
83     });
84
85     if ( $('input[name="actions"]:checked').length == 0 ) {
86         tickAll('actions');
87     }
88     $("#actionALL").change(function(){
89         if ( this.checked == true ){
90             tickAll('actions');
91         } else {
92             untickAll('actions');
93         }
94
95     });
96     $("input[name='actions']").change(function(){
97         if ( $("input[name='actions']:checked").length == $("input[name='actions']").length - 1 ){
98             tickAll('actions');
99         }
100     });
101
102     if ( $('input[name="interfaces"]:checked').length == 0 ) {
103         tickAll('interfaces');
104     }
105     $("#interfaceALL").change(function(){
106         if ( this.checked == true ){
107             tickAll('interfaces');
108         } else {
109             untickAll('interfaces');
110         }
111
112     });
113     $("input[name='interfaces']").change(function(){
114         if ( $("input[name='interfaces']:checked").length == $("input[name='interfaces']").length - 1 ){
115             tickAll('interfaces');
116         }
117     });
118
119     var logst = KohaTable("logst", {
120         "autoWidth": false,
121         "order": [[0, "desc"]],
122         "pagingType" : "full"
123     }, columns_settings);
124
125     $("body").on("click", ".compare_link", function(e){
126         e.preventDefault();
127         if( $(this).hasClass('disabled') ){
128             humanMsg.displayAlert( __("You must select two entries to compare") );
129         } else {
130             var firstid = $(".compare:checked").eq(0).data("actionid");
131             var secondid = $(".compare:checked").eq(1).data("actionid");
132             var firstvalue = $("#loginfo" + firstid).text();
133             var secondvalue = $("#loginfo" + secondid).text();
134             var diffs = diffString(secondvalue, firstvalue);
135             $("#col1 pre,#col2 pre").html(diffs);
136             $("#compareInfo").modal("show");
137         }
138     });
139     $("#compareInfo").on("hidden.bs.modal", function(){
140         $("#col1 pre,#col2 pre").html("");
141     });
142
143     $("#select_none").on("click", function(e){
144         e.preventDefault();
145         $(".compare:checked").prop("checked", false).change();
146     });
147     var obj = $("#user").autocomplete({
148         source: "/cgi-bin/koha/circ/ysearch.pl",
149         minLength: 3,
150         select: function ( event, ui ) {
151             $("#user").val( ui.item.borrowernumber );
152             return false;
153         }
154     }).data("ui-autocomplete");
155     if (obj) {
156         obj._renderItem = function (ul, item) {
157             var cardnumber = "";
158             if (item.cardnumber != "") {
159                 // Display card number in parentheses if it exists
160                 cardnumber = " (" + item.cardnumber + ") ";
161             }
162             var itemString = "<a href=\"#\">" + (item.surname ? item.surname.escapeHtml() : "") + ", " + (item.firstname ? item.firstname.escapeHtml() : "") + cardnumber.escapeHtml() + " <small>";
163             itemString += (item.address ? item.address.escapeHtml() : "") + " "
164                 + (item.city ? item.city.escapeHtml() : "") + " "
165                 + (item.zipcode ? item.zipcode.escapeHtml() : "") + " "
166                 + (item.country ? item.country.escapeHtml() : "")
167                 + "</small></a>";
168             return $("<li></li>")
169                 .data("ui-autocomplete-item", item)
170                 .append(itemString)
171                 .appendTo(ul);
172         };
173     }
174
175 });