Make dark theme a user level property, lazy load dark CSS

- Also now respects the colour-scheme media query
- Added meta tag to tell the browser we support dark theme, allowing dark UA stylesheet if the user sends said media query
- Means you only have to set it once per account rather than once per machine
- Dark themed embeds!
This commit is contained in:
2021-02-06 16:40:22 +00:00
parent 0598612c15
commit 4f912932ca
13 changed files with 90 additions and 71 deletions

View File

@@ -0,0 +1,18 @@
# Generated by Django 3.1.5 on 2021-02-06 10:43
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('RIGS', '0039_auto_20210123_1910'),
]
operations = [
migrations.AddField(
model_name='profile',
name='dark_theme',
field=models.BooleanField(default=False),
),
]

View File

@@ -26,6 +26,7 @@ class Profile(AbstractUser):
is_approved = models.BooleanField(default=False)
last_emailed = models.DateTimeField(blank=True,
null=True) # Currently only populated by the admin approval email. TODO: Populate it each time we send any email, might need that...
dark_theme = models.BooleanField(default=False)
@classmethod
def make_api_key(cls):

View File

@@ -79,7 +79,7 @@
{% block content %}
{% include 'item_modal.html' %}
<form class=" itemised_form" role="form" method="POST">
<form class="itemised_form" role="form" method="POST">
{% csrf_token %}
<div class="row">
<div class="col-12">

View File

@@ -146,10 +146,7 @@ class Asset(models.Model, RevisionMixin):
return reverse('asset_detail', kwargs={'pk': self.asset_id})
def __str__(self):
out = "{} | {}".format(self.asset_id, self.description)
if self.is_cable and self.cable_type is not None:
out += '{} - {}m - {}'.format(self.cable_type.plug, self.length, self.cable_type.socket)
return out
return "{} | {}".format(self.asset_id, self.description)
def clean(self):
errdict = {}

11
package-lock.json generated
View File

@@ -18,7 +18,6 @@
"bootstrap-select": "^1.13.17",
"clipboard": "^2.0.6",
"cssnano": "^4.1.10",
"dark-mode-switch": "^1.0.0",
"flatpickr": "^4.6.6",
"fullcalendar": "^5.3.2",
"gulp": "^4.0.2",
@@ -1663,11 +1662,6 @@
"type": "^1.0.1"
}
},
"node_modules/dark-mode-switch": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/dark-mode-switch/-/dark-mode-switch-1.0.0.tgz",
"integrity": "sha512-wRoqYGmph7mwRbPcSNRR6DpRQe5wOK0lG6O3Hz7nIWWDyLAHSqje7PUI6c/acOYACiSPHc7sqysn57GSuklb6w=="
},
"node_modules/dashdash": {
"version": "1.14.1",
"resolved": "https://registry.npmjs.org/dashdash/-/dashdash-1.14.1.tgz",
@@ -10583,11 +10577,6 @@
"type": "^1.0.1"
}
},
"dark-mode-switch": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/dark-mode-switch/-/dark-mode-switch-1.0.0.tgz",
"integrity": "sha512-wRoqYGmph7mwRbPcSNRR6DpRQe5wOK0lG6O3Hz7nIWWDyLAHSqje7PUI6c/acOYACiSPHc7sqysn57GSuklb6w=="
},
"dashdash": {
"version": "1.14.1",
"resolved": "https://registry.npmjs.org/dashdash/-/dashdash-1.14.1.tgz",

View File

@@ -14,7 +14,6 @@
"bootstrap-select": "^1.13.17",
"clipboard": "^2.0.6",
"cssnano": "^4.1.10",
"dark-mode-switch": "^1.0.0",
"flatpickr": "^4.6.6",
"fullcalendar": "^5.3.2",
"gulp": "^4.0.2",

View File

@@ -111,4 +111,12 @@
.custom-control-input:focus ~ .custom-control-label::before {
box-shadow: 0 0 0 $input-focus-width rgba($primary, 0.7) !important;
}
.source {
color: white !important;
}
.embed_container {
border-color: #3853a4 !important;
background: #222;
color: $gray-100;
}
}

View File

@@ -1,4 +1,3 @@
@import "dark_screen";
@import "custom-variables";
//Required
@import "node_modules/bootstrap/scss/bootstrap-reboot";

View File

@@ -10,11 +10,14 @@
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="theme-color" content="#3853a4">
<meta name="color-scheme" content="light dark">
<link rel="icon" type="image/png" href="{% static 'imgs/pyrigs-avatar.png' %}">
<link rel="apple-touch-icon" href="{% static 'imgs/pyrigs-avatar.png' %}">
<link href="{% static 'fonts/OpenSans-Regular.tff' %}">
<link rel="stylesheet" type="text/css" href="{% static 'css/dark_screen.css' %}" {% if not request.user.dark_theme %}media="(prefers-color-scheme: dark)"{% endif %}>
<link rel="stylesheet" type="text/css" href="{% static 'css/screen.css' %}">
{% block css %}
{% endblock %}
@@ -77,7 +80,11 @@
</div>
<div class="modal fade" id="modal" role="dialog" tabindex=-1></div>
<script>
if({{ request.user.dark_theme|lower }} || window.matchMedia('(prefers-color-scheme: dark)')) {
document.body.setAttribute('data-theme', 'dark');
}
</script>
<script>
Date.prototype.getISOString = function () {
var yyyy = this.getFullYear().toString();
@@ -123,7 +130,6 @@
easter_egg.load();
});
</script>
<script src="{% static 'js/dark-mode-switch.min.js' %}"></script>
<script>
document.body.addEventListener('keydown', function(e) {
if(e.keyCode == 13 && (e.metaKey || e.ctrlKey)) {

View File

@@ -7,8 +7,10 @@
lang="{% firstof LANGUAGE_CODE 'en' %}"
class="embedded">
<head>
<base target="_blank" />
<!-- Open all links in a new tab, not in the iframe -->
<base target="_blank" /><!-- Open all links in a new tab, not in the iframe -->
<meta name="color-scheme" content="light dark">
<link rel="stylesheet" type="text/css" href="{% static 'css/dark_screen.css' %}" {% if not request.user.dark_theme %}media="(prefers-color-scheme: dark)"{% endif %}>
<link href="{% static 'fonts/OpenSans-Regular.tff' %}">
@@ -34,6 +36,11 @@
{% endblock %}
</div>
</div>
<script>
if({{ request.user.dark_theme|lower }} || window.matchMedia('(prefers-color-scheme: dark)')) {
document.body.setAttribute('data-theme', 'dark');
}
</script>
{% block js %}
{% endblock %}
</body>

View File

@@ -3,22 +3,17 @@
<a class="nav-link dropdown-toggle" href="#" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Hi {{ user.first_name }}
</a>
<ul class="dropdown-menu p-3 clearfix" id="userdropdown">
<ul class="dropdown-menu clearfix" id="userdropdown">
<li class="media">
<a href="{% url 'profile_detail' %}">
<img src="{{ request.user.profile_picture }}" class="media-object"/>
<img src="{{ request.user.profile_picture }}" class="media-object float-left pr-2"/>
<div class="media-body">
<b>{{ request.user.first_name }} {{ request.user.last_name }}</b>
<p class="muted">{{ request.user.email }}</p>
<b>{{ request.user.first_name }} {{ request.user.last_name }}</b>
<p class="text-muted">{{ request.user.email }}</p>
</div>
</a>
</li>
<li class="mb-2">
<div class="custom-control custom-switch">
<input type="checkbox" class="custom-control-input" id="darkSwitch" />
<label class="custom-control-label" for="darkSwitch">Dark Mode</label>
</div>
</li>
<div class="dropdown-divider"></div>
<li class="float-right"><a href="{% url 'logout' %}" class="btn btn-primary"><i class="fas fa-sign-out-alt"></i> Logout</a></li>
</ul>
{% else %}

View File

@@ -5,47 +5,47 @@
{% block content %}
<div class="row">
<div class="col">
<div class="col-md-6 offset-md-3">
{% include 'form_errors.html' %}
<h3>Update Profile {{object.name}}</h3>
<div class="row">
<div class="col-md-6">
<form action="{{form.action|default:request.path}}" method="post">{% csrf_token %}
<div class="form-group">
{% include 'partials/form_field.html' with field=form.first_name %}
</div>
<div class="form-group">
{% include 'partials/form_field.html' with field=form.last_name %}
</div>
<div class="form-group">
<label for="{{form.email.id_for_label}}" class="col-form-label">{{form.email.label}}</label>
{% render_field form.email type="email" class+="form-control" placeholder=form.email.label %}
</div>
<div class="form-group">
{% include 'partials/form_field.html' with field=form.initials %}
</div>
<div class="form-group">
<label for="{{form.phone.id_for_label}}" class="col-form-label">{{form.phone.label}}</label>
{% render_field form.phone type="tel" class+="form-control" placeholder=form.phone.label %}
</div>
<div class="form-group">
<input class="btn btn-primary float-right" type="submit"/>
</div>
</form>
</div>
<div class="col">
<a href="https://gravatar.com/">
<img src="{{object.profile_picture}}" class="img-fluid rounded" />
<div class="text-center">
Images hosted by Gravatar
</div>
</a>
</div>
<div class="col-md-6">
<a href="https://gravatar.com/">
<img src="{{object.profile_picture}}" class="img-fluid rounded" />
<div class="text-center">
Images hosted by Gravatar
</div>
</a>
</div>
<div class="col-md-6">
<form action="{{form.action|default:request.path}}" method="post">{% csrf_token %}
<div class="form-group">
{% include 'partials/form_field.html' with field=form.first_name %}
</div>
<div class="form-group">
{% include 'partials/form_field.html' with field=form.last_name %}
</div>
<div class="form-group">
<label for="{{form.email.id_for_label}}" class="col-form-label">{{form.email.label}}</label>
{% render_field form.email type="email" class+="form-control" placeholder=form.email.label %}
</div>
<div class="form-group">
{% include 'partials/form_field.html' with field=form.initials %}
</div>
<div class="form-group">
<label for="{{form.phone.id_for_label}}" class="col-form-label">{{form.phone.label}}</label>
{% render_field form.phone type="tel" class+="form-control" placeholder=form.phone.label %}
</div>
<div class="form-group">
<label for="{{ form.dark_theme.id_for_label }}">Enable Dark Theme?</label>
{% render_field form.dark_theme %}
</div>
<div class="form-group">
<input class="btn btn-primary float-right" type="submit"/>
</div>
</form>
</div>
</div>
</div>
</div>
</div>

View File

@@ -48,7 +48,7 @@ class ProfileDetail(generic.DetailView):
class ProfileUpdateSelf(generic.UpdateView):
template_name = "profile_form.html"
model = models.Profile
fields = ['first_name', 'last_name', 'email', 'initials', 'phone']
fields = ['first_name', 'last_name', 'email', 'initials', 'phone', 'dark_theme']
def get_queryset(self):
pk = self.request.user.id