Skip to content

Commit 2bb3ecf

Browse files
committed
Enhance autocomplete and app functionality by synchronizing results width and improving event handling
1 parent 56ef75c commit 2bb3ecf

3 files changed

Lines changed: 82 additions & 39 deletions

File tree

lib/yard/server/templates/default/fulldoc/html/js/autocomplete.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@
3535
input.setAttribute("aria-expanded", "false");
3636
form.appendChild(results);
3737

38+
function syncResultsWidth() {
39+
results.style.width = input.offsetWidth + "px";
40+
}
41+
3842
function hideResults() {
3943
results.hidden = true;
4044
input.setAttribute("aria-expanded", "false");
@@ -59,6 +63,7 @@
5963
}
6064

6165
function renderItems(lines) {
66+
syncResultsWidth();
6267
list.innerHTML = "";
6368
items = lines.map(function(line, index) {
6469
var values = line.split(",");
@@ -181,6 +186,7 @@
181186
});
182187

183188
input.addEventListener("focus", function() {
189+
syncResultsWidth();
184190
if (items.length) {
185191
results.hidden = false;
186192
input.setAttribute("aria-expanded", "true");
@@ -190,6 +196,9 @@
190196
document.addEventListener("click", function(event) {
191197
if (!form.contains(event.target)) hideResults();
192198
});
199+
200+
window.addEventListener("resize", syncResultsWidth);
201+
syncResultsWidth();
193202
}
194203

195204
ready(function() {

templates/default/fulldoc/html/js/app.js

Lines changed: 59 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
(function() {
22
var appState = window.__yardAppState || (window.__yardAppState = {
3-
listenersBound: false,
4-
navigationListenerBound: false
3+
navigationListenerBound: false,
4+
navigationChangeBound: false,
5+
navResizerBound: false,
6+
searchFrameGlobalsBound: false
57
});
68
var safeLocalStorage = {};
79
var safeSessionStorage = {};
@@ -173,13 +175,17 @@
173175

174176
function searchFrameButtons() {
175177
queryAll(".full_list_link").forEach(function(link) {
178+
if (link.dataset.yardSearchFrameBound === "true") return;
179+
176180
link.addEventListener("click", function(event) {
177181
event.preventDefault();
178182
toggleSearchFrame(link, link.getAttribute("href"));
179183
});
184+
185+
link.dataset.yardSearchFrameBound = "true";
180186
});
181187

182-
if (appState.listenersBound) return;
188+
if (appState.searchFrameGlobalsBound) return;
183189

184190
window.addEventListener("message", function(event) {
185191
if (event.data === "navEscape") resetSearchFrame();
@@ -189,7 +195,7 @@
189195
if (!isVisible(query("#search"))) resetSearchFrame();
190196
});
191197

192-
appState.listenersBound = true;
198+
appState.searchFrameGlobalsBound = true;
193199
}
194200

195201
function linkSummaries() {
@@ -399,38 +405,42 @@
399405

400406
if (!resizer) return;
401407

402-
resizer.addEventListener(
403-
"pointerdown",
404-
function(event) {
405-
resizer.setPointerCapture(event.pointerId);
406-
event.preventDefault();
407-
event.stopPropagation();
408-
},
409-
false
410-
);
411-
resizer.addEventListener(
412-
"pointerup",
413-
function(event) {
414-
resizer.releasePointerCapture(event.pointerId);
415-
event.preventDefault();
416-
event.stopPropagation();
417-
},
418-
false
419-
);
420-
resizer.addEventListener(
421-
"pointermove",
422-
function(event) {
423-
if ((event.buttons & 1) === 0) return;
424-
425-
safeSessionStorage.navWidth = String(event.pageX);
426-
queryAll(".nav_wrap").forEach(function(node) {
427-
node.style.width = Math.max(200, event.pageX) + "px";
428-
});
429-
event.preventDefault();
430-
event.stopPropagation();
431-
},
432-
false
433-
);
408+
if (!appState.navResizerBound) {
409+
resizer.addEventListener(
410+
"pointerdown",
411+
function(event) {
412+
resizer.setPointerCapture(event.pointerId);
413+
event.preventDefault();
414+
event.stopPropagation();
415+
},
416+
false
417+
);
418+
resizer.addEventListener(
419+
"pointerup",
420+
function(event) {
421+
resizer.releasePointerCapture(event.pointerId);
422+
event.preventDefault();
423+
event.stopPropagation();
424+
},
425+
false
426+
);
427+
resizer.addEventListener(
428+
"pointermove",
429+
function(event) {
430+
if ((event.buttons & 1) === 0) return;
431+
432+
safeSessionStorage.navWidth = String(event.pageX);
433+
queryAll(".nav_wrap").forEach(function(node) {
434+
node.style.width = Math.max(200, event.pageX) + "px";
435+
});
436+
event.preventDefault();
437+
event.stopPropagation();
438+
},
439+
false
440+
);
441+
442+
appState.navResizerBound = true;
443+
}
434444

435445
if (safeSessionStorage.navWidth) {
436446
queryAll(".nav_wrap").forEach(function(node) {
@@ -461,10 +471,18 @@
461471
var hash = window.location.hash;
462472
if (!hash) return;
463473

474+
var targetId = hash.slice(1);
475+
var decodedTargetId = targetId;
476+
464477
try {
465-
var target = query(hash);
466-
if (target) target.scrollIntoView();
478+
decodedTargetId = decodeURIComponent(targetId);
467479
} catch (error) {}
480+
481+
var target =
482+
document.getElementById(decodedTargetId) ||
483+
document.getElementById(targetId);
484+
485+
if (target) target.scrollIntoView();
468486
}
469487

470488
function mainFocus() {
@@ -476,7 +494,10 @@
476494
}
477495

478496
function navigationChange() {
497+
if (appState.navigationChangeBound) return;
498+
479499
window.onpopstate = focusHashTarget;
500+
appState.navigationChangeBound = true;
480501
}
481502

482503
window.__app = function() {

templates/default/fulldoc/html/js/full_list.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,20 @@
7676
if (!targetLink) return false;
7777
mouseEvent = new MouseEvent("click", {
7878
bubbles: true,
79-
cancelable: true
79+
cancelable: true,
80+
view: event.view || window,
81+
detail: event.detail,
82+
screenX: event.screenX,
83+
screenY: event.screenY,
84+
clientX: event.clientX,
85+
clientY: event.clientY,
86+
ctrlKey: event.ctrlKey,
87+
shiftKey: event.shiftKey,
88+
altKey: event.altKey,
89+
metaKey: event.metaKey,
90+
button: event.button,
91+
buttons: event.buttons,
92+
relatedTarget: event.relatedTarget
8093
});
8194
targetLink.dispatchEvent(mouseEvent);
8295
event.preventDefault();

0 commit comments

Comments
 (0)