<commit_before>'use strict';

import ally from 'ally.js';

let overlay;
let overlayClick;

let hidden;
let disabled;

let active = [];

function recreate() {
	if(hidden) hidden.disengage();
	if(disabled) disabled.disengage();

	if(active.length === 0) return;

	hidden = ally.maintain.hidden({
		filter: active
	});

	disabled = ally.maintain.disabled({
		filter: active
	});
}

export default {
	show: function(self, onOverlayClick) {
		if(! overlay) {
			overlay = document.createElement('div');
			overlay.setAttribute('class', 'ml-page-overlay');
			overlay.addEventListener('click', e => {
				if(overlayClick) {
					overlayClick();
				}
			});
			document.body.appendChild(overlay);
		}

		active.push(self);
		recreate();

		overlayClick = onOverlayClick;
		overlay.classList.add('active');
		document.documentElement.classList.add('ml-no-scroll');
	},

	hide: function(self) {
		overlay.classList.remove('active');
		document.documentElement.classList.remove('ml-no-scroll');

		let idx = active.indexOf(self);
		if(idx >= 0) {
			active.splice(idx);
			recreate();
		}
	}
};<commit_msg>Smarter stack handling<commit_after>'use strict';

import ally from 'ally.js';

let overlay;
let overlayClick;

let hidden;
let disabled;

let active = [];

let highestZ = 0;

function recreate() {
	if(hidden) hidden.disengage();
	if(disabled) disabled.disengage();

	const top = active[active.length - 1];

	if(top) {
		document.documentElement.classList.add('mara-no-scroll');

		const elements = active.map(item => item.element);
		hidden = ally.maintain.hidden({
			filter: top.element
		});

		disabled = ally.maintain.disabled({
			filter: top.element
		});

		highestZ = 0;
		active.forEach((item, idx) => {
			highestZ = Math.max(highestZ, item.zIndex + 1);
		});
	} else {
		document.documentElement.classList.remove('mara-no-scroll');
	}
}

function hideActive() {
	const item = active[active.length - 1];
	if(! item) return;

	if(item.options.onOverlayClick) {
		item.options.onOverlayClick();
	}
}

export default {
	show: function(self, options) {
		if(typeof options === 'function') {
			options = {
				onOverlayClick: options
			};
		}

		if(! options) {
			options = {};
		}

		const overlay = document.createElement('div');
		overlay.className = 'mara-page-overlay ' + (options.className || '');
		overlay.addEventListener('click', hideActive);

		let closest = self.closest('[data-mara-stack]') || document.body;
		closest.append(overlay);

		if(highestZ === 0) {
			// Figure out the Z that the main overlay has
			const style = getComputedStyle(overlay);
			const z = style.zIndex;
			if(z !== 'auto') {
				highestZ = Number(z);
			}
		}

		const localZ = highestZ + 1;
		overlay.style.zIndex = localZ;
		self.style.zIndex = localZ + 1;
		self.setAttribute('data-mara-stack', 'true');

		active.push({
			element: self,
			options: options,
			overlay: overlay,
			zIndex: localZ
		});
		recreate();
	},

	hide: function(self) {
		for(let i=0; i<active.length; i++) {
			const item = active[i];
			if(item.element == self) {
				active.splice(i, 1);
				item.overlay.remove();
				item.element.removeAttribute('data-mara-stack');
				break;
			}
		}

		recreate();
	}
};
{'fraction_non_alphanumeric': 0.16337409846346818, 'fraction_numerical': 0.0037629350893697085, 'mean_word_length': 3.6980854197349045, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2594655', 'n_tokens_mistral': 1249, 'n_tokens_neox': 1075, 'n_words': 218}