|
| 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 | +}); |
0 commit comments