From d28f9342825ad399ac62d28810345b4bdaddb022 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Wed, 3 Nov 2021 12:22:24 -0300 Subject: [PATCH] Bug 29407: Make the pickup locations dropdown JS reusable This patch generates a function to be used in places where select2 dropdowns are usesd for choosing pickup locations. This cleans repeated/almost identical code introduced by different bugs. To test: 1. Make sure choosing pickup locations works 2. Apply this patch 3. Repeat 1 => SUCCESS: No functional changes 4. Sign off :-D Signed-off-by: Tomas Cohen Arazi Signed-off-by: Nick Clemens Signed-off-by: Martin Renvoize Signed-off-by: Jonathan Druart --- .../prog/en/includes/holds_table.inc | 6 +- .../prog/en/modules/reserve/request.tt | 125 ++---------------- koha-tmpl/intranet-tmpl/prog/js/holds.js | 120 +++++++++++------ 3 files changed, 96 insertions(+), 155 deletions(-) diff --git a/koha-tmpl/intranet-tmpl/prog/en/includes/holds_table.inc b/koha-tmpl/intranet-tmpl/prog/en/includes/holds_table.inc index a712bfa4c7..dfddd9c75c 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/includes/holds_table.inc +++ b/koha-tmpl/intranet-tmpl/prog/en/includes/holds_table.inc @@ -129,7 +129,11 @@ [%- IF Koha.Preference('IndependentBranches') && Branches.all().size == 1 -%] [% Branches.GetName(hold.branchcode) | html %] [%- ELSE -%] - diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/reserve/request.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/reserve/request.tt index 7fc53d3d1c..1928610741 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/reserve/request.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/reserve/request.tt @@ -486,7 +486,10 @@
  • [% UNLESS ( multi_hold ) %] - [% PROCESS options_for_libraries libraries => Branches.pickup_locations({ search_params => { biblio => biblionumber, patron => patron }, selected => pickup }) %] [% ELSE %] + data-patron-id="[% patron.borrowernumber | html %]" + data-pickup-location-source="item"> [% IF (itemloo.default_pickup_location) %] [% END %] @@ -1292,42 +1296,7 @@ [% END %] $(".pickup_location_dropdown").each( function () { - var this_dropdown = $(this); - var hold_id = $(this).data('hold_id'); - - this_dropdown.select2({ - allowClear: false, - ajax: { - url: '/api/v1/holds/' + encodeURIComponent(hold_id) + '/pickup_locations', - delay: 300, // wait 300 milliseconds before triggering the request - dataType: 'json', - data: function (params) { - var search_term = (params.term === undefined) ? '' : params.term; - var query = { - "q": JSON.stringify({"name":{"-like":'%'+search_term+'%'}}), - "_order_by": "name", - "_page": params.page - }; - - return query; - }, - processResults: function (data, params) { - var results = []; - data.results.forEach( function ( pickup_location ) { - results.push( - { - "id": pickup_location.library_id.escapeHtml(), - "text": pickup_location.name.escapeHtml(), - "needs_override": pickup_location.needs_override - } - ); - }); - return { "results": results, "pagination": { "more": data.pagination.more } }; - }, - transport: kohaSelect2Transport - }, - templateResult: display_pickup_location - }); + $(this).pickup_locations_dropdown(); }); $("#pickup_multi").select2({ @@ -1354,83 +1323,11 @@ }); $("#pickup").each( function () { - var this_dropdown = $(this); - var patron_id = $(this).data('patron-id'); - var biblio_id = $(this).data('biblio-id'); - - this_dropdown.select2({ - width: 'style', - allowClear: false, - ajax: { - url: '/api/v1/biblios/' + encodeURIComponent(biblio_id) + '/pickup_locations', - delay: 300, // wait 300 milliseconds before triggering the request - dataType: 'json', - data: function (params) { - var search_term = (params.term === undefined) ? '' : params.term; - var query = { - "q": JSON.stringify({"name":{"-like":'%'+search_term+'%'}}), - "_order_by": "name", - "patron_id": patron_id, - "_page": params.page - }; - return query; - }, - processResults: function (data) { - var results = []; - data.results.forEach( function ( pickup_location ) { - results.push( - { - "id": pickup_location.library_id.escapeHtml(), - "text": pickup_location.name.escapeHtml(), - "needs_override": pickup_location.needs_override - } - ); - }); - return { "results": results, "pagination": { "more": data.pagination.more } }; - }, - transport: kohaSelect2Transport, - }, - templateResult: display_pickup_location - }); + $(this).pickup_locations_dropdown(); }); - $(".pickup_locations").each( function () { - var this_dropdown = $(this); - var patron_id = $(this).data('patron-id'); - var item_id = $(this).data('item-id'); - - this_dropdown.select2({ - allowClear: true, - ajax: { - url: '/api/v1/items/' + encodeURIComponent(item_id) + '/pickup_locations', - delay: 300, // wait 300 milliseconds before triggering the request - dataType: 'json', - data: function (params) { - var search_term = (params.term === undefined) ? '' : params.term; - var query = { - "q": JSON.stringify({"name":{"-like":'%'+search_term+'%'}}), - "_order_by": "name", - "patron_id": patron_id, - "_page": params.page - }; - return query; - }, - processResults: function (data) { - var results = []; - data.results.forEach( function ( pickup_location ) { - results.push( - { - "id": pickup_location.library_id.escapeHtml(), - "text": pickup_location.name.escapeHtml(), - "needs_override": pickup_location.needs_override - } - ); - }); - return { "results": results, "pagination": { "more": data.pagination.more } }; - }, - transport: kohaSelect2Transport - }, - templateResult: display_pickup_location - }); + + $(".pickup_locations").each(function () { + $(this).pickup_locations_dropdown(); }); }); diff --git a/koha-tmpl/intranet-tmpl/prog/js/holds.js b/koha-tmpl/intranet-tmpl/prog/js/holds.js index b03c9b388b..a022482bce 100644 --- a/koha-tmpl/intranet-tmpl/prog/js/holds.js +++ b/koha-tmpl/intranet-tmpl/prog/js/holds.js @@ -14,6 +14,84 @@ function display_pickup_location (state) { return $text; }; +(function ($) { + + /** + * Generate a Select2 dropdown for pickup locations + * + * It expects the select object to contain several data-* attributes + * - data-pickup-location-source: 'biblio', 'item' or 'hold' (default) + * - data-patron-id: required for 'biblio' and 'item' + * - data-biblio-id: required for 'biblio' only + * - data-item-id: required for 'item' only + * + * @return {Object} The Select2 instance + */ + + $.fn.pickup_locations_dropdown = function () { + var select = $(this); + var pickup_location_source = $(this).data('pickup-location-source'); + var patron_id = $(this).data('patron-id'); + var biblio_id = $(this).data('biblio-id'); + var item_id = $(this).data('item-id'); + var hold_id = $(this).data('hold-id'); + + var url; + + if ( pickup_location_source === 'biblio' ) { + url = '/api/v1/biblios/' + encodeURIComponent(biblio_id) + '/pickup_locations'; + } + else if ( pickup_location_source === 'item' ) { + url = '/api/v1/items/' + encodeURIComponent(item_id) + '/pickup_locations'; + } + else { // hold + url = '/api/v1/holds/' + encodeURIComponent(hold_id) + '/pickup_locations'; + } + + select.select2({ + width: 'style', + allowClear: false, + ajax: { + url: url, + delay: 300, // wait 300 milliseconds before triggering the request + cache: true, + dataType: 'json', + data: function (params) { + var search_term = (params.term === undefined) ? '' : params.term; + var query = { + "q": JSON.stringify({"name":{"-like":'%'+search_term+'%'}}), + "_order_by": "name", + "_page": params.page + }; + + if ( pickup_location_source !== 'hold' ) { + query["patron_id"] = patron_id; + } + + return query; + }, + processResults: function (data) { + var results = []; + data.results.forEach( function ( pickup_location ) { + results.push( + { + "id": pickup_location.library_id.escapeHtml(), + "text": pickup_location.name.escapeHtml(), + "needs_override": pickup_location.needs_override + } + ); + }); + return { "results": results, "pagination": { "more": data.pagination.more } }; + }, + transport: kohaSelect2Transport, + }, + templateResult: display_pickup_location + }); + + return select; + }; +})(jQuery); + /* global __ dataTablesDefaults borrowernumber SuspendHoldsIntranet */ $(document).ready(function() { var holdsTable; @@ -94,7 +172,7 @@ $(document).ready(function() { { "mDataProp": function( oObj ) { if( oObj.branches.length > 1 && oObj.found !== 'W' && oObj.found !== 'T' ){ - var branchSelect=''; for ( var i=0; i < oObj.branches.length; i++ ){ var selectedbranch; var setbranch; @@ -235,45 +313,7 @@ $(document).ready(function() { }); }); - $(".hold_location_select").each( function () { - var this_dropdown = $(this); - var hold_id = $(this).data('hold-id'); - - this_dropdown.select2({ - allowClear: false, - ajax: { - url: '/api/v1/holds/' + encodeURIComponent(hold_id) + '/pickup_locations', - delay: 300, // wait 300 milliseconds before triggering the request - dataType: 'json', - cache: true, - data: function (params) { - var search_term = (params.term === undefined) ? '' : params.term; - var query = { - "q": JSON.stringify({"name":{"-like":'%'+search_term+'%'}}), - "_order_by": "name", - "_page": params.page - }; - return query; - }, - processResults: function (data) { - var results = []; - data.results.forEach( function ( pickup_location ) { - results.push( - { - "id": pickup_location.library_id.escapeHtml(), - "text": pickup_location.name.escapeHtml(), - "needs_override": pickup_location.needs_override - } - ); - }); - return { "results": results, "pagination": { "more": data.pagination.more } }; - }, - transport: kohaSelect2Transport - }, - templateResult: display_pickup_location - }); - $('.select2-container').css('width','100%'); - }); + $(".hold_location_select").each(function(){ $(this).pickup_locations_dropdown(); }); $(".hold_location_select").on("change", function(){ $(this).prop("disabled",true); -- 2.39.5