Extract js and css from select widgets to editable class attributes

This commit is contained in:
Antoine Bartuccio 2024-10-20 13:40:59 +02:00
parent 8bbebfdb13
commit bb3f277ba5
2 changed files with 14 additions and 11 deletions

View File

@ -1,5 +1,7 @@
<script-once src="{{ statics.js }}" defer></script-once> {% for js in statics.js %}
{% for css in statics.csss %} <script-once src="{{ js }}" defer></script-once>
{% endfor %}
{% for css in statics.css %}
<link-once rel="stylesheet" type="text/css" href="{{ css }}" defer></link-once> <link-once rel="stylesheet" type="text/css" href="{{ css }}" defer></link-once>
{% endfor %} {% endfor %}

View File

@ -14,6 +14,14 @@ class AutoCompleteSelectMixin:
schema: ModelSchema | None = None schema: ModelSchema | None = None
pk = "id" pk = "id"
js = [
"webpack/core/components/ajax-select-index.ts",
]
css = [
"webpack/core/components/ajax-select-index.css",
"core/components/ajax-select.scss",
]
def __init__(self, attrs=None, choices=()): def __init__(self, attrs=None, choices=()):
if self.is_ajax: if self.is_ajax:
choices = () # Avoid computing anything when in ajax mode choices = () # Avoid computing anything when in ajax mode
@ -33,15 +41,8 @@ class AutoCompleteSelectMixin:
context = super().get_context(name, value, attrs) context = super().get_context(name, value, attrs)
context["component"] = self.component_name context["component"] = self.component_name
context["statics"] = { context["statics"] = {
"js": staticfiles_storage.url( "js": [staticfiles_storage.url(file) for file in self.js],
"webpack/core/components/ajax-select-index.ts" "css": [staticfiles_storage.url(file) for file in self.css],
),
"csss": [
staticfiles_storage.url(
"webpack/core/components/ajax-select-index.css"
),
staticfiles_storage.url("core/components/ajax-select.scss"),
],
} }
if self.is_ajax: if self.is_ajax:
context["selected"] = [ context["selected"] = [