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 @use_datetimepicker decorator to use Tempus Dominus on Bootstrap 5 pages #34009

Merged
merged 4 commits into from
Jan 31, 2024
Merged
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
22 changes: 22 additions & 0 deletions corehq/apps/hqwebapp/decorators.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from collections import defaultdict
from functools import wraps

from django.urls import get_resolver

Check failure on line 4 in corehq/apps/hqwebapp/decorators.py

View workflow job for this annotation

GitHub Actions / Flake8

corehq/apps/hqwebapp/decorators.py#L4

'django.urls.get_resolver' imported but unused (F401)

from corehq.apps.hqwebapp.utils.bootstrap import set_bootstrap_version5

Expand Down Expand Up @@ -236,6 +236,28 @@
return _inner


def use_datetimepicker(view_func):
"""Use this decorator on the dispatch method of a TemplateView subclass
to include CSS for Tempus Dominus (Date and/or Time picking widget).
NOTE: Only available for Bootstrap 5 pages!

Example:
@use_datetimepicker
def dispatch(self, request, *args, **kwargs):
return super().dispatch(request, *args, **kwargs)

Or alternatively:
@method_decorator(use_datetimepicker, name='dispatch')
class MyViewClass(MyViewSubclass):
...
"""
@wraps(view_func)
def _inner(request, *args, **kwargs):
request.use_datetimepicker = True
return view_func(request, *args, **kwargs)
return _inner


def waf_allow(kind, hard_code_pattern=None):
"""
Using this decorator simply registers a function for later use
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ requirejs.config({
"datatables.bootstrap": "datatables.net-bs5/js/dataTables.bootstrap5.min",
"datatables.fixedColumns": "datatables.net-fixedcolumns/js/dataTables.fixedColumns.min",
"datatables.fixedColumns.bootstrap": "datatables.net-fixedcolumns/js/dataTables.fixedColumns.min",
"datepicker": "@eonasdan/tempus-dominus/dist/js/jQuery-provider.min", // import this if you need jquery plugin of tempus-dominus
"datetimepicker": "@eonasdan/tempus-dominus/dist/js/jQuery-provider.min", // import this if you need jquery plugin of tempus-dominus
"es6": "requirejs-babel7/es6",
"jquery": "jquery/dist/jquery.min",
"knockout": "knockout/build/output/knockout-latest.debug",
Expand All @@ -25,7 +25,7 @@ requirejs.config({
"ace-builds/src-min-noconflict/ace": { exports: "ace" },
"datatables.bootstrap": { deps: ['datatables'] },
"datatables.fixedColumns.bootstrap": { deps: ['datatables.fixedColumns'] },
"datepicker": {
"datetimepicker": {
deps: ['popper', 'tempus-dominus'],
},
"d3/d3.min": {
Expand Down
17 changes: 17 additions & 0 deletions corehq/apps/hqwebapp/templates/hqwebapp/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,15 @@
{% endif %}
{% endif %}

{% if request.use_datetimepicker and use_bootstrap5 %}
{% compress css %}
<link type="text/css"
rel="stylesheet"
media="screen"
href="{% static "@eonasdan/tempus-dominus/dist/css/tempus-dominus.min.css" %}" />
{% endcompress %}
{% endif %}

{% if request.use_multiselect %}
{% compress css %}
<link type="text/css"
Expand Down Expand Up @@ -400,6 +409,14 @@
{% endcompress %}
{% endif %}

{% if request.use_datetimepicker and not requirejs_main and use_bootstrap5 %}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not requirejs_main? Is it because these are included explicitly in requirejs pages?

Why not have that condition on the CSS include above?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the javascript in requirejs pages is included by specifying datetimepicker in the list of dependencies. There is no way to import css through requirejs dependencies, as requirejs is only responsible for javascript bundling. This is how it's done for all the libraries that have a css component

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense.

There is no way to import css through requirejs dependencies...

Vellum does it (with less too). I forgot that we did not have that capability in HQ.

{% compress js %}
<script src="{% static '@popperjs/core/dist/umd/popper.min.js' %}"></script>
<script src="{% static '@eonasdan/tempus-dominus/dist/js/tempus-dominus.min.js' %}"></script>
<script src="{% static '@eonasdan/tempus-dominus/dist/js/jQuery-provider.min.js' %}"></script>
{% endcompress %}
{% endif %}

{% if request.use_timepicker and not requirejs_main and not use_bootstrap5 %}
{% compress js %}
<script src="{% static 'bootstrap-timepicker/js/bootstrap-timepicker.js' %}"></script>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
+ "datatables.bootstrap": "datatables.net-bs5/js/dataTables.bootstrap5.min",
+ "datatables.fixedColumns": "datatables.net-fixedcolumns/js/dataTables.fixedColumns.min",
+ "datatables.fixedColumns.bootstrap": "datatables.net-fixedcolumns/js/dataTables.fixedColumns.min",
+ "datepicker": "@eonasdan/tempus-dominus/dist/js/jQuery-provider.min", // import this if you need jquery plugin of tempus-dominus
+ "datetimepicker": "@eonasdan/tempus-dominus/dist/js/jQuery-provider.min", // import this if you need jquery plugin of tempus-dominus
+ "es6": "requirejs-babel7/es6",
"jquery": "jquery/dist/jquery.min",
"knockout": "knockout/build/output/knockout-latest.debug",
Expand All @@ -31,7 +31,7 @@
- "bootstrap": { deps: ['jquery'] },
"datatables.bootstrap": { deps: ['datatables'] },
+ "datatables.fixedColumns.bootstrap": { deps: ['datatables.fixedColumns'] },
+ "datepicker": {
+ "datetimepicker": {
+ deps: ['popper', 'tempus-dominus'],
+ },
"d3/d3.min": {
Expand Down
Loading