From 8607ca87eab7ba95a6767199942c80f0fca49ed3 Mon Sep 17 00:00:00 2001 From: Matt Mascarenhas Date: Thu, 5 Jul 2018 20:13:25 +0100 Subject: [PATCH] cinera: Finer-grained search input autofocus --- cinera/cinera.c | 10 ++++------ cinera/cinera_search.js | 25 ++++++++++++++++++++++++- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/cinera/cinera.c b/cinera/cinera.c index c41279e..0a62e1e 100644 --- a/cinera/cinera.c +++ b/cinera/cinera.c @@ -16,7 +16,7 @@ typedef struct version CINERA_APP_VERSION = { .Major = 0, .Minor = 5, - .Patch = 64 + .Patch = 65 }; // TODO(matt): Copy in the DB 3 stuff from cinera_working.c @@ -2324,8 +2324,7 @@ PrintUsage(char *BinaryLocation, config *DefaultConfig) " Override default output player location (\"%s\")\n" "\n" " -1\n" - " Open search result links in the same browser tab and do not autofocus\n" - " search input box on page load\n" + " Open search result links in the same browser tab\n" " NOTE: Ideal for a guide embedded in an iframe\n" " -f\n" " Force integration with an incomplete template\n" @@ -5510,13 +5509,13 @@ IndexToBuffer(index *Index, buffers *CollationBuffers) // NOTE(matt): This guy m } char *Theme = StringsDiffer(Config.Theme, "") ? Config.Theme : Config.ProjectID; - int Allowance = StringLength(Theme) * 2 + StringLength(Config.ProjectID) + StringLength(Config.BaseURL) + StringLength(Config.PlayerLocation) + (Config.Mode & MODE_SINGLETAB ? 0 : 10); + int Allowance = StringLength(Theme) * 2 + StringLength(Config.ProjectID) + StringLength(Config.BaseURL) + StringLength(Config.PlayerLocation); char queryContainer[659 + Allowance]; // NOTE(matt): Update the size if changing the string CopyString(queryContainer, sizeof(queryContainer), "
\n" " \n" "
\n" - " \n" + " \n" "
\n" " Downloading data...\n" "
\n" @@ -5529,7 +5528,6 @@ IndexToBuffer(index *Index, buffers *CollationBuffers) // NOTE(matt): This guy m "
Sort: Old to New ⏶
\n" "
\n", Theme, - Config.Mode & MODE_SINGLETAB ? "": " autofocus", Config.Mode & MODE_SINGLETAB ? 1 : 0, Theme, Config.ProjectID, diff --git a/cinera/cinera_search.js b/cinera/cinera_search.js index 40764a5..03bf2ff 100644 --- a/cinera/cinera_search.js +++ b/cinera/cinera_search.js @@ -209,7 +209,30 @@ function renderResults() { } } -var queryEl = document.getElementById("query") +function IsVisible(el) { + var xPos = 0; + var yPos = 0; + var Height = parseInt(getComputedStyle(el).height); + + while (el) { + if (el.tagName == "BODY") { + var xScroll = el.scrollLeft || document.documentElement.scrollLeft; + var yScroll = el.scrollTop || document.documentElement.scrollTop; + + xPos += (el.offsetLeft - xScroll + el.clientLeft) + yPos += (el.offsetTop - yScroll + el.clientTop) + } else { + xPos += (el.offsetLeft - el.scrollLeft + el.clientLeft); + yPos += (el.offsetTop - el.scrollTop + el.clientTop); + } + + el = el.offsetParent; + } + return ((xPos > 0 && xPos < window.innerWidth) && (yPos > 0 && yPos + Height < window.innerHeight)); +} + +var queryEl = document.getElementById("query"); +if(document.hasFocus() && IsVisible(queryEl)) { queryEl.focus(); } queryEl.addEventListener("input", function(ev) { history.replaceState(null, null, "#" + encodeURIComponent(queryEl.value)); runSearch();