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