Convert header_dropdown.js to vanilla JavaScript (no jQuery).

This commit is contained in:
Ben Sturmfels 2021-08-19 18:44:03 +10:00
parent e705c4de61
commit 6c408fb4ea
No known key found for this signature in database
GPG Key ID: 023C05E2C9C068F0
2 changed files with 63 additions and 20 deletions

View File

@ -16,30 +16,73 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
$(document).ready(function(){ 'use strict';
var panel = {};
(function() {
// The header drop-down header panel defaults to open until you explicitly // The header drop-down header panel defaults to open until you explicitly
// close it. After that, the panel open/closed setting will persist across // close it. After that, the panel open/closed setting will persist across
// page loads. // page loads.
var panel_elem = document.getElementById('header_dropdown');
var up_arrow_elem = document.querySelector('.header_dropdown_up');
var down_arrow_elem = document.querySelector('.header_dropdown_down');
function hide(elem) {
elem.style.display = 'none';
}
function show(elem) {
elem.style.display = '';
}
function isDisplayed(elem) {
return elem.style.display === '';
}
function toggle(elem) {
if (isDisplayed(elem)) {
hide(elem);
}
else {
show(elem);
}
}
function showPanel() {
localStorage.removeItem('panel_closed');
show(panel_elem);
show(up_arrow_elem);
hide(down_arrow_elem);
}
function hidePanel() {
localStorage.setItem('panel_closed', 'true');
hide(panel_elem);
hide(up_arrow_elem);
show(down_arrow_elem);
}
function togglePanel() {
// Toggle and persist the panel status.
if (isDisplayed(panel_elem)) {
hidePanel();
}
else {
showPanel()
}
}
// Initialise the panel status when page is loaded. // Initialise the panel status when page is loaded.
if (localStorage.getItem("panel_closed")) { up_arrow_elem.addEventListener('click', togglePanel);
$("#header_dropdown").hide(); down_arrow_elem.addEventListener('click', togglePanel);
$(".header_dropdown_up").hide(); if (localStorage.getItem('panel_closed')) {
hidePanel();
} }
else { else {
$(".header_dropdown_down").hide(); showPanel();
} }
// Toggle and persist the panel status. // Export some functionality for use in other modules.
$(".header_dropdown_down, .header_dropdown_up").click(function() { panel.show = showPanel;
if (localStorage.getItem("panel_closed")) { })();
localStorage.removeItem("panel_closed");
}
else {
localStorage.setItem("panel_closed", "true");
}
$(".header_dropdown_down").toggle();
$(".header_dropdown_up").toggle();
$("#header_dropdown").slideToggle();
});
});

View File

@ -42,8 +42,6 @@
href="{{ request.staticdirect('/images/goblin.ico') }}" /> href="{{ request.staticdirect('/images/goblin.ico') }}" />
<script type="text/javascript" <script type="text/javascript"
src="{{ request.staticdirect('/js/extlib/jquery.js') }}"></script> src="{{ request.staticdirect('/js/extlib/jquery.js') }}"></script>
<script type="text/javascript"
src="{{ request.staticdirect('/js/header_dropdown.js') }}"></script>
<script type="text/javascript" <script type="text/javascript"
src="{{ request.staticdirect('/js/notifications.js') }}"></script> src="{{ request.staticdirect('/js/notifications.js') }}"></script>
<script> <script>
@ -185,6 +183,8 @@
{%- include "mediagoblin/bits/base_footer.html" %} {%- include "mediagoblin/bits/base_footer.html" %}
</div> </div>
{%- endblock mediagoblin_body %} {%- endblock mediagoblin_body %}
<script type="text/javascript"
src="{{ request.staticdirect('/js/header_dropdown.js') }}"></script>
{% include 'mediagoblin/bits/body_end.html' %} {% include 'mediagoblin/bits/body_end.html' %}
</body> </body>
</html> </html>