Skip to content
Snippets Groups Projects
Verified Commit d142abb9 authored by Jonathan Weth's avatar Jonathan Weth :keyboard:
Browse files

Use json_script to provide date for date and week pickers

- Remove some unnecessary stuff from JavaScript files
- Reverse URLs on server-side
- Include JS for datepicker only if needed
- Remove JS unix date template tag and add it as an util helper instead
parent f99d9ce4
No related branches found
No related tags found
1 merge request!31Biscuit merge. Closes #53.
function updateDatepicker() { var data = getJSONScript("datepicker_data");
if (!displayDateOnly) { var activeDate = new Date(data.date);
$("#date").val(formatDate(activeDate));
}
}
function update() { function updateDatepicker() {
console.log("Render new."); $("#date").val(formatDate(activeDate));
updateDatepicker();
} }
function loadNew() { function loadNew() {
window.location.href = dest + formatDateForDjango(activeDate); window.location.href = data.dest + formatDateForDjango(activeDate);
} }
function onDateChanged() { function onDateChanged() {
var str = $("#date").val(); var str = $("#date").val();
var split = str.split("."); var split = str.split(".");
activeDate = new Date(split[2], split[1] - 1, split[0]); activeDate = new Date(split[2], split[1] - 1, split[0]);
update(); updateDatepicker();
loadNew(); loadNew();
} }
...@@ -26,5 +21,5 @@ function onDateChanged() { ...@@ -26,5 +21,5 @@ function onDateChanged() {
$(document).ready(function () { $(document).ready(function () {
$("#date").change(onDateChanged); $("#date").change(onDateChanged);
update(); updateDatepicker();
}); });
var data = getJSONScript("week_select");
function goToCalendarWeek(cw, year) { function goToCalendarWeek(cw, year) {
window.location.href = dest + year + "/" + cw; window.location.href = data.dest + year + "/" + cw;
} }
function onCalendarWeekChanged(where) { function onCalendarWeekChanged(where) {
goToCalendarWeek($(where).val(), year); goToCalendarWeek($(where).val(), data.year);
} }
$(document).ready(function () { $(document).ready(function () {
......
...@@ -27,10 +27,6 @@ ...@@ -27,10 +27,6 @@
</div> </div>
</div> </div>
<script type="text/javascript">
var dest = Urls.myTimetable();
</script>
<div class="row"> <div class="row">
<div class="timetable-plan col s12 m12 xl4"> <div class="timetable-plan col s12 m12 xl4">
......
{% load date_js static %} {% load static %}
<script type="text/javascript" src="{% static "js/helper.js" %}"></script>
<script type="text/javascript">
var activeDate = new Date({{ day|date_unix }});
var displayDateOnly = {% if display_date_only %}true{% else %}false{% endif %};
</script>
<script type="text/javascript" src="{% static "js/chronos/date_select.js" %}"></script>
{% if not display_date_only %}
<script type="text/javascript" src="{% static "js/helper.js" %}"></script>
{{ datepicker|json_script:"datepicker_data" }}
<script type="text/javascript" src="{% static "js/chronos/date_select.js" %}"></script>
{% endif %}
<div class="col s2 no-padding"> <div class="col s2 no-padding">
<a class="waves-effect waves-teal btn-flat btn-flat-medium left" href="{{ url_prev }}"> <a class="waves-effect waves-teal btn-flat btn-flat-medium left" href="{{ url_prev }}">
<i class="material-icons center">navigate_before</i> <i class="material-icons center">navigate_before</i>
</a> </a>
</div> </div>
{% if display_date_only %} {% if display_date_only %}
<div class="col s8"> <div class="col s8">
<span class="card-title center-block" id="date"> <span class="card-title center-block" id="date">
......
...@@ -9,10 +9,6 @@ ...@@ -9,10 +9,6 @@
{% block no_page_title %}{% endblock %} {% block no_page_title %}{% endblock %}
{% block content %} {% block content %}
<script type="text/javascript">
var dest = Urls.substitutions();
</script>
<div class="row no-margin"> <div class="row no-margin">
<div class="col s10 m6 no-padding"> <div class="col s10 m6 no-padding">
<h4>{% trans "Substitutions" %}</h4> <h4>{% trans "Substitutions" %}</h4>
......
...@@ -7,14 +7,10 @@ ...@@ -7,14 +7,10 @@
{% endblock %} {% endblock %}
{% block content %} {% block content %}
{% if smart %} {% if smart %}
<script type="text/javascript"> <script type="text/javascript" src="{% static "js/helper.js" %}"></script>
var week = {{ week.week }}; {{ week_select|json_script:"week_select" }}
var year = {{ week.year }};
var type = "{{ type }}";
var pk = {{ pk }};
var dest = Urls.timetable(type, pk);
</script>
<script type="text/javascript" src="{% static "js/chronos/week_select.js" %}"></script> <script type="text/javascript" src="{% static "js/chronos/week_select.js" %}"></script>
{% endif %} {% endif %}
......
from datetime import datetime, time from datetime import datetime, time, date
from django import template
def date_unix(value: date) -> int:
""" Converts a date object to an UNIX timestamp """
register = template.Library()
@register.filter
def date_unix(value: datetime):
value = datetime.combine(value, time(hour=0, minute=0)) value = datetime.combine(value, time(hour=0, minute=0))
return int(value.timestamp()) * 1000 return int(value.timestamp()) * 1000
...@@ -17,6 +17,7 @@ from aleksis.core.util import messages ...@@ -17,6 +17,7 @@ from aleksis.core.util import messages
from .forms import LessonSubstitutionForm from .forms import LessonSubstitutionForm
from .models import LessonPeriod, LessonSubstitution, TimePeriod, Room from .models import LessonPeriod, LessonSubstitution, TimePeriod, Room
from .tables import LessonsTable from .tables import LessonsTable
from .util.js import date_unix
from .util.min_max import ( from .util.min_max import (
period_min, period_min,
period_max, period_max,
...@@ -183,6 +184,10 @@ def timetable( ...@@ -183,6 +184,10 @@ def timetable(
context["pk"] = pk context["pk"] = pk
context["el"] = el context["el"] = el
context["smart"] = is_smart context["smart"] = is_smart
context["week_select"] = {
"year": wanted_week.year,
"dest": reverse("timetable", args=[type_, pk])
}
week_prev = wanted_week - 1 week_prev = wanted_week - 1
week_next = wanted_week + 1 week_next = wanted_week + 1
...@@ -306,6 +311,10 @@ def substitutions( ...@@ -306,6 +311,10 @@ def substitutions(
context["substitutions"] = substitutions context["substitutions"] = substitutions
context["day"] = wanted_day context["day"] = wanted_day
context["datepicker"] = {
"date": date_unix(wanted_day),
"dest": reverse("substitutions")
}
context["url_prev"], context["url_next"] = get_prev_next_by_day( context["url_prev"], context["url_next"] = get_prev_next_by_day(
wanted_day, "substitutions_by_date" wanted_day, "substitutions_by_date"
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment