Koha/koha-tmpl/intranet-tmpl/prog/js/select2.js
Tomas Cohen Arazi a444eeac01 Bug 29397: Use kohaSelect for pickup locations
This patch makes the pickup location dropdowns that rely on Select2 and
the API use the new wrapper. The original transport function is removed
as it is now embedded in the wrapper.

To test:
1. Follow bug 29404 test plan
=> SUCCESS: All works
2. Apply this patches
3. Repeat 1
=> SUCCESS: All works!
4. sign off :-D

Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Signed-off-by: Fridolin Somers <fridolin.somers@biblibre.com>
2022-02-10 14:44:23 -10:00

76 lines
2.6 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($) {
/**
* Create a new Select2 instance that uses the Koha RESTful API response headers to
* read pagination information
* @param {Object} config Please see the Select2 documentation for further details
* @return {Object} The Select2 instance
*/
$.fn.kohaSelect = function(config) {
if (config.hasOwnProperty('ajax')) {
config.ajax.transport = function(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);
};
}
$(this).select2(config);
};
})(jQuery);