Skip to content

Dev prior #47

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 30, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 29 additions & 5 deletions dist/datetime.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,12 +214,17 @@ angular.module("datetime").directive("datetime", ["datetime", "$log", "$document
return !ngModel.$error.min && !ngModel.$error.max;
}

ngModel.$parsers.push(function (viewValue) {
ngModel.$parsers.unshift(function (viewValue) {
// You will get undefined when input is required and model get unset
if (angular.isUndefined(viewValue)) {
viewValue = parser.getText();
}

if (!angular.isString(viewValue)) {
// let unknown value pass through
return viewValue;
}

mask.digest(null, viewValue);

if (!parser.isInit()) {
Expand All @@ -240,13 +245,22 @@ angular.module("datetime").directive("datetime", ["datetime", "$log", "$document
return undefined;
});

function validModelType(model) {
if (angular.isDate(model) && !modelParser) {
return true;
}
if (angular.isString(model) && modelParser) {
return true;
}
return false;
}

ngModel.$formatters.push(function (modelValue) {

// formatter clean the error
ngModel.$setValidity("datetime", true);
if (!ngModel.$validate) {
validMinMax(modelValue);
}

// handle empty value
if (!modelValue) {
parser.unset();
// FIXME: input will be cleared if modelValue is empty and the input is required. This is a temporary fix.
Expand All @@ -256,18 +270,28 @@ angular.module("datetime").directive("datetime", ["datetime", "$log", "$document
return parser.getText();
}

// let unknown model type pass through
if (!validModelType(modelValue)) {
return modelValue;
}

if (modelParser) {
modelValue = modelParser.parse(modelValue).getDate();
}

if (!ngModel.$validate) {
validMinMax(modelValue);
}

return parser.setDate(modelValue).getText();
});
}

return {
restrict: "A",
require: "?ngModel",
link: linkFunc
link: linkFunc,
priority: 100
};
}]);

Expand Down
38 changes: 31 additions & 7 deletions lib/directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,11 +190,16 @@ angular.module("datetime").directive("datetime", function(datetime, $log, $docum
return !ngModel.$error.min && !ngModel.$error.max;
}

ngModel.$parsers.push(function(viewValue){
ngModel.$parsers.unshift(function(viewValue){
// You will get undefined when input is required and model get unset
if (angular.isUndefined(viewValue)) {
viewValue = parser.getText();
}

if (!angular.isString(viewValue)) {
// let unknown value pass through
return viewValue;
}

mask.digest(null, viewValue);

Expand All @@ -215,14 +220,23 @@ angular.module("datetime").directive("datetime", function(datetime, $log, $docum

return undefined;
});

function validModelType(model) {
if (angular.isDate(model) && !modelParser) {
return true;
}
if (angular.isString(model) && modelParser) {
return true;
}
return false;
}

ngModel.$formatters.push(function(modelValue){


// formatter clean the error
ngModel.$setValidity("datetime", true);
if (!ngModel.$validate) {
validMinMax(modelValue);
}


// handle empty value
if (!modelValue) {
parser.unset();
// FIXME: input will be cleared if modelValue is empty and the input is required. This is a temporary fix.
Expand All @@ -231,10 +245,19 @@ angular.module("datetime").directive("datetime", function(datetime, $log, $docum
});
return parser.getText();
}

// let unknown model type pass through
if (!validModelType(modelValue)) {
return modelValue;
}

if (modelParser) {
modelValue = modelParser.parse(modelValue).getDate();
}

if (!ngModel.$validate) {
validMinMax(modelValue);
}

return parser.setDate(modelValue).getText();
});
Expand All @@ -243,6 +266,7 @@ angular.module("datetime").directive("datetime", function(datetime, $log, $docum
return {
restrict: "A",
require: "?ngModel",
link: linkFunc
link: linkFunc,
priority: 100
};
});