Menu='Buttons:1'
Icon='icon-u-search'
Title='Search'
Code='e956'
---
<?
###################################################
#                                                 #
# GUI Search copyright 2021-2023, Andrew Zawadzki #
#           Licenced under GPLv2                  #
#                                                 #
###################################################

$currentUnraidPage = str_replace('Browse','Main',basename(explode('?',$_SERVER['REQUEST_URI'])[0]));
$guiSearchBoxSpan  = "<span id='guiSearchBoxSpan'><input type='text' id='guiSearchBox' autocomplete='new-password'></input></span>";
?>
<script>
var languageVisible;
var guiSearchSuggestions;

$(function(){
<?if ($themeHelper->isSidebarTheme()):?>
	$('.nav-item.gui_search').hover(function(){gui_search();},function(e){closeSearchBox(e);});
<?endif;?>
	$.post('/plugins/dynamix.gui.search/include/exec.php',function(data) {
		if (data) {
			try {guiSearchSuggestions = JSON.parse(data); setupGUIsearch();}
			catch(e) {console.log('Invalid JSON for GUI search autocomplete');}
		}
	});
});

function guiSearchBoxSpan() {
	return $('#guiSearchBoxSpan').length>0;
}

function setupGUIsearch() {
	window.addEventListener('keydown',function(e){
		if (!e.shiftKey && !e.altKey && (navigator.appVersion.indexOf('Mac')==-1 ? e.ctrlKey : e.metaKey) && e.keyCode==75) {
			// If a modal is visible, don't open the search box
			if ($('[role="modal"]').is(':visible')) return;

			e.preventDefault();
		<?if ($themeHelper->isTopNavTheme()):?>
			if (guiSearchBoxSpan()) closeSearchBox(e); else gui_search();
		<?endif;?>
		}
	});
}

function gui_search() {
<?if ($themeHelper->isTopNavTheme()):?>
	languageVisible = $('.nav-item.LanguageButton').is(':visible');
	$('.nav-tile.right').prepend("<?=$guiSearchBoxSpan?>").css('overflow','visible');
	$('.nav-item.util,.nav-user.show').hide();
<?else:?>
	if (!guiSearchBoxSpan()) $('.nav-item.gui_search a').append("<?=$guiSearchBoxSpan?>");
	$('.nav-item.gui_search').css('overflow','visible');
<?endif;?>
	if (guiSearchSuggestions) {
		var guiSearchAwesomplete = new Awesomplete(document.getElementById('guiSearchBox'));
		guiSearchAwesomplete.list = guiSearchSuggestions;
		guiSearchAwesomplete.maxItems = 15;
		guiSearchAwesomplete.autoFirst = true;
		Awesomplete.$('#guiSearchBox').removeEventListener('awesomplete-selectcomplete',guiSearch);
		Awesomplete.$('#guiSearchBox').addEventListener('awesomplete-selectcomplete',guiSearch);
		$('#guiSearchBox').attr('autocomplete','new-password'); // Stop awesomplete from resetting autocomplete
	}
	$('#guiSearchBox').focus().keydown(function(e){if (e.which==27) closeSearchBox(e);}).blur(function(e){closeSearchBox(e);});
}

function closeSearchBox(e) {
	e.stopPropagation();
	$('#guiSearchBoxSpan').remove();
<?if ($themeHelper->isTopNavTheme()):?>
	$('.nav-tile.right').css({'overflow-x':'auto','overflow-y':'hidden'});
	$('.nav-item.util,.nav-user.show').show();
	if (!languageVisible) $('.nav-item.LanguageButton').hide();
<?else:?>
	$('.nav-item.gui_search').css('overflow','hidden');
<?endif;?>
}

function guiSearch() {
	// If a modal is visible, don't navigate away from the page
	if ($('[role="modal"]').is(':visible')) return;

	var searchInfo = $('#guiSearchBox').val().split('**');
	var separator  = ('fragmentDirective' in document) ? '#:~:text=' : '#';
	var scrollText = (typeof searchInfo[1] != 'undefined') ? separator+searchInfo[1].replace(' ','%20').replace('-','%2d') : '';
	var newPage    = "<?=$currentUnraidPage?>/Settings/Tools".replace(searchInfo[0]+'/','');

	closeSearchBox(event);
	if (newPage == 'Dashboard/Settings/Tools') newPage = 'Settings';
  
	/**
	 * Prevents Community Apps from crashing
	 * Hook script provided by CA - https://github.com/unraid/community.applications/blob/master/source/community.applications/usr/local/emhttp/plugins/community.applications/javascript/helpers.js
	 */
 	if ( typeof guiSearchOnUnload === "function" ) {
		guiSearchOnUnload();
	}
  
	location.assign('/'+newPage+'/'+searchInfo[0]+scrollText);
}
</script>
