-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathautoform-selectize.js
201 lines (183 loc) · 5.67 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
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: {
"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
_id: '',
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) ?
_.contains(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/brianreavis/selectize.js/issues/658
$(event.toElement).next().children(":first-child").children("input:first").focus();
}
});
Template.afSelectize.rendered = function () {
// instanciate selectize
this.$('select').selectize(this.data.atts.selectizeOptions || {});
var isReactiveOptions;
if (_.has(this.data.atts, 'isReactiveOptions')) {
isReactiveOptions = this.data.atts.isReactiveOptions;
} else {
isReactiveOptions = _defaults.isReactiveOptions;
}
if (isReactiveOptions) {
var selectize = this.$('select')[0].selectize;
this.autorun(function () {
var items = Blaze.getData().items;
_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);
});
};