Skip to content

Commit

Permalink
Export initializer for module context
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoyne committed Mar 3, 2023
1 parent 827cf44 commit 41818eb
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 81 deletions.
17 changes: 17 additions & 0 deletions app/assets/javascripts/blacklight_range_limit.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,20 @@
//= require 'blacklight_range_limit/range_limit_plotting'
//= require 'blacklight_range_limit/range_limit_slider'
//= require 'blacklight_range_limit/range_limit_distro_facets'

BlacklightRangeLimit.initialize = function() {
// Support for Blacklight 7 and 8:
const modalSelector = Blacklight.modal?.modalSelector || Blacklight.Modal.modalSelector

RangeLimitDistroFacet.initialize(modalSelector)
RangeLimitSlider.initialize(modalSelector)
}

// Blacklight will only be defined if you aren't using this in a module context
if (Blacklight)
Blacklight.onLoad(() => BlacklightRangeLimit.initialize())

// In a module context you can do:
// import BlacklightRangeLimit from 'blacklight_range_limit'
// Blacklight.onLoad(() => BlacklightRangeLimit.initialize())
export default BlacklightRangeLimit
Original file line number Diff line number Diff line change
Expand Up @@ -6,72 +6,70 @@
* events.
*/

Blacklight.onLoad(function() {

// Facets already on the page? Turn em into a chart.
$(".range_limit .profile .distribution.chart_js ul").each(function() {
BlacklightRangeLimit.turnIntoPlot($(this).parent());
});
const RangeLimitDistroFacet = {
initialize: function(modalSelector) {
// Facets already on the page? Turn em into a chart.
$(".range_limit .profile .distribution.chart_js ul").each(function() {
BlacklightRangeLimit.turnIntoPlot($(this).parent());
});

BlacklightRangeLimit.checkForNeededFacetsToFetch();
BlacklightRangeLimit.checkForNeededFacetsToFetch();

// Listen for twitter bootstrap collapsible open events, to render flot
// in previously hidden divs on open, if needed.
$("body").on("show.bs.collapse", function(event) {
// Was the target a .facet-content including a .chart-js?
var container = $(event.target).filter(".facet-content").find(".chart_js");
// Listen for twitter bootstrap collapsible open events, to render flot
// in previously hidden divs on open, if needed.
$("body").on("show.bs.collapse", function(event) {
// Was the target a .facet-content including a .chart-js?
var container = $(event.target).filter(".facet-content").find(".chart_js");

// only if it doesn't already have a canvas, it isn't already drawn
if (container && container.find("canvas").length == 0) {
// be willing to wait up to 1100ms for container to
// have width -- right away on show.bs is too soon, but
// shown.bs is later than we want, we want to start rendering
// while animation is still in progress.
BlacklightRangeLimit.turnIntoPlot(container, 1100);
}
});
// only if it doesn't already have a canvas, it isn't already drawn
if (container && container.find("canvas").length == 0) {
// be willing to wait up to 1100ms for container to
// have width -- right away on show.bs is too soon, but
// shown.bs is later than we want, we want to start rendering
// while animation is still in progress.
BlacklightRangeLimit.turnIntoPlot(container, 1100);
}
});

// Support for Blacklight 7 and 8:
const modalSelector = Blacklight.modal?.modalSelector || Blacklight.Modal.modalSelector
// When loaded in a modal
$(modalSelector).on('shown.bs.modal', function() {
$(this).find(".range_limit .profile .distribution.chart_js ul").each(function() {
BlacklightRangeLimit.turnIntoPlot($(this).parent());
});

// When loaded in a modal
$(modalSelector).on('shown.bs.modal', function() {
$(this).find(".range_limit .profile .distribution.chart_js ul").each(function() {
BlacklightRangeLimit.turnIntoPlot($(this).parent());
// Case when there is no currently selected range
BlacklightRangeLimit.checkForNeededFacetsToFetch();
});

// Case when there is no currently selected range
BlacklightRangeLimit.checkForNeededFacetsToFetch();
});

$("body").on("shown.bs.collapse", function(event) {
var container = $(event.target).filter(".facet-content").find(".chart_js");
BlacklightRangeLimit.redrawPlot(container);
});
$("body").on("shown.bs.collapse", function(event) {
var container = $(event.target).filter(".facet-content").find(".chart_js");
BlacklightRangeLimit.redrawPlot(container);
});

// debouce borrowed from underscore
// Returns a function, that, as long as it continues to be invoked, will not
// be triggered. The function will be called after it stops being called for
// N milliseconds. If `immediate` is passed, trigger the function on the
// leading edge, instead of the trailing.
const debounce = function(func, wait, immediate) {
var timeout;
return function() {
var context = this, args = arguments;
var later = function() {
timeout = null;
if (!immediate) func.apply(context, args);
// debouce borrowed from underscore
// Returns a function, that, as long as it continues to be invoked, will not
// be triggered. The function will be called after it stops being called for
// N milliseconds. If `immediate` is passed, trigger the function on the
// leading edge, instead of the trailing.
const debounce = function(func, wait, immediate) {
var timeout;
return function() {
var context = this, args = arguments;
var later = function() {
timeout = null;
if (!immediate) func.apply(context, args);
};
var callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) func.apply(context, args);
};
var callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) func.apply(context, args);
};
};

$(window).on("resize", debounce(function() {
$(".chart_js").each(function(i, container) {
BlacklightRangeLimit.redrawPlot($(container));
});
}, 350));
});
$(window).on("resize", debounce(function() {
$(".chart_js").each(function(i, container) {
BlacklightRangeLimit.redrawPlot($(container));
});
}, 350));
}
}
44 changes: 21 additions & 23 deletions app/assets/javascripts/blacklight_range_limit/range_limit_slider.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,30 @@
// for Blacklight.onLoad:

Blacklight.onLoad(function() {

$(".range_limit .profile .range.slider_js").each(function() {
BlacklightRangeLimit.buildSlider(this);
});

// Support for Blacklight 7 and 8:
const modalSelector = Blacklight.modal?.modalSelector || Blacklight.Modal.modalSelector

$(modalSelector).on('shown.bs.modal', function() {
$(this).find(".range_limit .profile .range.slider_js").each(function() {
const RangeLimitSlider = {
initialize: function(modalSelector) {
$(".range_limit .profile .range.slider_js").each(function() {
BlacklightRangeLimit.buildSlider(this);
});
});

// catch event for redrawing chart, to redraw slider to match width
$("body").on("plotDrawn.blacklight.rangeLimit", function(event) {
var area = $(event.target).closest(".limit_content.range_limit");
var plot = area.find(".chart_js").data("plot");
var slider_el = area.find(".slider");
$(modalSelector).on('shown.bs.modal', function() {
$(this).find(".range_limit .profile .range.slider_js").each(function() {
BlacklightRangeLimit.buildSlider(this);
});
});

if (plot && slider_el) {
slider_el.width(plot.width());
slider_el.css("display", "block")
}
});
});
// catch event for redrawing chart, to redraw slider to match width
$("body").on("plotDrawn.blacklight.rangeLimit", function(event) {
var area = $(event.target).closest(".limit_content.range_limit");
var plot = area.find(".chart_js").data("plot");
var slider_el = area.find(".slider");

if (plot && slider_el) {
slider_el.width(plot.width());
slider_el.css("display", "block")
}
});
}
}

// returns two element array min/max as numbers. If there is a limit applied,
// it's boundaries are are limits. Otherwise, min/max in current result
Expand Down

0 comments on commit 41818eb

Please sign in to comment.