From a94a38a68f9d670b46ba23f9c5af67018fedd7d8 Mon Sep 17 00:00:00 2001 From: igochkov Date: Tue, 12 Jan 2016 23:38:11 +0100 Subject: [PATCH 1/8] Typeahead constructor and events signitures changed to reflect latest 0.11.1 typeahead documentation --- typeahead/typeahead.d.ts | 842 ++++++++++++++++++++++++++++++++------- 1 file changed, 700 insertions(+), 142 deletions(-) diff --git a/typeahead/typeahead.d.ts b/typeahead/typeahead.d.ts index 8164d430e..aa1db2b44 100644 --- a/typeahead/typeahead.d.ts +++ b/typeahead/typeahead.d.ts @@ -6,148 +6,706 @@ /// interface JQuery { - - /** - * Destroys previously initialized typeaheads. This entails reverting - * DOM modifications and removing event handlers. - * - * @constructor - * @param methodName Method 'destroy' - */ - typeahead(methodName: 'destroy'): JQuery; - - /** - * Opens the dropdown menu of typeahead. Note that being open does not mean that the menu is visible. - * The menu is only visible when it is open and has content. - * - * @constructor - * @param methodName Method 'open' - */ - typeahead(methodName: 'open'): JQuery; - - /** - * Closes the dropdown menu of typeahead. - * - * @constructor - * @param methodName Method 'close' - */ - typeahead(methodName: 'close'): JQuery; - - /** - * Returns the current value of the typeahead. - * The value is the text the user has entered into the input element. - * - * @constructor - * @param methodName Method 'val' - */ - typeahead(methodName: 'val'): string; - - /** - * Sets the value of the typeahead. This should be used in place of jQuery#val. - * - * @constructor - * @param methodName Method 'val' - * @param query The value to be set - */ - typeahead(methodName: 'val', val: string): JQuery; - - /** - * Accommodates the val overload. - * - * @constructor - * @param methodName Method name ('val') - */ - typeahead(methodName: string): string; - - - /** - * Accommodates multiple overloads. - * - * @constructor - * @param methodName Method name - * @param query The query to be set in case method 'val' is used. - */ - typeahead(methodName: string, query: string): JQuery; - - /** - * Accomodates specifying options such as hint and highlight. - * This is in correspondence to the examples mentioned in http://twitter.github.io/typeahead.js/examples/ - * - * @constructor - * @param options ('hint' or 'highlight' or 'minLength' all of which are optional) - * @param datasets Array of datasets - */ - typeahead(options: Twitter.Typeahead.Options, datasets: Twitter.Typeahead.Dataset[]): JQuery; - - /** - * Accomodates specifying options such as hint and highlight. - * This is in correspondence to the examples mentioned in http://twitter.github.io/typeahead.js/examples/ - * - * @constructor - * @param options ('hint' or 'highlight' or 'minLength' all of which are optional) - * @param datasets One or more datasets passed in as arguments. - */ - typeahead(options: Twitter.Typeahead.Options, ...datasets: Twitter.Typeahead.Dataset[]): JQuery; - - on(events: "typeahead:active", selector: string, data: any, handler: (ev: JQueryEventObject) => any): JQuery; - on(events: "typeahead:active", selector: string, handler: (ev: JQueryEventObject) => any): JQuery; - on(events: "typeahead:active", handler: (ev: JQueryEventObject) => any): JQuery; - off(events: "typeahead:active", handler: (ev: JQueryEventObject) => any): JQuery; - - on(events: "typeahead:idle", selector: string, data: any, handler: (ev: JQueryEventObject) => any): JQuery; - on(events: "typeahead:idle", selector: string, handler: (ev: JQueryEventObject) => any): JQuery; - on(events: "typeahead:idle", handler: (ev: JQueryEventObject) => any): JQuery; - off(events: "typeahead:idle", handler: (ev: JQueryEventObject) => any): JQuery; - - on(events: "typeahead:open", selector: string, data: any, handler: (ev: JQueryEventObject) => any): JQuery; - on(events: "typeahead:open", selector: string, handler: (ev: JQueryEventObject) => any): JQuery; - on(events: "typeahead:open", handler: (ev: JQueryEventObject) => any): JQuery; - off(events: "typeahead:open", handler: (ev: JQueryEventObject) => any): JQuery; - - on(events: "typeahead:close", selector: string, data: any, handler: (ev: JQueryEventObject) => any): JQuery; - on(events: "typeahead:close", selector: string, handler: (ev: JQueryEventObject) => any): JQuery; - on(events: "typeahead:close", handler: (ev: JQueryEventObject) => any): JQuery; - off(events: "typeahead:close", handler: (ev: JQueryEventObject) => any): JQuery; - - on(events: "typeahead:change", selector: string, data: any, handler: (ev: JQueryEventObject) => any): JQuery; - on(events: "typeahead:change", selector: string, handler: (ev: JQueryEventObject) => any): JQuery; - on(events: "typeahead:change", handler: (ev: JQueryEventObject) => any): JQuery; - off(events: "typeahead:change", handler: (ev: JQueryEventObject) => any): JQuery; - - on(events: "typeahead:render", selector: string, data: any, handler: (ev: JQueryEventObject, suggestions: Array, async: boolean, datasetName: string) => any): JQuery; - on(events: "typeahead:render", selector: string, handler: (ev: JQueryEventObject, suggestions: Array, async: boolean, datasetName: string) => any): JQuery; - on(events: "typeahead:render", handler: (ev: JQueryEventObject, suggestions: Array, async: boolean, datasetName: string) => any): JQuery; - off(events: "typeahead:render", handler: (ev: JQueryEventObject, suggestions: Array, async: boolean, datasetName: string) => any): JQuery; - - on(events: "typeahead:select", selector: string, data: any, handler: (ev: JQueryEventObject, suggestion: any) => any): JQuery; - on(events: "typeahead:select", selector: string, handler: (ev: JQueryEventObject, suggestion: any) => any): JQuery; - on(events: "typeahead:select", handler: (ev: JQueryEventObject, suggestion: any) => any): JQuery; - off(events: "typeahead:select", handler: (ev: JQueryEventObject, suggestion: any) => any): JQuery; - - on(events: "typeahead:autocomplete", selector: string, data: any, handler: (ev: JQueryEventObject, suggestion: any) => any): JQuery; - on(events: "typeahead:autocomplete", selector: string, handler: (ev: JQueryEventObject, suggestion: any) => any): JQuery; - on(events: "typeahead:autocomplete", handler: (ev: JQueryEventObject, suggestion: any) => any): JQuery; - off(events: "typeahead:autocomplete", handler: (ev: JQueryEventObject, suggestion: any) => any): JQuery; - - on(events: "typeahead:cursorchange", selector: string, data: any, handler: (ev: JQueryEventObject, suggestion: any) => any): JQuery; - on(events: "typeahead:cursorchange", selector: string, handler: (ev: JQueryEventObject, suggestion: any) => any): JQuery; - on(events: "typeahead:cursorchange", handler: (ev: JQueryEventObject, suggestion: any) => any): JQuery; - off(events: "typeahead:cursorchange", handler: (ev: JQueryEventObject, suggestion: any) => any): JQuery; - - on(events: "typeahead:asyncrequest", selector: string, data: any, handler: (ev: JQueryEventObject, query: string, datasetName: string) => any): JQuery; - on(events: "typeahead:asyncrequest", selector: string, handler: (ev: JQueryEventObject, query: string, datasetName: string) => any): JQuery; - on(events: "typeahead:asyncrequest", handler: (ev: JQueryEventObject, query: string, datasetName: string) => any): JQuery; - off(events: "typeahead:asyncrequest", handler: (ev: JQueryEventObject, query: string, datasetName: string) => any): JQuery; - - on(events: "typeahead:asynccancel", selector: string, data: any, handler: (ev: JQueryEventObject, query: string, datasetName: string) => any): JQuery; - on(events: "typeahead:asynccancel", selector: string, handler: (ev: JQueryEventObject, query: string, datasetName: string) => any): JQuery; - on(events: "typeahead:asynccancel", handler: (ev: JQueryEventObject, query: string, datasetName: string) => any): JQuery; - off(events: "typeahead:asynccancel", handler: (ev: JQueryEventObject, query: string, datasetName: string) => any): JQuery; - - on(events: "typeahead:asyncreceive", selector: string, data: any, handler: (ev: JQueryEventObject, query: string, datasetName: string) => any): JQuery; - on(events: "typeahead:asyncreceive", selector: string, handler: (ev: JQueryEventObject, query: string, datasetName: string) => any): JQuery; - on(events: "typeahead:asyncreceive", handler: (ev: JQueryEventObject, query: string, datasetName: string) => any): JQuery; - off(events: "typeahead:asyncreceive", handler: (ev: JQueryEventObject, query: string, datasetName: string) => any): JQuery; + /** + * For a given input[type="text"], enables typeahead functionality. + * + * @constructor + * @param options Options hash that's used for configuration + * @param datasets Array of datasets + */ + typeahead(options: Twitter.Typeahead.Options, datasets: Twitter.Typeahead.Dataset[]): JQuery; + + /** + * For a given input[type="text"], enables typeahead functionality. + * + * @constructor + * @param options Options hash that's used for configuration + * @param datasets One or more datasets passed as rest parameters. + */ + typeahead(options: Twitter.Typeahead.Options, ...datasets: Twitter.Typeahead.Dataset[]): JQuery; + + /** + * Returns the current value of the typeahead. + * The value is the text the user has entered into the input element. + * + * @constructor + * @param methodName Method 'val' + */ + typeahead(methodName: 'val'): string; + + /** + * Accommodates the val overload. + * + * @constructor + * @param methodName Method 'val' + */ + typeahead(methodName: string): string; + + /** + * Sets the value of the typeahead. This should be used in place of jQuery#val. + * + * @constructor + * @param methodName Method 'val' + * @param val The value to be set + */ + typeahead(methodName: 'val', val: string): JQuery; + + /** + * Accommodates the set val overload. + * + * @constructor + * @param methodName Method 'val' + * @param val The value to be set + */ + typeahead(methodName: string, val: string): JQuery; + + /** + * Opens the suggestion menu. + * + * @constructor + * @param methodName Method 'open' + */ + typeahead(methodName: 'open'): JQuery; + + /** + * Closes the suggestion menu. + * + * @constructor + * @param methodName Method 'close' + */ + typeahead(methodName: 'close'): JQuery; + + /** + * Removes typeahead functionality and reverts the input element back to its original state. + * + * @constructor + * @param methodName Method 'destroy' + */ + typeahead(methodName: 'destroy'): JQuery; + + /** + * Attach an event handler function for typeahead:active event to the selected elements. + * + * @param events typeahead:active event fired when the typeahead moves to active state. + * @param handler A function to execute when the event is triggered. The value false is also allowed as a shorthand for a function that simply does return false. Rest parameter args is for optional parameters passed to jQuery.trigger(). Note that the actual parameters on the event handler function must be marked as optional (? syntax). + */ + on(events: "typeahead:active", handler: (eventObject: JQueryEventObject, ...args: any[]) => any): JQuery; + + /** + * Attach an event handler function for typeahead:active event to the selected elements. + * + * @param events typeahead:active event fired when the typeahead moves to active state. + * @param data Data to be passed to the handler in event.data when an event is triggered. + * @param handler A function to execute when the event is triggered. The value false is also allowed as a shorthand for a function that simply does return false. + */ + on(events: "typeahead:active", data: any, handler: (eventObject: JQueryEventObject, ...args: any[]) => any): JQuery; + + /** + * Attach an event handler function for typeahead:active event to the selected elements. + * + * @param events typeahead:active event fired when the typeahead moves to active state. + * @param selector A selector string to filter the descendants of the selected elements that trigger the event. If the selector is null or omitted, the event is always triggered when it reaches the selected element. + * @param handler A function to execute when the event is triggered. The value false is also allowed as a shorthand for a function that simply does return false. + */ + on(events: "typeahead:active", selector: string, handler: (eventObject: JQueryEventObject, ...eventData: any[]) => any): JQuery; + + /** + * Attach an event handler function for typeahead:active event to the selected elements. + * + * @param events typeahead:active event fired when the typeahead moves to active state. + * @param selector A selector string to filter the descendants of the selected elements that trigger the event. If the selector is null or omitted, the event is always triggered when it reaches the selected element. + * @param data Data to be passed to the handler in event.data when an event is triggered. + * @param handler A function to execute when the event is triggered. The value false is also allowed as a shorthand for a function that simply does return false. + */ + on(events: "typeahead:active", selector: string, data: any, handler: (eventObject: JQueryEventObject, ...eventData: any[]) => any): JQuery; + + /** + * Attach an event handler function for typeahead:idle event to the selected elements. + * + * @param events typeahead:idle event fired when the typeahead moves to active state. + * @param handler A function to execute when the event is triggered. The value false is also allowed as a shorthand for a function that simply does return false. Rest parameter args is for optional parameters passed to jQuery.trigger(). Note that the actual parameters on the event handler function must be marked as optional (? syntax). + */ + on(events: "typeahead:idle", handler: (eventObject: JQueryEventObject, ...args: any[]) => any): JQuery; + + /** + * Attach an event handler function for typeahead:idle event to the selected elements. + * + * @param events typeahead:idle event fired when the typeahead moves to active state. + * @param data Data to be passed to the handler in event.data when an event is triggered. + * @param handler A function to execute when the event is triggered. The value false is also allowed as a shorthand for a function that simply does return false. + */ + on(events: "typeahead:idle", data: any, handler: (eventObject: JQueryEventObject, ...args: any[]) => any): JQuery; + + /** + * Attach an event handler function for typeahead:idle event to the selected elements. + * + * @param events typeahead:idle event fired when the typeahead moves to active state. + * @param selector A selector string to filter the descendants of the selected elements that trigger the event. If the selector is null or omitted, the event is always triggered when it reaches the selected element. + * @param handler A function to execute when the event is triggered. The value false is also allowed as a shorthand for a function that simply does return false. + */ + on(events: "typeahead:idle", selector: string, handler: (eventObject: JQueryEventObject, ...eventData: any[]) => any): JQuery; + + /** + * Attach an event handler function for typeahead:idle event to the selected elements. + * + * @param events typeahead:idle event fired when the typeahead moves to active state. + * @param selector A selector string to filter the descendants of the selected elements that trigger the event. If the selector is null or omitted, the event is always triggered when it reaches the selected element. + * @param data Data to be passed to the handler in event.data when an event is triggered. + * @param handler A function to execute when the event is triggered. The value false is also allowed as a shorthand for a function that simply does return false. + */ + on(events: "typeahead:idle", selector: string, data: any, handler: (eventObject: JQueryEventObject, ...eventData: any[]) => any): JQuery; + + /** + * Attach an event handler function for typeahead:open event to the selected elements. + * + * @param events typeahead:open event fired when the typeahead moves to active state. + * @param handler A function to execute when the event is triggered. The value false is also allowed as a shorthand for a function that simply does return false. Rest parameter args is for optional parameters passed to jQuery.trigger(). Note that the actual parameters on the event handler function must be marked as optional (? syntax). + */ + on(events: "typeahead:open", handler: (eventObject: JQueryEventObject, ...args: any[]) => any): JQuery; + + /** + * Attach an event handler function for typeahead:open event to the selected elements. + * + * @param events typeahead:open event fired when the typeahead moves to active state. + * @param data Data to be passed to the handler in event.data when an event is triggered. + * @param handler A function to execute when the event is triggered. The value false is also allowed as a shorthand for a function that simply does return false. + */ + on(events: "typeahead:open", data: any, handler: (eventObject: JQueryEventObject, ...args: any[]) => any): JQuery; + + /** + * Attach an event handler function for typeahead:open event to the selected elements. + * + * @param events typeahead:open event fired when the typeahead moves to active state. + * @param selector A selector string to filter the descendants of the selected elements that trigger the event. If the selector is null or omitted, the event is always triggered when it reaches the selected element. + * @param handler A function to execute when the event is triggered. The value false is also allowed as a shorthand for a function that simply does return false. + */ + on(events: "typeahead:open", selector: string, handler: (eventObject: JQueryEventObject, ...eventData: any[]) => any): JQuery; + + /** + * Attach an event handler function for typeahead:open event to the selected elements. + * + * @param events typeahead:open event fired when the typeahead moves to active state. + * @param selector A selector string to filter the descendants of the selected elements that trigger the event. If the selector is null or omitted, the event is always triggered when it reaches the selected element. + * @param data Data to be passed to the handler in event.data when an event is triggered. + * @param handler A function to execute when the event is triggered. The value false is also allowed as a shorthand for a function that simply does return false. + */ + on(events: "typeahead:open", selector: string, data: any, handler: (eventObject: JQueryEventObject, ...eventData: any[]) => any): JQuery; + + /** + * Attach an event handler function for typeahead:close event to the selected elements. + * + * @param events typeahead:close event fired when the typeahead moves to active state. + * @param handler A function to execute when the event is triggered. The value false is also allowed as a shorthand for a function that simply does return false. Rest parameter args is for optional parameters passed to jQuery.trigger(). Note that the actual parameters on the event handler function must be marked as optional (? syntax). + */ + on(events: "typeahead:close", handler: (eventObject: JQueryEventObject, ...args: any[]) => any): JQuery; + + /** + * Attach an event handler function for typeahead:close event to the selected elements. + * + * @param events typeahead:close event fired when the typeahead moves to active state. + * @param data Data to be passed to the handler in event.data when an event is triggered. + * @param handler A function to execute when the event is triggered. The value false is also allowed as a shorthand for a function that simply does return false. + */ + on(events: "typeahead:close", data: any, handler: (eventObject: JQueryEventObject, ...args: any[]) => any): JQuery; + + /** + * Attach an event handler function for typeahead:close event to the selected elements. + * + * @param events typeahead:close event fired when the typeahead moves to active state. + * @param selector A selector string to filter the descendants of the selected elements that trigger the event. If the selector is null or omitted, the event is always triggered when it reaches the selected element. + * @param handler A function to execute when the event is triggered. The value false is also allowed as a shorthand for a function that simply does return false. + */ + on(events: "typeahead:close", selector: string, handler: (eventObject: JQueryEventObject, ...eventData: any[]) => any): JQuery; + + /** + * Attach an event handler function for typeahead:close event to the selected elements. + * + * @param events typeahead:close event fired when the typeahead moves to active state. + * @param selector A selector string to filter the descendants of the selected elements that trigger the event. If the selector is null or omitted, the event is always triggered when it reaches the selected element. + * @param data Data to be passed to the handler in event.data when an event is triggered. + * @param handler A function to execute when the event is triggered. The value false is also allowed as a shorthand for a function that simply does return false. + */ + on(events: "typeahead:close", selector: string, data: any, handler: (eventObject: JQueryEventObject, ...eventData: any[]) => any): JQuery; + + /** + * Attach an event handler function for typeahead:change event to the selected elements. + * + * @param events typeahead:change event fired when the typeahead moves to active state. + * @param handler A function to execute when the event is triggered. The value false is also allowed as a shorthand for a function that simply does return false. Rest parameter args is for optional parameters passed to jQuery.trigger(). Note that the actual parameters on the event handler function must be marked as optional (? syntax). + */ + on(events: "typeahead:change", handler: (eventObject: JQueryEventObject, ...args: any[]) => any): JQuery; + + /** + * Attach an event handler function for typeahead:change event to the selected elements. + * + * @param events typeahead:change event fired when the typeahead moves to active state. + * @param data Data to be passed to the handler in event.data when an event is triggered. + * @param handler A function to execute when the event is triggered. The value false is also allowed as a shorthand for a function that simply does return false. + */ + on(events: "typeahead:change", data: any, handler: (eventObject: JQueryEventObject, ...args: any[]) => any): JQuery; + + /** + * Attach an event handler function for typeahead:change event to the selected elements. + * + * @param events typeahead:change event fired when the typeahead moves to active state. + * @param selector A selector string to filter the descendants of the selected elements that trigger the event. If the selector is null or omitted, the event is always triggered when it reaches the selected element. + * @param handler A function to execute when the event is triggered. The value false is also allowed as a shorthand for a function that simply does return false. + */ + on(events: "typeahead:change", selector: string, handler: (eventObject: JQueryEventObject, ...eventData: any[]) => any): JQuery; + + /** + * Attach an event handler function for typeahead:change event to the selected elements. + * + * @param events typeahead:change event fired when the typeahead moves to active state. + * @param selector A selector string to filter the descendants of the selected elements that trigger the event. If the selector is null or omitted, the event is always triggered when it reaches the selected element. + * @param data Data to be passed to the handler in event.data when an event is triggered. + * @param handler A function to execute when the event is triggered. The value false is also allowed as a shorthand for a function that simply does return false. + */ + on(events: "typeahead:change", selector: string, data: any, handler: (eventObject: JQueryEventObject, ...eventData: any[]) => any): JQuery; + + /** + * Attach an event handler function for typeahead:render event to the selected elements. + * + * @param events typeahead:render event fired when the typeahead moves to active state. + * @param handler A function to execute when the event is triggered. The value false is also allowed as a shorthand for a function that simply does return false. Rest parameter args is for optional parameters passed to jQuery.trigger(). Note that the actual parameters on the event handler function must be marked as optional (? syntax). + */ + on(events: "typeahead:render", handler: (eventObject: JQueryEventObject, ...args: any[]) => any): JQuery; + + /** + * Attach an event handler function for typeahead:render event to the selected elements. + * + * @param events typeahead:render event fired when the typeahead moves to active state. + * @param data Data to be passed to the handler in event.data when an event is triggered. + * @param handler A function to execute when the event is triggered. The value false is also allowed as a shorthand for a function that simply does return false. + */ + on(events: "typeahead:render", data: any, handler: (eventObject: JQueryEventObject, ...args: any[]) => any): JQuery; + + /** + * Attach an event handler function for typeahead:render event to the selected elements. + * + * @param events typeahead:render event fired when the typeahead moves to active state. + * @param selector A selector string to filter the descendants of the selected elements that trigger the event. If the selector is null or omitted, the event is always triggered when it reaches the selected element. + * @param handler A function to execute when the event is triggered. The value false is also allowed as a shorthand for a function that simply does return false. + */ + on(events: "typeahead:render", selector: string, handler: (eventObject: JQueryEventObject, ...eventData: any[]) => any): JQuery; + + /** + * Attach an event handler function for typeahead:render event to the selected elements. + * + * @param events typeahead:render event fired when the typeahead moves to active state. + * @param selector A selector string to filter the descendants of the selected elements that trigger the event. If the selector is null or omitted, the event is always triggered when it reaches the selected element. + * @param data Data to be passed to the handler in event.data when an event is triggered. + * @param handler A function to execute when the event is triggered. The value false is also allowed as a shorthand for a function that simply does return false. + */ + on(events: "typeahead:render", selector: string, data: any, handler: (eventObject: JQueryEventObject, ...eventData: any[]) => any): JQuery; + + /** + * Attach an event handler function for typeahead:select event to the selected elements. + * + * @param events typeahead:select event fired when the typeahead moves to active state. + * @param handler A function to execute when the event is triggered. The value false is also allowed as a shorthand for a function that simply does return false. Rest parameter args is for optional parameters passed to jQuery.trigger(). Note that the actual parameters on the event handler function must be marked as optional (? syntax). + */ + on(events: "typeahead:select", handler: (eventObject: JQueryEventObject, ...args: any[]) => any): JQuery; + + /** + * Attach an event handler function for typeahead:select event to the selected elements. + * + * @param events typeahead:select event fired when the typeahead moves to active state. + * @param data Data to be passed to the handler in event.data when an event is triggered. + * @param handler A function to execute when the event is triggered. The value false is also allowed as a shorthand for a function that simply does return false. + */ + on(events: "typeahead:select", data: any, handler: (eventObject: JQueryEventObject, ...args: any[]) => any): JQuery; + + /** + * Attach an event handler function for typeahead:select event to the selected elements. + * + * @param events typeahead:select event fired when the typeahead moves to active state. + * @param selector A selector string to filter the descendants of the selected elements that trigger the event. If the selector is null or omitted, the event is always triggered when it reaches the selected element. + * @param handler A function to execute when the event is triggered. The value false is also allowed as a shorthand for a function that simply does return false. + */ + on(events: "typeahead:select", selector: string, handler: (eventObject: JQueryEventObject, ...eventData: any[]) => any): JQuery; + + /** + * Attach an event handler function for typeahead:select event to the selected elements. + * + * @param events typeahead:select event fired when the typeahead moves to active state. + * @param selector A selector string to filter the descendants of the selected elements that trigger the event. If the selector is null or omitted, the event is always triggered when it reaches the selected element. + * @param data Data to be passed to the handler in event.data when an event is triggered. + * @param handler A function to execute when the event is triggered. The value false is also allowed as a shorthand for a function that simply does return false. + */ + on(events: "typeahead:select", selector: string, data: any, handler: (eventObject: JQueryEventObject, ...eventData: any[]) => any): JQuery; + + /** + * Attach an event handler function for typeahead:autocomplete event to the selected elements. + * + * @param events typeahead:autocomplete event fired when the typeahead moves to active state. + * @param handler A function to execute when the event is triggered. The value false is also allowed as a shorthand for a function that simply does return false. Rest parameter args is for optional parameters passed to jQuery.trigger(). Note that the actual parameters on the event handler function must be marked as optional (? syntax). + */ + on(events: "typeahead:autocomplete", handler: (eventObject: JQueryEventObject, ...args: any[]) => any): JQuery; + + /** + * Attach an event handler function for typeahead:autocomplete event to the selected elements. + * + * @param events typeahead:autocomplete event fired when the typeahead moves to active state. + * @param data Data to be passed to the handler in event.data when an event is triggered. + * @param handler A function to execute when the event is triggered. The value false is also allowed as a shorthand for a function that simply does return false. + */ + on(events: "typeahead:autocomplete", data: any, handler: (eventObject: JQueryEventObject, ...args: any[]) => any): JQuery; + + /** + * Attach an event handler function for typeahead:autocomplete event to the selected elements. + * + * @param events typeahead:autocomplete event fired when the typeahead moves to active state. + * @param selector A selector string to filter the descendants of the selected elements that trigger the event. If the selector is null or omitted, the event is always triggered when it reaches the selected element. + * @param handler A function to execute when the event is triggered. The value false is also allowed as a shorthand for a function that simply does return false. + */ + on(events: "typeahead:autocomplete", selector: string, handler: (eventObject: JQueryEventObject, ...eventData: any[]) => any): JQuery; + + /** + * Attach an event handler function for typeahead:autocomplete event to the selected elements. + * + * @param events typeahead:autocomplete event fired when the typeahead moves to active state. + * @param selector A selector string to filter the descendants of the selected elements that trigger the event. If the selector is null or omitted, the event is always triggered when it reaches the selected element. + * @param data Data to be passed to the handler in event.data when an event is triggered. + * @param handler A function to execute when the event is triggered. The value false is also allowed as a shorthand for a function that simply does return false. + */ + on(events: "typeahead:autocomplete", selector: string, data: any, handler: (eventObject: JQueryEventObject, ...eventData: any[]) => any): JQuery; + + /** + * Attach an event handler function for typeahead:cursorchange event to the selected elements. + * + * @param events typeahead:cursorchange event fired when the typeahead moves to active state. + * @param handler A function to execute when the event is triggered. The value false is also allowed as a shorthand for a function that simply does return false. Rest parameter args is for optional parameters passed to jQuery.trigger(). Note that the actual parameters on the event handler function must be marked as optional (? syntax). + */ + on(events: "typeahead:cursorchange", handler: (eventObject: JQueryEventObject, ...args: any[]) => any): JQuery; + + /** + * Attach an event handler function for typeahead:cursorchange event to the selected elements. + * + * @param events typeahead:cursorchange event fired when the typeahead moves to active state. + * @param data Data to be passed to the handler in event.data when an event is triggered. + * @param handler A function to execute when the event is triggered. The value false is also allowed as a shorthand for a function that simply does return false. + */ + on(events: "typeahead:cursorchange", data: any, handler: (eventObject: JQueryEventObject, ...args: any[]) => any): JQuery; + + /** + * Attach an event handler function for typeahead:cursorchange event to the selected elements. + * + * @param events typeahead:cursorchange event fired when the typeahead moves to active state. + * @param selector A selector string to filter the descendants of the selected elements that trigger the event. If the selector is null or omitted, the event is always triggered when it reaches the selected element. + * @param handler A function to execute when the event is triggered. The value false is also allowed as a shorthand for a function that simply does return false. + */ + on(events: "typeahead:cursorchange", selector: string, handler: (eventObject: JQueryEventObject, ...eventData: any[]) => any): JQuery; + + /** + * Attach an event handler function for typeahead:cursorchange event to the selected elements. + * + * @param events typeahead:cursorchange event fired when the typeahead moves to active state. + * @param selector A selector string to filter the descendants of the selected elements that trigger the event. If the selector is null or omitted, the event is always triggered when it reaches the selected element. + * @param data Data to be passed to the handler in event.data when an event is triggered. + * @param handler A function to execute when the event is triggered. The value false is also allowed as a shorthand for a function that simply does return false. + */ + on(events: "typeahead:cursorchange", selector: string, data: any, handler: (eventObject: JQueryEventObject, ...eventData: any[]) => any): JQuery; + + /** + * Attach an event handler function for typeahead:asyncrequest event to the selected elements. + * + * @param events typeahead:asyncrequest event fired when the typeahead moves to active state. + * @param handler A function to execute when the event is triggered. The value false is also allowed as a shorthand for a function that simply does return false. Rest parameter args is for optional parameters passed to jQuery.trigger(). Note that the actual parameters on the event handler function must be marked as optional (? syntax). + */ + on(events: "typeahead:asyncrequest", handler: (eventObject: JQueryEventObject, ...args: any[]) => any): JQuery; + + /** + * Attach an event handler function for typeahead:asyncrequest event to the selected elements. + * + * @param events typeahead:asyncrequest event fired when the typeahead moves to active state. + * @param data Data to be passed to the handler in event.data when an event is triggered. + * @param handler A function to execute when the event is triggered. The value false is also allowed as a shorthand for a function that simply does return false. + */ + on(events: "typeahead:asyncrequest", data: any, handler: (eventObject: JQueryEventObject, ...args: any[]) => any): JQuery; + + /** + * Attach an event handler function for typeahead:asyncrequest event to the selected elements. + * + * @param events typeahead:asyncrequest event fired when the typeahead moves to active state. + * @param selector A selector string to filter the descendants of the selected elements that trigger the event. If the selector is null or omitted, the event is always triggered when it reaches the selected element. + * @param handler A function to execute when the event is triggered. The value false is also allowed as a shorthand for a function that simply does return false. + */ + on(events: "typeahead:asyncrequest", selector: string, handler: (eventObject: JQueryEventObject, ...eventData: any[]) => any): JQuery; + + /** + * Attach an event handler function for typeahead:asyncrequest event to the selected elements. + * + * @param events typeahead:asyncrequest event fired when the typeahead moves to active state. + * @param selector A selector string to filter the descendants of the selected elements that trigger the event. If the selector is null or omitted, the event is always triggered when it reaches the selected element. + * @param data Data to be passed to the handler in event.data when an event is triggered. + * @param handler A function to execute when the event is triggered. The value false is also allowed as a shorthand for a function that simply does return false. + */ + on(events: "typeahead:asyncrequest", selector: string, data: any, handler: (eventObject: JQueryEventObject, ...eventData: any[]) => any): JQuery; + + /** + * Attach an event handler function for typeahead:asynccancel event to the selected elements. + * + * @param events typeahead:asynccancel event fired when the typeahead moves to active state. + * @param handler A function to execute when the event is triggered. The value false is also allowed as a shorthand for a function that simply does return false. Rest parameter args is for optional parameters passed to jQuery.trigger(). Note that the actual parameters on the event handler function must be marked as optional (? syntax). + */ + on(events: "typeahead:asynccancel", handler: (eventObject: JQueryEventObject, ...args: any[]) => any): JQuery; + + /** + * Attach an event handler function for typeahead:asynccancel event to the selected elements. + * + * @param events typeahead:asynccancel event fired when the typeahead moves to active state. + * @param data Data to be passed to the handler in event.data when an event is triggered. + * @param handler A function to execute when the event is triggered. The value false is also allowed as a shorthand for a function that simply does return false. + */ + on(events: "typeahead:asynccancel", data: any, handler: (eventObject: JQueryEventObject, ...args: any[]) => any): JQuery; + + /** + * Attach an event handler function for typeahead:asynccancel event to the selected elements. + * + * @param events typeahead:asynccancel event fired when the typeahead moves to active state. + * @param selector A selector string to filter the descendants of the selected elements that trigger the event. If the selector is null or omitted, the event is always triggered when it reaches the selected element. + * @param handler A function to execute when the event is triggered. The value false is also allowed as a shorthand for a function that simply does return false. + */ + on(events: "typeahead:asynccancel", selector: string, handler: (eventObject: JQueryEventObject, ...eventData: any[]) => any): JQuery; + + /** + * Attach an event handler function for typeahead:asynccancel event to the selected elements. + * + * @param events typeahead:asynccancel event fired when the typeahead moves to active state. + * @param selector A selector string to filter the descendants of the selected elements that trigger the event. If the selector is null or omitted, the event is always triggered when it reaches the selected element. + * @param data Data to be passed to the handler in event.data when an event is triggered. + * @param handler A function to execute when the event is triggered. The value false is also allowed as a shorthand for a function that simply does return false. + */ + on(events: "typeahead:asynccancel", selector: string, data: any, handler: (eventObject: JQueryEventObject, ...eventData: any[]) => any): JQuery; + + /** + * Attach an event handler function for typeahead:asyncreceive event to the selected elements. + * + * @param events typeahead:asyncreceive event fired when the typeahead moves to active state. + * @param handler A function to execute when the event is triggered. The value false is also allowed as a shorthand for a function that simply does return false. Rest parameter args is for optional parameters passed to jQuery.trigger(). Note that the actual parameters on the event handler function must be marked as optional (? syntax). + */ + on(events: "typeahead:asyncreceive", handler: (eventObject: JQueryEventObject, ...args: any[]) => any): JQuery; + + /** + * Attach an event handler function for typeahead:asyncreceive event to the selected elements. + * + * @param events typeahead:asyncreceive event fired when the typeahead moves to active state. + * @param data Data to be passed to the handler in event.data when an event is triggered. + * @param handler A function to execute when the event is triggered. The value false is also allowed as a shorthand for a function that simply does return false. + */ + on(events: "typeahead:asyncreceive", data: any, handler: (eventObject: JQueryEventObject, ...args: any[]) => any): JQuery; + + /** + * Attach an event handler function for typeahead:asyncreceive event to the selected elements. + * + * @param events typeahead:asyncreceive event fired when the typeahead moves to active state. + * @param selector A selector string to filter the descendants of the selected elements that trigger the event. If the selector is null or omitted, the event is always triggered when it reaches the selected element. + * @param handler A function to execute when the event is triggered. The value false is also allowed as a shorthand for a function that simply does return false. + */ + on(events: "typeahead:asyncreceive", selector: string, handler: (eventObject: JQueryEventObject, ...eventData: any[]) => any): JQuery; + + /** + * Attach an event handler function for typeahead:asyncreceive event to the selected elements. + * + * @param events typeahead:asyncreceive event fired when the typeahead moves to active state. + * @param selector A selector string to filter the descendants of the selected elements that trigger the event. If the selector is null or omitted, the event is always triggered when it reaches the selected element. + * @param data Data to be passed to the handler in event.data when an event is triggered. + * @param handler A function to execute when the event is triggered. The value false is also allowed as a shorthand for a function that simply does return false. + */ + on(events: "typeahead:asyncreceive", selector: string, data: any, handler: (eventObject: JQueryEventObject, ...eventData: any[]) => any): JQuery; + + /** + * Remove an event handler. + * + * @param events typeahead:active event. + * @param handler A handler function previously attached for the event(s), or the special value false. + */ + off(events: "typeahead:active", selector?: string, handler?: (eventObject: JQueryEventObject) => any): JQuery; + + /** + * Remove an event handler. + * + * @param events typeahead:active event. + * @param handler A handler function previously attached for the event(s), or the special value false. + */ + off(events: "typeahead:active", handler: (eventObject: JQueryEventObject) => any): JQuery; + + /** + * Remove an event handler. + * + * @param events typeahead:idle event. + * @param handler A handler function previously attached for the event(s), or the special value false. + */ + off(events: "typeahead:idle", selector?: string, handler?: (eventObject: JQueryEventObject) => any): JQuery; + + /** + * Remove an event handler. + * + * @param events typeahead:idle event. + * @param handler A handler function previously attached for the event(s), or the special value false. + */ + off(events: "typeahead:idle", handler: (eventObject: JQueryEventObject) => any): JQuery; + + /** + * Remove an event handler. + * + * @param events typeahead:open event. + * @param handler A handler function previously attached for the event(s), or the special value false. + */ + off(events: "typeahead:open", selector?: string, handler?: (eventObject: JQueryEventObject) => any): JQuery; + + /** + * Remove an event handler. + * + * @param events typeahead:open event. + * @param handler A handler function previously attached for the event(s), or the special value false. + */ + off(events: "typeahead:open", handler: (eventObject: JQueryEventObject) => any): JQuery; + + /** + * Remove an event handler. + * + * @param events typeahead:close event. + * @param handler A handler function previously attached for the event(s), or the special value false. + */ + off(events: "typeahead:close", selector?: string, handler?: (eventObject: JQueryEventObject) => any): JQuery; + + /** + * Remove an event handler. + * + * @param events typeahead:close event. + * @param handler A handler function previously attached for the event(s), or the special value false. + */ + off(events: "typeahead:close", handler: (eventObject: JQueryEventObject) => any): JQuery; + + /** + * Remove an event handler. + * + * @param events typeahead:change event. + * @param handler A handler function previously attached for the event(s), or the special value false. + */ + off(events: "typeahead:change", selector?: string, handler?: (eventObject: JQueryEventObject) => any): JQuery; + + /** + * Remove an event handler. + * + * @param events typeahead:change event. + * @param handler A handler function previously attached for the event(s), or the special value false. + */ + off(events: "typeahead:change", handler: (eventObject: JQueryEventObject) => any): JQuery; + + /** + * Remove an event handler. + * + * @param events typeahead:render event. + * @param handler A handler function previously attached for the event(s), or the special value false. + */ + off(events: "typeahead:render", selector?: string, handler?: (eventObject: JQueryEventObject) => any): JQuery; + + /** + * Remove an event handler. + * + * @param events typeahead:render event. + * @param handler A handler function previously attached for the event(s), or the special value false. + */ + off(events: "typeahead:render", handler: (eventObject: JQueryEventObject) => any): JQuery; + + /** + * Remove an event handler. + * + * @param events typeahead:select event. + * @param handler A handler function previously attached for the event(s), or the special value false. + */ + off(events: "typeahead:select", selector?: string, handler?: (eventObject: JQueryEventObject) => any): JQuery; + + /** + * Remove an event handler. + * + * @param events typeahead:select event. + * @param handler A handler function previously attached for the event(s), or the special value false. + */ + off(events: "typeahead:select", handler: (eventObject: JQueryEventObject) => any): JQuery; + + /** + * Remove an event handler. + * + * @param events typeahead:autocomplete event. + * @param handler A handler function previously attached for the event(s), or the special value false. + */ + off(events: "typeahead:autocomplete", selector?: string, handler?: (eventObject: JQueryEventObject) => any): JQuery; + + /** + * Remove an event handler. + * + * @param events typeahead:autocomplete event. + * @param handler A handler function previously attached for the event(s), or the special value false. + */ + off(events: "typeahead:autocomplete", handler: (eventObject: JQueryEventObject) => any): JQuery; + + /** + * Remove an event handler. + * + * @param events typeahead:cursorchange event. + * @param handler A handler function previously attached for the event(s), or the special value false. + */ + off(events: "typeahead:cursorchange", selector?: string, handler?: (eventObject: JQueryEventObject) => any): JQuery; + + /** + * Remove an event handler. + * + * @param events typeahead:cursorchange event. + * @param handler A handler function previously attached for the event(s), or the special value false. + */ + off(events: "typeahead:cursorchange", handler: (eventObject: JQueryEventObject) => any): JQuery; + + /** + * Remove an event handler. + * + * @param events typeahead:asyncrequest event. + * @param handler A handler function previously attached for the event(s), or the special value false. + */ + off(events: "typeahead:asyncrequest", selector?: string, handler?: (eventObject: JQueryEventObject) => any): JQuery; + + /** + * Remove an event handler. + * + * @param events typeahead:asyncrequest event. + * @param handler A handler function previously attached for the event(s), or the special value false. + */ + off(events: "typeahead:asyncrequest", handler: (eventObject: JQueryEventObject) => any): JQuery; + + /** + * Remove an event handler. + * + * @param events typeahead:asynccancel event. + * @param handler A handler function previously attached for the event(s), or the special value false. + */ + off(events: "typeahead:asynccancel", selector?: string, handler?: (eventObject: JQueryEventObject) => any): JQuery; + + /** + * Remove an event handler. + * + * @param events typeahead:asynccancel event. + * @param handler A handler function previously attached for the event(s), or the special value false. + */ + off(events: "typeahead:asynccancel", handler: (eventObject: JQueryEventObject) => any): JQuery; + + /** + * Remove an event handler. + * + * @param events typeahead:asyncreceive event. + * @param handler A handler function previously attached for the event(s), or the special value false. + */ + off(events: "typeahead:asyncreceive", selector?: string, handler?: (eventObject: JQueryEventObject) => any): JQuery; + + /** + * Remove an event handler. + * + * @param events typeahead:asyncreceive event. + * @param handler A handler function previously attached for the event(s), or the special value false. + */ + off(events: "typeahead:asyncreceive", handler: (eventObject: JQueryEventObject) => any): JQuery; } declare module Twitter.Typeahead { From f2851bcb9503a7f1b0a5d1485eb364a21c9c3534 Mon Sep 17 00:00:00 2001 From: igochkov Date: Wed, 13 Jan 2016 21:47:56 +0100 Subject: [PATCH 2/8] Typeahead options extended with classNames to reflect latest 0.11.1 typeahead documentation --- typeahead/typeahead.d.ts | 88 ++++++++++++++++++++++++++++++++-------- 1 file changed, 71 insertions(+), 17 deletions(-) diff --git a/typeahead/typeahead.d.ts b/typeahead/typeahead.d.ts index aa1db2b44..13315b6aa 100644 --- a/typeahead/typeahead.d.ts +++ b/typeahead/typeahead.d.ts @@ -709,6 +709,34 @@ interface JQuery { } declare module Twitter.Typeahead { + /** + * When initializing a typeahead, there are a number of options you can configure. + */ + interface Options { + /** + * If true, when suggestions are rendered, pattern matches for the current query in text nodes will be wrapped in a strong element with its class set to {{classNames.highlight}}. + * Defaults to false. + */ + highlight?: boolean; + + /** + * If false, the typeahead will not show a hint. + * Defaults to true. + */ + hint?: boolean; + + /** + * The minimum character length needed before suggestions start getting rendered. + * Defaults to 1. + */ + minLength?: number; + + /** + * Used for overriding the default class names. + */ + classNames?: ClassNames; + } + /** * A dataset is an object that defines a set of data that hydrates * suggestions. Typeaheads can be backed by multiple datasets. @@ -743,7 +771,7 @@ declare module Twitter.Typeahead { /** * Can be used in place of display above. * - */ + */ displayKey?: string | ((obj: any) => string); /** @@ -801,27 +829,53 @@ declare module Twitter.Typeahead { } - /** - * When initializing a typeahead, there are a number of options you can configure. + * Used for overriding the default class names. */ - interface Options { + interface ClassNames { /** - * highlight: If true, when suggestions are rendered, - * pattern matches for the current query in text nodes will be wrapped in a strong element. - * Defaults to false. - */ - highlight?: boolean; - + * Added to input that's initialized into a typeahead. Defaults to tt-input. + */ + input?: string; + /** - * If false, the typeahead will not show a hint. Defaults to true. - */ - hint?: boolean; - + * Added to hint input.Defaults to tt- hint. + */ + hint?: string; + /** - * The minimum character length needed before suggestions start getting rendered. Defaults to 1. - */ - minLength?: number; + * Added to menu element.Defaults to tt- menu. + */ + menu?: string; + + /** + * Added to dataset elements.to Defaults to tt- dataset. + */ + dataset?: string; + /** + * Added to suggestion elements.Defaults to tt- suggestion. + */ + suggestion?: string; + + /** + * Added to menu element when it contains no content.Defaults to tt- empty. + */ + empty?: string; + + /** + * Added to menu element when it is opened.Defaults to tt- open. + */ + open?: string; + + /** + * Added to suggestion element when menu cursor moves to said suggestion.Defaults to tt- cursor. + */ + cursor?: string; + + /** + * Added to the element that wraps highlighted text.Defaults to tt- highlight. + */ + highlight?: string; } } From 9e53dceb17d7fb134e68b811ef1c96f7102c5406 Mon Sep 17 00:00:00 2001 From: igochkov Date: Wed, 13 Jan 2016 22:09:31 +0100 Subject: [PATCH 3/8] Typeahead dataset and templates changed to reflect latest 0.11.1 typeahead documentation --- typeahead/typeahead.d.ts | 167 ++++++++++++++++++++------------------- 1 file changed, 87 insertions(+), 80 deletions(-) diff --git a/typeahead/typeahead.d.ts b/typeahead/typeahead.d.ts index 13315b6aa..69da168ea 100644 --- a/typeahead/typeahead.d.ts +++ b/typeahead/typeahead.d.ts @@ -709,9 +709,6 @@ interface JQuery { } declare module Twitter.Typeahead { - /** - * When initializing a typeahead, there are a number of options you can configure. - */ interface Options { /** * If true, when suggestions are rendered, pattern matches for the current query in text nodes will be wrapped in a strong element with its class set to {{classNames.highlight}}. @@ -737,96 +734,106 @@ declare module Twitter.Typeahead { classNames?: ClassNames; } - /** - * A dataset is an object that defines a set of data that hydrates - * suggestions. Typeaheads can be backed by multiple datasets. - * Given a query, a typeahead instance will inspect its backing - * datasets and display relevant suggestions to the end-user. - */ - interface Dataset { - /** - * The backing data source for suggestions. - * Expected to be a function with the signature (query, cb). - * It is expected that the function will compute the suggestion set (i.e. an array of JavaScript objects) for query and then invoke cb with said set. - * cb can be invoked synchronously or asynchronously. - * - */ - source: ((query: string, syncResults: (result: Array) => void, asyncResults?: (result: Array) => void) => void); - - /** - * The name of the dataset. - * This will be appended to tt-dataset- to form the class name of the containing DOM element. - * Must only consist of underscores, dashes, letters (a-z), and numbers. - * Defaults to a random number. - */ - name?: string; - - /** - * For a given suggestion object, determines the string representation of it. - * This will be used when setting the value of the input control after a suggestion is selected. Can be either a key string or a function that transforms a suggestion object into a string. - * Defaults to value. - */ - display?: string | ((obj: any) => string); - - /** - * Can be used in place of display above. - * - */ - displayKey?: string | ((obj: any) => string); - - /** - * A hash of templates to be used when rendering the dataset. - * Note a precompiled template is a function that takes a JavaScript object as its first argument and returns a HTML string. - */ - templates?: Templates; - async?: boolean; + /** + * A typeahead is composed of one or more datasets. When an end-user + * modifies the value of a typeahead, each dataset will attempt to render + * suggestions for the new value. + * For most use cases, one dataset should suffice. It's only in the scenario + * where you want rendered suggestions to be grouped based on some sort of + * categorical relationship that you'd need to use multiple datasets. For + * example, on twitter.com, the search typeahead groups results into recent + * searches, trends, and accounts – that would be a great use case for using + * multiple datasets. + */ + interface Dataset { + /** + * The backing data source for suggestions. + * Expected to be a function with the signature (query, syncResults, asyncResults). + * syncResults should be called with suggestions computed synchronously and + * asyncResults should be called with suggestions computed asynchronously + * (e.g. suggestions that come for an AJAX request). + * source can also be a Bloodhound instance. + */ + source: Bloodhound | ((query: string, syncResults: (result: Array) => void, asyncResults?: (result: Array) => void) => void); + + /** + * Lets the dataset know if async suggestions should be expected. + * If not set, this information is inferred from the signature of + * source i.e. if the source function expects 3 arguments, async will + * be set to true. + */ + async?: boolean; + + /** + * The name of the dataset. + * This will be appended to {{classNames.dataset}} - to form the class name of the containing DOM element. + * Must only consist of underscores, dashes, letters (a-z), and numbers. + * Defaults to a random number. + */ + name?: string; + + /** + * The max number of suggestions to be displayed. Defaults to 5. + */ + limit?: number; + + /** + * For a given suggestion, determines the string representation of it. + * This will be used when setting the value of the input control after + * a suggestion is selected. Can be either a key string or a function + * that transforms a suggestion object into a string. + * Defaults to stringifying the suggestion. + */ + display?: string | ((obj: T) => string); + + /** + * A hash of templates to be used when rendering the dataset. Note a + * precompiled template is a function that takes a JavaScript object as + * its first argument and returns a HTML string. + */ + templates?: Templates; } - - interface Templates { - /** - * Rendered when 0 suggestions are available for the given query. - * Can be either a HTML string or a precompiled template. - * If it's a precompiled template, the passed in context will contain query - */ - empty?: any; - - /** - * Rendered at the bottom of the dataset. - * Can be either a HTML string or a precompiled template. - * If it's a precompiled template, the passed in context will contain query and isEmpty. - */ - footer?: any; - - /** - * Rendered at the top of the dataset. - * Can be either a HTML string or a precompiled template. - * If it's a precompiled template, the passed in context will contain query and isEmpty. - */ - header?: any; - + /** + * A hash of templates to be used when rendering the dataset. Note a + * precompiled template is a function that takes a JavaScript object as + * its first argument and returns a HTML string. + */ + interface Templates { /** * Rendered when 0 suggestions are available for the given query. * Can be either a HTML string or a precompiled template. * If it's a precompiled template, the passed in context will contain query. - */ - notFound?: (query: string) => string; - + */ + notFound?: string | ((query: string) => string); + /** * Rendered when 0 synchronous suggestions are available but asynchronous suggestions are expected. * Can be either a HTML string or a precompiled template. * If it's a precompiled template, the passed in context will contain query. - */ - pending?: (query: string) => string; + */ + pending?: string | ((query: string) => string); /** - * Used to render a single suggestion. - * If set, this has to be a precompiled template. - * The associated suggestion object will serve as the context. - * Defaults to the value of displayKey wrapped in a p tag i.e.

{{value}}

. - */ - suggestion?: (datum: any) => string; + * Rendered at the top of the dataset when suggestions are present. Can be either a HTML string or + * a precompiled template. If it's a precompiled template, the passed in context will contain + * query and suggestions. + */ + header?: string | ((query: string, suggestions: Array) => string); + /** + * Rendered at the bottom of the dataset when suggestions are present. Can be either a HTML string or + * a precompiled template. If it's a precompiled template, the passed in context will contain + * query and suggestions. + */ + footer?: string | ((query: string, suggestions: Array) => string); + + /** + * Used to render a single suggestion. If set, this has to be a precompiled template. + * The associated suggestion object will serve as the context. + * Defaults to the value of display wrapped in a div tag i.e.
{{value}}
. + */ + suggestion?: (suggestion: T) => string; } /** From 954b68bfffc7475491c245d4595ccdf8b01f15ba Mon Sep 17 00:00:00 2001 From: igochkov Date: Wed, 13 Jan 2016 22:16:17 +0100 Subject: [PATCH 4/8] Typeahead constructors changed to be generic --- typeahead/typeahead.d.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/typeahead/typeahead.d.ts b/typeahead/typeahead.d.ts index 69da168ea..b229bb29d 100644 --- a/typeahead/typeahead.d.ts +++ b/typeahead/typeahead.d.ts @@ -13,7 +13,7 @@ interface JQuery { * @param options Options hash that's used for configuration * @param datasets Array of datasets */ - typeahead(options: Twitter.Typeahead.Options, datasets: Twitter.Typeahead.Dataset[]): JQuery; + typeahead(options: Twitter.Typeahead.Options, datasets: Twitter.Typeahead.Dataset[]): JQuery; /** * For a given input[type="text"], enables typeahead functionality. @@ -22,7 +22,7 @@ interface JQuery { * @param options Options hash that's used for configuration * @param datasets One or more datasets passed as rest parameters. */ - typeahead(options: Twitter.Typeahead.Options, ...datasets: Twitter.Typeahead.Dataset[]): JQuery; + typeahead(options: Twitter.Typeahead.Options, ...datasets: Twitter.Typeahead.Dataset[]): JQuery; /** * Returns the current value of the typeahead. From 5da897b7fb2f4efced8a56f3209793ee141dc353 Mon Sep 17 00:00:00 2001 From: igochkov Date: Wed, 13 Jan 2016 22:44:50 +0100 Subject: [PATCH 5/8] Bloodhoud class changed to reflect latest 0.11.1 typeahead documentation --- typeahead/typeahead.d.ts | 127 +++++++++++++++++++++++---------------- 1 file changed, 75 insertions(+), 52 deletions(-) diff --git a/typeahead/typeahead.d.ts b/typeahead/typeahead.d.ts index b229bb29d..2b6a577d9 100644 --- a/typeahead/typeahead.d.ts +++ b/typeahead/typeahead.d.ts @@ -1057,65 +1057,88 @@ declare module Bloodhound { } } +/** + * Bloodhound is the typeahead.js suggestion engine. Bloodhound is robust, + * flexible, and offers advanced functionalities such as prefetching, + * intelligent caching, fast lookups, and backfilling with remote data. + */ declare class Bloodhound { + /** + * The constructor function. + * + * @constructor + * @param options Options hash + */ constructor(options: Bloodhound.BloodhoundOptions); - /** - * wraps the suggestion engine in an adapter that is compatible with the typeahead jQuery plugin - */ - public ttAdapter(): any; - /** - * Kicks off the initialization of the suggestion engine. This includes processing the data provided through local and fetching/processing the data provided through prefetch. - * Until initialized, all other methods will behave as no-ops. - * Returns a jQuery promise which is resolved when engine has been initialized. - * - * After the initial call of initialize, how subsequent invocations of the method behave depends on the reinitialize argument. - * If reinitialize is falsy, the method will not execute the initialization logic and will just return the same jQuery promise returned by the initial invocation. - * If reinitialize is truthy, the method will behave as if it were being called for the first time. - * - * var promise1 = engine.initialize(); - * var promise2 = engine.initialize(); - * var promise3 = engine.initialize(true); - * - * promise1 === promise2; - * promise3 !== promise1 && promise3 !== promise2; - */ - public initialize(reinitialize?: boolean): JQueryPromise; - /** - * Takes one argument, datums, which is expected to be an array of datums. - * The passed in datums will get added to the search index that powers the suggestion engine. - */ - public add(datums: T[]): void; - /** - * Removes all suggestions from the search index. - */ - public clear(): void; - /** - * If you're using prefetch, data gets cached in local storage in an effort to cut down on unnecessary network requests. - * clearPrefetchCache offers a way to programmatically clear said cache. - */ - public clearPrefetchCache(): void; - /** - * If you're using remote, Bloodhound will cache the 10 most recent responses in an effort to provide a better user experience. - * clearRemoteCache offers a way to programmatically clear said cache. - */ - public clearRemoteCache(): void; - /** - * Returns a reference to the Bloodhound constructor and reverts window.Bloodhound to its previous value. Can be used to avoid naming collisions. - */ - public noConflict(): any; /** - * Computes a set of suggestions for query. cb will be invoked with an array of datums that represent said set. - * cb will always be invoked once synchronously with suggestions that were available on the client. - * If those suggestions are insufficient (# of suggestions is less than limit) and remote was configured, cb may also be invoked asynchronously with the suggestions available on the client mixed with suggestions from the remote source. - */ - public get(query: string, cb: (datums: T[]) => void): void; + * Returns a reference to Bloodhound and reverts window.Bloodhound to its + * previous value. Can be used to avoid naming collisions. + */ + public static noConflict(): any; /** - * The Bloodhound suggestion engine is token-based, so how datums and queries are tokenized plays a vital role in the quality of search results. - * Specify how you want datums and queries tokenized. - */ + * The Bloodhound suggestion engine is token-based, so how datums and queries are tokenized plays a vital role in the quality of search results. + * Specify how you want datums and queries tokenized. + */ public static tokenizers: Bloodhound.Tokenizers; + + /** + * Kicks off the initialization of the suggestion engine. Initialization + * entails adding the data provided by local and prefetch to the internal + * search index as well as setting up transport mechanism used by remote. + * Before #initialize is called, the #get and #search methods will effectively be no-ops. + * + * Note, unless the initialize option is false, this method is implicitly called by the constructor. + * + * After initialization, how subsequent invocations of #initialize behave depends on + * the reinitialize argument. If reinitialize is falsy, the method will not execute the + * initialization logic and will just return the same jQuery promise returned + * by the initial invocation. If reinitialize is truthy, the method will behave + * as if it were being called for the first time. + */ + public initialize(reinitialize?: boolean): JQueryPromise; + + /** + * Takes one argument, data, which is expected to be an array. + * The data passed in will get added to the internal search index. + */ + public add(data: T[]): void; + + /** + * Returns the data in the local search index corresponding to ids + */ + public get(ids: number[]): T[]; + + /** + * Returns the data that matches query. Matches found in the local search + * index will be passed to the sync callback. If the data passed to sync + * doesn't contain at least sufficient number of datums, remote data will + * be requested and then passed to the async callback. + */ + public search(query: string, sync: (datums: T[]) => void, async: (datums: T[]) => void): T[]; + + /** + * Returns all items from the internal search index. + */ + public all(): T[]; + + /** + * Clears the internal search index that's powered by local, prefetch, and #add. + */ + public clear(): Bloodhound; + + /** + * If you're using prefetch, data gets cached in local storage in an effort to cut down on unnecessary network requests. + * clearPrefetchCache offers a way to programmatically clear said cache. + */ + public clearPrefetchCache(): Bloodhound; + + /** + * If you're using remote, Bloodhound will cache the 10 most recent responses in an effort to provide a better user experience. + * clearRemoteCache offers a way to programmatically clear said cache. + */ + public clearRemoteCache(): Bloodhound; } declare module "bloodhound" { From 8665d354b1c5af70ec2c09d4439c7768d00f6ced Mon Sep 17 00:00:00 2001 From: igochkov Date: Thu, 14 Jan 2016 00:10:13 +0100 Subject: [PATCH 6/8] BloodhoudOptions, PrefetchOptions and RemoteOptions changed to reflect latest 0.11.1 typeahead documentation --- typeahead/typeahead.d.ts | 304 +++++++++++++++++++++++---------------- 1 file changed, 182 insertions(+), 122 deletions(-) diff --git a/typeahead/typeahead.d.ts b/typeahead/typeahead.d.ts index 2b6a577d9..482e557a3 100644 --- a/typeahead/typeahead.d.ts +++ b/typeahead/typeahead.d.ts @@ -754,7 +754,7 @@ declare module Twitter.Typeahead { * (e.g. suggestions that come for an AJAX request). * source can also be a Bloodhound instance. */ - source: Bloodhound | ((query: string, syncResults: (result: Array) => void, asyncResults?: (result: Array) => void) => void); + source: Bloodhound | ((query: string, syncResults: (result: T[]) => void, asyncResults?: (result: T[]) => void) => void); /** * Lets the dataset know if async suggestions should be expected. @@ -819,14 +819,14 @@ declare module Twitter.Typeahead { * a precompiled template. If it's a precompiled template, the passed in context will contain * query and suggestions. */ - header?: string | ((query: string, suggestions: Array) => string); + header?: string | ((query: string, suggestions: T[]) => string); /** * Rendered at the bottom of the dataset when suggestions are present. Can be either a HTML string or * a precompiled template. If it's a precompiled template, the passed in context will contain * query and suggestions. */ - footer?: string | ((query: string, suggestions: Array) => string); + footer?: string | ((query: string, suggestions: T[]) => string); /** * Used to render a single suggestion. If set, this has to be a precompiled template. @@ -889,171 +889,218 @@ declare module Twitter.Typeahead { declare module Bloodhound { interface BloodhoundOptions { /** - * Transforms a datum into an array of string tokens - * - * @constructor - * @param datum individual units that compose the dataset - */ - datumTokenizer?: any; + * Transforms a datum into an array of string tokens. + * + * @param datum Suggestion. + * @returns An array of string tokens. + */ + datumTokenizer: (datum: T) => string[]; + /** - * Transforms a query into an array of string tokens - * - * @constructor - * @param query tokenizer query - */ - queryTokenizer?: any; + * Transforms a query into an array of string tokens. + * + * @param quiery Query. + * @returns An array of string tokens. + */ + queryTokenizer: (query: string) => string[]; + /** - * The max number of suggestions to return from Bloodhound#get. - * If not reached, the data source will attempt to backfill the suggestions from remote. Defaults to 5 - */ - limit?: number; + * If set to false, the Bloodhound instance will not be implicitly + * initialized by the constructor function. Defaults to true. + */ + initialize: boolean; + /** - * If set, this is expected to be a function with the signature (remoteMatch, localMatch) that returns true if the datums are duplicates or false otherwise. - * If not set, duplicate detection will not be performed. - */ - dupDetector?: (remoteMatch: T, localMatch: T) => boolean; + * Given a datum, returns a unique id for it. + * Defaults to JSON.stringify. Note that it is highly recommended + * to override this option. + * + * @param datum Suggestion. + * @returns Unique id for the suggestion. + */ + identify: (datum: T) => number; + /** - * A compare function used to sort matched datums for a given query. - */ + * If the number of datums provided from the internal search index is + * less than sufficient, remote will be used to backfill search + * requests triggered by calling #search. Defaults to 5. + */ + sufficient?: number; + + /** + * A compare function used to sort data returned from the internal search index. + * + * @param a First suggestion. + * @param b Second suggestion. + * @returns Comparison result. + */ sorter?: (a: T, b: T) => number; + /** - * An array of datums or a function that returns an array of datums. - */ - local?: () => T[]; + * An array of data or a function that returns an array of data. + * The data will be added to the internal search index when #initialize is called. + */ + local?: T[] | (() => T[]); + /** - * Can be a URL to a JSON file containing an array of datums or, if more configurability is needed, a prefetch options hash. - */ - prefetch?: PrefetchOptions; + * Can be a URL to a JSON file containing an array of data or, + * if more configurability is needed, a prefetch options hash. + */ + prefetch?: string | PrefetchOptions; + /** - * Can be a URL to fetch suggestions from when the data provided by local and prefetch is insufficient or, if more configurability is needed, a remote options hash. - */ - remote?: RemoteOptions; + * Can be a URL to fetch data from when the data provided by the internal + * search index is insufficient or, if more configurability is needed, + * a remote options hash. + */ + remote?: string | RemoteOptions; } - /** - * Prefetched data is fetched and processed on initialization. - * If the browser supports localStorage, the processed data will be cached - * there to prevent additional network requests on subsequent page loads. - */ + /** + * Prefetched data is fetched and processed on initialization. If the browser + * supports local storage, the processed data will be cached there to prevent + * additional network requests on subsequent page loads. + * + * WARNING: While it's possible to get away with it for smaller data sets, + * prefetched data isn't meant to contain entire sets of data. Rather, it should + * act as a first-level cache. Ignoring this warning means you'll run the risk + * of hitting local storage limits. + */ interface PrefetchOptions { /** - * A URL to a JSON file containing an array of datums. Required. - */ + * The URL prefetch data should be loaded from. + */ url: string; + /** - * The time (in milliseconds) the prefetched data should be cached - * in localStorage. Defaults to 86400000 (1 day). - */ + * If false, will not attempt to read or write to local storage and + * will always load prefetch data from url on initialization. Defaults to true. + */ + cache?: boolean; + + /** + * The time (in milliseconds) the prefetched data should be cached in + * local storage. Defaults to 86400000 (1 day). + */ ttl?: number; + /** - * A function that transforms the response body into an array of datums. - * - * @param parsedResponse Response body - */ - filter?: (parsedResponse: any) => T[]; - /** The key that data will be stored in local storage under. Defaults to value of url. - * - */ + * The key that data will be stored in local storage under. + * Defaults to value of url. + */ cacheKey?: string; + /** - * A string used for thumbprinting prefetched data. If this doesn't match what's stored in local storage, the data will be refetched. - */ + * A string used for thumbprinting prefetched data. If this doesn't + * match what's stored in local storage, the data will be refetched. + */ thumbprint?: string; + /** - * The ajax settings object passed to jQuery.ajax. - */ - ajax?: JQueryAjaxSettings; + * A function that provides a hook to allow you to prepare the settings + * object passed to transport when a request is about to be made. + * Defaults to the identity function. + * + * @param settings The default settings object created internally by the Bloodhound instance. + * @returns A settings object. + */ + prepare?: (settings: JQueryAjaxSettings) => JQueryAjaxSettings; + + /** + * A function with the signature transform(response) that allows you to + * transform the prefetch response before the Bloodhound instance operates + * on it. Defaults to the identity function. + * + * @param response Prefetch response. + * @returns Transform response. + */ + transform?: (response: JQueryPromise) => JQueryPromise; } /** - * Remote data is only used when the data provided by local and prefetch - * is insufficient. In order to prevent an obscene number of requests - * being made to remote endpoint, typeahead.js rate-limits remote requests. - */ + * Bloodhound only goes to the network when the internal search engine cannot + * provide a sufficient number of results. In order to prevent an obscene + * number of requests being made to the remote endpoint, requests are rate-limited. + */ interface RemoteOptions { /** - * A URL to make requests to when the data provided by local and - * prefetch is insufficient. Required. - */ + * The URL remote data should be loaded from. + */ url: string; - /** - * The pattern in url that will be replaced with the user's query - * when a request is made. Defaults to %QUERY. - */ - wildcard?: string; - /** - * Overrides the request URL. If set, no wildcard substitution will - * be performed on url. - * - * @param url Replacement URL - * @param uriEncodedQuery Encoded query - * @returns A valid URL - */ - replace?: (url: string, uriEncodedQuery: string) => string; - /** - * The function used for rate-limiting network requests. - * Can be either 'debounce' or 'throttle'. Defaults to 'debounce'. - */ - rateLimitby?: string; - /** - * The time interval in milliseconds that will be used by rateLimitFn. - * Defaults to 300. - */ - rateLimitWait?: number; /** - * Transforms the response body into an array of datums. - * - * @param parsedResponse Response body - */ - filter?: (parsedResponse: any) => T[]; - /** - * The ajax settings object passed to jQuery.ajax. - */ - ajax?: JQueryAjaxSettings; - - /** - * A function that provides a hook to allow you to prepare the settings object passed to transport - * when a request is about to be made. The function signature should be prepare(query, settings), - * where query is the query #search was called with and settings is the default settings object - * created internally by the Bloodhound instance. The prepare function should return a settings object. - * [Note: Added in 0.11.1] + * A function that provides a hook to allow you to prepare the settings + * object passed to transport when a request is about to be made. + * The function signature should be prepare(query, settings), where query + * is the query #search was called with and settings is the default settings + * object created internally by the Bloodhound instance. The prepare function + * should return a settings object. Defaults to the identity function. * * @param query The query #search was called with. * @param settings The default settings object created internally by Bloodhound. * @returns A JqueryAjaxSettings object. */ prepare?: (query: string, settings: JQueryAjaxSettings) => JQueryAjaxSettings; + + /** + * A convenience option for prepare. If set, prepare will be a function + * that replaces the value of this option in url with the URI encoded query. + */ + wildcard?: string; + + /** + * The method used to rate-limit network requests. + * Can be either debounce or throttle. Defaults to debounce. + */ + rateLimitby?: string; + + /** + * The time interval in milliseconds that will be used by rateLimitBy. + * Defaults to 300. + */ + rateLimitWait?: number; + + /** + * A function with the signature transform(response) that allows you to + * transform the remote response before the Bloodhound instance operates on it. + * Defaults to the identity function. + * + * @param response Prefetch response. + * @returns Transform response. + */ + transform?: (response: JQueryPromise) => JQueryPromise; } /** - * The most common tokenization methods. + * Build-in tokenization methods. */ interface Tokenizers { /** - * Split a given string on whitespace characters. - */ - whitespace(query: string): string[]; + * Split a given string on whitespace characters. + */ + whitespace(str: string): string[]; + /** - * Split a given string on non-word characters. - */ - nonword(query: string): string[]; + * Split a given string on non-word characters. + */ + nonword(str: string): string[]; /** - * Instances of the most common tokenization methods. - */ + * Instances of the build-in tokenization methods. + */ obj: ObjTokenizer; } interface ObjTokenizer { /** - * Split a given string on whitespace characters. - */ - whitespace(query: string): string[]; + * Split a given string on whitespace characters. + */ + whitespace(str: string): string[]; + /** - * Split a given string on non-word characters. - */ - nonword(query: string): string[]; + * Split a given string on non-word characters. + */ + nonword(str: string): string[]; } } @@ -1067,7 +1114,7 @@ declare class Bloodhound { * The constructor function. * * @constructor - * @param options Options hash + * @param options Options hash. */ constructor(options: Bloodhound.BloodhoundOptions); @@ -1096,17 +1143,25 @@ declare class Bloodhound { * initialization logic and will just return the same jQuery promise returned * by the initial invocation. If reinitialize is truthy, the method will behave * as if it were being called for the first time. + * + * @param reinitialize How subsequent invocations of #initialize will behave. + * @returns jQuery promise. */ - public initialize(reinitialize?: boolean): JQueryPromise; + public initialize(reinitialize?: boolean): JQueryPromise; /** * Takes one argument, data, which is expected to be an array. * The data passed in will get added to the internal search index. + * + * @param data Data to be added to the internal search index. */ public add(data: T[]): void; /** - * Returns the data in the local search index corresponding to ids + * Returns the data in the local search index corresponding to ids. + * + * @param ids Data ids. + * @returns The corresponding data. */ public get(ids: number[]): T[]; @@ -1115,6 +1170,11 @@ declare class Bloodhound { * index will be passed to the sync callback. If the data passed to sync * doesn't contain at least sufficient number of datums, remote data will * be requested and then passed to the async callback. + * + * @param query Query. + * @param sync Sync callback + * @param async Async callback. + * @returns The data that matches query. */ public search(query: string, sync: (datums: T[]) => void, async: (datums: T[]) => void): T[]; From bc795de3e0d6c1d2bfbe088acdc0f5545eed7296 Mon Sep 17 00:00:00 2001 From: Ivaylo Gochkov Date: Thu, 14 Jan 2016 10:02:30 +0100 Subject: [PATCH 7/8] Version number changed in the header of the file. * Version number changed in the header of the file. * BloodhoundOptions - initialize made optional - identify made optional * PrefetchOptions - the signiture of transform changed * RemoteOptions - the signiture of transform changed --- typeahead/typeahead.d.ts | 814 +++++++++++++++++++-------------------- 1 file changed, 407 insertions(+), 407 deletions(-) diff --git a/typeahead/typeahead.d.ts b/typeahead/typeahead.d.ts index 482e557a3..b9228b430 100644 --- a/typeahead/typeahead.d.ts +++ b/typeahead/typeahead.d.ts @@ -1,11 +1,11 @@ -// Type definitions for typeahead.js 0.10.4 -// Project: http://twitter.github.io/typeahead.js/ -// Definitions by: Ivaylo Gochkov , Gidon Junge -// Definitions: https://github.com/borisyankov/DefinitelyTyped - -/// - -interface JQuery { +// Type definitions for typeahead.js 0.11.1 +// Project: http://twitter.github.io/typeahead.js/ +// Definitions by: Ivaylo Gochkov , Gidon Junge +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +/// + +interface JQuery { /** * For a given input[type="text"], enables typeahead functionality. * @@ -705,35 +705,35 @@ interface JQuery { * @param events typeahead:asyncreceive event. * @param handler A handler function previously attached for the event(s), or the special value false. */ - off(events: "typeahead:asyncreceive", handler: (eventObject: JQueryEventObject) => any): JQuery; -} - -declare module Twitter.Typeahead { - interface Options { - /** - * If true, when suggestions are rendered, pattern matches for the current query in text nodes will be wrapped in a strong element with its class set to {{classNames.highlight}}. - * Defaults to false. - */ - highlight?: boolean; - - /** - * If false, the typeahead will not show a hint. - * Defaults to true. - */ - hint?: boolean; - - /** - * The minimum character length needed before suggestions start getting rendered. - * Defaults to 1. - */ - minLength?: number; - - /** - * Used for overriding the default class names. - */ - classNames?: ClassNames; - } - + off(events: "typeahead:asyncreceive", handler: (eventObject: JQueryEventObject) => any): JQuery; +} + +declare module Twitter.Typeahead { + interface Options { + /** + * If true, when suggestions are rendered, pattern matches for the current query in text nodes will be wrapped in a strong element with its class set to {{classNames.highlight}}. + * Defaults to false. + */ + highlight?: boolean; + + /** + * If false, the typeahead will not show a hint. + * Defaults to true. + */ + hint?: boolean; + + /** + * The minimum character length needed before suggestions start getting rendered. + * Defaults to 1. + */ + minLength?: number; + + /** + * Used for overriding the default class names. + */ + classNames?: ClassNames; + } + /** * A typeahead is composed of one or more datasets. When an end-user * modifies the value of a typeahead, each dataset will attempt to render @@ -742,7 +742,7 @@ declare module Twitter.Typeahead { * where you want rendered suggestions to be grouped based on some sort of * categorical relationship that you'd need to use multiple datasets. For * example, on twitter.com, the search typeahead groups results into recent - * searches, trends, and accounts – that would be a great use case for using + * searches, trends, and accounts – that would be a great use case for using * multiple datasets. */ interface Dataset { @@ -792,170 +792,170 @@ declare module Twitter.Typeahead { * its first argument and returns a HTML string. */ templates?: Templates; - } - + } + /** * A hash of templates to be used when rendering the dataset. Note a * precompiled template is a function that takes a JavaScript object as * its first argument and returns a HTML string. - */ - interface Templates { - /** - * Rendered when 0 suggestions are available for the given query. - * Can be either a HTML string or a precompiled template. - * If it's a precompiled template, the passed in context will contain query. - */ - notFound?: string | ((query: string) => string); - - /** - * Rendered when 0 synchronous suggestions are available but asynchronous suggestions are expected. - * Can be either a HTML string or a precompiled template. - * If it's a precompiled template, the passed in context will contain query. - */ - pending?: string | ((query: string) => string); - - /** - * Rendered at the top of the dataset when suggestions are present. Can be either a HTML string or - * a precompiled template. If it's a precompiled template, the passed in context will contain - * query and suggestions. - */ - header?: string | ((query: string, suggestions: T[]) => string); - - /** - * Rendered at the bottom of the dataset when suggestions are present. Can be either a HTML string or - * a precompiled template. If it's a precompiled template, the passed in context will contain - * query and suggestions. - */ - footer?: string | ((query: string, suggestions: T[]) => string); - - /** - * Used to render a single suggestion. If set, this has to be a precompiled template. - * The associated suggestion object will serve as the context. - * Defaults to the value of display wrapped in a div tag i.e.
{{value}}
. - */ - suggestion?: (suggestion: T) => string; - } - - /** - * Used for overriding the default class names. - */ - interface ClassNames { - /** - * Added to input that's initialized into a typeahead. Defaults to tt-input. - */ + */ + interface Templates { + /** + * Rendered when 0 suggestions are available for the given query. + * Can be either a HTML string or a precompiled template. + * If it's a precompiled template, the passed in context will contain query. + */ + notFound?: string | ((query: string) => string); + + /** + * Rendered when 0 synchronous suggestions are available but asynchronous suggestions are expected. + * Can be either a HTML string or a precompiled template. + * If it's a precompiled template, the passed in context will contain query. + */ + pending?: string | ((query: string) => string); + + /** + * Rendered at the top of the dataset when suggestions are present. Can be either a HTML string or + * a precompiled template. If it's a precompiled template, the passed in context will contain + * query and suggestions. + */ + header?: string | ((query: string, suggestions: T[]) => string); + + /** + * Rendered at the bottom of the dataset when suggestions are present. Can be either a HTML string or + * a precompiled template. If it's a precompiled template, the passed in context will contain + * query and suggestions. + */ + footer?: string | ((query: string, suggestions: T[]) => string); + + /** + * Used to render a single suggestion. If set, this has to be a precompiled template. + * The associated suggestion object will serve as the context. + * Defaults to the value of display wrapped in a div tag i.e.
{{value}}
. + */ + suggestion?: (suggestion: T) => string; + } + + /** + * Used for overriding the default class names. + */ + interface ClassNames { + /** + * Added to input that's initialized into a typeahead. Defaults to tt-input. + */ input?: string; - /** - * Added to hint input.Defaults to tt- hint. + /** + * Added to hint input.Defaults to tt- hint. */ hint?: string; - /** - * Added to menu element.Defaults to tt- menu. + /** + * Added to menu element.Defaults to tt- menu. */ menu?: string; - /** - * Added to dataset elements.to Defaults to tt- dataset. + /** + * Added to dataset elements.to Defaults to tt- dataset. */ dataset?: string; - /** - * Added to suggestion elements.Defaults to tt- suggestion. + /** + * Added to suggestion elements.Defaults to tt- suggestion. */ suggestion?: string; - /** - * Added to menu element when it contains no content.Defaults to tt- empty. + /** + * Added to menu element when it contains no content.Defaults to tt- empty. */ empty?: string; - /** - * Added to menu element when it is opened.Defaults to tt- open. + /** + * Added to menu element when it is opened.Defaults to tt- open. */ open?: string; - /** - * Added to suggestion element when menu cursor moves to said suggestion.Defaults to tt- cursor. + /** + * Added to suggestion element when menu cursor moves to said suggestion.Defaults to tt- cursor. */ cursor?: string; - /** - * Added to the element that wraps highlighted text.Defaults to tt- highlight. + /** + * Added to the element that wraps highlighted text.Defaults to tt- highlight. */ - highlight?: string; - } -} - -declare module Bloodhound { - interface BloodhoundOptions { - /** - * Transforms a datum into an array of string tokens. - * - * @param datum Suggestion. - * @returns An array of string tokens. - */ - datumTokenizer: (datum: T) => string[]; - - /** - * Transforms a query into an array of string tokens. - * - * @param quiery Query. - * @returns An array of string tokens. - */ - queryTokenizer: (query: string) => string[]; - - /** - * If set to false, the Bloodhound instance will not be implicitly - * initialized by the constructor function. Defaults to true. - */ - initialize: boolean; - - /** - * Given a datum, returns a unique id for it. - * Defaults to JSON.stringify. Note that it is highly recommended - * to override this option. - * - * @param datum Suggestion. - * @returns Unique id for the suggestion. - */ - identify: (datum: T) => number; - - /** - * If the number of datums provided from the internal search index is - * less than sufficient, remote will be used to backfill search - * requests triggered by calling #search. Defaults to 5. - */ - sufficient?: number; - - /** - * A compare function used to sort data returned from the internal search index. - * - * @param a First suggestion. - * @param b Second suggestion. - * @returns Comparison result. - */ - sorter?: (a: T, b: T) => number; - - /** - * An array of data or a function that returns an array of data. - * The data will be added to the internal search index when #initialize is called. - */ - local?: T[] | (() => T[]); - - /** - * Can be a URL to a JSON file containing an array of data or, - * if more configurability is needed, a prefetch options hash. - */ - prefetch?: string | PrefetchOptions; - - /** - * Can be a URL to fetch data from when the data provided by the internal - * search index is insufficient or, if more configurability is needed, - * a remote options hash. - */ - remote?: string | RemoteOptions; - } - + highlight?: string; + } +} + +declare module Bloodhound { + interface BloodhoundOptions { + /** + * Transforms a datum into an array of string tokens. + * + * @param datum Suggestion. + * @returns An array of string tokens. + */ + datumTokenizer: (datum: T) => string[]; + + /** + * Transforms a query into an array of string tokens. + * + * @param quiery Query. + * @returns An array of string tokens. + */ + queryTokenizer: (query: string) => string[]; + + /** + * If set to false, the Bloodhound instance will not be implicitly + * initialized by the constructor function. Defaults to true. + */ + initialize?: boolean; + + /** + * Given a datum, returns a unique id for it. + * Defaults to JSON.stringify. Note that it is highly recommended + * to override this option. + * + * @param datum Suggestion. + * @returns Unique id for the suggestion. + */ + identify?: (datum: T) => number; + + /** + * If the number of datums provided from the internal search index is + * less than sufficient, remote will be used to backfill search + * requests triggered by calling #search. Defaults to 5. + */ + sufficient?: number; + + /** + * A compare function used to sort data returned from the internal search index. + * + * @param a First suggestion. + * @param b Second suggestion. + * @returns Comparison result. + */ + sorter?: (a: T, b: T) => number; + + /** + * An array of data or a function that returns an array of data. + * The data will be added to the internal search index when #initialize is called. + */ + local?: T[] | (() => T[]); + + /** + * Can be a URL to a JSON file containing an array of data or, + * if more configurability is needed, a prefetch options hash. + */ + prefetch?: string | PrefetchOptions; + + /** + * Can be a URL to fetch data from when the data provided by the internal + * search index is insufficient or, if more configurability is needed, + * a remote options hash. + */ + remote?: string | RemoteOptions; + } + /** * Prefetched data is fetched and processed on initialization. If the browser * supports local storage, the processed data will be cached there to prevent @@ -965,242 +965,242 @@ declare module Bloodhound { * prefetched data isn't meant to contain entire sets of data. Rather, it should * act as a first-level cache. Ignoring this warning means you'll run the risk * of hitting local storage limits. - */ - interface PrefetchOptions { - /** - * The URL prefetch data should be loaded from. - */ - url: string; - - /** - * If false, will not attempt to read or write to local storage and - * will always load prefetch data from url on initialization. Defaults to true. - */ - cache?: boolean; - - /** - * The time (in milliseconds) the prefetched data should be cached in - * local storage. Defaults to 86400000 (1 day). - */ - ttl?: number; - - /** - * The key that data will be stored in local storage under. - * Defaults to value of url. - */ - cacheKey?: string; - - /** - * A string used for thumbprinting prefetched data. If this doesn't - * match what's stored in local storage, the data will be refetched. - */ - thumbprint?: string; - - /** - * A function that provides a hook to allow you to prepare the settings - * object passed to transport when a request is about to be made. - * Defaults to the identity function. - * - * @param settings The default settings object created internally by the Bloodhound instance. - * @returns A settings object. - */ - prepare?: (settings: JQueryAjaxSettings) => JQueryAjaxSettings; - - /** - * A function with the signature transform(response) that allows you to - * transform the prefetch response before the Bloodhound instance operates - * on it. Defaults to the identity function. - * - * @param response Prefetch response. - * @returns Transform response. - */ - transform?: (response: JQueryPromise) => JQueryPromise; - } - - /** - * Bloodhound only goes to the network when the internal search engine cannot - * provide a sufficient number of results. In order to prevent an obscene - * number of requests being made to the remote endpoint, requests are rate-limited. - */ - interface RemoteOptions { - /** - * The URL remote data should be loaded from. - */ - url: string; - - /** - * A function that provides a hook to allow you to prepare the settings - * object passed to transport when a request is about to be made. - * The function signature should be prepare(query, settings), where query - * is the query #search was called with and settings is the default settings - * object created internally by the Bloodhound instance. The prepare function - * should return a settings object. Defaults to the identity function. - * - * @param query The query #search was called with. - * @param settings The default settings object created internally by Bloodhound. - * @returns A JqueryAjaxSettings object. - */ - prepare?: (query: string, settings: JQueryAjaxSettings) => JQueryAjaxSettings; - - /** - * A convenience option for prepare. If set, prepare will be a function - * that replaces the value of this option in url with the URI encoded query. - */ - wildcard?: string; - - /** - * The method used to rate-limit network requests. - * Can be either debounce or throttle. Defaults to debounce. - */ - rateLimitby?: string; - - /** - * The time interval in milliseconds that will be used by rateLimitBy. - * Defaults to 300. - */ - rateLimitWait?: number; - - /** - * A function with the signature transform(response) that allows you to - * transform the remote response before the Bloodhound instance operates on it. - * Defaults to the identity function. - * - * @param response Prefetch response. - * @returns Transform response. - */ - transform?: (response: JQueryPromise) => JQueryPromise; - } - - /** - * Build-in tokenization methods. - */ - interface Tokenizers { - /** - * Split a given string on whitespace characters. - */ - whitespace(str: string): string[]; - - /** - * Split a given string on non-word characters. - */ - nonword(str: string): string[]; - - /** - * Instances of the build-in tokenization methods. - */ - obj: ObjTokenizer; - } - - interface ObjTokenizer { - /** - * Split a given string on whitespace characters. - */ - whitespace(str: string): string[]; - - /** - * Split a given string on non-word characters. - */ - nonword(str: string): string[]; - } -} - -/** - * Bloodhound is the typeahead.js suggestion engine. Bloodhound is robust, - * flexible, and offers advanced functionalities such as prefetching, - * intelligent caching, fast lookups, and backfilling with remote data. - */ -declare class Bloodhound { + */ + interface PrefetchOptions { + /** + * The URL prefetch data should be loaded from. + */ + url: string; + + /** + * If false, will not attempt to read or write to local storage and + * will always load prefetch data from url on initialization. Defaults to true. + */ + cache?: boolean; + + /** + * The time (in milliseconds) the prefetched data should be cached in + * local storage. Defaults to 86400000 (1 day). + */ + ttl?: number; + + /** + * The key that data will be stored in local storage under. + * Defaults to value of url. + */ + cacheKey?: string; + + /** + * A string used for thumbprinting prefetched data. If this doesn't + * match what's stored in local storage, the data will be refetched. + */ + thumbprint?: string; + + /** + * A function that provides a hook to allow you to prepare the settings + * object passed to transport when a request is about to be made. + * Defaults to the identity function. + * + * @param settings The default settings object created internally by the Bloodhound instance. + * @returns A settings object. + */ + prepare?: (settings: JQueryAjaxSettings) => JQueryAjaxSettings; + + /** + * A function with the signature transform(response) that allows you to + * transform the prefetch response before the Bloodhound instance operates + * on it. Defaults to the identity function. + * + * @param response Prefetch response. + * @returns Transform response. + */ + transform?: (response: T[]) => T[]; + } + + /** + * Bloodhound only goes to the network when the internal search engine cannot + * provide a sufficient number of results. In order to prevent an obscene + * number of requests being made to the remote endpoint, requests are rate-limited. + */ + interface RemoteOptions { + /** + * The URL remote data should be loaded from. + */ + url: string; + + /** + * A function that provides a hook to allow you to prepare the settings + * object passed to transport when a request is about to be made. + * The function signature should be prepare(query, settings), where query + * is the query #search was called with and settings is the default settings + * object created internally by the Bloodhound instance. The prepare function + * should return a settings object. Defaults to the identity function. + * + * @param query The query #search was called with. + * @param settings The default settings object created internally by Bloodhound. + * @returns A JqueryAjaxSettings object. + */ + prepare?: (query: string, settings: JQueryAjaxSettings) => JQueryAjaxSettings; + + /** + * A convenience option for prepare. If set, prepare will be a function + * that replaces the value of this option in url with the URI encoded query. + */ + wildcard?: string; + + /** + * The method used to rate-limit network requests. + * Can be either debounce or throttle. Defaults to debounce. + */ + rateLimitby?: string; + + /** + * The time interval in milliseconds that will be used by rateLimitBy. + * Defaults to 300. + */ + rateLimitWait?: number; + + /** + * A function with the signature transform(response) that allows you to + * transform the remote response before the Bloodhound instance operates on it. + * Defaults to the identity function. + * + * @param response Prefetch response. + * @returns Transform response. + */ + transform?: (response: T[]) => T[]; + } + + /** + * Build-in tokenization methods. + */ + interface Tokenizers { + /** + * Split a given string on whitespace characters. + */ + whitespace(str: string): string[]; + + /** + * Split a given string on non-word characters. + */ + nonword(str: string): string[]; + + /** + * Instances of the build-in tokenization methods. + */ + obj: ObjTokenizer; + } + + interface ObjTokenizer { + /** + * Split a given string on whitespace characters. + */ + whitespace(str: string): string[]; + + /** + * Split a given string on non-word characters. + */ + nonword(str: string): string[]; + } +} + +/** + * Bloodhound is the typeahead.js suggestion engine. Bloodhound is robust, + * flexible, and offers advanced functionalities such as prefetching, + * intelligent caching, fast lookups, and backfilling with remote data. + */ +declare class Bloodhound { /** * The constructor function. * * @constructor * @param options Options hash. - */ - constructor(options: Bloodhound.BloodhoundOptions); - - /** - * Returns a reference to Bloodhound and reverts window.Bloodhound to its - * previous value. Can be used to avoid naming collisions. - */ - public static noConflict(): any; - - /** - * The Bloodhound suggestion engine is token-based, so how datums and queries are tokenized plays a vital role in the quality of search results. - * Specify how you want datums and queries tokenized. - */ - public static tokenizers: Bloodhound.Tokenizers; - - /** - * Kicks off the initialization of the suggestion engine. Initialization - * entails adding the data provided by local and prefetch to the internal - * search index as well as setting up transport mechanism used by remote. - * Before #initialize is called, the #get and #search methods will effectively be no-ops. - * - * Note, unless the initialize option is false, this method is implicitly called by the constructor. - * - * After initialization, how subsequent invocations of #initialize behave depends on - * the reinitialize argument. If reinitialize is falsy, the method will not execute the - * initialization logic and will just return the same jQuery promise returned - * by the initial invocation. If reinitialize is truthy, the method will behave - * as if it were being called for the first time. - * - * @param reinitialize How subsequent invocations of #initialize will behave. - * @returns jQuery promise. - */ - public initialize(reinitialize?: boolean): JQueryPromise; - - /** - * Takes one argument, data, which is expected to be an array. - * The data passed in will get added to the internal search index. - * - * @param data Data to be added to the internal search index. - */ - public add(data: T[]): void; - - /** - * Returns the data in the local search index corresponding to ids. - * - * @param ids Data ids. - * @returns The corresponding data. - */ - public get(ids: number[]): T[]; - - /** - * Returns the data that matches query. Matches found in the local search - * index will be passed to the sync callback. If the data passed to sync - * doesn't contain at least sufficient number of datums, remote data will - * be requested and then passed to the async callback. - * - * @param query Query. - * @param sync Sync callback - * @param async Async callback. - * @returns The data that matches query. - */ - public search(query: string, sync: (datums: T[]) => void, async: (datums: T[]) => void): T[]; - + */ + constructor(options: Bloodhound.BloodhoundOptions); + + /** + * Returns a reference to Bloodhound and reverts window.Bloodhound to its + * previous value. Can be used to avoid naming collisions. + */ + public static noConflict(): any; + + /** + * The Bloodhound suggestion engine is token-based, so how datums and queries are tokenized plays a vital role in the quality of search results. + * Specify how you want datums and queries tokenized. + */ + public static tokenizers: Bloodhound.Tokenizers; + + /** + * Kicks off the initialization of the suggestion engine. Initialization + * entails adding the data provided by local and prefetch to the internal + * search index as well as setting up transport mechanism used by remote. + * Before #initialize is called, the #get and #search methods will effectively be no-ops. + * + * Note, unless the initialize option is false, this method is implicitly called by the constructor. + * + * After initialization, how subsequent invocations of #initialize behave depends on + * the reinitialize argument. If reinitialize is falsy, the method will not execute the + * initialization logic and will just return the same jQuery promise returned + * by the initial invocation. If reinitialize is truthy, the method will behave + * as if it were being called for the first time. + * + * @param reinitialize How subsequent invocations of #initialize will behave. + * @returns jQuery promise. + */ + public initialize(reinitialize?: boolean): JQueryPromise; + + /** + * Takes one argument, data, which is expected to be an array. + * The data passed in will get added to the internal search index. + * + * @param data Data to be added to the internal search index. + */ + public add(data: T[]): void; + + /** + * Returns the data in the local search index corresponding to ids. + * + * @param ids Data ids. + * @returns The corresponding data. + */ + public get(ids: number[]): T[]; + + /** + * Returns the data that matches query. Matches found in the local search + * index will be passed to the sync callback. If the data passed to sync + * doesn't contain at least sufficient number of datums, remote data will + * be requested and then passed to the async callback. + * + * @param query Query. + * @param sync Sync callback + * @param async Async callback. + * @returns The data that matches query. + */ + public search(query: string, sync: (datums: T[]) => void, async: (datums: T[]) => void): T[]; + /** * Returns all items from the internal search index. - */ - public all(): T[]; - - /** - * Clears the internal search index that's powered by local, prefetch, and #add. - */ - public clear(): Bloodhound; - - /** - * If you're using prefetch, data gets cached in local storage in an effort to cut down on unnecessary network requests. - * clearPrefetchCache offers a way to programmatically clear said cache. - */ - public clearPrefetchCache(): Bloodhound; - - /** - * If you're using remote, Bloodhound will cache the 10 most recent responses in an effort to provide a better user experience. - * clearRemoteCache offers a way to programmatically clear said cache. - */ - public clearRemoteCache(): Bloodhound; -} - -declare module "bloodhound" { - export = Bloodhound; -} + */ + public all(): T[]; + + /** + * Clears the internal search index that's powered by local, prefetch, and #add. + */ + public clear(): Bloodhound; + + /** + * If you're using prefetch, data gets cached in local storage in an effort to cut down on unnecessary network requests. + * clearPrefetchCache offers a way to programmatically clear said cache. + */ + public clearPrefetchCache(): Bloodhound; + + /** + * If you're using remote, Bloodhound will cache the 10 most recent responses in an effort to provide a better user experience. + * clearRemoteCache offers a way to programmatically clear said cache. + */ + public clearRemoteCache(): Bloodhound; +} + +declare module "bloodhound" { + export = Bloodhound; +} From 6e2b3966c7656379784f5bf54f0711b227df766b Mon Sep 17 00:00:00 2001 From: igochkov Date: Thu, 14 Jan 2016 23:43:19 +0100 Subject: [PATCH 8/8] Complete rewrite of the typeahead tests to reflect latest 0.11.1 typeahead documentation --- typeahead/typeahead-tests.ts | 717 +++++++++++++++++++++-------------- typeahead/typeahead.d.ts | 7 +- 2 files changed, 446 insertions(+), 278 deletions(-) diff --git a/typeahead/typeahead-tests.ts b/typeahead/typeahead-tests.ts index ec2b2e2f3..be2f3b34b 100644 --- a/typeahead/typeahead-tests.ts +++ b/typeahead/typeahead-tests.ts @@ -1,302 +1,469 @@ /// /// -// -// Examples from http://twitter.github.com/typeahead.js/examples -// - -var substringMatcher = function (strs: any) { - return function findMatches(q: string, syncResults: (x: Array) => void) { - var matches: Array<{ value: string }> = []; - // regex used to determine if a string contains the substring `q` - var substrRegex = new RegExp(q, 'i'); - - // iterate through the pool of strings and for any string that - // contains the substring `q`, add it to the `matches` array - $.each(strs, function (i, str) { - if (substrRegex.test(str)) { - // the typeahead jQuery plugin expects suggestions to a - // JavaScript object, refer to typeahead docs for more info - matches.push({ value: str }); - } - }); - - syncResults(matches); - } -} - -var states = ['Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California', - 'Colorado', 'Connecticut', 'Delaware', 'Florida', 'Georgia', 'Hawaii', - 'Idaho', 'Illinois', 'Indiana', 'Iowa', 'Kansas', 'Kentucky', 'Louisiana', - 'Maine', 'Maryland', 'Massachusetts', 'Michigan', 'Minnesota', - 'Mississippi', 'Missouri', 'Montana', 'Nebraska', 'Nevada', 'New Hampshire', - 'New Jersey', 'New Mexico', 'New York', 'North Carolina', 'North Dakota', - 'Ohio', 'Oklahoma', 'Oregon', 'Pennsylvania', 'Rhode Island', - 'South Carolina', 'South Dakota', 'Tennessee', 'Texas', 'Utah', 'Vermont', - 'Virginia', 'Washington', 'West Virginia', 'Wisconsin', 'Wyoming' -]; - - -function test_method_names() { - $('#the-basics .typeahead').typeahead('destroy'); - $('#the-basics .typeahead').typeahead('open'); - $('#the-basics .typeahead').typeahead('close'); - $('#the-basics .typeahead').typeahead('val'); - $('#the-basics .typeahead').typeahead('val', 'test value'); -} - - -function test_options() { - - var dataSets: Twitter.Typeahead.Dataset[] = []; - - function with_empty_options() { - $('#the-basics .typeahead').typeahead({}, dataSets); - } - - function with_hint_option() { - $('#the-basics .typeahead').typeahead({ hint: true }, dataSets); - } - - function with_highlight_option() { - $('#the-basics .typeahead').typeahead({ highlight: true }, dataSets); - } - - function with_minLength_option() { - $('#the-basics .typeahead').typeahead({ minLength: 1 }, dataSets); - } - - function with_all_options() { - $('#the-basics .typeahead').typeahead({ - hint: true, - highlight: true, - minLength: 1 - }, - dataSets - ); - } -} - -function test_datasets_array() { - +function test_typeahead() { var options: Twitter.Typeahead.Options = {}; + var dataset: Twitter.Typeahead.Dataset = { source: null }; - function with_only_source() { - $('#the-basics .typeahead').typeahead(options, [{ - source: substringMatcher(states) - }]); + function test_typeahead_methods() { + $('.typeahead').typeahead(options, dataset); + $('.typeahead').typeahead(options, new Array(dataset)); + $('.typeahead').typeahead('val'); + $('.typeahead').typeahead('val', 'test value'); + $('.typeahead').typeahead('open'); + $('.typeahead').typeahead('close'); + $('.typeahead').typeahead('destroy'); } - function with_name_option() { + function test_typeahead_options() { + function options_empty() { + $('.typeahead').typeahead({}, dataset); + } - $('#the-basics .typeahead').typeahead(options, [{ - name: 'states', - source: substringMatcher(states), - }]); + function test_typeahead_option_hint() { + $('.typeahead').typeahead({ hint: true }, dataset); + } + + function test_typeahead_option_minLength() { + $('.typeahead').typeahead({ minLength: 1 }, dataset); + } + + function test_typeahead_option_highlight() { + $('.typeahead').typeahead({ highlight: true }, dataset); + } + + function test_typeahead_option_classNames() { + $('.typeahead').typeahead({ classNames: { input: 'tt-input' } }, dataset); + } + + function test_typeahead_options_all() { + $('.typeahead').typeahead({ + hint: true, + minLength: 1, + highlight: true, + classNames: { input: 'tt-input' } + }, dataset); + } } - function with_displayKey_option() { - $('#the-basics .typeahead').typeahead(options, [{ - display: 'value', - source: substringMatcher(states) - }] - ); + function test_typeahead_classNames() { + function test_typeahead_classNames_empty() { + var className: Twitter.Typeahead.ClassNames = {}; + } + + function test_typeahead_className_input() { + var className: Twitter.Typeahead.ClassNames = { input: 'tt-input' }; + } + + function test_typeahead_className_hint() { + var className: Twitter.Typeahead.ClassNames = { hint: 'tt-hint' }; + } + + function test_typeahead_className_menu() { + var className: Twitter.Typeahead.ClassNames = { menu: 'tt-menu' }; + } + + function test_typeahead_className_dataset() { + var className: Twitter.Typeahead.ClassNames = { dataset: 'tt-dataset' }; + } + + function test_typeahead_className_suggestion() { + var className: Twitter.Typeahead.ClassNames = { suggestion: 'tt-suggestion' }; + } + + function test_typeahead_className_empty() { + var className: Twitter.Typeahead.ClassNames = { empty: 'tt-empty' }; + } + + function test_typeahead_className_open() { + var className: Twitter.Typeahead.ClassNames = { open: 'tt-open' }; + } + + function test_typeahead_className_cursor() { + var className: Twitter.Typeahead.ClassNames = { cursor: 'tt-cursor' }; + } + + function test_typeahead_className_highlight() { + var className: Twitter.Typeahead.ClassNames = { highlight: 'tt-highlight' }; + } + + function test_typeahead_classNames_all() { + var className: Twitter.Typeahead.ClassNames = { + input: 'tt-input', + hint: 'tt-hint', + menu: 'tt-menu', + dataset: 'tt-dataset', + suggestion: 'tt-suggestion', + empty: 'tt-empty', + open: 'tt-open', + cursor: 'tt-cursor', + highlight: 'tt-highlight' + }; + } } - function with_templates_option() { - $('#the-basics .typeahead').typeahead(options, [{ - templates: {}, - source: substringMatcher(states) - }] - ); + function test_typeahead_datasets() { + function test_typeahead_dataset_source_bloodhout() { + var bo: Bloodhound.BloodhoundOptions = { datumTokenizer: null, queryTokenizer: null }; + var engine: Bloodhound = new Bloodhound(bo); + var dataset: Twitter.Typeahead.Dataset = { source: engine }; + } + + function test_typeahead_dataset_source_function() { + var dataset: Twitter.Typeahead.Dataset = { source: (query: string, syncResults: (result: string[]) => void, asyncResults?: (result: string[]) => void) => { } }; + } + + function test_typeahead_dataset_async() { + var dataset: Twitter.Typeahead.Dataset = { + source: null, + async: true + }; + } + + function test_typeahead_dataset_name() { + var dataset: Twitter.Typeahead.Dataset = { + source: null, + name: 'name' + }; + } + + function test_typeahead_dataset_limit() { + var dataset: Twitter.Typeahead.Dataset = { + source: null, + limit: 5 + }; + } + + function test_typeahead_dataset_display_string() { + var dataset: Twitter.Typeahead.Dataset = { + source: null, + display: "key" + }; + } + + function test_typeahead_dataset_display_function() { + var dataset: Twitter.Typeahead.Dataset = { + source: null, + display: (obj: string) => { return 'key'; } + }; + } } - function with_all_options() { - $('#the-basics .typeahead').typeahead(options, [{ - name: 'states', - display: 'value', - templates: {}, - source: substringMatcher(states) - }] - ); - } + function test_typeahead_templates() { + function test_typeahead_templates_empty() { + var templates: Twitter.Typeahead.Templates = {}; + } - function with_multiple_datasets() { - $('#the-basics .typeahead').typeahead(options, [ - { - name: 'states', - display: 'value', - templates: {}, - source: substringMatcher(states) - }, - { - name: 'states alternative', - display: 'value', - templates: {}, - source: substringMatcher(states) - } - ]); + function dataset_template_notfound_string() { + var templates: Twitter.Typeahead.Templates = { notFound: 'not found' }; + } + + function dataset_template_notfound_function() { + var templates: Twitter.Typeahead.Templates = { notFound: (query: string) => { return 'not found'; } }; + } + + function dataset_template_pending_string() { + var templates: Twitter.Typeahead.Templates = { pending: 'pending' }; + } + + function dataset_template_pending_function() { + var templates: Twitter.Typeahead.Templates = { pending: (query: string) => { return 'pending'; } }; + } + + function dataset_template_header_string() { + var templates: Twitter.Typeahead.Templates = { header: 'header' }; + } + + function dataset_template_header_function() { + var templates: Twitter.Typeahead.Templates = { header: (query: string) => { return 'header'; } }; + } + + function dataset_template_footer_string() { + var templates: Twitter.Typeahead.Templates = { footer: 'footer' }; + } + + function dataset_template_footer_function() { + var templates: Twitter.Typeahead.Templates = { footer: (query: string) => { return 'footer'; } }; + } + + function dataset_template_suggestion() { + var templates: Twitter.Typeahead.Templates = { suggestion: (suggestion: string) => { return 'suggestion'; } }; + } } } +function test_bloodhout() { + var options: Bloodhound.BloodhoundOptions = { datumTokenizer: null, queryTokenizer: null }; + var engine: Bloodhound = new Bloodhound(options); -function test_datasets_objects() { - - var options: Twitter.Typeahead.Options = {}; - - function with_only_source() { - $('#the-basics .typeahead').typeahead(options, { - source: substringMatcher(states) - }); + function test_bloodhout_static() { + var old: Bloodhound = Bloodhound.noConflict(); + var tokenizers: Bloodhound.Tokenizers = Bloodhound.tokenizers; } - function with_name_option() { + function test_bloodhout_methods() { + // initialize + var promise1: JQueryPromise = engine.initialize(); + var promise2: JQueryPromise = engine.initialize(); + var promise3: JQueryPromise = engine.initialize(true); - $('#the-basics .typeahead').typeahead(options, { - name: 'states', - source: substringMatcher(states), - }); + // add + engine.add(new Array()); + + // get + var data1: string[] = engine.get(new Array()); + + // search + var sync: (datums: string[]) => {}; + var async: (datums: string[]) => {}; + var data2: string[] = engine.search("query", sync, async); + + // all + var data3: string[] = engine.all(); + + // clear + var engine1: Bloodhound = engine.clear(); + + // clearPrefetchCache + var engine2: Bloodhound = engine.clearPrefetchCache(); + + // clearRemoteCache + var engine3: Bloodhound = engine.clearRemoteCache(); } - function with_displayKey_option() { - $('#the-basics .typeahead').typeahead(options, - { - display: 'value', - source: substringMatcher(states) + function test_bloodhout_options() { + function test_bloodhout_options_datumTokenizer() { + var options: Bloodhound.BloodhoundOptions = { + datumTokenizer: (datum: string) => { return new Array(); }, + queryTokenizer: null + }; + } + + function test_bloodhout_options_queryTokenizer() { + var options: Bloodhound.BloodhoundOptions = { + datumTokenizer: null, + queryTokenizer: (query: string) => { return new Array(); } + }; + } + + function test_bloodhout_options_initialize() { + var options: Bloodhound.BloodhoundOptions = { + datumTokenizer: null, + queryTokenizer: null, + initialize: true + }; + } + + function test_bloodhout_options_sufficient() { + var options: Bloodhound.BloodhoundOptions = { + datumTokenizer: null, + queryTokenizer: null, + sufficient: 5 + }; + } + + function test_bloodhout_options_sorter() { + var options: Bloodhound.BloodhoundOptions = { + datumTokenizer: null, + queryTokenizer: null, + sorter: (a: string, b: string) => { return 0 } + }; + } + + function test_bloodhout_options_local_array() { + var options: Bloodhound.BloodhoundOptions = { + datumTokenizer: null, + queryTokenizer: null, + local: new Array() + }; + } + + function test_bloodhout_options_local_function() { + var options: Bloodhound.BloodhoundOptions = { + datumTokenizer: null, + queryTokenizer: null, + local: () => { return new Array() } + }; + } + + function test_bloodhout_options_prefetch_string() { + var options: Bloodhound.BloodhoundOptions = { + datumTokenizer: null, + queryTokenizer: null, + prefetch: 'url' + }; + } + + function test_bloodhout_options_prefetch_object() { + var options: Bloodhound.BloodhoundOptions = { + datumTokenizer: null, + queryTokenizer: null, + prefetch: { url: 'url' } + }; + } + + function test_bloodhout_options_remote_string() { + var options: Bloodhound.BloodhoundOptions = { + datumTokenizer: null, + queryTokenizer: null, + remote: 'url' + }; + } + + function test_bloodhout_options_remote_object() { + var options: Bloodhound.BloodhoundOptions = { + datumTokenizer: null, + queryTokenizer: null, + remote: { url: 'url' } + }; + } + + function test_bloodhout_options_all() { + var options: Bloodhound.BloodhoundOptions = { + datumTokenizer: (datum: string) => { return new Array(); }, + queryTokenizer: (query: string) => { return new Array(); }, + initialize: true, + sufficient: 5, + sorter: (a: string, b: string) => { return 0 }, + local: () => { return new Array() }, + prefetch: { url: 'url' }, + remote: { url: 'url' } + }; + } + } + + function test_bloodhout_prefetch_options() { + function test_bloodhout_prefetch_options_url() { + var options: Bloodhound.PrefetchOptions = { + url: 'url' + }; + } + + function test_bloodhout_prefetch_options_cache() { + var options: Bloodhound.PrefetchOptions = { + url: 'url', + cache: true + }; + } + + function test_bloodhout_prefetch_options_ttl() { + var options: Bloodhound.PrefetchOptions = { + url: 'url', + ttl: 86400000 // 1 day + }; + } + + function test_bloodhout_prefetch_options_cacheKey() { + var options: Bloodhound.PrefetchOptions = { + url: 'url', + cacheKey: 'url' + }; + } + + function test_bloodhout_prefetch_options_thumbprint() { + var options: Bloodhound.PrefetchOptions = { + url: 'url', + thumbprint: 'thumbprint' + }; + } + + function test_bloodhout_prefetch_options_prepare() { + var ajaxSettings: JQueryAjaxSettings = { url: 'url' }; + + var options: Bloodhound.PrefetchOptions = { + url: 'url', + prepare: (settings: JQueryAjaxSettings) => { return ajaxSettings; } + }; + } + + function test_bloodhout_prefetch_options_transform() { + var options: Bloodhound.PrefetchOptions = { + url: 'url', + transform: (response: string[]) => { return new Array(); } + }; + } + + function test_bloodhout_prefetch_options_all() { + var ajaxSettings: JQueryAjaxSettings = { url: 'url' }; + + var options: Bloodhound.PrefetchOptions = { + url: 'url', + cache: true, + ttl: 86400000, + cacheKey: 'url', + thumbprint: 'thumbprint', + prepare: (settings: JQueryAjaxSettings) => { return ajaxSettings; }, + transform: (response: string[]) => { return new Array(); } + }; + } + } + + function test_bloodhout_remote_options() { + function test_bloodhout_remote_options_url() { + var options: Bloodhound.RemoteOptions = { + url: 'url' + }; + } + + function test_bloodhout_remote_options_prepare() { + var ajaxSettings: JQueryAjaxSettings = { url: 'url' }; + + var options: Bloodhound.RemoteOptions = { + url: 'url', + prepare: (query: string, settings: JQueryAjaxSettings) => { return ajaxSettings; } + }; + } + + function test_bloodhout_remote_options_wildcard() { + var options: Bloodhound.RemoteOptions = { + url: 'url', + wildcard: '%QUERY' + }; + } + + function test_bloodhout_remote_options_rateLimitby() { + var options: Bloodhound.RemoteOptions = { + url: 'url', + rateLimitby: 'debounce' + }; + } + + function test_bloodhout_remote_options_rateLimitWait() { + var options: Bloodhound.RemoteOptions = { + url: 'url', + rateLimitWait: 300 + }; + } + + function test_bloodhout_remote_options_transform() { + var options: Bloodhound.RemoteOptions = { + url: 'url', + transform: (response: string[]) => { return new Array(); } + }; + } + + function test_bloodhout_remote_options_all() { + var ajaxSettings: JQueryAjaxSettings = { url: 'url' }; + + var options: Bloodhound.RemoteOptions = { + url: 'url', + prepare: (query: string, settings: JQueryAjaxSettings) => { return ajaxSettings; }, + wildcard: '%QUERY', + rateLimitby: 'debounce', + rateLimitWait: 300, + transform: (response: string[]) => { return new Array(); } + }; + } + } + + function test_bloodhout_tokenizers() { + var tokenizers: Bloodhound.Tokenizers = { + whitespace: (str: string) => { return new Array(); }, + nonword: (str: string) => { return new Array(); }, + obj: { + whitespace: (str: string) => { return new Array(); }, + nonword: (str: string) => { return new Array(); } } - ); + }; } - - function with_templates_option() { - $('#the-basics .typeahead').typeahead(options, - { - templates: {}, - source: substringMatcher(states) - } - ); - } - - function with_all_options() { - $('#the-basics .typeahead').typeahead(options, - { - name: 'states', - display: x => x.value, - templates: {}, - source: substringMatcher(states) - } - ); - } - - function with_multiple_objects() { - $('#the-basics .typeahead').typeahead(options, - { - name: 'states', - display: 'value', - templates: {}, - source: substringMatcher(states) - }, - { - name: 'states alternative', - display: 'value', - templates: {}, - source: substringMatcher(states) - } - ); - } -} - -function test_dataset_templates() { - - var options: Twitter.Typeahead.Options = {}; - - function with_no_options() { - $('#the-basics .typeahead').typeahead(options, { - source: substringMatcher(states), - templates: {} - }); - } - - function with_empty_option() { - $('#the-basics .typeahead').typeahead(options, { - source: substringMatcher(states), - templates: { empty: 'no results' } - }); - } - - function with_empty_option_as_a_function() { - $('#the-basics .typeahead').typeahead(options, { - source: substringMatcher(states), - templates: { - empty: function (context: any) { - return context.name; - } - } - }); - } - - function with_footer_option() { - $('#the-basics .typeahead').typeahead(options, { - source: substringMatcher(states), - templates: { footer: 'custom footer' } - }); - } - - function with_footer_option_as_a_function() { - $('#the-basics .typeahead').typeahead(options, { - source: substringMatcher(states), - templates: { - footer: function (context: any) { - return context.name; - } - } - }); - } - - function with_header_option() { - $('#the-basics .typeahead').typeahead(options, { - source: substringMatcher(states), - templates: { header: 'custom header' } - }); - } - - function with_header_option_as_a_function() { - $('#the-basics .typeahead').typeahead(options, { - source: substringMatcher(states), - templates: { - header: function (context: any) { - return context.name; - } - } - }); - } - - function with_suggestion_option() { - $('#the-basics .typeahead').typeahead(options, { - source: substringMatcher(states), - templates: { - suggestion: function (context) { - return context.name; - } - } - }); - } - - function with_all_options() { - $('#the-basics .typeahead').typeahead(options, { - source: substringMatcher(states), - templates: { - empty: 'no results', - footer: 'custom footer', - header: 'custom header', - suggestion: function (context) { - return context.name; - } - }, - }); - } -} - -function test_value() { - var value: string = $('foo').typeahead('val'); - $('foo').typeahead('val', value); } \ No newline at end of file diff --git a/typeahead/typeahead.d.ts b/typeahead/typeahead.d.ts index b9228b430..b842786cc 100644 --- a/typeahead/typeahead.d.ts +++ b/typeahead/typeahead.d.ts @@ -20,9 +20,10 @@ interface JQuery { * * @constructor * @param options Options hash that's used for configuration - * @param datasets One or more datasets passed as rest parameters. + * @param dataset At least one dataset is required + * @param datasets Rest of the datasets. */ - typeahead(options: Twitter.Typeahead.Options, ...datasets: Twitter.Typeahead.Dataset[]): JQuery; + typeahead(options: Twitter.Typeahead.Options, dataset: Twitter.Typeahead.Dataset, ...datasets: Twitter.Typeahead.Dataset[]): JQuery; /** * Returns the current value of the typeahead. @@ -1122,7 +1123,7 @@ declare class Bloodhound { * Returns a reference to Bloodhound and reverts window.Bloodhound to its * previous value. Can be used to avoid naming collisions. */ - public static noConflict(): any; + public static noConflict(): Bloodhound; /** * The Bloodhound suggestion engine is token-based, so how datums and queries are tokenized plays a vital role in the quality of search results.