|
2 | 2 | /*jslint vars:true */
|
3 | 3 |
|
4 | 4 | /**
|
5 |
| - * @license angular-date-time-input version: 0.1.0 |
| 5 | + * @license angular-date-time-input |
6 | 6 | * (c) 2013-2015 Knight Rider Consulting, Inc. http://www.knightrider.com
|
7 | 7 | * License: MIT
|
8 | 8 | *
|
|
35 | 35 | // Behaviors
|
36 | 36 | switch (modelType) {
|
37 | 37 | case 'Date':
|
38 |
| - result = dateParser; |
| 38 | + result = handleEmpty(dateParser); |
39 | 39 | break;
|
40 | 40 | case 'moment':
|
41 |
| - result = momentParser; |
| 41 | + result = handleEmpty(momentParser); |
42 | 42 | break;
|
43 | 43 | case 'milliseconds':
|
44 |
| - result = millisecondParser; |
| 44 | + result = handleEmpty(millisecondParser); |
45 | 45 | break;
|
46 | 46 | default: // It is assumed that the modelType is a formatting string.
|
47 |
| - result = stringParserFactory(modelType); |
| 47 | + result = handleEmpty(stringParserFactory(modelType)); |
48 | 48 | }
|
49 | 49 |
|
50 | 50 | return result;
|
51 | 51 |
|
| 52 | + function handleEmpty(delegate) { |
| 53 | + return function (viewValue) { |
| 54 | + if (angular.isUndefined(viewValue) || viewValue === '' || viewValue === null) { |
| 55 | + return null; |
| 56 | + } else { |
| 57 | + return delegate(viewValue); |
| 58 | + } |
| 59 | + }; |
| 60 | + } |
| 61 | + |
52 | 62 | function dateParser(viewValue) {
|
53 | 63 | return momentParser(viewValue).toDate();
|
54 | 64 | }
|
|
118 | 128 | }
|
119 | 129 |
|
120 | 130 | function validator(modelValue, viewValue) {
|
121 |
| - return angular.isDefined(viewValue) ? moment(viewValue, inputFormats, moment.locale(), dateParseStrict).isValid() : true; |
| 131 | + if (angular.isUndefined(viewValue) || viewValue === '' || viewValue === null) { |
| 132 | + return true; |
| 133 | + } |
| 134 | + return moment(viewValue, inputFormats, moment.locale(), dateParseStrict).isValid(); |
122 | 135 | }
|
123 | 136 |
|
124 | 137 | function formatter(modelValue) {
|
| 138 | + if (angular.isUndefined(modelValue) || modelValue === '' || modelValue === null) { |
| 139 | + return null; |
| 140 | + } |
125 | 141 |
|
126 | 142 | if (angular.isDate(modelValue)) {
|
127 | 143 | return moment(modelValue).format(displayFormat);
|
128 | 144 | } else if (angular.isNumber(modelValue)) {
|
129 | 145 | return moment.utc(modelValue).format(displayFormat);
|
130 |
| - } else if (angular.isDefined(modelValue)) { |
131 |
| - return moment(modelValue, inputFormats, moment.locale(), dateParseStrict).format(displayFormat); |
132 | 146 | }
|
133 |
| - |
134 |
| - return modelValue; |
| 147 | + return moment(modelValue, inputFormats, moment.locale(), dateParseStrict).format(displayFormat); |
135 | 148 | }
|
136 | 149 |
|
137 | 150 | function applyFormatters() {
|
|
0 commit comments