Files

94 lines
3.7 KiB
JavaScript
Executable File

/* Helpdesk shell: deliberately ES5-compatible so Safari on iOS 12 can parse it. */
(function () {
'use strict';
var STORAGE_KEY = 'helpdesk.sidebar.width';
var MIN_WIDTH = 220;
var MAX_WIDTH = 420;
function each(nodes, callback) {
Array.prototype.forEach.call(nodes || [], callback);
}
function setSidebarWidth(width) {
document.documentElement.style.setProperty('--hd-sidebar-width', width + 'px');
}
function initialiseShell() {
var shell = document.querySelector('[data-hd-shell]');
var sidebar = document.querySelector('[data-hd-sidebar]');
var resizer = document.querySelector('[data-hd-sidebar-resizer]');
if (!shell || !sidebar) return;
try {
var stored = Number(window.localStorage.getItem(STORAGE_KEY));
if (isFinite(stored) && stored >= MIN_WIDTH && stored <= MAX_WIDTH) setSidebarWidth(stored);
} catch (ignore) {}
each(document.querySelectorAll('[data-hd-sidebar-toggle]'), function (button) {
if (button.getAttribute('data-hd-bound') === '1') return;
button.setAttribute('data-hd-bound', '1');
button.addEventListener('click', function () {
document.body.classList.toggle('hd-sidebar-open');
}, false);
});
each(sidebar.querySelectorAll('a'), function (link) {
if (link.getAttribute('data-hd-close-bound') === '1') return;
link.setAttribute('data-hd-close-bound', '1');
link.addEventListener('click', function () {
document.body.classList.remove('hd-sidebar-open');
}, false);
});
if (!resizer || resizer.getAttribute('data-hd-bound') === '1') return;
resizer.setAttribute('data-hd-bound', '1');
var dragging = false;
function desktopLayout() {
return window.innerWidth >= 1024;
}
function move(clientX) {
if (!dragging) return;
var width = Math.min(MAX_WIDTH, Math.max(MIN_WIDTH, clientX));
setSidebarWidth(width);
}
function finish() {
if (!dragging) return;
dragging = false;
document.body.classList.remove('hd-sidebar-resizing');
var value = parseInt(window.getComputedStyle(document.documentElement).getPropertyValue('--hd-sidebar-width'), 10);
try {
if (isFinite(value)) window.localStorage.setItem(STORAGE_KEY, String(value));
} catch (ignore) {}
}
resizer.addEventListener('mousedown', function (event) {
if (!desktopLayout()) return;
event.preventDefault();
dragging = true;
document.body.classList.add('hd-sidebar-resizing');
}, false);
document.addEventListener('mousemove', function (event) { move(event.clientX); }, false);
document.addEventListener('mouseup', finish, false);
resizer.addEventListener('touchstart', function (event) {
if (!desktopLayout() || !event.touches || !event.touches.length) return;
dragging = true;
document.body.classList.add('hd-sidebar-resizing');
}, { passive: true });
document.addEventListener('touchmove', function (event) {
if (dragging && event.touches && event.touches.length) move(event.touches[0].clientX);
}, { passive: true });
document.addEventListener('touchend', finish, false);
document.addEventListener('touchcancel', finish, false);
}
if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', initialiseShell, false);
else initialiseShell();
document.addEventListener('livewire:navigated', initialiseShell, false);
}());