From b9cd868ab2139413bb71319958f94e1095416595 Mon Sep 17 00:00:00 2001 From: Justin Coyne Date: Mon, 10 Oct 2022 16:20:56 -0500 Subject: [PATCH] Ensure only one copy by using a const declaration This code was written before javascript had const, so it went through a bunch of gymnastics to be sure the code wasn't defined twice. This is no longer necessary --- .../range_limit_shared.js | 129 ++++++++---------- 1 file changed, 59 insertions(+), 70 deletions(-) diff --git a/app/assets/javascripts/blacklight_range_limit/range_limit_shared.js b/app/assets/javascripts/blacklight_range_limit/range_limit_shared.js index 456825a8..898779ee 100644 --- a/app/assets/javascripts/blacklight_range_limit/range_limit_shared.js +++ b/app/assets/javascripts/blacklight_range_limit/range_limit_shared.js @@ -1,84 +1,73 @@ /** - * Global BlacklightRangeLimit module setup. + * BlacklightRangeLimit module setup. */ -!function(global) { - 'use strict'; +'use strict'; - var previousBlacklightRangeLimit = global.BlacklightRangeLimit; +const BlacklightRangeLimit = function (options) { + this.options = options || {}; +} - function BlacklightRangeLimit(options) { - this.options = options || {}; - } - - BlacklightRangeLimit.display_ratio = 1/(1.618 * 2); // half a golden rectangle, why not - /* A custom event "plotDrawn.blacklight.rangeLimit" will be sent when flot plot - is (re-)drawn on screen possibly with a new size. target of event will be the DOM element - containing the plot. Used to resize slider to match. */ - BlacklightRangeLimit.redrawnEvent = "plotDrawn.blacklight.rangeLimit"; +BlacklightRangeLimit.display_ratio = 1/(1.618 * 2); // half a golden rectangle, why not +/* A custom event "plotDrawn.blacklight.rangeLimit" will be sent when flot plot + is (re-)drawn on screen possibly with a new size. target of event will be the DOM element + containing the plot. Used to resize slider to match. */ +BlacklightRangeLimit.redrawnEvent = "plotDrawn.blacklight.rangeLimit"; - // takes a string and parses into an integer, but throws away commas first, to avoid truncation when there is a comma - // use in place of javascript's native parseInt - BlacklightRangeLimit.parseNum = function parseNum(str) { - str = String(str).replace(/[^0-9-]/g, ''); - return parseInt(str, 10); - }; +// takes a string and parses into an integer, but throws away commas first, to avoid truncation when there is a comma +// use in place of javascript's native parseInt +BlacklightRangeLimit.parseNum = function parseNum(str) { + str = String(str).replace(/[^0-9-]/g, ''); + return parseInt(str, 10); +}; - BlacklightRangeLimit.form_selection = function form_selection(form, min, max) { - var begin_val = BlacklightRangeLimit.parseNum($(form).find("input.range_begin").val()); - if (isNaN(begin_val) || begin_val < min) { - begin_val = min; - } - var end_val = BlacklightRangeLimit.parseNum($(form).find("input.range_end").val()); - if (isNaN(end_val) || end_val > max) { - end_val = max; - } - - return BlacklightRangeLimit.normalized_selection(begin_val, end_val); +BlacklightRangeLimit.form_selection = function form_selection(form, min, max) { + var begin_val = BlacklightRangeLimit.parseNum($(form).find("input.range_begin").val()); + if (isNaN(begin_val) || begin_val < min) { + begin_val = min; } - - // Add AJAX fetched range facets if needed, and add a chart to em - BlacklightRangeLimit.checkForNeededFacetsToFetch = function checkForNeededFacetsToFetch() { - $(".range_limit .profile .distribution a.load_distribution").each(function() { - var container = $(this).parent('div.distribution'); - - $(container).load($(this).attr('href'), function(response, status) { - if ($(container).hasClass("chart_js") && status == "success" ) { - BlacklightRangeLimit.turnIntoPlot(container); - } - }); - }); + var end_val = BlacklightRangeLimit.parseNum($(form).find("input.range_end").val()); + if (isNaN(end_val) || end_val > max) { + end_val = max; } - BlacklightRangeLimit.function_for_find_segment = function function_for_find_segment(pointer_lookup_arr) { - return function(x_coord) { - for (var i = pointer_lookup_arr.length-1 ; i >= 0 ; i--) { - var hash = pointer_lookup_arr[i]; - if (x_coord >= hash.from) - return hash; - } - return pointer_lookup_arr[0]; - }; - } - - // Send endpoint to endpoint+0.99999 to have display - // more closely approximate limiting behavior esp - // at small resolutions. (Since we search on whole numbers, - // inclusive, but flot chart is decimal.) - BlacklightRangeLimit.normalized_selection = function normalized_selection(min, max) { - max += 0.99999; + return BlacklightRangeLimit.normalized_selection(begin_val, end_val); +} - return {xaxis: { 'from':min, 'to':max}} - } +// Add AJAX fetched range facets if needed, and add a chart to em +BlacklightRangeLimit.checkForNeededFacetsToFetch = function checkForNeededFacetsToFetch() { + $(".range_limit .profile .distribution a.load_distribution").each(function() { + var container = $(this).parent('div.distribution'); - // Check if Flot is loaded - BlacklightRangeLimit.domDependenciesMet = function domDependenciesMet() { - return typeof $.plot != "undefined" - } + $(container).load($(this).attr('href'), function(response, status) { + if ($(container).hasClass("chart_js") && status == "success" ) { + BlacklightRangeLimit.turnIntoPlot(container); + } + }); + }); +} - BlacklightRangeLimit.noConflict = function noConflict() { - global.BlacklightRangeLimit = previousBlacklightRangeLimit; - return BlacklightRangeLimit; +BlacklightRangeLimit.function_for_find_segment = function function_for_find_segment(pointer_lookup_arr) { + return function(x_coord) { + for (var i = pointer_lookup_arr.length-1 ; i >= 0 ; i--) { + var hash = pointer_lookup_arr[i]; + if (x_coord >= hash.from) + return hash; + } + return pointer_lookup_arr[0]; }; +} + +// Send endpoint to endpoint+0.99999 to have display +// more closely approximate limiting behavior esp +// at small resolutions. (Since we search on whole numbers, +// inclusive, but flot chart is decimal.) +BlacklightRangeLimit.normalized_selection = function normalized_selection(min, max) { + max += 0.99999; + + return {xaxis: { 'from':min, 'to':max}} +} - global.BlacklightRangeLimit = BlacklightRangeLimit; -}(this); +// Check if Flot is loaded +BlacklightRangeLimit.domDependenciesMet = function domDependenciesMet() { + return typeof $.plot != "undefined" +} \ No newline at end of file