Skip to content

Commit

Permalink
feature: use Django media to load in widget dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
tymees committed Oct 20, 2023
1 parent e03f246 commit 1040fc4
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 19 deletions.
10 changes: 6 additions & 4 deletions src/cdh/core/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,12 @@ def to_python(self, value):
class TinyMCEWidget(forms.Widget):
"""A TinyMCE widget for HTML editting"""
template_name = "cdh.core/forms/widgets/tinymce.html"
class Media:
js = [
'cdh.core/js/tinymce/tinymce.min.js',
'cdh.core/js/tinymce/tinymce-jquery.min.js',
'cdh.core/js/tinymce/shim.js'
]

def __init__(
self,
Expand Down Expand Up @@ -387,10 +393,6 @@ def __init__(
self.plugins = plugins
self.toolbar = toolbar

add_js_file('cdh.core/js/tinymce/tinymce.min.js')
add_js_file('cdh.core/js/tinymce/tinymce-jquery.min.js')
add_js_file('cdh.core/js/tinymce/shim.js')

def get_context(self, *args, **kwargs):
context = super().get_context(*args, **kwargs)

Expand Down
4 changes: 2 additions & 2 deletions src/cdh/core/mail/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class EmailContentEditWidget(TinyMCEWidget):
Will add a 'preview email' button to the editor.
"""
class Media:
js = ['cdh.core/js/tinymce-preview-mail-plugin.js']

def __init__(
self,
Expand Down Expand Up @@ -40,8 +42,6 @@ def __init__(
self.toolbar += " | preview-mail"
self.plugins.append('preview-mail')

add_js_file('cdh.core/js/tinymce-preview-mail-plugin.js')

def get_context(self, *args, **kwargs):
context = super().get_context(*args, **kwargs)

Expand Down
5 changes: 0 additions & 5 deletions src/cdh/files/apps.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from django.apps import AppConfig

from cdh.core.file_loading import add_css_file, add_js_file


class FilesConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
Expand All @@ -10,6 +8,3 @@ class FilesConfig(AppConfig):
def ready(self):
import cdh.files.checks # noQA, the import has side effects, linter!
import cdh.files.signals # noQA, same story

add_js_file('cdh.files/widgets.js')
add_css_file('cdh.files/widgets.css')
12 changes: 12 additions & 0 deletions src/cdh/files/forms/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@


class SimpleFileInput(FileInput):
class Media:
js = ['cdh.files/widgets.js']
css = {
'screen': ['cdh.files/widgets.css']
}

clear_checkbox_label = _('Clear')
initial_text = _('Currently')
input_text = _('Change')
Expand Down Expand Up @@ -59,6 +65,12 @@ def value_omitted_from_data(self, data, files, name):


class TrackedFileInput(SimpleFileInput):
class Media:
js = ['cdh.files/widgets.js']
css = {
'screen': ['cdh.files/widgets.css']
}

template_name = 'cdh.files/widgets/tracked_file.html'

# Strings used in the template
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,13 @@


class SingleUserWidget(widgets.Input):
class Media:
js = [
'cdh.integration_platform/digital_identity_api/single_user_widget.js'
]
input_type = 'hidden'
template_name = "cdh.integration_platform/digital_identity_api/single_user_widget.html"

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

add_js_file(
'cdh.integration_platform/digital_identity_api/single_user_widget'
'.js'
)

@property
def is_hidden(self):
return False
Expand Down

0 comments on commit 1040fc4

Please sign in to comment.