|
| 1 | +{% load i18n rangefilter_compat %} |
| 2 | +<h3>{{ title }}</h3> |
| 3 | +<link rel="stylesheet" type="text/css" href="{% static 'admin/css/widgets.css' %}"> |
| 4 | +<style nonce="{{ spec.request.csp_nonce }}"> |
| 5 | + {% default_css_vars_if_needed %} |
| 6 | + .admindatefilter .button, .admindatefilter input[type=submit], .admindatefilter input[type=button], .admindatefilter .submit-row input, .admindatefilter a.button, |
| 7 | + .admindatefilter .button, .admindatefilter input[type=reset] { |
| 8 | + background: var(--button-bg); |
| 9 | + padding: 4px 5px; |
| 10 | + border: none; |
| 11 | + border-radius: 4px; |
| 12 | + color: var(--button-fg); |
| 13 | + cursor: pointer; |
| 14 | + } |
| 15 | + .admindatefilter { |
| 16 | + padding-left: 15px; |
| 17 | + padding-bottom: 10px; |
| 18 | + border-bottom: 1px solid var(--border-color); |
| 19 | + } |
| 20 | + .admindatefilter p { |
| 21 | + padding-left: 0px; |
| 22 | + line-height: 0; |
| 23 | + } |
| 24 | + .admindatefilter p.datetime { |
| 25 | + line-height: 0; |
| 26 | + } |
| 27 | + .admindatefilter .timezonewarning { |
| 28 | + display: none; |
| 29 | + } |
| 30 | + .admindatefilter .datetimeshortcuts a:first-child { |
| 31 | + margin-right: 4px; |
| 32 | + display: none; |
| 33 | + } |
| 34 | + .calendarbox { |
| 35 | + z-index: 1100; |
| 36 | + } |
| 37 | + .clockbox { |
| 38 | + z-index: 1100; |
| 39 | + margin-left: -8em !important; |
| 40 | + margin-top: 5em !important; |
| 41 | + } |
| 42 | + .admindatefilter .datetimeshortcuts { |
| 43 | + font-size: 0; |
| 44 | + float: right; |
| 45 | + position: absolute; |
| 46 | + padding-top: 4px; |
| 47 | + } |
| 48 | + .admindatefilter a { |
| 49 | + color: #999; |
| 50 | + position: absolute; |
| 51 | + padding-top: 3px; |
| 52 | + padding-left: 4px; |
| 53 | + } |
| 54 | + @media (min-width: 768px) { |
| 55 | + .calendarbox { |
| 56 | + margin-left: -16em !important; |
| 57 | + margin-top: 9em !important; |
| 58 | + } |
| 59 | + } |
| 60 | + @media (max-width: 767px) { |
| 61 | + .calendarbox { |
| 62 | + overflow: visible; |
| 63 | + } |
| 64 | + } |
| 65 | +</style> |
| 66 | + |
| 67 | +{% comment %} |
| 68 | +Force load jsi18n, issues #5 |
| 69 | +https://github.com/django/django/blob/stable/1.10.x/django/contrib/admin/templates/admin/change_list.html#L7 |
| 70 | +{% endcomment %} |
| 71 | +<script type="text/javascript" src="{% url 'admin:jsi18n' %}"></script> |
| 72 | +<script type="text/javascript" nonce="{{ spec.request.csp_nonce }}"> |
| 73 | + django.jQuery('document').ready(function () { |
| 74 | + django.jQuery('.admindatefilter #{{ choices.0.system_name }}-form input[type="submit"]').click(function(event) { |
| 75 | + event.preventDefault(); |
| 76 | + var form = django.jQuery(this).closest('div.admindatefilter').find('form'); |
| 77 | + var query_string = django.jQuery('input#{{ choices.0.system_name }}-query-string').val(); |
| 78 | + var form_data = form.serialize(); |
| 79 | + var amp = query_string === "?" ? "" : "&"; // avoid leading ?& combination |
| 80 | + window.location = window.location.pathname + query_string + amp + form_data; |
| 81 | + }); |
| 82 | + |
| 83 | + django.jQuery('.admindatefilter #{{ choices.0.system_name }}-form input[type="reset"]').click(function() { |
| 84 | + var form = django.jQuery(this).closest('div.admindatefilter').find('form'); |
| 85 | + var query_string = form.find('input#{{ choices.0.system_name }}-query-string').val(); |
| 86 | + window.location = window.location.pathname + query_string; |
| 87 | + }); |
| 88 | + }); |
| 89 | + {% comment %} |
| 90 | + // Code below makes sure that the DateTimeShortcuts.js is loaded exactly once |
| 91 | + // regardless the presence of AdminDateWidget |
| 92 | + // How it worked: |
| 93 | + // - First Django loads the model formset with predefined widgets for different |
| 94 | + // field types. If there's a date based field, then it loads the AdminDateWidget |
| 95 | + // and it's required media to context under {{media.js}} in admin/change_list.html. |
| 96 | + // (Note: it accumulates media in django.forms.widgets.Media object, |
| 97 | + // which prevents duplicates, but the DateRangeFilter is not included yet |
| 98 | + // since it's not model field related. |
| 99 | + // List of predefined widgets is in django.contrib.admin.options.FORMFIELD_FOR_DBFIELD_DEFAULTS) |
| 100 | + // - After that Django starts rendering forms, which have the {{form.media}} |
| 101 | + // tag. Only then the DjangoRangeFilter.get_media is called and rendered, |
| 102 | + // which creates the duplicates. |
| 103 | + // How it works: |
| 104 | + // - first step is the same, if there's a AdminDateWidget to be loaded then |
| 105 | + // nothing changes |
| 106 | + // - DOM gets rendered and if the AdminDateWidget was rendered then |
| 107 | + // the DateTimeShortcuts.js is initiated which sets the window.DateTimeShortcuts. |
| 108 | + // Otherwise, the window.DateTimeShortcuts is undefined. |
| 109 | + // - The lines below check if the DateTimeShortcuts has been set and if not |
| 110 | + // then the DateTimeShortcuts.js and calendar.js is rendered |
| 111 | + // |
| 112 | + // https://github.com/silentsokolov/django-admin-rangefilter/issues/9 |
| 113 | + // |
| 114 | + // Django 2.1 |
| 115 | + // https://github.com/silentsokolov/django-admin-rangefilter/issues/21 |
| 116 | + {% endcomment %} |
| 117 | + function embedScript(url) { |
| 118 | + return new Promise(function pr(resolve, reject) { |
| 119 | + var newScript = document.createElement("script"); |
| 120 | + newScript.type = "text/javascript"; |
| 121 | + newScript.src = url; |
| 122 | + newScript.onload = resolve; |
| 123 | + if ("{{ spec.request.csp_nonce }}" !== "") { |
| 124 | + newScript.setAttribute("nonce", "{{ spec.request.csp_nonce }}"); |
| 125 | + } |
| 126 | + document.head.appendChild(newScript); |
| 127 | + }); |
| 128 | + } |
| 129 | + |
| 130 | + django.jQuery('document').ready(function () { |
| 131 | + if (!('DateTimeShortcuts' in window)) { |
| 132 | + var promiseList = []; |
| 133 | + |
| 134 | + {% for m in spec.form.js %} |
| 135 | + promiseList.push(embedScript("{{ m }}")); |
| 136 | + {% endfor %} |
| 137 | + |
| 138 | + Promise.all(promiseList).then(function() { |
| 139 | + django.jQuery('.datetimeshortcuts').remove(); |
| 140 | + if ('DateTimeShortcuts' in window) { |
| 141 | + window.DateTimeShortcuts.init(); |
| 142 | + } |
| 143 | + }); |
| 144 | + } |
| 145 | + }); |
| 146 | +</script> |
| 147 | +{% block quick-select-choices %}{% endblock %} |
| 148 | +<div class="admindatefilter"> |
| 149 | + <form method="GET" action="." id="{{ choices.0.system_name }}-form"> |
| 150 | + {{ spec.form.as_p }} |
| 151 | + {% for choice in choices %} |
| 152 | + <input type="hidden" id="{{ choice.system_name }}-query-string" value="{{ choice.query_string }}"> |
| 153 | + {% endfor %} |
| 154 | + <div class="controls"> |
| 155 | + <input type="submit" class="button" value="{% trans "Search" %}"> |
| 156 | + <input type="reset" class="button" value="{% trans "Reset" %}"> |
| 157 | + </div> |
| 158 | + </form> |
| 159 | +</div> |
0 commit comments