Skip to content

Commit 5c016b2

Browse files
committed
Initial commit
1 parent 205b2ae commit 5c016b2

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed

autoform-datemask.html

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<template name="afDateMask">
2+
<input type="text" value="{{this.value}}" {{atts}}/>
3+
</template>

autoform-datemask.js

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/* global AutoForm, moment */
2+
/* jshint esversion: 6 */
3+
4+
5+
let options = {};
6+
7+
AutoForm.addInputType('datemask', {
8+
template: 'afDateMask',
9+
valueOut: function afDateMaskValOut() {
10+
11+
if (this.val() === '') {
12+
return this.val();
13+
}
14+
15+
const val = moment(this.val(), options.format);
16+
17+
if (
18+
val._isValid &&
19+
typeof options.minlength === 'number' &&
20+
val._i.length >= options.minlength
21+
) {
22+
return val.toDate();
23+
}
24+
25+
return new Date('invalid');
26+
},
27+
valueConverters: {
28+
string: function dateToDateString(val) {
29+
return (val instanceof Date) ? moment(val).format(options.format) : val;
30+
},
31+
stringArray: function dateTostringArray(val) {
32+
return (val instanceof Date) ? [this.valueConverters.string] : val;
33+
}
34+
},
35+
contextAdjust: function afDateMaskCtxAjst(ctx) {
36+
options = {
37+
mask: ctx.atts.mask,
38+
maskoptions: ctx.atts.maskoptions,
39+
format: ctx.atts.format,
40+
minlength: ctx.atts.minlength,
41+
};
42+
43+
delete ctx.atts.mask;
44+
delete ctx.atts.maskoptions;
45+
delete ctx.atts.format;
46+
delete ctx.atts.minlength;
47+
48+
return ctx;
49+
}
50+
});
51+
52+
53+
Template.afDateMask.helpers({
54+
atts: function afDateMaskAtts() {
55+
let atts = _.clone(this.atts);
56+
57+
if (AutoForm.getDefaultTemplate() === 'bootstrap3') {
58+
// Add bootstrap class
59+
atts = AutoForm.Utility.addClass(atts, "form-control");
60+
}
61+
62+
return atts;
63+
}
64+
});
65+
66+
Template.afDateMask.onRendered(function onRendered() {
67+
const $input = this.$(':input');
68+
69+
$input.mask(options.mask, options.maskoptions);
70+
});

package.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Package.describe({
2+
name: 'jkutianski:autoform-datemask',
3+
summary: 'Custom date mask input type for AutoForm',
4+
version: '0.0.1'
5+
});
6+
7+
Package.onUse(function(api) {
8+
api.use([
9+
'ecmascript',
10+
11+
12+
13+
'momentjs:[email protected]',
14+
15+
'igorescobar:jquery-mask-plugin'
16+
], 'client');
17+
18+
api.addFiles([
19+
'autoform-datemask.html',
20+
'autoform-datemask.js'
21+
], 'client');
22+
});

0 commit comments

Comments
 (0)