Update litepicker and add ranges plugin

Fix litepicker colors

Fix litepicker event

Add custom ranges to litepicker

Fix code style

Remove some date ranges

Fix date ranges

Replace yesterday date range with last 3 days
This commit is contained in:
Paweł Jastrzębski 2021-04-21 11:47:50 +02:00 committed by R. Miles McCain
parent a76e0feaf3
commit 0e37e7f042
6 changed files with 79 additions and 24 deletions

6
package-lock.json generated
View File

@ -37,9 +37,9 @@
"integrity": "sha512-6v0WK8FHkVYbNQZ7L9O5tP8280pgTBR9ydxqYwssMuUH6SZO70ZFK/NQ1Ob8nNmOOzpUJAzT0WE73ty96z1tAQ==" "integrity": "sha512-6v0WK8FHkVYbNQZ7L9O5tP8280pgTBR9ydxqYwssMuUH6SZO70ZFK/NQ1Ob8nNmOOzpUJAzT0WE73ty96z1tAQ=="
}, },
"litepicker": { "litepicker": {
"version": "1.5.7", "version": "2.0.11",
"resolved": "https://registry.npmjs.org/litepicker/-/litepicker-1.5.7.tgz", "resolved": "https://registry.npmjs.org/litepicker/-/litepicker-2.0.11.tgz",
"integrity": "sha512-4L2ZcF8iqCE4A/qGWS3PbdFplZR1g751x5SsZ87zCRZ4LQN1Fgezarnvqi0eHk/kDWK7Qx0HZ9Y4bNznJMF1xA==" "integrity": "sha512-7MECMp2EDGIYDIz9QT24t9hWpgBD9JD57ZdDrbffNMGfbw0JVhBhvlYsyaIUuYhywtLvgmI5lfulM7XF2HLEkg=="
}, },
"svg.draggable.js": { "svg.draggable.js": {
"version": "2.2.2", "version": "2.2.2",

View File

@ -22,6 +22,6 @@
"apexcharts": "^3.24.0", "apexcharts": "^3.24.0",
"flag-icon-css": "^3.5.0", "flag-icon-css": "^3.5.0",
"inter-ui": "^3.15.0", "inter-ui": "^3.15.0",
"litepicker": "^1.5.7" "litepicker": "^2.0.11"
} }
} }

View File

@ -1,12 +1,11 @@
from datetime import datetime, time from datetime import datetime, time
from urllib.parse import urlparse
from django.utils import timezone from django.utils import timezone
class DateRangeMixin: class DateRangeMixin:
def get_start_date(self): def get_start_date(self):
if self.request.GET.get("startDate") != None: if self.request.GET.get("startDate") is not None:
found_time = timezone.datetime.strptime( found_time = timezone.datetime.strptime(
self.request.GET.get("startDate"), "%Y-%m-%d" self.request.GET.get("startDate"), "%Y-%m-%d"
) )
@ -15,7 +14,7 @@ class DateRangeMixin:
return timezone.now() - timezone.timedelta(days=30) return timezone.now() - timezone.timedelta(days=30)
def get_end_date(self): def get_end_date(self):
if self.request.GET.get("endDate") != None: if self.request.GET.get("endDate") is not None:
found_time = timezone.datetime.strptime( found_time = timezone.datetime.strptime(
self.request.GET.get("endDate"), "%Y-%m-%d" self.request.GET.get("endDate"), "%Y-%m-%d"
) )
@ -23,8 +22,45 @@ class DateRangeMixin:
else: else:
return timezone.now() return timezone.now()
def get_date_ranges(self):
now = timezone.now()
return [
{
'name': 'Last 3 days',
'start': now - timezone.timedelta(days=2),
'end': now,
},
{
'name': 'Last 30 days',
'start': now - timezone.timedelta(days=29),
'end': now,
},
{
'name': 'Last 90 days',
'start': now - timezone.timedelta(days=89),
'end': now,
},
{
'name': 'This month',
'start': now.replace(day=1),
'end': now,
},
{
'name': 'Last month',
'start': now.replace(day=1, month=now.month - 1),
'end': now.replace(day=1, month=now.month) - timezone.timedelta(days=1),
},
{
'name': 'This year',
'start': now.replace(day=1, month=1),
'end': now,
},
]
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
data = super().get_context_data(**kwargs) data = super().get_context_data(**kwargs)
data["start_date"] = self.get_start_date() data["start_date"] = self.get_start_date()
data["end_date"] = self.get_end_date() data["end_date"] = self.get_end_date()
data["date_ranges"] = self.get_date_ranges()
return data return data

View File

@ -10,7 +10,10 @@
{% include 'a17t/includes/head.html' %} {% include 'a17t/includes/head.html' %}
<link rel="icon" type="image/png" href="{% static 'dashboard/images/icon.png' %}"> <link rel="icon" type="image/png" href="{% static 'dashboard/images/icon.png' %}">
<script src="{% static 'apexcharts/dist/apexcharts.min.js'%}"></script> <script src="{% static 'apexcharts/dist/apexcharts.min.js'%}"></script>
<script src="{% static 'litepicker/dist/js/main.js' %}"></script> <script src="{% static 'litepicker/dist/nocss/litepicker.js' %}"></script>
<script src="{% static 'litepicker/dist/plugins/ranges.js' %}"></script>
<link rel="stylesheet" href="{% static 'litepicker/dist/css/litepicker.css' %}">
<script src="{% static 'dashboard/js/base.js' %}"></script> <script src="{% static 'dashboard/js/base.js' %}"></script>
<link rel="stylesheet" href="{% static 'dashboard/css/global.css' %}"> <link rel="stylesheet" href="{% static 'dashboard/css/global.css' %}">
<link rel="stylesheet" href="{% static 'flag-icon-css/css/flag-icon.min.css' %}"> <link rel="stylesheet" href="{% static 'flag-icon-css/css/flag-icon.min.css' %}">
@ -122,4 +125,4 @@
{% endblock %} {% endblock %}
</body> </body>
</html> </html>

View File

@ -5,29 +5,41 @@
<input type="input" id="rangePicker" placeholder="Date range" class="input ~neutral bg-neutral-000 cursor-pointer w-auto" readonly> <input type="input" id="rangePicker" placeholder="Date range" class="input ~neutral bg-neutral-000 cursor-pointer w-auto" readonly>
<style> <style>
:root { :root {
--litepickerMonthButtonHover: var(--color-urge); --litepicker-button-prev-month-color-hover: var(--color-urge);
--litepickerDayColorHover: var(--color-urge); --litepicker-button-next-month-color-hover: var(--color-urge);
--litepickerDayIsTodayColor: var(--color-urge); --litepicker-day-color-hover: var(--color-urge);
--litepickerDayIsInRange: var(--color-urge-normal-fill); --litepicker-is-today-color: var(--color-urge);
--litepickerDayIsStartBg: var(--color-urge); --litepicker-is-in-range-color: var(--color-urge-normal-fill);
--litepickerDayIsEndBg: var(--color-urge); --litepicker-is-start-color-bg: var(--color-urge);
--litepickerButtonApplyBg: var(--color-urge); --litepicker-is-end-color-bg: var(--color-urge);
--litepicker-button-apply-color-bg: var(--color-urge);
} }
</style> </style>
<script> <script>
var picker = new Litepicker({ var picker = new Litepicker({
element: document.getElementById('rangePicker'), element: document.getElementById('rangePicker'),
plugins: ['ranges'],
singleMode: false, singleMode: false,
format: "MMM DD 'YY", format: "MMM DD 'YY",
maxDate: new Date(), maxDate: new Date(),
startDate: Date.parse(document.getElementById("startDate").getAttribute("value")), startDate: Date.parse(document.getElementById("startDate").getAttribute("value")),
endDate: Date.parse(document.getElementById("endDate").getAttribute("value")), endDate: Date.parse(document.getElementById("endDate").getAttribute("value")),
onSelect: function (startDate, endDate) { ranges: {
document.getElementById("startDate").setAttribute("value", startDate.getFullYear() + customRanges: {
"-" + (startDate.getMonth() + 1) + "-" + startDate.getDate()); {% for date_range in date_ranges %}
document.getElementById("endDate").setAttribute("value", endDate.getFullYear() + "-" + '{{ date_range.name }}': [
(endDate.getMonth() + 1) + "-" + endDate.getDate()); new Date('{{ date_range.start.isoformat }}'),
document.getElementById("datePicker").submit(); new Date('{{ date_range.end.isoformat }}')
],
{% endfor %}
}
} }
}); });
</script> picker.on('selected', (startDate, endDate) => {
document.getElementById("startDate").setAttribute("value", startDate.getFullYear() +
"-" + (startDate.getMonth() + 1) + "-" + startDate.getDate());
document.getElementById("endDate").setAttribute("value", endDate.getFullYear() + "-" +
(endDate.getMonth() + 1) + "-" + endDate.getDate());
document.getElementById("datePicker").submit();
});
</script>

View File

@ -301,7 +301,11 @@ NPM_ROOT_PATH = "../"
NPM_FILE_PATTERNS = { NPM_FILE_PATTERNS = {
"a17t": [os.path.join("dist", "a17t.css"), os.path.join("dist", "tailwind.css")], "a17t": [os.path.join("dist", "a17t.css"), os.path.join("dist", "tailwind.css")],
"apexcharts": [os.path.join("dist", "apexcharts.min.js")], "apexcharts": [os.path.join("dist", "apexcharts.min.js")],
"litepicker": [os.path.join("dist", "js", "main.js")], "litepicker": [
os.path.join("dist", "nocss", "litepicker.js"),
os.path.join("dist", "css", "litepicker.css"),
os.path.join("dist", "plugins", "ranges.js"),
],
"turbolinks": [os.path.join("dist", "turbolinks.js")], "turbolinks": [os.path.join("dist", "turbolinks.js")],
"stimulus": [os.path.join("dist", "stimulus.umd.js")], "stimulus": [os.path.join("dist", "stimulus.umd.js")],
"inter-ui": [os.path.join("Inter (web)", "*")], "inter-ui": [os.path.join("Inter (web)", "*")],