forked from DefinitelyTyped/DefinitelyTyped
-
Notifications
You must be signed in to change notification settings - Fork 0
/
selectize.d.ts
612 lines (504 loc) · 18.8 KB
/
selectize.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
// Type definitions for Selectize 0.11.2
// Project: https://github.com/brianreavis/selectize.js
// Definitions by: Adi Dahiya <https://github.com/adidahiya>
// Definitions: https://github.com/borisyankov/DefinitelyTyped
/// <reference path="../jquery/jquery.d.ts"/>
declare module Selectize {
// see https://github.com/brianreavis/selectize.js/blob/master/docs/usage.md
// option identifiers are parameterized by T; data is parameterized by U
interface IOptions<T, U> {
// General
// ------------------------------------------------------------------------------------------------------------
/**
* The string to separate items by. This option is only used when Selectize is instantiated from a
* <input type="text"> element.
*
* Default: ','
*/
delimiter?: string;
/**
* Enable or disable international character support.
*
* Default: true
*/
diacritics?: boolean;
/**
* Allows the user to create a new items that aren't in the list of options.
* This option can be any of the following: "true", "false" (disabled), or a function that accepts two
* arguments: "input" and "callback". The callback should be invoked with the final data for the option.
*
* Default: false
*/
create?: any;
/**
* If true, when user exits the field (clicks outside of input or presses ESC) new option is created and
* selected (if `create`-option is enabled).
*
* Default: false
*/
createOnBlur?: boolean;
/**
* Specifies a RegExp or String containing a regular expression that the current search filter must match to
* be allowed to be created. May also be a predicate function that takes the filter text and returns whether
* it is allowed.
*
* Default: null
*/
createFilter?: any;
/**
* Toggles match highlighting within the dropdown menu.
*
* Default: true
*/
highlight?: boolean;
/**
* If false, items created by the user will not show up as available options once they are unselected.
*
* Default: true
*/
persist?: boolean;
/**
* Show the dropdown immediately when the control receives focus.
*
* Default: true
*/
openOnFocus?: boolean;
/**
* The max number of items to render at once in the dropdown list of options.
*
* Default: 1000
*/
maxOptions?: number;
/**
* The max number of items the user can select.
*
* Default: Infinity
*/
maxItems?: number;
/**
* If true, the items that are currently selected will not be shown in the dropdown list of available options.
*
* Default: false
*/
hideSelected?: boolean;
/**
* If true, Selectize will treat any options with a "" value like normal. This defaults to false to
* accomodate the common <select> practice of having the first empty option act as a placeholder.
*
* Default: false
*/
allowEmptyOption?: boolean;
/**
* The animation duration (in milliseconds) of the scroll animation triggered when going [up] and [down] in
* the options dropdown.
*
* Default: 60
*/
scrollDuration?: number;
/**
* The number of milliseconds to wait before requesting options from the server or null.
* If null, throttling is disabled.
*
* Default: 300
*/
loadThrottle?: number;
/**
* If true, the "load" function will be called upon control initialization (with an empty search).
* Alternatively it can be set to "focus" to call the "load" function when control receives focus.
*
* Default: false
*/
preload?: any;
/**
* The element the dropdown menu is appended to. This should be "body" or null.
* If null, the dropdown will be appended as a child of the selectize control.
*
* Default: null
*/
dropdownParent?: string;
/**
* Sets if the "Add..." option should be the default selection in the dropdown.
*
* Default: false
*/
addPrecedence?: boolean;
/**
* If true, the tab key will choose the currently selected item.
*
* Default: false
*/
selectOnTab?: boolean;
/**
* An array of plugins to use
*
* Default: null
*/
plugins?: string[];
// Data / Searching
// ------------------------------------------------------------------------------------------------------------
/**
* Options available to select; array of objects. If your element is <select> with <option>s specified this
* property gets populated accordingly. Setting this property is convenient if you have your data as an
* array and want to automatically generate the <option>s.
*
* Default: []
*/
options?: U[];
/**
* The <option> attribute from which to read JSON data about the option.
*
* Default: "data-data"
*/
dataAttr?: string;
/**
* The name of the property to use as the "value" when an item is selected.
*
* Default: "value"
*/
valueField?: string;
/**
* Option groups that options will be bucketed into.
* If your element is a <select> with <optgroup>s this property gets populated automatically.
* Make sure each object in the array has a property named whatever "optgroupValueField" is set to.
*/
optgroups?: U[];
/**
* The name of the option group property that serves as its unique identifier.
*
* Default: "value"
*/
optgroupValueField?: string;
/**
* The name of the property to render as an option / item label (not needed when custom rendering
* functions are defined).
*
* Default: "text"
*/
labelField?: string;
/**
* The name of the property to render as an option group label (not needed when custom rendering
* functions are defined).
*
* Default: "label"
*/
optgroupLabelField?: string;
/**
* The name of the property to group items by.
*
* Default: "optgroup"
*/
optgroupField?: string;
/**
* A single field or an array of fields to sort by. Each item in the array should be an object containing at
* least a "field" property. Optionally, "direction" can be set to "asc" or "desc". The order of the array
* defines the sort precedence.
*
* Unless present, a special "$score" field will be automatically added to the beginning of the sort list.
* This will make results sorted primarily by match quality (descending).
*
* Default: "$order"
*/
sortField?: any;
/**
* An array of property names to analyze when filtering options.
*
* Default: ["text"]
*/
searchField?: any;
/**
* When searching for multiple terms (separated by a space), this is the operator used. Can be "and" or "or".
*
* Default: "and"
*/
searchConjunction?: string;
/**
* An array of optgroup values that indicates the order they should be listed in in the dropdown.
* If not provided, groups will be ordered by the ranking of the options within them.
*
* Default: null
*/
optgroupOrder?: string[];
/**
* Copy the original input classes to the Dropdown element.
*
* Default: true
*/
copyClassesToDropdown?: boolean;
// Callbacks
// ------------------------------------------------------------------------------------------------------------
/**
* Invoked when new options should be loaded from the server.
*/
load?(query: string, callback: Function): any;
/**
* Overrides the scoring function used to sort available options. The provided function should return a
* function that returns a number greater than or equal to zero to represent the "score" of an item
* (the function's first argument). If 0, the option is declared not a match.
*/
score?(search: ISearch): (item: any) => number;
/**
* Invoked once the control is completely initialized.
*/
onInitialize?(): any;
/**
* Invoked when the value of the control changes.
*
* If single select, value is of type T.
* If multi select, value is of type T[].
*/
onChange?(value: any): any;
/**
* Invoked when an item is selected.
*/
onItemAdd?(value: T, item: JQuery): any;
/**
* Invoked when an item is deselected.
*/
onItemRemove?(value: T): any;
/**
* Invoked when the control is manually cleared via the clear() method.
*/
onClear?(): any;
/**
* Invoked when the user attempts to delete the current selection.
*/
onDelete?(values: T[]): any;
/**
* Invoked when a new option is added to the available options list.
*/
onOptionAdd?(value: T, data: U): any;
/**
* Invoked when an option is removed from the available options.
*/
onOptionRemove?(value: T): any;
/**
* Invoked when the dropdown opens.
*/
onDropdownOpen?(dropdown: JQuery): any;
/**
* Invoked when the dropdown closes.
*/
onDropdownClose?(dropdown: JQuery): any;
/**
* Invoked when the user types while filtering options.
*/
onType?(srt: string): any;
/**
* Invoked when new options have been loaded and added to the control (via the "load" option or "load" API method).
*/
onLoad?(data: U[]): any;
// Rendering
// ------------------------------------------------------------------------------------------------------------
render?: ICustomRenderers<U>;
}
/**
* Custom rendering functions. Each function should accept two arguments: "data" and "escape" and return
* HTML (string) with a single root element. The "escape" argument is a function that takes a string and
* escapes all special HTML characters. This is very important to use to prevent XSS vulnerabilities.
*/
interface ICustomRenderers<U> {
// An option in the dropdown list of available options.
option?(data: U, escape: (input: string) => string): string;
// An item the user has selected.
item?(data: U, escape: (input: string) => string): string;
// The "create new" option at the bottom of the dropdown. The data contains one property: "input"
// (which is what the user has typed).
option_create?(data: U, escape: (input: string) => string): string;
// The header of an option group.
optgroup_header?(data: U, escape: (input: string) => string): string;
// The wrapper for an optgroup. The "html" property in the data will be the raw html of the optgroup's header
// and options.
optgroup?(data: U, escape: (input: string) => string): string;
}
// see https://github.com/brianreavis/selectize.js/blob/master/docs/api.md
interface IApi<T, U> {
// Dropdown Options
// ------------------------------------------------------------------------------------------------------------
/**
* Adds an available option. If it already exists, nothing will happen.
* Note: this does not refresh the options list dropdown (use refreshOptions() for that).
*/
addOption(data: U): void;
/**
* Updates an option available for selection. If it is visible in the selected items or options dropdown,
* it will be re-rendered automatically.
*/
updateOption(value: T, data: U): void;
/**
* Removes the option identified by the given value.
*/
removeOption(value: T): void;
/**
* Removes all options from the control.
*/
clearOptions(): void;
/**
* Retrieves the jQuery element for the option identified by the given value.
*/
getOption(value: T): any;
/**
* Retrieves the jQuery element for the previous or next option, relative to the currently highlighted option.
* The "direction" argument should be 1 for "next" or -1 for "previous".
*/
getAdjacentOption(value: T, direction: number): void;
/**
* Refreshes the list of available options shown in the autocomplete dropdown menu.
*/
refreshOptions(triggerDropdown: boolean): void;
// Selected Items
// ------------------------------------------------------------------------------------------------------------
/**
* Resets / clears all selected items from the control.
*/
clear(): void;
/**
* Returns the jQuery element of the item matching the given value.
*/
getItem(value: T): JQuery;
/**
* "Selects" an item. Adds it to the list at the current caret position.
*/
addItem(value: T): void;
/**
* Removes the selected item matching the provided value.
*/
removeItem(value: T): void;
/**
* Invokes the "create" method provided in the selectize options that should provide the data for the
* new item, given the user input. Once this completes, it will be added to the item list.
*/
createItem(value: T): void;
/**
* Re-renders the selected item lists.
*/
refreshItems(): void;
// Optgroups
// ------------------------------------------------------------------------------------------------------------
/**
* Registers a new optgroup for options to be bucketed into.
* The "id" argument refers to a value of the property in option identified by the "optgroupField" setting.
*/
addOptionGroup(id: string, data: U): void;
// Events
// ------------------------------------------------------------------------------------------------------------
/**
* Adds an event listener.
*/
on(eventName: string, handler: Function): void;
/**
* Removes an event listener. If no handler is provided, all event listeners are removed.
*/
off(eventName: string, handler?: Function): void;
/**
* Triggers event listeners.
*/
trigger(eventName: string, ...args: any[]): void;
// Dropdown Actions
// ------------------------------------------------------------------------------------------------------------
/**
* Shows the autocomplete dropdown containing the available options.
*/
open(): void;
/**
* Closes the autocomplete dropdown menu.
*/
close(): void;
/**
* Calculates and applies the appropriate position of the dropdown.
*/
positionDropdown(): void;
// Other
// ------------------------------------------------------------------------------------------------------------
/**
* Destroys the control and unbinds event listeners so that it can be garbage collected.
*/
destroy(): void;
/**
* Loads options by invoking the the provided function. The function should accept one argument (callback)
* and invoke the callback with the results once they are available.
*/
load(callback: (results: any) => any): void;
/**
* Brings the control into focus.
*/
focus(): void;
/**
* Forces the control out of focus.
*/
blur(): void;
/**
* Disables user input on the control (note: the control can still receive focus).
*/
lock(): void;
/**
* Re-enables user input on the control.
*/
unlock(): void;
/**
* Disables user input on the control completely. While disabled, it cannot receive focus.
*/
disable(): void;
/**
* Enables the control so that it can respond to focus and user input.
*/
enable(): void;
/**
* Returns the value of the control. If multiple items can be selected (e.g. <select multiple>), this
* returns an array. If only one item can be selected, this returns a string.
*/
getValue(): any;
/**
* Resets the selected items to the given value(s).
*/
setValue(value: T): void;
setValue(value: T[]): void;
/**
* Moves the caret to the specified position ("index" being the index in the list of selected items).
*/
setCaret(index: number): void;
/**
* Returns whether or not the user can select more items.
*/
isFull(): boolean;
/**
* Clears the render cache. Takes an optional template argument (e.g. "option", "item") to clear only that cache.
*/
clearCache(template?: string): void;
}
interface ISearchToken {
regex: RegExp;
string: string;
}
interface ISearchResult {
id: string;
score: number;
}
interface ISearch {
/**
* Original search options.
*/
options: any;
/**
* The raw user input
*/
query: string;
/**
* An array containing parsed search tokens. A token is an object containing two properties: "string" and "regex".
*/
tokens: ISearchToken[];
/**
* The total number of results.
*/
total: number;
/**
* A list of matched results. Each result is an object containing two properties: "score" and "id".
*/
items: ISearchResult[];
}
}
interface JQuery {
selectize(options?: Selectize.IOptions<any, any>): JQuery;
}
interface HTMLElement {
selectize: Selectize.IApi<any, any>;
}