1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-11-11 12:46:49 +00:00

refactor(templates): the shared page scripts load from static files

A step towards a Content-Security-Policy without inline scripts. The
scripts every page or many pages share now load from static files: the
error focus of base.html, the please-wait dialog, the sign-in forms'
password toggle and busy button, the jobs pages' reload while a job
runs, and the start of the job panel and of the VM list's state poll,
whose values come as json_script data. Cancel links go back through a
data-history-back attribute instead of a javascript: URL. The IE8
html5shiv/respond.js block, X-UA-Compatible and shrink-to-fit are gone:
no supported browser reads them.
This commit is contained in:
catborise 2026-11-02 08:47:58 +03:00
parent ab15fafe9c
commit 393aecb8f1
23 changed files with 112 additions and 137 deletions

View file

@ -0,0 +1,6 @@
// Reloads the page while it shows an active job, so the page follows the job to its end.
setInterval(function () {
if (!document.hidden && !document.querySelector("form :focus")) {
window.location.reload();
}
}, 5000);

View file

@ -0,0 +1,9 @@
// Starts the job panel of a resource page from its settings (job_panel_script tag).
(function () {
var settings = JSON.parse(document.getElementById("job-panel-settings").textContent);
window.resourceJobPanel = jobPanel(document.getElementById("job-panel"), {
resource: settings.resource,
job: function (id) { return settings.jobs + id + "/"; },
cancel: function (id) { return settings.jobs + id + "/cancel/"; },
}, settings.csrf_token, settings.remembered);
})();

View file

@ -1,8 +1,3 @@
{% load static %}
{# Reloads the page while it shows an active job, so the page follows the job to its end. #}
<script id="follow-active-jobs">
setInterval(function () {
if (!document.hidden && !document.querySelector("form :focus")) {
window.location.reload();
}
}, 5000);
</script>
<script id="follow-active-jobs" src="{% static 'operations/follow_active.js' %}"></script>

View file

@ -1,9 +1,4 @@
{% load static %}
{{ settings|json_script:"job-panel-settings" }}
<script src="{% static 'operations/job_panel.js' %}"></script>
<script>
window.resourceJobPanel = jobPanel(document.getElementById("job-panel"), {
resource: "{% url 'operation-resource' %}?key={{ resource|urlencode:'' }}",
job: function (id) { return "{% url 'operation-list' %}" + id + "/"; },
cancel: function (id) { return "{% url 'operation-list' %}" + id + "/cancel/"; },
}, "{{ csrf_token }}", {{ remembered|safe }});
</script>
<script src="{% static 'operations/job_panel_start.js' %}"></script>

View file

@ -1,7 +1,9 @@
import json
import uuid
from urllib.parse import quote
from django import template
from django.middleware.csrf import get_token
from django.urls import reverse
from django.utils.html import format_html, format_html_join
from django.utils.safestring import mark_safe
from django.utils.timesince import timesince
@ -71,5 +73,9 @@ def wrap_at_slash(label):
@register.inclusion_tag("operations/job_panel_script.html", takes_context=True)
def job_panel_script(context, resource):
"""The job panel's script for the page of resource."""
return {"resource": resource, "csrf_token": context.get("csrf_token"),
"remembered": json.dumps(take_remembered(context["request"], resource))}
return {"settings": {
"resource": reverse("operation-resource") + "?key=" + quote(resource, safe=""),
"jobs": reverse("operation-list"),
"csrf_token": get_token(context["request"]),
"remembered": take_remembered(context["request"], resource),
}}