// DCloud - Document selection & search UI
// Copyright 2020-2022 Perette Barella.
// All rights reserved.
// vim:ts=8:sw=8
// $Id: cloud.js 38 2022-09-27 15:04:29Z perette $


var top_level; /// Contains top level element of list
var back_button;
var search_input;
var current_menu_displayed; /// Current display level in selection mode

function hide_all () {
	var all_list_items = top_level.getElementsByTagName ("LI");
	for (var i = 0; i < all_list_items.length; i++) {
		all_list_items [i].style.display = "none";
	}
	back_button.style.display = "block";
}

function display_up_chain (item) {
	while (item.nodeName == "LI") {
		item.style.display = "";
		item = item.parentNode.parentNode;
	}
}

function process_search () {
	var value = search_input.value;
	// Set the mode, and restore selection mode if input empty.
	top_level.id = ((value == '') ? 'selecting' : 'searching');
	if (value == '') {
		process_menu_click (current_menu_displayed);
		return;
	}
	hide_all ();
	var all_documents = top_level.querySelectorAll ("LI.document");
	for (var i = 0; i < all_documents.length; i++) {
		var doc = all_documents [i];
		if (doc.innerText.toLowerCase().includes (value.toLowerCase())) {
			display_up_chain (doc);
		}
	}
}

function process_menu_click (item) {
	// Ignore clicks on menus in search mode.
	var value = search_input.value;
	if (value != '')
		return;

	// Check and handle top level/menu as a special case
	if (item.nodeName != "LI") {
		hide_all();
		current_menu_displayed = top_level;
		for (var i = 0; i < top_level.children.length; i++) {
			top_level.children [i].style.display = "block";
		}
	} else {
		// Update the shown level/menu
		current_menu_displayed = item;
		hide_all ();
		var descendents = item.querySelectorAll (":scope > UL > LI");
		for (var i = 0; i < descendents.length; i++) {
			descendents [i].style.display = "block";
		}
		display_up_chain (item);
	}
}

// Walk an unordered list and set up the items.
var element_number = 0;
function wire_menu_elements (list) {
	for (var i = 0; i < list.children.length; i++) {
		var item = list.children [i];
		var children = item.querySelectorAll (":scope > UL");
		if (children.length > 0) {
			item.id = 'hash' + (element_number++);
			item.classList.add ('menu');
			item.addEventListener ("click", function (event) {
				// Events for subelements are delivered to parents.
				// Make sure it's really ours before dispatching.
				if (event.target == this) {
					process_menu_click (event.target);
					return false;
				};
				return true;
			});
			for (var j = 0; j < children.length; j++) {
				wire_menu_elements (children [j]);
			};
		} else {
			item.classList.add ('document');
		}
	}
};

document.addEventListener ("DOMContentLoaded", function () {
	top_level = document.querySelectorAll ('BODY>UL')[1];
	current_menu_displayed = top_level;
	wire_menu_elements (top_level);

	search_input = document.getElementById ("search");
	search_input.style.display = "initial";
	search_input.addEventListener ("keyup", process_search);

	/* global */ back_button = document.createElement ("LI");
	back_button.id = "back";
	back_button.appendChild (document.createTextNode ("<<< Back <<<"));
	back_button.addEventListener ("click", function () {
		if (search_input.value != '') {
			search_input.value = '';
			process_search();
		} else {
			process_menu_click (current_menu_displayed.parentNode.parentNode);
		}
		return false;
	});
	top_level.insertBefore (back_button, top_level.children[0]);

	document.querySelector('label[for=search]').style.display="initial";
	process_search();
});


