-
-
Notifications
You must be signed in to change notification settings - Fork 32
/
autoform-bs-datepicker.js
135 lines (120 loc) · 4.1 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
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
/* global AutoForm, $ */
AutoForm.addInputType('bootstrap-datepicker', {
template: 'afBootstrapDatepicker',
valueOut: function () {
var val;
if (this.val()) {
val = this.datepicker('getUTCDate');
}
return (val instanceof Date) ? val : this.val();
},
valueConverters: {
string: function (val) {
return (val instanceof Date) ? AutoForm.valueConverters.dateToDateStringUTC(val) : val;
},
stringArray: function (val) {
if (val instanceof Date) {
return [AutoForm.valueConverters.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) {
var valArray = this.datepicker('getUTCDates');
if (allArrayItemsAreDates(valArray)) return valArray;
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.onRendered(function onRendered() {
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
var previousValue;
this.autorun(function () {
var data = Template.currentData();
var nextValue = data.value;
// set field value
if (String(previousValue) !== String(nextValue)) {
if (typeof nextValue === 'string' && AutoForm.Utility.isValidDateString(nextValue)) {
nextValue = utcToLocal(new Date(nextValue + 'T00:00:00.000Z'));
}
if (nextValue instanceof Date) {
$input.datepicker('setUTCDate', nextValue);
} else if (typeof nextValue === 'string') {
$input.datepicker('update', nextValue);
}
if (Array.isArray(nextValue)) {
if (allArrayItemsAreDates(nextValue)) {
$input.datepicker('setUTCDates', nextValue);
} else if (allArrayItemsAreValidDateStrings(nextValue)) {
var nextUTCDates = _.map(nextValue, function (dateString) {
return utcToLocal(new Date(dateString + 'T00:00:00.000Z'));
});
$input.datepicker('setUTCDates', nextUTCDates);
} else {
$input.datepicker('update', nextValue);
}
}
previousValue = nextValue;
}
// 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.onDestroyed(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;
}
function allArrayItemsAreDates(items) {
var nonDates = _.filter(items, function(val) { return !(val instanceof Date); });
return nonDates.length === 0;
}
function allArrayItemsAreValidDateStrings(items) {
var nonValid = _.filter(items, function(val) {
return !(typeof val === 'string' && AutoForm.Utility.isValidDateString(val));
});
return nonValid.length === 0;
}