Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Jan 19, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 33 additions & 2 deletions assets/script/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ interface Bloodhound<T> {
local: T[];
}

function typeSearch(el: HTMLInputElement) {
const jqueryEl = $(el);
function typeSearch(jqueryEl: JQuery, search?: string) {
const opts: Twitter.Typeahead.Options = {
highlight: true,
minLength: 0
Expand All @@ -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<MinifiedSearchRecord> {
let query = "";
return new Bloodhound<MinifiedSearchRecord>({
Expand Down Expand Up @@ -83,3 +99,18 @@ function typeSearch(el: HTMLInputElement) {
});
}
}

$(() => {
const params = window.location.search
.substring(1)
.split('&')
.reduce<Record<string, string>>((params, pair) => {
const [key, value] = pair.split('=');
params[key] = value;
return params;
}, {});
const jqueryEl = $("#demo");

typeSearch(jqueryEl, params.search);
jqueryEl.focus();
});
7 changes: 0 additions & 7 deletions assets/static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,6 @@
<script src="lib/jquery.min.js"></script>
<script src="lib/typeahead.bundle.min.js"></script>
<script src="script/search.js"></script>
<script>
$(function() {
var el = document.getElementById('demo');
typeSearch(el);
$(el).focus();
});
</script>
</head>
<body>
<h1><span class="typescript"><span class="bold">Type</span>Search</span></h1>
Expand Down