forked from comerc/meteor-autoform-selectize
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautoform-selectize.js
226 lines (208 loc) · 6.57 KB
/
autoform-selectize.js
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
AutoForm.addInputType("selectize", {
template: "afSelectize",
valueOut: function () {
// FIXME: may be related https://github.com/aldeed/meteor-autoform/issues/569
if (this[0].selectize) {
return this[0].selectize.getValue();
}
},
valueConverters: {
"stringArray": function (val) {
if (_.isArray(val)) {
return _.map(val, function (item) {
return $.trim(item);
});
}
return val;
},
"number": AutoForm.Utility.stringToNumber,
"numberArray": function (val) {
if (_.isArray(val)) {
return _.map(val, function (item) {
item = $.trim(item);
return AutoForm.Utility.stringToNumber(item);
});
}
return val;
},
"boolean": AutoForm.Utility.stringToBool,
"booleanArray": function (val) {
if (_.isArray(val)) {
return _.map(val, function (item) {
item = $.trim(item);
return AutoForm.Utility.stringToBool(item);
});
}
return val;
},
"date": AutoForm.Utility.stringToDate,
"dateArray": function (val) {
if (_.isArray(val)) {
return _.map(val, function (item) {
item = $.trim(item);
return AutoForm.Utility.stringToDate(item);
});
}
return val;
}
},
contextAdjust: function (context) {
//can fix issues with some browsers selecting the firstOption instead of the selected option
context.atts.autocomplete = 'off';
var itemAtts = _.omit(context.atts, 'firstOption');
var firstOption = context.atts.firstOption;
// build items list
context.items = [];
// If a firstOption was provided, add that to the items list first
if (firstOption === false) {
// nothing
} else if (typeof firstOption === 'string' || typeof _defaults.firstOption === "string") {
context.items.push({
name: context.name,
label: (typeof firstOption === 'string' ? firstOption : _defaults.firstOption),
value: '',
// _id must be included because it is a special property that
// #each uses to track unique list items when adding and removing them
// See https://github.com/meteor/meteor/issues/2174
//
// Setting this to an empty string caused problems if option with value
// 1 was in the options list because Spacebars evaluates "" to 1 and
// considers that a duplicate.
// See https://github.com/aldeed/meteor-autoform/issues/656
_id: 'AUTOFORM_EMPTY_FIRST_OPTION',
selected: false,
// atts: itemAtts
});
}
var fetchOpt = function fetchOpt(opt) {
return {
name: context.name,
label: opt.label,
value: opt.value,
// _id must be included because it is a special property that
// #each uses to track unique list items when adding and removing them
// See https://github.com/meteor/meteor/issues/2174
_id: opt.value,
selected: _.isArray(context.value) ?
_.include(context.value, opt.value) : (opt.value === context.value),
// atts: itemAtts
};
};
// Add all defined options
_.each(context.selectOptions, function(opt) {
if (opt.optgroup) {
context.items.push({
optgroup: opt.optgroup,
items: _.map(opt.options, fetchOpt)
});
} else {
context.items.push(fetchOpt(opt));
}
});
return context;
}
});
Template.afSelectize.helpers({
optionAtts: function () {
var item = this
var atts = {
value: item.value
};
if (item.selected) {
atts.selected = '';
}
return atts;
},
atts: function () {
var atts = _.clone(this.atts);
// TODO: if (style == 'bootstrap3') ...
// Add bootstrap class
atts = AutoForm.Utility.addClass(atts, 'form-control');
delete atts.selectizeOptions;
delete atts.isReactiveOptions;
return atts;
},
isReactiveOptions: function () {
var isReactiveOptions;
if (_.has(this.atts, 'isReactiveOptions')) {
isReactiveOptions = this.atts.isReactiveOptions;
} else {
isReactiveOptions = _defaults.isReactiveOptions;
}
return isReactiveOptions;
}
});
Template.afSelectize.events({
"click .selectized": function (event) {
// TODO: https://github.com/selectize/selectize.js/issues/658
$(event.toElement).next().children(":first-child").children("input:first").focus();
}
});
Template.afSelectize.rendered = function () {
var selectizeOptions = this.data.atts.selectizeOptions || {};
// selectize rearranges one option from the middle of the list
// https://github.com/selectize/selectize.js/issues/640#issuecomment-71788203
if (!selectizeOptions.sortField) {
selectizeOptions.sortField = 'text';
}
// instanciate selectize
this.$('select').selectize(selectizeOptions);
var isReactiveOptions;
if (_.has(this.data.atts, 'isReactiveOptions')) {
isReactiveOptions = this.data.atts.isReactiveOptions;
} else {
isReactiveOptions = _defaults.isReactiveOptions;
}
if (isReactiveOptions) {
var test = false;
var selectize = this.$('select')[0].selectize;
this.autorun(function (e) {
var items = Template.currentData().items;
// FIXED double autorun
// TODO may be computation.firstRun?
test = !test;
//if (!e.firstRun) {
_refreshSelectizeOptions(selectize, items);
//}
});
}
};
Template.afSelectize.destroyed = function () {
this.$('select')[0].selectize.destroy();
};
var _defaults = {
firstOption: 'Select an option',
isReactiveOptions: false
};
AutoForm.Selectize = {};
AutoForm.Selectize.setDefaults = function (o) {
if (_.has(o, 'firstOption')) {
_defaults.firstOption = o.firstOption;
}
if (_.has(o, 'isReactiveOptions')) {
_defaults.isReactiveOptions = o.isReactiveOptions;
}
}
var _refreshSelectizeOptions = function (selectize, options) {
var items = selectize.items;
selectize.clearOptions();
_.each(options, function (option) {
if (option.optgroup) {
selectize.addOptionGroup(option.optgroup, {label: option.optgroup});
_.each(option.items, function (groupOption) {
selectize.addOption({value: groupOption.value, text: groupOption.label, optgroup: option.optgroup});
if (groupOption.selected) {
selectize.addItem(groupOption.value, true);
}
});
} else if (option.value) {
selectize.addOption({value: option.value, text: option.label});
if (option.selected) {
selectize.addItem(option.value, true);
}
}
});
_.each(items, function (item) {
selectize.addItem(item, true);
});
};