Bug 29407: Make the pickup locations dropdown JS reusable
authorTomas Cohen Arazi <tomascohen@theke.io>
Wed, 3 Nov 2021 15:22:24 +0000 (12:22 -0300)
committerJonathan Druart <jonathan.druart@bugs.koha-community.org>
Mon, 8 Nov 2021 10:42:07 +0000 (11:42 +0100)
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 <tomascohen@theke.io>
Signed-off-by: Nick Clemens <nick@bywatersolutions.com>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
koha-tmpl/intranet-tmpl/prog/en/includes/holds_table.inc
koha-tmpl/intranet-tmpl/prog/en/modules/reserve/request.tt
koha-tmpl/intranet-tmpl/prog/js/holds.js

index a712bfa..dfddd9c 100644 (file)
                     [%- IF Koha.Preference('IndependentBranches') && Branches.all().size == 1 -%]
                         [% Branches.GetName(hold.branchcode) | html %] <input type="hidden" name="pickup" value="[% hold.branchcode | html %]" />
                     [%- ELSE -%]
-                        <select class="pickup_location_dropdown" data-selected="[% hold.branchcode | html %]" data-hold_id="[% hold.reserve_id | html %]" name="pickup">
+                        <select class="pickup_location_dropdown"
+                                data-selected="[% hold.branchcode | html %]"
+                                data-hold-id="[% hold.reserve_id | html %]"
+                                data-pickup-location-source="hold"
+                                name="pickup">
                             <option selected="selected" value="[% hold.branchcode | html %]">[% Branches.GetName(hold.branchcode) | html %]</option>
                             <option value="" disabled="disabled" class="loading">Loading...</option>
                         </select>
index 7fc53d3..1928610 100644 (file)
                             <li>
                                 <label for="pickup">Pickup at:</label>
                             [% UNLESS ( multi_hold ) %]
-                                <select name="pickup" id="pickup" data-biblio-id="[% biblio.biblionumber | html %]" data-patron-id="[% patron.borrowernumber | html %]">
+                                <select name="pickup" id="pickup"
+                                        data-biblio-id="[% biblio.biblionumber | html %]"
+                                        data-patron-id="[% patron.borrowernumber | html %]"
+                                        data-pickup-location-source="biblio">
                                     [% PROCESS options_for_libraries libraries => Branches.pickup_locations({ search_params => { biblio => biblionumber, patron => patron }, selected => pickup }) %]
                             [% ELSE %]
                                 <select name="pickup" id="pickup_multi" data-patron-id="[% patron.borrowernumber | html %]">
                                                     [% IF (itemloo.pickup_locations_count > 0) %]
                                                         <select name="item_pickup_[% itemloo.itemnumber | html %]" class="pickup_locations"
                                                                 data-item-id="[% itemloo.itemnumber | html %]"
-                                                                data-patron-id="[% patron.borrowernumber | html %]">
+                                                                data-patron-id="[% patron.borrowernumber | html %]"
+                                                                data-pickup-location-source="item">
                                                         [% IF (itemloo.default_pickup_location) %]
                                                             <option value="[% itemloo.default_pickup_location.branchcode | html %]" selected="selected">[% itemloo.default_pickup_location.branchname | html %]</option>
                                                         [% END %]
             [% 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({
             });
 
             $("#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();
             });
         });
 
index b03c9b3..a022482 100644 (file)
@@ -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='<select priority='+oObj.priority+' class="hold_location_select" data-hold-id="'+oObj.reserve_id+'" reserve_id="'+oObj.reserve_id+'" name="pick-location">';
+                                var branchSelect='<select priority='+oObj.priority+' class="hold_location_select" data-hold-id="'+oObj.reserve_id+'" reserve_id="'+oObj.reserve_id+'" name="pick-location" data-pickup-location-source="hold">';
                                 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);