-
Notifications
You must be signed in to change notification settings - Fork 23
/
autoform-selectize-input.js
81 lines (77 loc) · 2.25 KB
/
autoform-selectize-input.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
AutoForm.addInputType("selectize-input", {
template: "afSelectizeInput",
valueOut: function () {
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;
}
}
});
Template.afSelectizeInput.helpers({
atts: function afSelectAtts() {
var atts = _.clone(this.atts);
// TODO: if (style == 'bootstrap3') ...
// Add bootstrap class
atts = AutoForm.Utility.addClass(atts, "form-control");
delete atts.selectizeOptions;
return atts;
}
});
Template.afSelectizeInput.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.afSelectizeInput.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.$('input').selectize(selectizeOptions);
};
Template.afSelectizeInput.destroyed = function () {
this.$('input')[0].selectize.destroy();
};