Skip to content

Commit

Permalink
add option to enable/disable js modules reinit
Browse files Browse the repository at this point in the history
  • Loading branch information
mutantsan committed Jun 17, 2024
1 parent 141b0ac commit 58b5451
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 9 deletions.
17 changes: 11 additions & 6 deletions ckanext/charts/assets/js/charts-global.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,22 @@ ckan.module("charts-global", function ($, _) {
"use strict";

return {
options: {
reinitJs: false
},
initialize: function () {
$.proxyAll(this, /_/);

// initialize CKAN modules for HTMX loaded pages
htmx.on("htmx:afterSettle", function (event) {
var elements = event.target.querySelectorAll("[data-module]");
if (this.options.reinitJs) {
htmx.on("htmx:afterSettle", function (event) {
var elements = event.target.querySelectorAll("[data-module]");

for (let node of elements) {
ckan.module.initializeElement(node);
}
});
for (let node of elements) {
ckan.module.initializeElement(node);
}
});
}
}
};
});
6 changes: 6 additions & 0 deletions ckanext/charts/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
CONF_ENABLE_CACHE = "ckanext.charts.enable_cache"
CONF_SERVERSIDE_RENDER = "ckanext.charts.use_serverside_rendering"
CONF_ENABLE_HTMX = "ckanext.charts.include_htmx_asset"
CONF_REINIT_JS = "ckanext.charts.reinit_ckan_js_modules"


def get_cache_strategy() -> str:
Expand Down Expand Up @@ -36,3 +37,8 @@ def use_serverside_rendering() -> bool:
def include_htmx_asset() -> bool:
"""Include HTMX library asset. Disable it, if no other library do it."""
return tk.config[CONF_ENABLE_HTMX]


def reinit_ckan_js_modules() -> bool:
"""Reinitialize CKAN JS modules."""
return tk.config[CONF_REINIT_JS]
6 changes: 6 additions & 0 deletions ckanext/charts/config_declaration.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,9 @@ groups:
default: true
type: bool
validators: ignore_empty boolean_validator

- key: ckanext.charts.reinit_ckan_js_modules
description: Reinit CKAN JS modules
default: false
type: bool
validators: ignore_empty boolean_validator
7 changes: 6 additions & 1 deletion ckanext/charts/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,16 @@ def get_available_chart_engines_options():
return [{"value": engine, "text": engine} for engine in get_chart_engines()]


def charts_include_htmx_asset():
def charts_include_htmx_asset() -> bool:
"""Include HTMX asset if enabled."""
return config.include_htmx_asset()


def charts_reinit_ckan_js_modules() -> bool:
"""Reinitialize CKAN JS modules."""
return config.reinit_ckan_js_modules()


def charts_get_resource_columns(resource_id: str) -> str:
"""Get the columns of the given resource."""
fetcher = DatastoreDataFetcher(resource_id)
Expand Down
2 changes: 1 addition & 1 deletion ckanext/charts/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ def info(self) -> dict[str, Any]:
"name": "charts_builder_view",
"title": tk._("Chart Builder"),
"schema": {},
"icon": "chart-line",
"icon": "chart-area",
"iframed": False,
"filterable": False,
"preview_enabled": False,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
{% asset "charts/tom-select-js" %}
{% asset "charts/tom-select-css" %}

<div class="charts-view--form" data-module="charts-global">
<div class="charts-view--form" data-module="charts-global" data-module-reinit-js="{{ h.charts_reinit_ckan_js_modules() }}">
{% snippet "charts/snippets/charts_form_fields.html", resource_id=resource_id, data=settings,
builder=form_builder, errors=errors, exclude_tabs=exclude_tabs or false,
user_chart_builder=user_chart_builder or false %}
Expand Down

0 comments on commit 58b5451

Please sign in to comment.