[select2] Extend from JQueryAjaxSettings (#18942)

* Extend from JQueryAjaxSettings

* Restore url and dataType
This commit is contained in:
Jason Kaczmarsky 2017-08-14 16:50:13 -04:00 committed by Mohamed Hegazy
parent cc070e767f
commit 1d4eb8f7ca
2 changed files with 57 additions and 35 deletions

View File

@ -18,22 +18,21 @@ interface AjaxFunction {
(settings: JQueryAjaxSettings, success?: (data: any) => null, failure?: () => null): JQueryXHR;
}
interface Select2AjaxOptions {
interface Select2AjaxOptions extends JQueryAjaxSettings {
transport?: AjaxFunction;
/**
* Url to make request to, Can be string or a function returning a string.
*/
url?: any;
type?: string;
dataType?: string;
delay?: number;
headers?: any;
cache?: boolean;
data?: (params: Select2QueryOptions, page: number, context: any) => any;
results?: (term: any, page: number, context: any) => any;
processResults?:(data: any, params: any) => any;
processResults?: (data: any, params: any) => any;
templateResult?: (data: any) => any;
templateSelection?: (data: any) => any;
templateSelection?: (data: any) => any;
}
interface IdTextPair {
@ -59,7 +58,7 @@ interface Select2Options {
openOnEnter?: boolean;
id?: (object: any) => string;
matcher?: (term: string, text: string, option: any) => boolean;
formatSelection?: (object: any, container: JQuery, escapeMarkup:(markup: string) => string) => string;
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;
@ -68,8 +67,8 @@ interface Select2Options {
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;
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;
@ -116,73 +115,73 @@ interface Select2SelectionObject {
}
interface Select2Plugin {
amd: any;
amd: any;
(): JQuery;
(it: IdTextPair): JQuery;
(): JQuery;
(it: IdTextPair): JQuery;
/**
* Get the id value of the current selection
*/
(method: 'val'): any;
(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;
(method: 'val', value: any, triggerChange?: boolean): any;
/**
* Get the data object of the current selection
*/
(method: 'data'): any;
(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;
(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;
(method: 'destroy'): JQuery;
/**
* Opens the dropdown
*/
(method: 'open'): JQuery;
(method: 'open'): JQuery;
/**
* Closes the dropdown
*/
(method: 'close'): JQuery;
(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;
(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;
(method: 'readonly', value: boolean): JQuery;
/**
* Retrieves the main container element that wraps all of DOM added by Select2
*/
(method: 'container'): JQuery;
(method: 'container'): JQuery;
/**
* Notifies Select2 that a drag and drop sorting operation has started
*/
(method: 'onSortStart'): JQuery;
(method: 'onSortStart'): JQuery;
/**
* Notifies Select2 that a drag and drop sorting operation has finished
*/
(method: 'onSortEnd'): JQuery;
(method: 'onSortEnd'): JQuery;
(method: string): any;
(method: string, value: any, trigger?: boolean): any;
(options: Select2Options): JQuery;
(method: string): any;
(method: string, value: any, trigger?: boolean): any;
(options: Select2Options): JQuery;
}
interface JQuery {
select2: Select2Plugin;
select2: Select2Plugin;
off(events?: "change", selector?: any, handler?: (eventObject: Select2JQueryEventObject) => any): JQuery;
on(events: "change", selector?: string, data?: any, handler?: (eventObject: Select2JQueryEventObject) => any): JQuery;
@ -222,5 +221,5 @@ declare class Select2 {
$dropdown: JQuery;
$selection: JQuery;
$results: JQuery;
options: {options: Select2Options};
options: { options: Select2Options };
}

View File

@ -94,6 +94,29 @@ $("#e6").select2({
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 (params, page) {
return {
q: params.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,
@ -120,10 +143,10 @@ $("#e7").select2({
});
function sort(elements) {
return elements.sort();
return elements.sort();
}
$("#e20").select2({
sorter: sort
sorter: sort
});
$("#e8").select2();
@ -158,12 +181,12 @@ function log(e) {
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"); });
// 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"); });
.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"],
@ -188,7 +211,7 @@ $("#e17").select2({
$("#e17_2").select2({
matcher: function (term, text, opt) {
return text.toUpperCase().indexOf(term.toUpperCase()) >= 0
|| opt.attr("alt").toUpperCase().indexOf(term.toUpperCase()) >= 0;
|| opt.attr("alt").toUpperCase().indexOf(term.toUpperCase()) >= 0;
}
});
$("#e18,#e18_2").select2();