Skip to content

Commit

Permalink
refactor(directive): Split out parsers into their own service
Browse files Browse the repository at this point in the history
It may be reasonable to share parsers with other directives
  • Loading branch information
dalelotts committed Jan 31, 2016
1 parent 7343782 commit ca3b2a2
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 49 deletions.
4 changes: 2 additions & 2 deletions demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@
</div>

<p class="help-block">
Input format is 'M/D/YYYY' ( note the MMM for month ).
<br> Display format is 'M/D/YYYY'.
Input format is 'M/D/YYYY h:mm A'.
<br> Display format is 'M/D/YYYY h:mm A'.
<br>Stored as: {{ data.dateDropDown }}
</p>
<div class="alert alert-info" role="alert">
Expand Down
2 changes: 1 addition & 1 deletion karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ module.exports = function (config) {
// - Safari (only Mac)
// - PhantomJS
// - IE (only Windows)
browsers: ['Chrome'],
browsers: ['PhantomJS'],


// If browser does not capture in given timeout [ms], kill it
Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"grunt": "^0.4.4",
"grunt-bump": "^0.7.0",
"gulp": "^3.8.11",
"gulp-htmlmin": "^1.3.0",
"gulp-jscs": "^3.0.2",
"gulp-jshint": "^2.0.0",
"jasmine-core": "^2.4.1",
Expand All @@ -35,11 +36,11 @@
"karma-coverage": "^0.5.3",
"karma-firefox-launcher": "^0.1.7",
"karma-jasmine": "^0.3.2",
"karma-phantomjs-launcher": "^0.2.3",
"karma-phantomjs-launcher": "^1.0.0",
"karma-threshold-reporter": "^0.1.12",
"lodash": "^4.0.0",
"lodash": "^4.1.0",
"matchdep": "^1.0.0",
"phantomjs": "^1.9.19",
"phantomjs-prebuilt": "^2.1.3",
"plato": "^1.5.0",
"run-browser": "^2.0.2",
"semantic-release": "^6.2.0",
Expand Down
91 changes: 48 additions & 43 deletions src/dateTimeInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,54 @@
}(function (angular, moment) {
'use strict';
angular.module('ui.dateTimeInput', [])
.value('dateTimeInputConfig')
.directive('dateTimeInput', [dateTimeInputDirective]);
.service('dateTimeParserFactory', DateTimeParserFactoryService)
.directive('dateTimeInput', DateTimeInputDirective);

function dateTimeInputDirective() {
DateTimeParserFactoryService.$inject = [];

function DateTimeParserFactoryService() {
return function ParserFactory(modelType, inputFormats, dateParseStrict) {
var result;
// Behaviors
switch (modelType) {
case 'Date':
result = dateParser;
break;
case 'moment':
result = momentParser;
break;
case 'milliseconds':
result = millisecondParser;
break;
default: // It is assumed that the modelType is a formatting string.
result = stringParserFactory(modelType);
}

return result;

function dateParser(viewValue) {
return momentParser(viewValue).toDate();
}

function momentParser(viewValue) {
return moment(viewValue, inputFormats, moment.locale(), dateParseStrict);
}

function millisecondParser(viewValue) {
return moment.utc(viewValue, inputFormats, moment.locale(), dateParseStrict).valueOf();
}

function stringParserFactory(modelFormat) {
return function stringParser(viewValue) {
return momentParser(viewValue).format(modelFormat);
};
}
};
}

DateTimeInputDirective.$inject = ['dateTimeParserFactory'];

function DateTimeInputDirective(dateTimeParserFactory) {
return {
require: 'ngModel',
restrict: 'A',
Expand Down Expand Up @@ -58,15 +102,14 @@
var inputFormats = [attrs.dateTimeInput, modelType].concat(scope.dateFormats).concat([moment.ISO_8601]).filter(unique);

// Behaviors
controller.$parsers.unshift(parserFactory(modelType));
controller.$parsers.unshift(dateTimeParserFactory(modelType, inputFormats, dateParseStrict));

controller.$formatters.push(formatter);

controller.$validators.dateTimeInput = validator;

element.bind('blur', applyFormatters);


// Implementation

function unique(value, index, self) {
Expand All @@ -91,44 +134,6 @@
return modelValue;
}

function parserFactory(modelType) {
var result;
// Behaviors
switch (modelType) {
case 'Date':
result = dateParser;
break;
case 'moment':
result = momentParser;
break;
case 'milliseconds':
result = millisecondParser;
break;
default: // It is assumed that the modelType is a formatting string.
result = stringParserFactory(modelType);
}

return result;

function dateParser(viewValue) {
return momentParser(viewValue).toDate();
}

function momentParser(viewValue) {
return moment(viewValue, inputFormats, moment.locale(), dateParseStrict);
}

function millisecondParser(viewValue) {
return moment.utc(viewValue, inputFormats, moment.locale(), dateParseStrict).valueOf();
}

function stringParserFactory(modelFormat) {
return function stringParser(viewValue) {
return momentParser(viewValue).format(modelFormat);
};
}
}

function applyFormatters() {
controller.$viewValue = controller.$formatters.filter(keepAll).reverse().reduce(applyFormatter, controller.$modelValue);
controller.$render();
Expand Down

0 comments on commit ca3b2a2

Please sign in to comment.