diff --git a/assets/script/search.ts b/assets/script/search.ts index 5ccd3d9..372a587 100644 --- a/assets/script/search.ts +++ b/assets/script/search.ts @@ -19,8 +19,7 @@ interface Bloodhound { local: T[]; } -function typeSearch(el: HTMLInputElement) { - const jqueryEl = $(el); +function typeSearch(jqueryEl: JQuery, search?: string) { const opts: Twitter.Typeahead.Options = { highlight: true, minLength: 0 @@ -47,13 +46,30 @@ function typeSearch(el: HTMLInputElement) { if (k.keyCode === 13) { // Enter key const selectables = jqueryEl.siblings(".tt-menu").find(".tt-selectable"); $(selectables[0]).trigger("click"); + } else { + updateSearch(jqueryEl.val()); } }); + if (search) { + jqueryEl.typeahead('val', search).typeahead('open'); + } + function navigate(record: MinifiedSearchRecord) { window.location.href = `https://www.npmjs.org/package/@types/${record.t}`; } + function updateSearch(newValue: string) { + if (!URLSearchParams) { + return; + } + + const params = new URLSearchParams(window.location.search); + params.set('search', newValue); + + history.pushState(null, '', `${window.location.pathname}?${params}`); + } + function createDataSource(): Bloodhound { let query = ""; return new Bloodhound({ @@ -83,3 +99,18 @@ function typeSearch(el: HTMLInputElement) { }); } } + +$(() => { + const params = window.location.search + .substring(1) + .split('&') + .reduce>((params, pair) => { + const [key, value] = pair.split('='); + params[key] = value; + return params; + }, {}); + const jqueryEl = $("#demo"); + + typeSearch(jqueryEl, params.search); + jqueryEl.focus(); +}); diff --git a/assets/static/index.html b/assets/static/index.html index 1d1167a..d0e6798 100644 --- a/assets/static/index.html +++ b/assets/static/index.html @@ -8,13 +8,6 @@ -

TypeSearch