-
Notifications
You must be signed in to change notification settings - Fork 0
/
autoform-datemask.js
70 lines (55 loc) · 1.53 KB
/
autoform-datemask.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
/* global AutoForm, moment */
/* jshint esversion: 6 */
let options = {};
AutoForm.addInputType('datemask', {
template: 'afDateMask',
valueOut: function afDateMaskValOut() {
if (this.val() === '') {
return this.val();
}
const val = moment(this.val(), options.format);
if (
val._isValid &&
typeof options.minlength === 'number' &&
val._i.length >= options.minlength
) {
return val.toDate();
}
return new Date('invalid');
},
valueConverters: {
string: function dateToDateString(val) {
return (val instanceof Date) ? moment(val).format(options.format) : val;
},
stringArray: function dateTostringArray(val) {
return (val instanceof Date) ? [this.valueConverters.string] : val;
}
},
contextAdjust: function afDateMaskCtxAjst(ctx) {
options = {
mask: ctx.atts.mask,
maskoptions: ctx.atts.maskoptions,
format: ctx.atts.format,
minlength: ctx.atts.minlength,
};
delete ctx.atts.mask;
delete ctx.atts.maskoptions;
delete ctx.atts.format;
delete ctx.atts.minlength;
return ctx;
}
});
Template.afDateMask.helpers({
atts: function afDateMaskAtts() {
let atts = _.clone(this.atts);
if (AutoForm.getDefaultTemplate() === 'bootstrap3') {
// Add bootstrap class
atts = AutoForm.Utility.addClass(atts, "form-control");
}
return atts;
}
});
Template.afDateMask.onRendered(function onRendered() {
const $input = this.$(':input');
$input.mask(options.mask, options.maskoptions);
});