Bug 29407: Make the pickup locations dropdown JS reusable
[koha.git] / koha-tmpl / intranet-tmpl / prog / js / z3950_search.js
1 /* global __ total_pages */
2 //z3950_search.js for Authorities, Bib records and Acquisitions module
3
4 var last_action;
5
6 function validate_goto_page() {
7     var page = $('#goto_page').val();
8     if (isNaN(page)) {
9         alert( __("The page entered is not a number.") );
10         return false;
11     } else if (page < 1 || page > total_pages) {
12         alert( __("The page should be a number between 1 and %s.").format(total_pages) );
13         return false;
14     } else {
15         return true;
16     }
17 }
18
19 $( document ).ready( function() {
20
21     $( "#CheckAll" ).click( function(e) {
22         e.preventDefault();
23         $( ".checkboxed input:checkbox" ).prop("checked", true);
24     });
25     $( "#CheckNone" ).click( function(e) {
26         e.preventDefault();
27         $( ".checkboxed input:checkbox" ).prop("checked", false);
28     });
29
30     $( ".submit" ).on( "click", function() {
31         $( "body" ).css( "cursor", "wait" );
32     });
33     $( "[name='changepage_prev']" ).on( "click", function() {
34         var data_current_page_prev = $( this ).data( "currentpage" );
35         $( '#current_page' ).val( data_current_page_prev - 1 );
36         $( '#page_form' ).submit();
37     });
38     $( "[name='changepage_next']" ).on( "click", function() {
39         var data_current_page_next = $( this ).data( "currentpage" );
40         $( '#current_page' ).val( data_current_page_next + 1 );
41         $( '#page_form' ).submit();
42     });
43     $( "[name='changepage_goto']" ).on( "click", function() {
44         return validate_goto_page();
45     });
46     $( "#resetZ3950Search" ).click( function(e) {
47         e.preventDefault();
48         $( "form[name='f']" ).find( "input[type=text]" ).val( "" );
49     });
50     $( "form[name='f']" ).submit( function() {
51         if ( $( 'input[type=checkbox]' ).filter( ':checked' ).length == 0 ) {
52             alert( __("Please choose at least one external target") );
53             $( "body" ).css( "cursor", "default" );
54             return false;
55         } else {
56             return true;
57         }
58     });
59
60     /* Display actions menu anywhere the table is clicked */
61     /* Note: The templates where this is included must have a search results
62        table with the id "resultst" and "action" table cells with the class "actions" */
63     $("#resultst").on("click", "td", function(event){
64         event.preventDefault();
65         var tgt = $(event.target);
66         var row = $(this).closest('tr');
67         /* Remove highlight from all rows and add to the clicked row */
68         $("tr").removeClass("highlighted-row");
69         row.addClass("highlighted-row");
70         /* Remove any menus created on the fly for other rows */
71         $(".btn-wrapper").remove();
72
73         if( tgt.hasClass("z3950actions")  ) { // direct button click
74             var link = $( "a[title='" + tgt.text() + "']", row );
75             if( link.length == 1) link.click();
76             row.find('ul.dropdown-menu').hide();
77         } else {
78             event.stopPropagation();
79             /* Remove the "open" class from all dropup menus in case one is open */
80             $(".dropup").removeClass("open");
81             /* Create a clone of the Bootstrap dropup menu in the "Actions" column */
82             var menu_clone = $(".dropdown-menu", row)
83                 .clone()
84                 .addClass("menu-clone")
85                 .css({
86                     "display" : "block",
87                     "position" : "absolute",
88                     "top" : "auto",
89                     "bottom" : "100%",
90                     "right" : "auto",
91                     "left" : "0",
92                 });
93             /* Append the menu clone to the table cell which was clicked.
94                 The menu must first be wrapped in a block-level div to clear
95                 the table cell's text contents and then a relative-positioned
96                 div to allow the menu to be positioned correctly */
97             if( tgt.prop('nodeName') != 'TD' ) {
98                 // handling click on caret to improve menu position
99                 tgt = tgt.closest('td');
100             }
101             tgt.append(
102                 $('<div/>', {'class': 'btn-wrapper'}).append(
103                     $('<div/>', {'class': 'btn-group'}).append(
104                         menu_clone
105                     )
106                 )
107             );
108         }
109     });
110
111     $( "#dataPreview" ).on( "hidden", function() {
112         $( "#dataPreviewLabel" ).html( "" );
113         $( "#dataPreview .modal-body" ).html( "<div id='loading'><img src='" + interface + "/" + theme + "/img/spinner-small.gif' alt='' /> " + __("Loading") + "</div>" );
114     });
115
116     $( "#resultst" ).on("click", ".previewData", function(e) {
117         e.preventDefault();
118         ChangeLastAction( $(this).data('action'), $(this).attr('title') );
119         var long_title = $( this ).text();
120         var page = $( this ).attr( "href" );
121         $( "#dataPreviewLabel" ).text( long_title );
122         $( "#dataPreview .modal-body" ).load( page + " div" );
123         $( '#dataPreview' ).modal( {show:true} );
124     });
125
126     $( "#resultst" ).on("click", ".chosen", function(e) {
127         e.preventDefault();
128         var action = $(this).data('action');
129         ChangeLastAction( action );
130         if( action == 'order' ) window.location = $(this).attr('href');
131         else { // import
132             opener.document.location = $(this).attr('href');
133             window.close();
134         }
135     });
136 });
137
138 function InitLastAction() {
139     if( $("#resultst").length == 0 ) return;
140     try { last_action = localStorage.getItem('z3950search_last_action'); } catch (err) {}
141     if( last_action ) {
142         // get short title from attr
143         var short_title = $(".z3950actions:eq(0)").siblings(".dropdown-menu").find("a[data-action='"+last_action+"']").attr('title');
144         if( short_title && last_action != 'show_marc' ) {
145             $( ".z3950actions" ).text( short_title );
146         }
147     }
148 }
149
150 function ChangeLastAction( action, short_title ) {
151     if( last_action && last_action == action ) return;
152     last_action = action;
153     if( short_title ) { // Save choice for preview (MARC or Card)
154         $( ".z3950actions" ).text( short_title );
155         try { localStorage.setItem('z3950search_last_action', last_action ); } catch(err) {}
156     }
157 }