Koha/koha-tmpl/intranet-tmpl/prog/js/select2.js
Tomas Cohen Arazi 7af1a0f052 Bug 29404: Add infinite scrolling to pickup location dropdowns
This patch adds infinite scrolling to the pickup locations select2
dropdowns on the staff interface.

It does so by adding a new transport function (in select2.js) to read
the response headers Koha's API sends back, and converting to the right
data structure Select2 expects for the feature to work.

This is manually used in the different pickup locations dropdowns.
There's a separate bug that will introduce a select2 wrapper that will probably embed this function in it.

To test:
1. Run the [DO NOT PUSH] script inside koha-shell to generate random
   pickup locations:
   $ kshell
  k$ perl generate_pickup_locations.pl
2. Try placing holds. Notice the visible pickup locations dropdowns
   display some pickup locations based on the matches you got. They are
   all fetched once
=> SUCCESS: It works
3. Repeat for the current holds page and the patron holds listing
=> SUCCESS: Same behavior
4. Apply this patch
5. Repeat 2 and 3
=> SUCCESS: Things work, but pickup locations are retrieved as needed,
while you scroll.
6. Sign off :-D

Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>

Signed-off-by: Nick Clemens <nick@bywatersolutions.com>

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
2021-11-08 11:42:07 +01:00

59 lines
2 KiB
JavaScript

/* global __ */
$.fn.select2.defaults.set("allowClear", true);
$.fn.select2.defaults.set("placeholder", "");
$.fn.select2.defaults.set("width", "element" );
// Internationalization
$.fn.select2.defaults.set("language", {
errorLoading:function(){ return __("The results could not be loaded"); },
inputTooLong:function(args){
var n = args.input.length - args.maximum;
return __("Please delete %s character(s)").format(n);
},
inputTooShort:function(args){
var n = args.minimum - args.input.length;
return __("Please enter %s or more characters").format(n);
},
formatResult: function(item) {
return $('<div>', {title: item.element[0].title}).text(item.text);
},
loadingMore:function(){ return __("Loading more results…"); },
maximumSelected:function(args){
return __("You can only select %s item(s)").format(args.maximum);
},
noResults:function(){return __("No results found"); },
searching:function(){return __("Searching…"); },
removeAllItems:function(){return __("Remove all items"); },
removeItem:function(){return __("Remove item"); }
});
$(document).ready(function(){
$(".select2").select2();
$(".select2").on("select2:clear", function () {
$(this).on("select2:opening.cancelOpen", function (evt) {
evt.preventDefault();
$(this).off("select2:opening.cancelOpen");
});
});
});
function kohaSelect2Transport(params, success, failure) {
var read_headers = function (data, textStatus, jqXHR) {
var more = false;
var link = jqXHR.getResponseHeader('Link') || '';
if (link.search(/<([^>]+)>;\s*rel\s*=\s*['"]?next['"]?\s*(,|$)/i) > -1) {
more = true;
}
return {
results: data,
pagination: {
more: more
}
};
};
var $request = $.ajax(params);
$request.then(read_headers).then(success);
$request.fail(failure);
}