Skip to content

Commit 2859600

Browse files
committed
Fix #1893
1 parent 40a88bb commit 2859600

File tree

1 file changed

+161
-160
lines changed

1 file changed

+161
-160
lines changed

src/selectize.jquery.js

Lines changed: 161 additions & 160 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,61 @@
1-
$.fn.selectize = function(settings_user) {
2-
var defaults = $.fn.selectize.defaults;
3-
var settings = $.extend({}, defaults, settings_user);
4-
var attr_data = settings.dataAttr;
5-
var field_label = settings.labelField;
6-
var field_value = settings.valueField;
7-
var field_disabled = settings.disabledField;
8-
var field_optgroup = settings.optgroupField;
9-
var field_optgroup_label = settings.optgroupLabelField;
10-
var field_optgroup_value = settings.optgroupValueField;
11-
12-
/**
13-
* Initializes selectize from a <input type="text"> element.
14-
*
15-
* @param {JQuery} $input
16-
* @param {Object} settings_element
17-
*/
18-
var init_textbox = function($input, settings_element) {
19-
var i, n, values, option;
20-
21-
var data_raw = $input.attr(attr_data);
22-
23-
if (!data_raw) {
24-
var value = ($input.val() || '').trim();
25-
if (!settings.allowEmptyOption && !value.length) return;
26-
values = value.split(settings.delimiter);
27-
for (i = 0, n = values.length; i < n; i++) {
28-
option = {};
29-
option[field_label] = values[i];
30-
option[field_value] = values[i];
31-
settings_element.options.push(option);
32-
}
33-
settings_element.items = values;
34-
} else {
35-
settings_element.options = JSON.parse(data_raw);
36-
for (i = 0, n = settings_element.options.length; i < n; i++) {
37-
settings_element.items.push(settings_element.options[i][field_value]);
38-
}
39-
}
40-
};
41-
42-
/**
43-
* Initializes selectize from a <select> element.
44-
*
45-
* @param {object} $input
46-
* @param {object} settings_element
47-
*/
48-
var init_select = function($input, settings_element) {
49-
var i, n, tagName, $children, order = 0;
50-
var options = settings_element.options;
51-
var optionsMap = {};
52-
53-
var readData = function($el) {
1+
$.fn.selectize = function (settings_user) {
2+
var defaults = $.fn.selectize.defaults;
3+
var settings = $.extend({}, defaults, settings_user);
4+
var attr_data = settings.dataAttr;
5+
var field_label = settings.labelField;
6+
var field_value = settings.valueField;
7+
var field_disabled = settings.disabledField;
8+
var field_optgroup = settings.optgroupField;
9+
var field_optgroup_label = settings.optgroupLabelField;
10+
var field_optgroup_value = settings.optgroupValueField;
11+
12+
/**
13+
* Initializes selectize from a <input type="text"> element.
14+
*
15+
* @param {JQuery} $input
16+
* @param {Object} settings_element
17+
*/
18+
var init_textbox = function ($input, settings_element) {
19+
var i, n, values, option;
20+
21+
var data_raw = $input.attr(attr_data);
22+
23+
if (!data_raw) {
24+
var value = ($input.val() || '').trim();
25+
if (!settings.allowEmptyOption && !value.length) return;
26+
values = value.split(settings.delimiter);
27+
for (i = 0, n = values.length; i < n; i++) {
28+
option = {};
29+
option[field_label] = values[i];
30+
option[field_value] = values[i];
31+
settings_element.options.push(option);
32+
}
33+
settings_element.items = values;
34+
} else {
35+
settings_element.options = JSON.parse(data_raw);
36+
for (i = 0, n = settings_element.options.length; i < n; i++) {
37+
settings_element.items.push(settings_element.options[i][field_value]);
38+
}
39+
}
40+
};
41+
42+
/**
43+
* Initializes selectize from a <select> element.
44+
*
45+
* @param {object} $input
46+
* @param {object} settings_element
47+
*/
48+
var init_select = function ($input, settings_element) {
49+
var i, n, tagName, $children, order = 0;
50+
var options = settings_element.options;
51+
var optionsMap = {};
52+
53+
var readData = function ($el) {
5454
var data = attr_data && $el.attr(attr_data);
5555
var allData = $el.data();
5656
var obj = {};
5757

58-
if (typeof data === 'string' && data.length) {
58+
if (typeof data === 'string' && data.length) {
5959
if (isJSON(data)) {
6060
Object.assign(obj, JSON.parse(data))
6161
} else {
@@ -66,116 +66,117 @@ $.fn.selectize = function(settings_user) {
6666

6767
Object.assign(obj, allData);
6868

69-
return obj || null;
70-
};
71-
72-
var addOption = function($option, group) {
73-
$option = $($option);
74-
75-
var value = hash_key($option.val());
76-
if (!value && !settings.allowEmptyOption) return;
77-
78-
// if the option already exists, it's probably been
79-
// duplicated in another optgroup. in this case, push
80-
// the current group to the "optgroup" property on the
81-
// existing option so that it's rendered in both places.
82-
if (optionsMap.hasOwnProperty(value)) {
83-
if (group) {
84-
var arr = optionsMap[value][field_optgroup];
85-
if (!arr) {
86-
optionsMap[value][field_optgroup] = group;
87-
} else if (!Array.isArray(arr)) {
88-
optionsMap[value][field_optgroup] = [arr, group];
89-
} else {
90-
arr.push(group);
91-
}
92-
}
93-
return;
94-
}
95-
96-
var option = readData($option) || {};
97-
option[field_label] = option[field_label] || $option.text();
98-
option[field_value] = option[field_value] || value;
99-
option[field_disabled] = option[field_disabled] || $option.prop('disabled');
69+
return obj || null;
70+
};
71+
72+
var addOption = function ($option, group) {
73+
$option = $($option);
74+
75+
var value = hash_key($option.val());
76+
if (!value && !settings.allowEmptyOption) return;
77+
78+
// if the option already exists, it's probably been
79+
// duplicated in another optgroup. in this case, push
80+
// the current group to the "optgroup" property on the
81+
// existing option so that it's rendered in both places.
82+
if (optionsMap.hasOwnProperty(value)) {
83+
if (group) {
84+
var arr = optionsMap[value][field_optgroup];
85+
if (!arr) {
86+
optionsMap[value][field_optgroup] = group;
87+
} else if (!Array.isArray(arr)) {
88+
optionsMap[value][field_optgroup] = [arr, group];
89+
} else {
90+
arr.push(group);
91+
}
92+
}
93+
return;
94+
}
95+
96+
var option = readData($option) || {};
97+
option[field_label] = option[field_label] || $option.text();
98+
option[field_value] = option[field_value] || value;
99+
option[field_disabled] = option[field_disabled] || $option.prop('disabled');
100100
option[field_optgroup] = option[field_optgroup] || group;
101101
option.styles = $option.attr('style') || '';
102-
option.classes = $option.attr('class') || '';
103-
104-
optionsMap[value] = option;
105-
options.push(option);
106-
107-
if ($option.is(':selected')) {
108-
settings_element.items.push(value);
109-
}
110-
};
111-
112-
var addGroup = function($optgroup) {
113-
var i, n, id, optgroup, $options;
114-
115-
$optgroup = $($optgroup);
116-
id = $optgroup.attr('label');
117-
118-
if (id) {
119-
optgroup = readData($optgroup) || {};
120-
optgroup[field_optgroup_label] = id;
121-
optgroup[field_optgroup_value] = id;
122-
optgroup[field_disabled] = $optgroup.prop('disabled');
123-
settings_element.optgroups.push(optgroup);
124-
}
125-
126-
$options = $('option', $optgroup);
127-
for (i = 0, n = $options.length; i < n; i++) {
128-
addOption($options[i], id);
129-
}
130-
};
131-
132-
settings_element.maxItems = $input.attr('multiple') ? null : 1;
133-
134-
$children = $input.children();
135-
for (i = 0, n = $children.length; i < n; i++) {
136-
tagName = $children[i].tagName.toLowerCase();
137-
if (tagName === 'optgroup') {
138-
addGroup($children[i]);
139-
} else if (tagName === 'option') {
140-
addOption($children[i]);
141-
}
142-
}
143-
};
144-
145-
return this.each(function() {
146-
if (this.selectize) return;
147-
148-
var instance;
149-
var $input = $(this);
150-
var tag_name = this.tagName.toLowerCase();
151-
var placeholder = $input.attr('placeholder') || $input.attr('data-placeholder');
152-
if (!placeholder && !settings.allowEmptyOption) {
153-
placeholder = $input.children('option[value=""]').text();
154-
}
155-
if (settings.allowEmptyOption && settings.showEmptyOptionInDropdown && !$input.children('option[value=""]').length) {
156-
var input_html = $input.html();
157-
$input.html('<option value="">'+settings.emptyOptionLabel+'</option>'+input_html);
158-
}
159-
160-
var settings_element = {
161-
'placeholder' : placeholder,
162-
'options' : [],
163-
'optgroups' : [],
164-
'items' : []
165-
};
166-
167-
if (tag_name === 'select') {
168-
init_select($input, settings_element);
169-
} else {
170-
init_textbox($input, settings_element);
171-
}
172-
173-
instance = new Selectize($input, $.extend(true, {}, defaults, settings_element, settings_user));
174-
instance.settings_user = settings_user;
175-
});
102+
option.classes = $option.attr('class') || '';
103+
104+
optionsMap[value] = option;
105+
options.push(option);
106+
107+
if ($option.is(':selected')) {
108+
settings_element.items.push(value);
109+
}
110+
};
111+
112+
var addGroup = function ($optgroup) {
113+
var i, n, id, optgroup, $options;
114+
115+
$optgroup = $($optgroup);
116+
id = $optgroup.attr('label');
117+
118+
if (id) {
119+
optgroup = readData($optgroup) || {};
120+
optgroup[field_optgroup_label] = id;
121+
optgroup[field_optgroup_value] = id;
122+
optgroup[field_disabled] = $optgroup.prop('disabled');
123+
settings_element.optgroups.push(optgroup);
124+
}
125+
126+
$options = $('option', $optgroup);
127+
for (i = 0, n = $options.length; i < n; i++) {
128+
addOption($options[i], id);
129+
}
130+
};
131+
132+
settings_element.maxItems = $input.attr('multiple') ? null : 1;
133+
134+
$children = $input.children();
135+
for (i = 0, n = $children.length; i < n; i++) {
136+
tagName = $children[i].tagName.toLowerCase();
137+
if (tagName === 'optgroup') {
138+
addGroup($children[i]);
139+
} else if (tagName === 'option') {
140+
addOption($children[i]);
141+
}
142+
}
143+
};
144+
145+
return this.each(function () {
146+
if (this.selectize) return;
147+
148+
var instance;
149+
var $input = $(this);
150+
var tag_name = this.tagName.toLowerCase();
151+
var placeholder = $input.attr('placeholder') || $input.attr('data-placeholder');
152+
if (!placeholder && !settings.allowEmptyOption) {
153+
placeholder = $input.children('option[value=""]').text();
154+
}
155+
if (settings.allowEmptyOption && settings.showEmptyOptionInDropdown && !$input.children('option[value=""]').length) {
156+
var input_html = $input.html();
157+
var label = escape_html(settings.emptyOptionLabel || '--');
158+
$input.html('<option value="">' + label + '</option>' + input_html);
159+
}
160+
161+
var settings_element = {
162+
'placeholder': placeholder,
163+
'options': [],
164+
'optgroups': [],
165+
'items': []
166+
};
167+
168+
if (tag_name === 'select') {
169+
init_select($input, settings_element);
170+
} else {
171+
init_textbox($input, settings_element);
172+
}
173+
174+
instance = new Selectize($input, $.extend(true, {}, defaults, settings_element, settings_user));
175+
instance.settings_user = settings_user;
176+
});
176177
};
177178

178179
$.fn.selectize.defaults = Selectize.defaults;
179180
$.fn.selectize.support = {
180-
validity: SUPPORTS_VALIDITY_API
181+
validity: SUPPORTS_VALIDITY_API
181182
};

0 commit comments

Comments
 (0)