/*
 * hud-pinning.js
 * https://git.leftypol.org/leftypol/leftypol
 *
 * Released under the MIT license
 * Copyright (c) 2024 Zankaria <zankaria (dot) auxa (at) mailu (dot) io>
 *
 * Usage:
 *   $config['additional_javascript'][] = 'js/jquery.min.js';
 *   $config['additional_javascript'][] = 'js/hud-pinning.js';
 */

/**
 * You know the bar on the top of the page that is created if you specify the $config['boards'] array? That one.
 * Also know the bottom bar with the "Return" button and thread update controls? Also that one.
 *
 * Both bars are pinned on the top and bottom of the page. This script adds an option to unpin them.
 */
$(document).ready(function() {
	'use strict';

	if (window.Options && Options.get_tab('general') && window.jQuery) {
		function setHudPinning(pin) {
			let style = pin ? '' : 'initial';
			$('#top-hud').css('position', style);
			$('#bottom-hud').css('position', style);
		}

		// Insert the option in the panel.
		Options.extend_tab('general', '<label id="hud-pinning"><input type="checkbox">' + _('Unpin the top and bottom bars') + '</label>');

		// Trigger if the panel's checkbox is toggled.
		$('#hud-pinning>input').on('change', function() {
			if (this.checked) {
				localStorage.hud_pinning = 'false';
				setHudPinning(false);
			} else {
				localStorage.hud_pinning = 'true';
				setHudPinning(true);
			}
		});

		// Reload on new post: allows it to work with auto-reload.js etc.
		$(document).on('new_post', function(e, post) {
			setHudPinning(localStorage.hud_pinning !== 'false');
		});

		// Enforce the setting on loading.
		if (localStorage.hud_pinning === 'false') {
			$('#hud-pinning>input').attr('checked', 'checked');
			setHudPinning(false);
		}
	}
});