diff --git a/types/select2/index.d.ts b/types/select2/index.d.ts index 51e297a9a8..3f04a52414 100644 --- a/types/select2/index.d.ts +++ b/types/select2/index.d.ts @@ -1,10 +1,9 @@ -// Type definitions for Select2 4.0.1 +// Type definitions for Select2 4.0 // Project: http://ivaynberg.github.com/select2/ // Definitions by: Boris Yankov // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.3 - /// interface Select2QueryOptions { @@ -14,15 +13,14 @@ interface Select2QueryOptions { callback?: (result: { results: any; more?: boolean; context?: any; }) => void; } -interface AjaxFunction { - (settings: JQueryAjaxSettings, success?: (data: any) => null, failure?: () => null): JQueryXHR; -} +type AjaxFunction = + (settings: JQueryAjaxSettings, success?: (data: any) => null, failure?: () => null) => JQueryXHR; interface Select2AjaxOptions extends JQueryAjaxSettings { transport?: AjaxFunction; /** - * Url to make request to, Can be string or a function returning a string. - */ + * Url to make request to, can be string or a function returning a string. + */ url?: any; dataType?: string; delay?: number; @@ -66,7 +64,6 @@ interface Select2Options { formatInputTooShort?: (term: string, minLength: number) => string; formatSelectionTooBig?: (maxSize: number) => string; formatLoadMore?: (pageNumber: number) => string; - createSearchChoice?: (term: string, data: any) => any; initSelection?: (element: JQuery, callback: (data: any) => void) => void; tokenizer?: (input: string, selection: any[], selectCallback: () => void, options: Select2Options) => string; tokenSeparators?: string[]; @@ -82,8 +79,8 @@ interface Select2Options { escapeMarkup?: (markup: string) => string; theme?: string; /** - * Template can return both plain string that will be HTML escaped and a jquery object that can render HTML - */ + * Template can return both plain string that will be HTML escaped and a jquery object that can render HTML + */ templateSelection?: (object: Select2SelectionObject, container: JQuery) => any; templateResult?: (object: Select2SelectionObject) => any; language?: string | string[] | {}; @@ -92,8 +89,6 @@ interface Select2Options { dropdownParent?: JQuery; debug?: boolean; dropdownAdapter?: any; - selectionAdapter?: any; - resultsAdapter?: any; } interface Select2JQueryEventObject extends JQueryEventObject { @@ -189,32 +184,22 @@ interface JQuery { on(events: "change", selector?: string, data?: any, handler?: (eventObject: Select2JQueryEventObject) => any): JQuery; on(events: "change", selector?: string, handler?: (eventObject: Select2JQueryEventObject) => any): JQuery; on(events: "change", handler?: (eventObject: Select2JQueryEventObject) => any): JQuery; - on(events: "select2-opening", handler?: (eventObject: Select2JQueryEventObject) => any): JQuery; - on(events: "select2-open", handler?: (eventObject: Select2JQueryEventObject) => any): JQuery; - on(events: "select2-close", handler?: (eventObject: Select2JQueryEventObject) => any): JQuery; - on(events: "select2-highlight", handler?: (eventObject: Select2JQueryEventObject) => any): JQuery; - on(events: "select2-selecting", handler?: (eventObject: Select2JQueryEventObject) => any): JQuery; - on(events: "select2-removing", handler?: (eventObject: Select2JQueryEventObject) => any): JQuery; - on(events: "select2-removed", handler?: (eventObject: Select2JQueryEventObject) => any): JQuery; - on(events: "select2-loaded", handler?: (eventObject: Select2JQueryEventObject) => any): JQuery; - on(events: "select2-focus", handler?: (eventObject: Select2JQueryEventObject) => any): JQuery; - on(events: "select2-blur", handler?: (eventObject: Select2JQueryEventObject) => any): JQuery; - on(events: "select2-opening", handler?: (eventObject: Select2JQueryEventObject) => any): JQuery; - on(events: "select2-open", handler?: (eventObject: Select2JQueryEventObject) => any): JQuery; - on(events: "select2-close", handler?: (eventObject: Select2JQueryEventObject) => any): JQuery; - on(events: "select2-highlight", handler?: (eventObject: Select2JQueryEventObject) => any): JQuery; - on(events: "select2-selecting", handler?: (eventObject: Select2JQueryEventObject) => any): JQuery; - on(events: "select2-removing", handler?: (eventObject: Select2JQueryEventObject) => any): JQuery; - on(events: "select2-removed", handler?: (eventObject: Select2JQueryEventObject) => any): JQuery; - on(events: "select2-loaded", handler?: (eventObject: Select2JQueryEventObject) => any): JQuery; - on(events: "select2-focus", handler?: (eventObject: Select2JQueryEventObject) => any): JQuery; - on(events: "select2-blur", handler?: (eventObject: Select2JQueryEventObject) => any): JQuery; + on(events: "select2:closing", handler?: (eventObject: Select2JQueryEventObject) => any): JQuery; + on(events: "select2:close", handler?: (eventObject: Select2JQueryEventObject) => any): JQuery; + on(events: "select2:opening", handler?: (eventObject: Select2JQueryEventObject) => any): JQuery; + on(events: "select2:open", handler?: (eventObject: Select2JQueryEventObject) => any): JQuery; + on(events: "select2:selecting", handler?: (eventObject: Select2JQueryEventObject) => any): JQuery; + on(events: "select2:select", handler?: (eventObject: Select2JQueryEventObject) => any): JQuery; + on(events: "select2:unselecting", handler?: (eventObject: Select2JQueryEventObject) => any): JQuery; + on(events: "select2:unselect", handler?: (eventObject: Select2JQueryEventObject) => any): JQuery; } declare class Select2 { constructor(element: JQuery, options: Select2Options); focus(): void; destroy(): void; + // TODO: Don't use 'Function' as a type. + // tslint:disable-next-line:ban-types on(event: string, callback: Function): void; selection: any; dropdown: any; diff --git a/types/select2/select2-tests.ts b/types/select2/select2-tests.ts index 29da3b6f81..0a5458063a 100644 --- a/types/select2/select2-tests.ts +++ b/types/select2/select2-tests.ts @@ -12,9 +12,9 @@ $("#e2_3").select2({ $("#e3").select2({ minimumInputLength: 2 }); -function format(state) { +function format(state: any) { if (!state.id) return state.text; - return "" + state.text; + return `` + state.text; } $("#e4").select2({ formatResult: format, @@ -22,11 +22,11 @@ $("#e4").select2({ }); $("#e5").select2({ minimumInputLength: 1, - query: function (query) { - var data = { results: [] }, i, j, s; - for (i = 1; i < 5; i++) { - s = ""; - for (j = 0; j < i; j++) { s = s + query.term; } + query(query) { + const data = { results: [] as IdTextPair[] }; + for (let i = 1; i < 5; i++) { + let s = ""; + for (let j = 0; j < i; j++) { s = s + query.term; } data.results.push({ id: query.term + i, text: s }); } } @@ -37,7 +37,7 @@ $("#e10").select2({ data: [{ id: 0, text: 'enhancement' }, { id: 1, text: 'bug' }, { id: 2, text: 'duplicate' }, { id: 3, text: 'invalid' }, { id: 4, text: 'wontfix' }] }); -var data = [{ id: 0, tag: 'enhancement' }, { id: 1, tag: 'bug' }, { id: 2, tag: 'duplicate' }, { id: 3, tag: 'invalid' }, { id: 4, tag: 'wontfix' }]; +const data = [{ id: 0, tag: 'enhancement' }, { id: 1, tag: 'bug' }, { id: 2, tag: 'duplicate' }, { id: 3, tag: 'invalid' }, { id: 4, tag: 'wontfix' }]; $("#e10_2").select2({ data: { results: data, text: 'tag' }, @@ -46,11 +46,18 @@ $("#e10_2").select2({ }); $("#e10_3").select2({ - data: { results: data, text: function (item) { console.log('called with', item); return item.tag; } }, + data: { + results: data, + text: (item: {tag: string}) => { + console.log('called with', item); + return item.tag; + }}, formatSelection: format, formatResult: format }); -var movieFormatResult, movieFormatSelection; + +let movieFormatResult; +let movieFormatSelection; $("#e6").select2({ placeholder: "Search for a movie", minimumInputLength: 1, @@ -58,14 +65,14 @@ $("#e6").select2({ url: "http://api.rottentomatoes.com/api/public/v1.0/movies.json", dataType: 'jsonp', cache: false, - data: function (params, page) { + data(params, page) { return { q: params.term, page_limit: 10, apikey: "ju6z9mjyajq2djue3gbvv26t" }; }, - results: function (data, page) { + results(data, page) { return { results: data.movies }; } }, @@ -73,20 +80,22 @@ $("#e6").select2({ formatSelection: movieFormatSelection, dropdownCssClass: "bigdrop" }); + +let t: ArrayLike; $("#e6").select2({ placeholder: "Search for a movie", minimumInputLength: 1, ajax: { - url: () => { return "http://api.rottentomatoes.com/api/public/v1.0/movies.json"; }, + url: () => "http://api.rottentomatoes.com/api/public/v1.0/movies.json", dataType: 'jsonp', - data: function (params, page) { + data(params, page) { return { q: params.term, page_limit: 10, apikey: "ju6z9mjyajq2djue3gbvv26t" }; }, - results: function (data, page) { + results(data, page) { return { results: data.movies }; } }, @@ -102,14 +111,14 @@ $("#e6").select2({ type: 'GET', dataType: 'jsonp', cache: false, - data: function (params, page) { + data(params, page) { return { q: params.term, page_limit: 10, apikey: "ju6z9mjyajq2djue3gbvv26t" }; }, - results: function (data, page) { + results(data, page) { return { results: data.movies }; } }, @@ -124,17 +133,17 @@ $("#e7").select2({ url: "http://api.rottentomatoes.com/api/public/v1.0/movies.json", dataType: 'jsonp', delay: 100, - data: function (params, page) { + data(params, aPage) { return { q: params.term, page_limit: 10, - page: page, + page: aPage, apikey: "ju6z9mjyajq2djue3gbvv26t" }; }, - results: function (data, page) { - var more = (page * 10) < data.total; - return { results: data.movies, more: more }; + results(data, page) { + const moreValue = (page * 10) < data.total; + return { results: data.movies, more: moreValue }; } }, formatResult: movieFormatResult, @@ -142,7 +151,7 @@ $("#e7").select2({ dropdownCssClass: "bigdrop" }); -function sort(elements) { +function sort(elements: any) { return elements.sort(); } $("#e20").select2({ @@ -150,66 +159,65 @@ $("#e20").select2({ }); $("#e8").select2(); -$("#e8_get").click(function () { alert("Selected value is: " + $("#e8").select2("val")); }); -$("#e8_set").click(function () { $("#e8").select2("val", "CA"); }); -$("#e8_cl").click(function () { $("#e8").select2("val", ""); }); -$("#e8_get2").click(function () { alert("Selected data is: " + JSON.stringify($("#e8").select2("data"))); }); -$("#e8_set2").click(function () { $("#e8").select2("data", { id: "CA", text: "California" }); }); -$("#e8_open").click(function () { $("#e8").select2("open"); }); -$("#e8_close").click(function () { $("#e8").select2("close"); }); +$("#e8_get").click(() => alert("Selected value is: " + $("#e8").select2("val"))); +$("#e8_set").click(() => $("#e8").select2("val", "CA")); +$("#e8_cl").click(() => $("#e8").select2("val", "")); +$("#e8_get2").click(() => alert("Selected data is: " + JSON.stringify($("#e8").select2("data")))); +$("#e8_set2").click(() => $("#e8").select2("data", { id: "CA", text: "California" })); +$("#e8_open").click(() => $("#e8").select2("open")); +$("#e8_close").click(() => $("#e8").select2("close")); $("#e8_2").select2(); -$("#e8_2_get").click(function () { alert("Selected value is: " + $("#e8_2").select2("val")); }); -$("#e8_2_set").click(function () { $("#e8_2").select2("val", ["CA", "MA"]); }); -$("#e8_2_get2").click(function () { alert("Selected value is: " + JSON.stringify($("#e8_2").select2("data"))); }); -$("#e8_2_set2").click(function () { $("#e8_2").select2("data", [{ id: "CA", text: "California" }, { id: "MA", text: "Massachusetts" }]); }); -$("#e8_2_cl").click(function () { $("#e8_2").select2("val", ""); }); -$("#e8_2_open").click(function () { $("#e8_2").select2("open"); }); -$("#e8_2_close").click(function () { $("#e8_2").select2("close"); }); +$("#e8_2_get").click(() => alert("Selected value is: " + $("#e8_2").select2("val"))); +$("#e8_2_set").click(() => $("#e8_2").select2("val", ["CA", "MA"])); +$("#e8_2_get2").click(() => alert("Selected value is: " + JSON.stringify($("#e8_2").select2("data")))); +$("#e8_2_set2").click(() => $("#e8_2").select2("data", [{ id: "CA", text: "California" }, { id: "MA", text: "Massachusetts" }])); +$("#e8_2_cl").click(() => $("#e8_2").select2("val", "")); +$("#e8_2_open").click(() => $("#e8_2").select2("open")); +$("#e8_2_close").click(() => $("#e8_2").select2("close")); $("#e11").select2({ placeholder: "Select report type", allowClear: true, data: [{ id: 0, text: 'story' }, { id: 1, text: 'bug' }, { id: 2, text: 'task' }] }); $("#e11_2").select2({ - createSearchChoice: function (term, data) { if ($(data).filter(function () { return this.textContent.localeCompare(term) === 0; }).length === 0) { return { id: term, text: term }; } }, multiple: true, data: [{ id: 0, text: 'story' }, { id: 1, text: 'bug' }, { id: 2, text: 'task' }] }); -function log(e) { - var item = $("
  • " + e + "
  • "); +function log(e: string) { + const item = $(`
  • ${e}
  • `); $("#events_11").append(item); - item.animate({ opacity: 1 }, 10000, 'linear', function () { item.animate({ opacity: 0 }, 2000, 'linear', function () { item.remove(); }); }); + item.animate({ opacity: 1 }, 10000, 'linear', () => item.animate({ opacity: 0 }, 2000, 'linear', () => item.remove())); } $("#e11") // TS 0.9.5: correct overload not resolved https://typescript.codeplex.com/discussions/472172 - .on("change", function (e: Select2JQueryEventObject) { log(JSON.stringify({ val: e.val, added: e.added, removed: e.removed })); }) - .on("open", function () { log("open"); }); + .on("change", (e: Select2JQueryEventObject) => log(JSON.stringify({ val: e.val, added: e.added, removed: e.removed }))) + .on("open", () => log("open")); $("#e11_2") - .on("change", function (e: Select2JQueryEventObject) { log(JSON.stringify({ val: e.val, added: e.added, removed: e.removed })); }) - .on("open", function () { log("open"); }); + .on("change", (e: Select2JQueryEventObject) => log(JSON.stringify({ val: e.val, added: e.added, removed: e.removed }))) + .on("open", () => log("open")); $("#e12").select2({ tags: ["red", "green", "blue"] }); $("#e20").select2({ tags: ["red", "green", "blue"], tokenSeparators: [",", " "] }); $("#e13").select2(); -$("#e13_ca").click(function () { $("#e13").val("CA").trigger("change"); }); -$("#e13_ak_co").click(function () { $("#e13").val(["AK", "CO"]).trigger("change"); }); +$("#e13_ca").click(() => $("#e13").val("CA").trigger("change")); +$("#e13_ak_co").click(() => $("#e13").val(["AK", "CO"]).trigger("change")); $("#e14").val(["AL", "AZ"]).select2(); -$("#e14_init").click(function () { $("#e14").select2(); }); -$("#e14_destroy").click(function () { $("#e14").select2("destroy"); }); +$("#e14_init").click(() => $("#e14").select2()); +$("#e14_destroy").click(() => $("#e14").select2("destroy")); $("#e15").select2({ tags: ["red", "green", "blue", "orange", "white", "black", "purple", "cyan", "teal"] }); -$("#e15").on("change", function () { $("#e15_val").html($("#e15").val() as string); }); +$("#e15").on("change", () => $("#e15_val").html($("#e15").val() as string)); $("#e16").select2(); $("#e16_2").select2(); -$("#e16_enable").click(function () { $("#e16,#e16_2").select2("enable"); }); -$("#e16_disable").click(function () { $("#e16,#e16_2").select2("disable"); }); +$("#e16_enable").click(() => $("#e16,#e16_2").select2("enable")); +$("#e16_disable").click(() => $("#e16,#e16_2").select2("disable")); $("#e17").select2({ - matcher: function (term, text) { return text.toUpperCase().indexOf(term.toUpperCase()) == 0; } + matcher: (term, text) => text.toUpperCase().indexOf(term.toUpperCase()) === 0 }); $("#e17_2").select2({ - matcher: function (term, text, opt) { + matcher: (term, text, opt) => { return text.toUpperCase().indexOf(term.toUpperCase()) >= 0 || opt.attr("alt").toUpperCase().indexOf(term.toUpperCase()) >= 0; } diff --git a/types/select2/tsconfig.json b/types/select2/tsconfig.json index 292cf878b4..257d07ea49 100644 --- a/types/select2/tsconfig.json +++ b/types/select2/tsconfig.json @@ -5,8 +5,8 @@ "es6", "dom" ], - "noImplicitAny": false, - "noImplicitThis": false, + "noImplicitAny": true, + "noImplicitThis": true, "strictNullChecks": false, "strictFunctionTypes": true, "baseUrl": "../", diff --git a/types/select2/tslint.json b/types/select2/tslint.json index a41bf5d19a..08b1465cd6 100644 --- a/types/select2/tslint.json +++ b/types/select2/tslint.json @@ -1,79 +1,6 @@ { "extends": "dtslint/dt.json", "rules": { - "adjacent-overload-signatures": false, - "array-type": false, - "arrow-return-shorthand": false, - "ban-types": false, - "callable-types": false, - "comment-format": false, - "dt-header": false, - "eofline": false, - "export-just-namespace": false, - "import-spacing": false, - "interface-name": false, - "interface-over-type-literal": false, - "jsdoc-format": false, - "max-line-length": false, - "member-access": false, - "new-parens": false, - "no-any-union": false, - "no-boolean-literal-compare": false, - "no-conditional-assignment": false, - "no-consecutive-blank-lines": false, - "no-construct": false, - "no-declare-current-package": false, - "no-duplicate-imports": false, - "no-duplicate-variable": false, - "no-empty-interface": false, - "no-for-in-array": false, - "no-inferrable-types": false, - "no-internal-module": false, - "no-irregular-whitespace": false, - "no-mergeable-namespace": false, - "no-misused-new": false, - "no-namespace": false, - "no-object-literal-type-assertion": false, - "no-padding": false, - "no-redundant-jsdoc": false, - "no-redundant-jsdoc-2": false, - "no-redundant-undefined": false, - "no-reference-import": false, - "no-relative-import-in-test": false, - "no-self-import": false, - "no-single-declare-module": false, - "no-string-throw": false, - "no-unnecessary-callback-wrapper": false, - "no-unnecessary-class": false, - "no-unnecessary-generics": false, - "no-unnecessary-qualifier": false, - "no-unnecessary-type-assertion": false, - "no-useless-files": false, - "no-var-keyword": false, - "no-var-requires": false, - "no-void-expression": false, - "no-trailing-whitespace": false, - "object-literal-key-quotes": false, - "object-literal-shorthand": false, - "one-line": false, - "one-variable-per-declaration": false, - "only-arrow-functions": false, - "prefer-conditional-expression": false, - "prefer-const": false, - "prefer-declare-function": false, - "prefer-for-of": false, - "prefer-method-signature": false, - "prefer-template": false, - "radix": false, - "semicolon": false, - "space-before-function-paren": false, - "space-within-parens": false, - "strict-export-declare-modifiers": false, - "trim-file": false, - "triple-equals": false, - "typedef-whitespace": false, - "unified-signatures": false, - "void-return": false, - "whitespace": false + "unified-signatures": false } } diff --git a/types/select2/v3/index.d.ts b/types/select2/v3/index.d.ts new file mode 100644 index 0000000000..21bb2611c7 --- /dev/null +++ b/types/select2/v3/index.d.ts @@ -0,0 +1,174 @@ +// Type definitions for Select2 3.5 +// Project: http://ivaynberg.github.com/select2/ +// Definitions by: Boris Yankov +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.3 + + +/// + +interface Select2QueryOptions { + term?: string; + page?: number; + context?: any; + callback?: (result: { results: any; more?: boolean; context?: any; }) => void; +} + +interface AjaxFunction { + (settings: JQueryAjaxSettings): JQueryXHR; + (url: string, settings?: JQueryAjaxSettings): JQueryXHR; +} + +interface Select2AjaxOptions extends JQueryAjaxSettings { + transport?: AjaxFunction; + /** + * Url to make request to, Can be string or a function returning a string. + */ + url?: any; + dataType?: string; + quietMillis?: number; + cache?: boolean; + jsonpCallback?: any; + data?: (term: string, page: number, context: any) => any; + results?: (term: any, page: number, context: any) => any; + params?: any; +} + +interface IdTextPair { + id: any; + text: string; +} + +interface Select2Options { + width?: string; + dropdownAutoWidth?: boolean; + minimumInputLength?: number; + maximumInputLength?: number; + minimumResultsForSearch?: number; + maximumSelectionSize?: number; + placeholder?: string; + separator?: string; + allowClear?: boolean; + multiple?: boolean; + closeOnSelect?: boolean; + openOnEnter?: boolean; + id?: (object: any) => string; + matcher?: (term: string, text: string, option: any) => boolean; + formatSelection?: (object: any, container: JQuery, escapeMarkup: (markup: string) => string) => string; + formatResult?: (object: any, container: JQuery, query: any, escapeMarkup: (markup: string) => string) => string; + formatResultCssClass?: (object: any) => string; + formatNoMatches?: (term: string) => string; + formatSearching?: () => string; + formatInputTooShort?: (term: string, minLength: number) => string; + formatSelectionTooBig?: (maxSize: number) => string; + formatLoadMore?: (pageNumber: number) => string; + createSearchChoice?: (term: string, data: any) => any; + initSelection?: (element: JQuery, callback: (data: any) => void) => void; + tokenizer?: (input: string, selection: any[], selectCallback: () => void, options: Select2Options) => string; + tokenSeparators?: string[]; + query?: (options: Select2QueryOptions) => void; + ajax?: Select2AjaxOptions; + data?: any; + tags?: any; + containerCss?: any; + containerCssClass?: any; + dropdownCss?: any; + dropdownCssClass?: any; + escapeMarkup?: (markup: string) => string; +} + +interface Select2JQueryEventObject extends JQueryEventObject { + val: any; + added: any; + removed: any; + choice: { + id: any; + text: string; + }; +} + +interface Select2Plugin { + (): JQuery; + (it: IdTextPair): JQuery; + + /** + * Get the id value of the current selection + */ + (method: 'val'): any; + /** + * Set the id value of the current selection + * @params value Value to set the id to + * @params triggerChange Should a change event be triggered + */ + (method: 'val', value: any, triggerChange?: boolean): any; + /** + * Get the data object of the current selection + */ + (method: 'data'): any; + /** + * Set the data of the current selection + * @params value Object to set the data to + * @params triggerChange Should a change event be triggered + */ + (method: 'data', value: any, triggerChange?: boolean): any; + /** + * Reverts changes to DOM done by Select2. Any selection done via Select2 will be preserved. + */ + (method: 'destroy'): JQuery; + /** + * Opens the dropdown + */ + (method: 'open'): JQuery; + /** + * Closes the dropdown + */ + (method: 'close'): JQuery; + /** + * Enables or disables Select2 and its underlying form component + * @param value True if it should be enabled false if it should be disabled + */ + (method: 'enable', value?: boolean): JQuery; + /** + * Toggles readonly mode on Select2 and its underlying form component + * @param value True if it should be readonly false if it should be read write + */ + (method: 'readonly', value: boolean): JQuery; + /** + * Retrieves the main container element that wraps all of DOM added by Select2 + */ + (method: 'container'): JQuery; + /** + * Notifies Select2 that a drag and drop sorting operation has started + */ + (method: 'onSortStart'): JQuery; + /** + * Notifies Select2 that a drag and drop sorting operation has finished + */ + (method: 'onSortEnd'): JQuery; + /** + * Executes a new search using the provided value. Example: $("#tags").select2("search", "California") + */ + (method: 'search'): JQuery; + + (options: Select2Options): JQuery; +} + +interface JQuery { + select2: Select2Plugin; + off(events?: "change", selector?: any, handler?: (eventObject: Select2JQueryEventObject) => any): JQuery; + + on(events: "change", selector?: string, data?: any, handler?: (eventObject: Select2JQueryEventObject) => any): JQuery; + on(events: "change", selector?: string, handler?: (eventObject: Select2JQueryEventObject) => any): JQuery; + on(events: "change", handler?: (eventObject: Select2JQueryEventObject) => any): JQuery; + on(events: "select2-opening", handler?: (eventObject: Select2JQueryEventObject) => any): JQuery; + on(events: "select2-open", handler?: (eventObject: Select2JQueryEventObject) => any): JQuery; + on(events: "select2-close", handler?: (eventObject: Select2JQueryEventObject) => any): JQuery; + on(events: "select2-highlight", handler?: (eventObject: Select2JQueryEventObject) => any): JQuery; + on(events: "select2-selecting", handler?: (eventObject: Select2JQueryEventObject) => any): JQuery; + on(events: "select2-clearing", handler?: (eventObject: Select2JQueryEventObject) => any): JQuery; + on(events: "select2-removing", handler?: (eventObject: Select2JQueryEventObject) => any): JQuery; + on(events: "select2-removed", handler?: (eventObject: Select2JQueryEventObject) => any): JQuery; + on(events: "select2-loaded", handler?: (eventObject: Select2JQueryEventObject) => any): JQuery; + on(events: "select2-focus", handler?: (eventObject: Select2JQueryEventObject) => any): JQuery; + on(events: "select2-blur", handler?: (eventObject: Select2JQueryEventObject) => any): JQuery; +} diff --git a/types/select2/v3/select2-tests.ts b/types/select2/v3/select2-tests.ts new file mode 100644 index 0000000000..3b331cec3b --- /dev/null +++ b/types/select2/v3/select2-tests.ts @@ -0,0 +1,224 @@ +$("#e9").select2(); +$("#e2").select2({ + placeholder: "Select a State", + allowClear: true +}); +$("#e2_2").select2({ + placeholder: "Select a State" +}); +$("#e3").select2({ + minimumInputLength: 2 +}); +function format(state) { + if (!state.id) return state.text; + return "" + state.text; +} +$("#e4").select2({ + formatResult: format, + formatSelection: format +}); +$("#e5").select2({ + minimumInputLength: 1, + query: function (query) { + var data = { results: [] }, i, j, s; + for (i = 1; i < 5; i++) { + s = ""; + for (j = 0; j < i; j++) { s = s + query.term; } + data.results.push({ id: query.term + i, text: s }); + } + } +}); + +$("#e19").select2({ maximumSelectionSize: 3 }); +$("#e10").select2({ + data: [{ id: 0, text: 'enhancement' }, { id: 1, text: 'bug' }, { id: 2, text: 'duplicate' }, { id: 3, text: 'invalid' }, { id: 4, text: 'wontfix' }] +}); + +var data = [{ id: 0, tag: 'enhancement' }, { id: 1, tag: 'bug' }, { id: 2, tag: 'duplicate' }, { id: 3, tag: 'invalid' }, { id: 4, tag: 'wontfix' }]; + +$("#e10_2").select2({ + data: { results: data, text: 'tag' }, + formatSelection: format, + formatResult: format +}); + +$("#e10_3").select2({ + data: { results: data, text: function (item) { console.log('called with', item); return item.tag; } }, + formatSelection: format, + formatResult: format +}); +var movieFormatResult, movieFormatSelection; +$("#e6").select2({ + placeholder: "Search for a movie", + minimumInputLength: 1, + ajax: { + url: "http://api.rottentomatoes.com/api/public/v1.0/movies.json", + dataType: 'jsonp', + cache: false, + data: function (term, page) { + return { + q: term, + page_limit: 10, + apikey: "ju6z9mjyajq2djue3gbvv26t" + }; + }, + results: function (data, page) { + return { results: data.movies }; + } + }, + formatResult: movieFormatResult, + formatSelection: movieFormatSelection, + dropdownCssClass: "bigdrop" +}); +$("#e6").select2({ + placeholder: "Search for a movie", + minimumInputLength: 1, + ajax: { + url: () => { return "http://api.rottentomatoes.com/api/public/v1.0/movies.json"; }, + dataType: 'jsonp', + data: function (term, page) { + return { + q: term, + page_limit: 10, + apikey: "ju6z9mjyajq2djue3gbvv26t" + }; + }, + results: function (data, page) { + return { results: data.movies }; + } + }, + formatResult: movieFormatResult, + formatSelection: movieFormatSelection, + dropdownCssClass: "bigdrop" +}); +$("#e6").select2({ + placeholder: "Search for a movie", + minimumInputLength: 1, + ajax: { + url: "http://api.rottentomatoes.com/api/public/v1.0/movies.json", + type: 'GET', + dataType: 'jsonp', + cache: false, + data: function (term, page) { + return { + q: term, + page_limit: 10, + apikey: "ju6z9mjyajq2djue3gbvv26t" + }; + }, + results: function (data, page) { + return { results: data.movies }; + } + }, + formatResult: movieFormatResult, + formatSelection: movieFormatSelection, + dropdownCssClass: "bigdrop" +}); +$("#e7").select2({ + placeholder: "Search for a movie", + minimumInputLength: 3, + ajax: { + url: "http://api.rottentomatoes.com/api/public/v1.0/movies.json", + dataType: 'jsonp', + quietMillis: 100, + data: function (term, page) { + return { + q: term, + page_limit: 10, + page: page, + apikey: "ju6z9mjyajq2djue3gbvv26t" + }; + }, + results: function (data, page) { + var more = (page * 10) < data.total; + return { results: data.movies, more: more }; + } + }, + formatResult: movieFormatResult, + formatSelection: movieFormatSelection, + dropdownCssClass: "bigdrop" +}); + +function sort(elements) { + return elements.sort(); +} + +$("#e8").select2(); +$("#e8_get").click(function () { alert("Selected value is: " + $("#e8").select2("val")); }); +$("#e8_set").click(function () { $("#e8").select2("val", "CA"); }); +$("#e8_cl").click(function () { $("#e8").select2("val", ""); }); +$("#e8_get2").click(function () { alert("Selected data is: " + JSON.stringify($("#e8").select2("data"))); }); +$("#e8_set2").click(function () { $("#e8").select2("data", { id: "CA", text: "California" }); }); +$("#e8_open").click(function () { $("#e8").select2("open"); }); +$("#e8_close").click(function () { $("#e8").select2("close"); }); +$("#e8_2").select2(); +$("#e8_2_get").click(function () { alert("Selected value is: " + $("#e8_2").select2("val")); }); +$("#e8_2_set").click(function () { $("#e8_2").select2("val", ["CA", "MA"]); }); +$("#e8_2_get2").click(function () { alert("Selected value is: " + JSON.stringify($("#e8_2").select2("data"))); }); +$("#e8_2_set2").click(function () { $("#e8_2").select2("data", [{ id: "CA", text: "California" }, { id: "MA", text: "Massachusetts" }]); }); +$("#e8_2_cl").click(function () { $("#e8_2").select2("val", ""); }); +$("#e8_2_open").click(function () { $("#e8_2").select2("open"); }); +$("#e8_2_close").click(function () { $("#e8_2").select2("close"); }); +$("#e11").select2({ + placeholder: "Select report type", + allowClear: true, + data: [{ id: 0, text: 'story' }, { id: 1, text: 'bug' }, { id: 2, text: 'task' }] +}); +$("#e11_2").select2({ + createSearchChoice: function (term, data) { if ($(data).filter(function () { return this.textContent.localeCompare(term) === 0; }).length === 0) { return { id: term, text: term }; } }, + multiple: true, + data: [{ id: 0, text: 'story' }, { id: 1, text: 'bug' }, { id: 2, text: 'task' }] +}); +function log(e) { + var item = $("
  • " + e + "
  • "); + $("#events_11").append(item); + item.animate({ opacity: 1 }, 10000, 'linear', function () { item.animate({ opacity: 0 }, 2000, 'linear', function () { item.remove(); }); }); +} +$("#e11") + // TS 0.9.5: correct overload not resolved https://typescript.codeplex.com/discussions/472172 + .on("change", function (e: Select2JQueryEventObject) { log(JSON.stringify({ val: e.val, added: e.added, removed: e.removed })); }) + .on("open", function () { log("open"); }); +$("#e11_2") + .on("change", function (e: Select2JQueryEventObject) { log(JSON.stringify({ val: e.val, added: e.added, removed: e.removed })); }) + .on("open", function () { log("open"); }); +$("#e12").select2({ tags: ["red", "green", "blue"] }); +$("#e20").select2({ + tags: ["red", "green", "blue"], + tokenSeparators: [",", " "] +}); +$("#e13").select2(); +$("#e13_ca").click(function () { $("#e13").val("CA").trigger("change"); }); +$("#e13_ak_co").click(function () { $("#e13").val(["AK", "CO"]).trigger("change"); }); +$("#e14").val(["AL", "AZ"]).select2(); +$("#e14_init").click(function () { $("#e14").select2(); }); +$("#e14_destroy").click(function () { $("#e14").select2("destroy"); }); +$("#e15").select2({ tags: ["red", "green", "blue", "orange", "white", "black", "purple", "cyan", "teal"] }); +$("#e15").on("change", function () { $("#e15_val").html($("#e15").val() as string); }); + +$("#e16").select2(); +$("#e16_2").select2(); +$("#e16_enable").click(function () { $("#e16,#e16_2").select2("enable"); }); +$("#e16_disable").click(function () { $("#e16,#e16_2").select2("enable", false); }); +$("#e17").select2({ + matcher: function (term, text) { return text.toUpperCase().indexOf(term.toUpperCase()) == 0; } +}); +$("#e17_2").select2({ + matcher: function (term, text, opt) { + return text.toUpperCase().indexOf(term.toUpperCase()) >= 0 + || opt.attr("alt").toUpperCase().indexOf(term.toUpperCase()) >= 0; + } +}); +$("#e18,#e18_2").select2(); +alert("Selected value is: " + $("#e8").select2("val")); $("#e8").select2("val", { id: "CA", text: "Califoria" }); + +$("#e8").select2("val"); +$("#e8").select2("val", "CA"); +$("#e8").select2("data"); +$("#e8").select2("data", { id: "CA", text: "Califoria" }); +$("#e8").select2("destroy"); +$("#e8").select2("open"); +$("#e8").select2("enable", false); +$("#e8").select2("readonly", false); +$("#e8").select2('container'); +$("#e8").select2('onSortStart'); +$("#e8").select2('onSortEnd'); diff --git a/types/select2/v3/tsconfig.json b/types/select2/v3/tsconfig.json new file mode 100644 index 0000000000..8b8f30ed05 --- /dev/null +++ b/types/select2/v3/tsconfig.json @@ -0,0 +1,29 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6", + "dom" + ], + "noImplicitAny": false, + "noImplicitThis": false, + "strictNullChecks": false, + "strictFunctionTypes": true, + "baseUrl": "../../", + "typeRoots": [ + "../../" + ], + "types": [], + "paths": { + "select2": [ + "select2/v3" + ] + }, + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "select2-tests.ts" + ] +} \ No newline at end of file diff --git a/types/select2/v3/tslint.json b/types/select2/v3/tslint.json new file mode 100644 index 0000000000..a41bf5d19a --- /dev/null +++ b/types/select2/v3/tslint.json @@ -0,0 +1,79 @@ +{ + "extends": "dtslint/dt.json", + "rules": { + "adjacent-overload-signatures": false, + "array-type": false, + "arrow-return-shorthand": false, + "ban-types": false, + "callable-types": false, + "comment-format": false, + "dt-header": false, + "eofline": false, + "export-just-namespace": false, + "import-spacing": false, + "interface-name": false, + "interface-over-type-literal": false, + "jsdoc-format": false, + "max-line-length": false, + "member-access": false, + "new-parens": false, + "no-any-union": false, + "no-boolean-literal-compare": false, + "no-conditional-assignment": false, + "no-consecutive-blank-lines": false, + "no-construct": false, + "no-declare-current-package": false, + "no-duplicate-imports": false, + "no-duplicate-variable": false, + "no-empty-interface": false, + "no-for-in-array": false, + "no-inferrable-types": false, + "no-internal-module": false, + "no-irregular-whitespace": false, + "no-mergeable-namespace": false, + "no-misused-new": false, + "no-namespace": false, + "no-object-literal-type-assertion": false, + "no-padding": false, + "no-redundant-jsdoc": false, + "no-redundant-jsdoc-2": false, + "no-redundant-undefined": false, + "no-reference-import": false, + "no-relative-import-in-test": false, + "no-self-import": false, + "no-single-declare-module": false, + "no-string-throw": false, + "no-unnecessary-callback-wrapper": false, + "no-unnecessary-class": false, + "no-unnecessary-generics": false, + "no-unnecessary-qualifier": false, + "no-unnecessary-type-assertion": false, + "no-useless-files": false, + "no-var-keyword": false, + "no-var-requires": false, + "no-void-expression": false, + "no-trailing-whitespace": false, + "object-literal-key-quotes": false, + "object-literal-shorthand": false, + "one-line": false, + "one-variable-per-declaration": false, + "only-arrow-functions": false, + "prefer-conditional-expression": false, + "prefer-const": false, + "prefer-declare-function": false, + "prefer-for-of": false, + "prefer-method-signature": false, + "prefer-template": false, + "radix": false, + "semicolon": false, + "space-before-function-paren": false, + "space-within-parens": false, + "strict-export-declare-modifiers": false, + "trim-file": false, + "triple-equals": false, + "typedef-whitespace": false, + "unified-signatures": false, + "void-return": false, + "whitespace": false + } +}