-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautoform-bs-datepicker.js
100 lines (90 loc) · 2.84 KB
/
autoform-bs-datepicker.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
/* global AutoForm, $ */
AutoForm.addInputType("bootstrap-datepicker", {
template: "afBootstrapDatepicker",
valueOut: function () {
if (this.val()) {
var val = this.datepicker('getUTCDate');
return (val instanceof Date) ? val : this.val();
}
},
valueConverters: {
"string": function (val) {
return (val instanceof Date) ? AutoForm.Utility.dateToDateStringUTC(val) : val;
},
"stringArray": function (val) {
if (val instanceof Date) {
return [AutoForm.Utility.dateToDateStringUTC(val)];
}
return val;
},
"number": function (val) {
return (val instanceof Date) ? val.getTime() : val;
},
"numberArray": function (val) {
if (val instanceof Date) {
return [val.getTime()];
}
return val;
},
"dateArray": function (val) {
if (val instanceof Date) {
return [val];
}
return val;
}
}
});
Template.afBootstrapDatepicker.helpers({
atts: function addFormControlAtts() {
var atts = _.clone(this.atts);
// Add bootstrap class
atts = AutoForm.Utility.addClass(atts, "form-control");
delete atts.datePickerOptions;
return atts;
}
});
Template.afBootstrapDatepicker.rendered = function () {
var $input = this.data.atts.buttonClasses ? this.$('.input-group.date') : this.$('input');
var data = this.data;
// instanciate datepicker
$input.datepicker(data.atts.datePickerOptions);
// set and reactively update values
this.autorun(function () {
var data = Template.currentData();
// set field value
if (data.value instanceof Date) {
$input.datepicker('setUTCDate', data.value);
} else if (typeof data.value === "string") {
$input.datepicker('update', data.value);
}
// set start date if there's a min in the schema
if (data.min instanceof Date) {
// datepicker plugin expects local Date object,
// so convert UTC Date object to local
var startDate = utcToLocal(data.min);
$input.datepicker('setStartDate', startDate);
}
// set end date if there's a max in the schema
if (data.max instanceof Date) {
// datepicker plugin expects local Date object,
// so convert UTC Date object to local
var endDate = utcToLocal(data.max);
$input.datepicker('setEndDate', endDate);
}
});
};
Template.afBootstrapDatepicker.destroyed = function () {
var $input = this.data.atts.buttonClasses ? this.$('.input-group.date') : this.$('input');
$input.datepicker('remove');
};
function utcToLocal(utcDate) {
var localDateObj = new Date();
localDateObj.setDate(utcDate.getUTCDate());
localDateObj.setMonth(utcDate.getUTCMonth());
localDateObj.setFullYear(utcDate.getUTCFullYear());
localDateObj.setHours(0);
localDateObj.setMinutes(0);
localDateObj.setSeconds(0);
localDateObj.setMilliseconds(0);
return localDateObj;
}