app: Implement toggle button in app page

- revamp the old form submission model to enable/disable apps into a
  simple toggle button
- provide backwards compatibility when javascript is disabled

Reviewed-by: Joseph Nuthalapati <njoseph@riseup.net>
This commit is contained in:
Alice Kile 2019-11-05 15:09:03 +05:30 committed by Joseph Nuthalapati
parent 1504e182e4
commit ec62f331b8
No known key found for this signature in database
GPG Key ID: 5398F00A2FA43C35
4 changed files with 97 additions and 21 deletions

View File

@ -35,6 +35,7 @@ import plinth
class AppForm(forms.Form):
"""Generic configuration form for an app."""
is_enabled = forms.BooleanField(
widget=CheckboxInput(attrs={'id': 'app-toggle-input'}),
label=_('Enable application'), required=False)

View File

@ -26,17 +26,29 @@
{% load static %}
{% block content %}
<div id='app-toggle-container' class="toggle-button-container">
<form id="app-toggle" class="form form-configuration" method="post">
{% csrf_token %}
{{ form|bootstrap }}
{% if is_enabled %}
<button type="submit" value="False" class="btn toggle-button toggle-button--toggled"></button>
{% else %}
<button type="submit" value="True" class="btn toggle-button"></button>
{% endif %}
</form>
</div>
<header>
{% block pagetitle %}
<h2>{{ name }}</h2>
{% endblock %}
{% block pagetitle %}
<h2>{{ name }}</h2>
{% endblock %}
{% block description %}
{% for paragraph in description %}
<p>{{ paragraph|safe }}</p>
{% endfor %}
{% endblock %}
{% block description %}
{% for paragraph in description %}
<p>{{ paragraph|safe }}</p>
{% endfor %}
{% endblock %}
</header>
{% if manual_page %}
<p class="manual-page">
<a href="{% url 'help:manual-page' lang='-' page=manual_page %}">
@ -98,17 +110,19 @@
{% include "port-forwarding-info.html" with service_name=name %}
{% block configuration %}
<h3>{% trans "Configuration" %}</h3>
<noscript>
{% block configuration %}
<h3>{% trans "Configuration" %}</h3>
<form class="form form-configuration" method="post">
{% csrf_token %}
<form class="form form-configuration" method="post">
{% csrf_token %}
{{ form|bootstrap }}
<input type="submit" class="btn btn-primary"
value="{% trans "Update setup" %}"/>
</form>
{% endblock %}
{{ form|bootstrap }}
<input type="submit" class="btn btn-primary"
value="{% trans "Update setup" %}"/>
</form>
{% endblock %}
</noscript>
<script src="{% static 'theme/js/app.template.js' %}"></script>
{% endblock %}

View File

@ -464,3 +464,46 @@ a.menu_link_active {
.names-domain-column {
width: 50%;
}
.toggle-button-container {
display: none;
flex-flow: row;
justify-content: flex-end;
}
.toggle-button-container .form-group {
display: none;
}
.toggle-button {
border-radius: 25px;
border: none;
width: 55px;
height: 25px;
background: #ccc;
position: relative;
}
.toggle-button::before {
content: '';
display: block;
height: 18px;
width: 18px;
border-radius: 100%;
background: #fff;
position: absolute;
top: 50%;
left: 6%;
transform: translateY(-50%);
}
.toggle-button--toggled {
background: #337ab7;
}
.toggle-button--toggled::before {
content: '';
position: absolute;
top: 50%;
left: 100%;
transform: translateY(-50%) translateX(-116%)
}

View File

@ -0,0 +1,18 @@
const appToggleForm = document.querySelector('#app-toggle')
const appToggleContainer = appToggleForm.parentElement
const appToggleInput = document.querySelector('#app-toggle-input')
const onSubmit = (e) => {
e.preventDefault
appToggleInput.checked = !appToggleInput.checked
appToggleForm.submit()
}
appToggleForm.addEventListener('submit', onSubmit)
/**
* if javascript is enabled, this script will run and show the toggle button
*/
appToggleContainer.style.display = 'flex';