MediaWiki:Common.js: Difference between revisions
No edit summary |
No edit summary |
||
| Line 228: | Line 228: | ||
/** | /** | ||
* Quick Search — | * Quick Search — Navigate directly to wiki page | ||
* | * Intercepts the Page Forms #forminput inside #crt-quick-search so that | ||
* clicking "Go" (or pressing Enter, or selecting an autocomplete suggestion) | |||
* navigates to the wiki article instead of opening the form editor. | |||
* | |||
* Page Forms uses OOUI and a custom autocomplete. When a suggestion is | |||
* clicked the input's value changes from the partial text to the full page | |||
* name, firing a native "change" event. We detect this by tracking the | |||
* last value the user actually typed (via "input" events) and navigating | |||
* when "change" produces a *different* value — meaning autocomplete filled | |||
* it in. | |||
*/ | */ | ||
(function () { | (function () { | ||
| Line 236: | Line 245: | ||
if (mw.config.get('wgPageName') !== 'Main_Page') return; | if (mw.config.get('wgPageName') !== 'Main_Page') return; | ||
function | function goToPage(value) { | ||
value = String(value || '').trim(); | |||
if ( | if (value) { | ||
window.location.href = mw.util.getUrl(value); | |||
} | |||
} | |||
var | function setup(container, form) { | ||
if (! | var input = container.querySelector('.pfFormInputWrapper input:not([type="hidden"])'); | ||
if (!input) return false; | |||
var lastTyped = ''; | |||
// Track what the user is actually typing keystroke-by-keystroke | |||
input.addEventListener('input', function () { | |||
lastTyped = input.value; | |||
}); | |||
// When autocomplete fills in a suggestion and the input loses focus, | |||
// "change" fires with a value different from what was last typed. | |||
input.addEventListener('change', function () { | |||
var current = input.value.trim(); | |||
if (current && current !== lastTyped.trim()) { | |||
}); | goToPage(current); | ||
} | |||
}); | |||
// Intercept form submission (Enter key / Go button) | |||
form.addEventListener('submit', function (e) { | |||
e.preventDefault(); | |||
e.stopPropagation(); | |||
goToPage(input.value); | |||
}); | |||
return true; | |||
} | |||
function init() { | |||
var container = document.getElementById('crt-quick-search'); | |||
if (!container) return; | |||
var form = container.querySelector('form'); | |||
if (!form) return; | |||
if (setup(container, form)) return; | |||
// Page Forms creates the input dynamically — wait for it | |||
var observer = new MutationObserver(function (mutations, obs) { | |||
if (setup(container, form)) obs.disconnect(); | |||
}); | |||
observer.observe(container, { childList: true, subtree: true }); | |||
setTimeout(function () { observer.disconnect(); }, 10000); | |||
} | } | ||