Skip to content

Commit

Permalink
Add assets via importmap
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoyne committed Oct 11, 2022
1 parent b9cd868 commit 580a3fc
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ Blacklight.onLoad(function() {
});

// When loaded in a modal
$(Blacklight.modal.modalSelector).on('shown.bs.modal', function() {
$(this).find(".range_limit .profile .distribution.chart_js ul").each(function() {
BlacklightRangeLimit.turnIntoPlot($(this).parent());
});
// $(Blacklight.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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ Blacklight.onLoad(function() {
BlacklightRangeLimit.buildSlider(this);
});

$(Blacklight.modal.modalSelector).on('shown.bs.modal', function() {
$(this).find(".range_limit .profile .range.slider_js").each(function() {
BlacklightRangeLimit.buildSlider(this);
});
});
// $(Blacklight.modal.modalSelector).on('shown.bs.modal', function() {
// $(this).find(".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) {
Expand All @@ -30,7 +30,7 @@ Blacklight.onLoad(function() {
// set as sniffed from HTML. Pass in a DOM element for a div.range
// Will return NaN as min or max in case of error or other weirdness.
BlacklightRangeLimit.min_max = function min_max(range_element) {
var current_limit = $(range_element).closest(".limit_content.range_limit").find(".current")
const current_limit = $(range_element).closest(".limit_content.range_limit").find(".current")
let min, max
min = max = BlacklightRangeLimit.parseNum(current_limit.find(".single").data('blrlSingle'))
if ( isNaN(min)) {
Expand Down
6 changes: 6 additions & 0 deletions lib/blacklight_range_limit/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@

module BlacklightRangeLimit
class Engine < Rails::Engine
initializer "blacklight_range_limit.assets", before: 'assets' do |app|
if defined? Sprockets
app.config.assets.precompile << 'blacklight_range_limit.js'
end
end

config.action_dispatch.rescue_responses.merge!(
"BlacklightRangeLimit::InvalidRange" => :not_acceptable
)
Expand Down
41 changes: 33 additions & 8 deletions lib/generators/blacklight_range_limit/assets_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module BlacklightRangeLimit
class AssetsGenerator < Rails::Generators::Base
source_root File.join(BlacklightRangeLimit::Engine.root, 'app', 'assets')

def assets
def stylesheet
application_css = Dir["app/assets/stylesheets/application{.css,.scss,.css.scss}"].first

if application_css
Expand All @@ -31,19 +31,44 @@ def assets
else
say_status "warning", "Can not find application.css, did not insert our require", :red
end
end

append_to_file "app/assets/javascripts/application.js" do
%q{
def javascript
if using_importmap?
pin_javascript_dependencies
else
install_javascript_dependencies
end
end

// For blacklight_range_limit built-in JS, if you don't want it you don't need
// this:
//= require 'blacklight_range_limit'
private

}
end
def root
@root ||= Pathname(destination_root)
end

def using_importmap?
@using_importmap ||= root.join('config/importmap.rb').exist?
end

def install_javascript_dependencies
append_to_file "app/assets/javascripts/application.js", <<~RUBY
// For blacklight_range_limit built-in JS, if you don't want it you don't need
// this:
//= require 'blacklight_range_limit'
RUBY
end

def pin_javascript_dependencies
say 'blacklight_range_limit Importmap asset generation'

append_to_file 'config/importmap.rb', <<~RUBY
pin "blacklight_range_limit", to: "blacklight_range_limit.js"
RUBY

append_to_file 'app/javascript/application.js' do
'import("blacklight_range_limit")'
end
end
end
end

0 comments on commit 580a3fc

Please sign in to comment.