Skip to content

Commit

Permalink
Ensure only one copy by using a const declaration
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jcoyne committed Oct 11, 2022
1 parent 3d19fcc commit b9cd868
Showing 1 changed file with 59 additions and 70 deletions.
129 changes: 59 additions & 70 deletions app/assets/javascripts/blacklight_range_limit/range_limit_shared.js
Original file line number Diff line number Diff line change
@@ -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"
}

0 comments on commit b9cd868

Please sign in to comment.