From 935234cd49db3f7e2c6fe5b4b1bdf632630696bb Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Wed, 3 Nov 2021 14:37:11 +0000 Subject: [PATCH] Bug 29397: Add .kohaSelect select2 wrapper function This patch adds a kohaSelect wrapper function to simplify paging/infinite scrolling with select2 using the Koha RESTful api's. Invoke select2 select boxes as you normally would from JS, but instead of calling .select2(config) use .kohaSelect(config). If an 'ajax' property is defined in your config object, we wrap the transport such that responses include a pagination key as expected by select2. --- koha-tmpl/intranet-tmpl/prog/js/select2.js | 30 ++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/koha-tmpl/intranet-tmpl/prog/js/select2.js b/koha-tmpl/intranet-tmpl/prog/js/select2.js index 42383dd547..af86e07943 100644 --- a/koha-tmpl/intranet-tmpl/prog/js/select2.js +++ b/koha-tmpl/intranet-tmpl/prog/js/select2.js @@ -37,3 +37,33 @@ $(document).ready(function(){ }); }); }); + +/* wrapper that nests paging information into the results object for use with koha REST apis */ +(function($) { + $.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); -- 2.20.1