forked from selectize/selectize.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselectize.jquery.js
More file actions
106 lines (93 loc) · 2.94 KB
/
selectize.jquery.js
File metadata and controls
106 lines (93 loc) · 2.94 KB
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
var defaults = {
delimiter: ',',
persist: true,
diacritics: true,
create: false,
highlight: true,
openOnFocus: true,
maxOptions: 1000,
maxItems: null,
hideSelected: null,
scrollDuration: 60,
loadThrottle: 300,
dataAttr: 'data-data',
sortField: null,
sortDirection: 'asc',
valueField: 'value',
labelField: 'text',
searchField: ['text'],
mode: null,
theme: 'default',
wrapperClass: 'selectize-control',
inputClass: 'selectize-input',
dropdownClass: 'selectize-dropdown',
load : null, // function(query, callback)
score : null, // function(search)
onChange : null, // function(value)
onItemAdd : null, // function(value, $item) { ... }
onItemRemove : null, // function(value) { ... }
onClear : null, // function() { ... }
onOptionAdd : null, // function(value, data) { ... }
onOptionRemove : null, // function(value) { ... }
onDropdownOpen : null, // function($dropdown) { ... }
onDropdownClose : null, // function($dropdown) { ... }
onType : null, // function(str) { ... }
render: {
item: null,
option: null,
option_create: null
}
};
$.fn.selectize = function (settings) {
var defaults = $.fn.selectize.defaults;
settings = settings || {};
return this.each(function() {
var instance, value, values, i, n, data, dataAttr, settings_element, tagName;
var $options, $option, $input = $(this);
tagName = $input[0].tagName.toLowerCase();
if (typeof settings === 'string') {
instance = $input.data('selectize');
instance[settings].apply(instance, Array.prototype.splice.apply(arguments, 1));
} else {
dataAttr = settings.dataAttr || defaults.dataAttr;
settings_element = {};
settings_element.placeholder = $input.attr('placeholder');
settings_element.options = {};
settings_element.items = [];
if (tagName === 'select') {
settings_element.maxItems = !!$input.attr('multiple') ? null : 1;
$options = $input.children();
for (i = 0, n = $options.length; i < n; i++) {
$option = $($options[i]);
value = $option.attr('value') || '';
if (!value.length) continue;
data = (dataAttr && $option.attr(dataAttr)) || {
'text' : $option.html(),
'value' : value
};
if (typeof data === 'string') data = JSON.parse(data);
settings_element.options[value] = data;
if ($option.is(':selected')) {
settings_element.items.push(value);
}
}
} else {
value = $.trim($input.val() || '');
if (value.length) {
values = value.split(settings.delimiter || defaults.delimiter);
for (i = 0, n = values.length; i < n; i++) {
settings_element.options[values[i]] = {
'text' : values[i],
'value' : values[i]
};
}
settings_element.items = values;
}
}
instance = new Selectize($input, $.extend(true, {}, defaults, settings_element, settings));
$input.data('selectize', instance);
$input.addClass('selectized');
}
});
};
$.fn.selectize.defaults = defaults;