Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add assets via importmap #211

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/assets/javascripts/blacklight_range_limit.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
//= require 'flot/jquery.flot.selection.js'
//= require 'bootstrap-slider'

import Blacklight from "blacklight"
window.Blacklight = Blacklight
// Ensure that range_limit_shared is loaded first
//= require 'blacklight_range_limit/range_limit_shared'
//= require 'blacklight_range_limit/range_limit_plotting'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ Blacklight.onLoad(function() {
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());
});
// $(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 @@ -9,11 +9,11 @@ Blacklight.onLoad(function() {
// 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() {
BlacklightRangeLimit.buildSlider(this);
});
});
// $(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 @@ -33,7 +33,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
43 changes: 35 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,46 @@ 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 "jquery", to: "https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.js"
pin "blacklight_range_limit", to: "blacklight_range_limit.js"
RUBY

append_to_file 'app/javascript/application.js', <<~JS
import 'jquery'
import "blacklight_range_limit"
JS
end
end
end
Loading