diff --git a/README.md b/README.md index 0108e49..d060ece 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ And then execute: bundle bundle exec rake decidim_homepage_interactive_map:install:migrations bundle exec rake db:migrate -bundle exec rake decidim_homepage_interactive_map:initialize +bundle exec rake decidim_homepage_interactive_map:webpacker:install ``` ## Contributing diff --git a/app/cells/decidim/homepage_interactive_map/content_blocks/interactive_map/show.erb b/app/cells/decidim/homepage_interactive_map/content_blocks/interactive_map/show.erb index cf57c1c..c82501e 100644 --- a/app/cells/decidim/homepage_interactive_map/content_blocks/interactive_map/show.erb +++ b/app/cells/decidim/homepage_interactive_map/content_blocks/interactive_map/show.erb @@ -3,25 +3,9 @@
<%= interactive_map_for(assemblies_data_for_map(@geolocalized_assemblies)) do %> - - <%= stylesheet_link_tag "decidim/map" %> - <%= stylesheet_link_tag "decidim/homepage_interactive_map/map" %> + " id="view-participatory-process"> + <%= stylesheet_pack_tag "decidim_map" %> + <%= stylesheet_pack_tag "decidim_homepage_interactive_map_css" %> <% end %>
diff --git a/app/packs/entrypoints/decidim_homepage_interactive_map.scss b/app/packs/entrypoints/decidim_homepage_interactive_map.scss new file mode 100644 index 0000000..58edd27 --- /dev/null +++ b/app/packs/entrypoints/decidim_homepage_interactive_map.scss @@ -0,0 +1 @@ +@import "stylesheets/decidim/homepage_interactive_map/map"; diff --git a/app/packs/src/decidim/homepage_interactive_map/interactive_map.js b/app/packs/src/decidim/homepage_interactive_map/interactive_map.js index 09131e0..24bc4f9 100644 --- a/app/packs/src/decidim/homepage_interactive_map/interactive_map.js +++ b/app/packs/src/decidim/homepage_interactive_map/interactive_map.js @@ -23,12 +23,10 @@ L.DivIcon.SVGIcon.DecidimIcon = L.DivIcon.SVGIcon.extend({ }); (() => { - $(document).ready(() => { const here_api_key = $("#interactive_map").data("here-api-key"); const geoJson = $("#interactive_map").data("geojson-data"); - const popupInteractiveTemplateId = "marker-popup-interactive_map"; - $.template(popupInteractiveTemplateId, $(`#${popupInteractiveTemplateId}`).html()); + const $viewParticipatoryProcess = $("#view-participatory-process"); // Used to prevent click event when double click navigating const clickDelay = 500; @@ -115,6 +113,27 @@ L.DivIcon.SVGIcon.DecidimIcon = L.DivIcon.SVGIcon.extend({ return (delta + 2) * 2; } + function tmpl(participatoryProcess, linkTxt) { + return ` +
+

${participatoryProcess.title}

+
+
+
+ ${participatoryProcess.start_date} - ${participatoryProcess.end_date} +
+
+ +
+
+ ` + } + + L.tileLayer.here({ apiKey: here_api_key, scheme: "normal.day.grey" @@ -251,7 +270,8 @@ L.DivIcon.SVGIcon.DecidimIcon = L.DivIcon.SVGIcon.extend({ ); let node = document.createElement("div"); - $.tmpl(popupInteractiveTemplateId, participatory_process).appendTo(node); + $(node).html((tmpl(participatory_process, $viewParticipatoryProcess.val()))); + marker.bindPopup(node, { maxwidth: popupMaxwidth(), minWidth: popupMinwidth(), diff --git a/config/assets.rb b/config/assets.rb index 5a22f12..c4599b2 100644 --- a/config/assets.rb +++ b/config/assets.rb @@ -19,10 +19,11 @@ # SCSS files within the entrypoints, they become available for inclusion using # `stylesheet_pack_tag`. Decidim::Webpacker.register_entrypoints( - decidim_homepage_interactive_map: "#{base_path}/app/packs/entrypoints/decidim_homepage_interactive_map.js" + decidim_homepage_interactive_map: "#{base_path}/app/packs/entrypoints/decidim_homepage_interactive_map.js", + decidim_homepage_interactive_map_css: "#{base_path}/app/packs/entrypoints/decidim_homepage_interactive_map.scss" ) # If you want to import some extra SCSS files in the Decidim main SCSS file # without adding any extra stylesheet inclusion tags, you can use the following # method to register the stylesheet import for the main application. -Decidim::Webpacker.register_stylesheet_import("stylesheets/decidim/homepage_interactive_map/map.scss") +# Decidim::Webpacker.register_stylesheet_import("stylesheets/decidim/homepage_interactive_map/map.scss") diff --git a/decidim-homepage_interactive_map.gemspec b/decidim-homepage_interactive_map.gemspec index 3029ae1..6432d84 100644 --- a/decidim-homepage_interactive_map.gemspec +++ b/decidim-homepage_interactive_map.gemspec @@ -16,7 +16,7 @@ Gem::Specification.new do |s| s.summary = "A decidim homepage_interactive_map module" s.description = "Displays an interactive map on homepage." - s.files = Dir["{app,config,lib,vendor}/**/*", "LICENSE-AGPLv3.txt", "Rakefile", "README.md"] + s.files = Dir["{app,config,lib,vendor}/**/*", "LICENSE-AGPLv3.txt", "Rakefile", "README.md", "package.json"] s.add_dependency "decidim-admin", Decidim::HomepageInteractiveMap::COMPAT_DECIDIM_VERSION s.add_dependency "decidim-core", Decidim::HomepageInteractiveMap::COMPAT_DECIDIM_VERSION diff --git a/lib/tasks/map.rake b/lib/tasks/map.rake deleted file mode 100644 index a562e9d..0000000 --- a/lib/tasks/map.rake +++ /dev/null @@ -1,10 +0,0 @@ -# frozen_string_literal: true - -namespace :decidim_homepage_interactive_map do - desc "Install dependencies for decidim interactive maps" - task initialize: :environment do - puts "installing dependencies" - system! "npm i --save proj4 proj4leaflet leaflet-tilelayer-here leaflet leaflet-svgicon leaflet.markercluster" - system! "./bin/webpack" - end -end diff --git a/lib/tasks/webpacker_tasks.rake b/lib/tasks/webpacker_tasks.rake new file mode 100644 index 0000000..32d07e2 --- /dev/null +++ b/lib/tasks/webpacker_tasks.rake @@ -0,0 +1,61 @@ +# frozen_string_literal: true + +require "decidim/gem_manager" + +# Copied from : https://github.com/Platoniq/decidim-module-decidim_awesome/blob/main/lib/tasks/decidim_awesome_webpacker_tasks.rake + +namespace :decidim_homepage_interactive_map do + namespace :webpacker do + desc "Installs module's webpacker files in Rails instance application" + task install: :environment do + raise "Decidim gem is not installed" if decidim_path.nil? + + install_npm + end + + desc "Adds JS dependencies in package.json" + task upgrade: :environment do + raise "Decidim gem is not installed" if decidim_path.nil? + + install_npm + end + + def install_npm + npm_dependencies.each do |type, packages| + puts "install NPM packages. You can also do this manually with this command:" + puts "npm i --save-#{type} #{packages.join(" ")}" + system! "npm i --save-#{type} #{packages.join(" ")}" + end + end + + def npm_dependencies + @npm_dependencies ||= begin + package_json = JSON.parse(File.read(module_path.join("package.json"))) + + { + prod: package_json["dependencies"].map { |package, version| "#{package}@#{version}" } + }.freeze + end + end + + def module_path + @module_path ||= Pathname.new(module_gemspec.full_gem_path) if Gem.loaded_specs.has_key?(gem_name) + end + + def module_gemspec + @module_gemspec ||= Gem.loaded_specs[gem_name] + end + + def rails_app_path + @rails_app_path ||= Rails.root + end + + def system!(command) + system("cd #{rails_app_path} && #{command}") || abort("\n== Command #{command} failed ==") + end + + def gem_name + "decidim-homepage_interactive_map" + end + end +end