diff --git a/.gitignore b/.gitignore
index 36170a7..18aabf6 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,2 +1,3 @@
node_modules
public
+gulpfile.js
\ No newline at end of file
diff --git a/README.md b/README.md
index ca701ee..7394f8a 100644
--- a/README.md
+++ b/README.md
@@ -1,11 +1,21 @@
-TODO: finish README
+## Deprecated
-Online [here](http://microsoft.github.io/TypeSearch).
+Replaced by https://www.typescriptlang.org/dt/search
-### Run locally
+---
-Run `gulp`, then see [localhost](http://localhost).
+A web-based search for `@types/*` packages.
-### Publish
+When using TypeScript an easy way to install library *Type Definitions* is using "@types" npm packages.
+
+View the running site: [http://microsoft.github.io/TypeSearch](http://microsoft.github.io/TypeSearch).
+### Run locally
+```bash
+npm i
+npm run build
+gulp serve --port 8080
+```
+
+### Publish
`gulp publish`
diff --git a/assets/script/search.js b/assets/script/search.js
new file mode 100644
index 0000000..2cd5f50
--- /dev/null
+++ b/assets/script/search.js
@@ -0,0 +1,56 @@
+var searchIndexUrl = "https://typespublisher.blob.core.windows.net/typespublisher/data/search-index-min.json";
+function typeSearch(el) {
+ var jqueryEl = $(el);
+ var opts = {
+ highlight: true,
+ minLength: 0
+ };
+ var data = {
+ source: createDataSource(),
+ displayKey: 't',
+ templates: {
+ suggestion: function (obj) {
+ return "
\n\t\t\t\t\t\t" + obj.t + "\n\t\t\t\t\t\t" + obj.l + "\n\t\t\t\t\t\t
";
+ }
+ }
+ };
+ jqueryEl.typeahead(opts, data);
+ jqueryEl.on('typeahead:select', function (unused, obj) { return navigate(obj); });
+ jqueryEl.keyup(function (k) {
+ if (k.keyCode === 13) {
+ var selectables = jqueryEl.siblings(".tt-menu").find(".tt-selectable");
+ $(selectables[0]).trigger("click");
+ }
+ });
+ function navigate(record) {
+ window.location.href = "https://www.npmjs.org/package/@types/" + record.t;
+ }
+ function createDataSource() {
+ var query = "";
+ return new Bloodhound({
+ // See https://github.com/twitter/typeahead.js/blob/master/doc/bloodhound.md#options
+ prefetch: searchIndexUrl,
+ datumTokenizer: function (entry) {
+ return [entry.l, entry.p, entry.t].concat(entry.g).concat(entry.m);
+ },
+ queryTokenizer: function (input) {
+ query = input;
+ return [input];
+ },
+ identify: function (e) { return e.t; },
+ sorter: function (x, y) {
+ // TODO: Include edit distance as additional weighting factor
+ // Direct matches should be ranked higher, else rank on basis of download count
+ if (x.t === query || x.t === (query + "js") || x.t === (query + ".js") || x.t === (query + "-js")) {
+ return -1;
+ }
+ else if (y.t === query || y.t === (query + "js") || y.t === (query + ".js") || y.t === (query + "-js")) {
+ return 1;
+ }
+ else {
+ return y.d - x.d;
+ }
+ }
+ });
+ }
+}
diff --git a/assets/script/search.ts b/assets/script/search.ts
index 10bee51..372a587 100644
--- a/assets/script/search.ts
+++ b/assets/script/search.ts
@@ -1,8 +1,3 @@
-interface Window {
- appInsights: any;
-}
-const ai = window.appInsights;
-
const searchIndexUrl = "https://typespublisher.blob.core.windows.net/typespublisher/data/search-index-min.json";
interface MinifiedSearchRecord {
@@ -24,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
@@ -36,10 +30,12 @@ function typeSearch(el: HTMLInputElement) {
displayKey: 't',
templates: {
suggestion: (obj: MinifiedSearchRecord) => {
- return `
+ return `
+
${obj.t}
${obj.l}
-
`
+
+ `;
}
}
};
@@ -50,19 +46,28 @@ 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());
}
});
- jqueryEl.focus(() => {
- ai.trackEvent('focus');
- });
+ if (search) {
+ jqueryEl.typeahead('val', search).typeahead('open');
+ }
function navigate(record: MinifiedSearchRecord) {
- if (ai) {
- ai.trackEvent('navigate', { target: record.t });
+ window.location.href = `https://www.npmjs.org/package/@types/${record.t}`;
+ }
+
+ function updateSearch(newValue: string) {
+ if (!URLSearchParams) {
+ return;
}
- window.location.href = `https://www.npmjs.org/package/@types/${record.t}`;
+ const params = new URLSearchParams(window.location.search);
+ params.set('search', newValue);
+
+ history.pushState(null, '', `${window.location.pathname}?${params}`);
}
function createDataSource(): Bloodhound {
@@ -70,7 +75,7 @@ function typeSearch(el: HTMLInputElement) {
return new Bloodhound({
// See https://github.com/twitter/typeahead.js/blob/master/doc/bloodhound.md#options
prefetch: searchIndexUrl,
- datumTokenizer: entry => {
+ datumTokenizer: (entry: MinifiedSearchRecord) => {
return [entry.l, entry.p, entry.t].concat(entry.g).concat(entry.m);
},
queryTokenizer: input => {
@@ -84,13 +89,28 @@ function typeSearch(el: HTMLInputElement) {
if (x.t === query || x.t === (query + "js") || x.t === (query + ".js") || x.t === (query + "-js")) {
return -1;
}
- else if (y.t === query || y.t === (query + "js") || y.t === (query + ".js") || y.t === (query + "-js")) {
+
+ if (y.t === query || y.t === (query + "js") || y.t === (query + ".js") || y.t === (query + "-js")) {
return 1;
}
- else {
- return y.d - x.d;
- }
+
+ return y.d - x.d;
}
});
}
}
+
+$(() => {
+ 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/demo.css b/assets/static/demo.css
deleted file mode 100644
index 77ce0a1..0000000
--- a/assets/static/demo.css
+++ /dev/null
@@ -1,31 +0,0 @@
-body {
- margin: 0;
- text-align: center;
- font-family: 'Palatino Linotype';
- font-size: 24px;
- line-height: 1.4;
- background: 'red';
- position: relative;
- padding-top: 1em;
-}
-
-.typescript, a, a:visited {
- color: #294E80;
-}
-
-h1 {
- margin-bottom: 1.4em;
-}
-
-.footer {
- font-size: 80%;
- position: fixed;
- bottom: 12px;
- left: 0px;
- right: 0px;
- text-align: center;
-}
-
-#demo {
- width: 20em;
-}
\ No newline at end of file
diff --git a/assets/static/index.html b/assets/static/index.html
index 684ab98..c56b2f4 100644
--- a/assets/static/index.html
+++ b/assets/static/index.html
@@ -4,29 +4,32 @@
TypeScript Types Search
-
-
-
+
TypeSearch
-
-
-
+
-
+
-

+
+
+