Bug 18729: Add PUT /holds/{hold_id}/pickup_location
[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, previewed = 0;
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     $( "#resultst" ).on("click", ".previewData", function(e) {
112         e.preventDefault();
113         previewed = 1;
114         ChangeLastAction( $(this).attr('title'), 1 );
115         var ltitle = $( this ).text();
116         var page = $( this ).attr( "href" );
117         $( "#dataPreviewLabel" ).text( ltitle );
118         $( "#dataPreview .modal-body" ).load( page + " div" );
119         $( '#dataPreview' ).modal( {show:true} );
120     });
121
122     $( "#dataPreview" ).on( "hidden", function() {
123         $( "#dataPreviewLabel" ).html( "" );
124         $( "#dataPreview .modal-body" ).html( "<div id='loading'><img src='" + interface + "/" + theme + "/img/spinner-small.gif' alt='' /> " + __("Loading") + "</div>" );
125     });
126
127     $( "#resultst" ).on("click", ".chosen", function(e) {
128         e.preventDefault();
129         var title = $(this).attr('title');
130         ChangeLastAction( title, 0 );
131         if( title == 'Order' ) window.location = $(this).attr('href');
132         else {
133             opener.document.location = $(this).attr('href');
134             window.close();
135         }
136     });
137 });
138
139 function InitLastAction() {
140     if( $("#resultst").length == 0 ) return;
141     try { last_action = localStorage.getItem('z3950search_last_action'); } catch (err) {}
142     if( last_action ) {
143         var linkcount = $(".z3950actions:eq(0)").siblings(".dropdown-menu").find("a[title='"+last_action+"']").length;
144         if( linkcount == 0 ) return;
145         if( last_action != 'MARC' ) $( ".z3950actions" ).text( last_action );
146     }
147 }
148
149 function ChangeLastAction(title, change_text) {
150     if( last_action && last_action == title ) return;
151     last_action = title;
152     if( change_text ) $( ".z3950actions" ).text( last_action );
153     if( previewed == 0 || change_text == 1 )
154         try { localStorage.setItem('z3950search_last_action', last_action); } catch(err) {}
155 }