From f718e967550c33ce18da177d3bff48fcb70afc8a Mon Sep 17 00:00:00 2001 From: Andy Knight Date: Wed, 29 Jan 2014 00:19:58 -0500 Subject: [PATCH] Added returnType configuration option to allow return of a moment instead of a date as desired (as well as karma tests) --- src/js/datetimepicker.js | 11 +++++++--- test/returnType.spec.js | 43 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 3 deletions(-) create mode 100644 test/returnType.spec.js diff --git a/src/js/datetimepicker.js b/src/js/datetimepicker.js index 9a2fd0e9..0d4e6a66 100644 --- a/src/js/datetimepicker.js +++ b/src/js/datetimepicker.js @@ -18,12 +18,13 @@ angular.module('ui.bootstrap.datetimepicker', []) startView: 'day', minView: 'minute', minuteStep: 5, - dropdownSelector: null + dropdownSelector: null, + returnType: 'date' }) .constant('dateTimePickerConfigValidation', function (configuration) { "use strict"; - var validOptions = ['startView', 'minView', 'minuteStep', 'dropdownSelector']; + var validOptions = ['startView', 'minView', 'minuteStep', 'dropdownSelector', 'returnType']; for (var prop in configuration) { if (configuration.hasOwnProperty(prop)) { @@ -35,6 +36,7 @@ angular.module('ui.bootstrap.datetimepicker', []) // Order of the elements in the validViews array is significant. var validViews = ['minute', 'hour', 'day', 'month', 'year']; + var validReturnTypes = ['moment', 'date']; if (validViews.indexOf(configuration.startView) < 0) { throw ("invalid startView value: " + configuration.startView); @@ -57,6 +59,9 @@ angular.module('ui.bootstrap.datetimepicker', []) if (configuration.dropdownSelector !== null && !angular.isString(configuration.dropdownSelector)) { throw ("dropdownSelector must be a string"); } + if (validReturnTypes.indexOf(configuration.returnType) < 0) { + throw ("returnType must be either moment or date. Received: " + configuration.returnType); + } } ) .directive('datetimepicker', ['dateTimePickerConfig', 'dateTimePickerConfigValidation', function (defaultConfig, validateConfigurationFunction) { @@ -302,7 +307,7 @@ angular.module('ui.bootstrap.datetimepicker', []) if (angular.isFunction(scope.onSetTime)) { scope.onSetTime(newDate, scope.ngModel); } - scope.ngModel = newDate; + scope.ngModel = configuration.returnType === 'date' ? newDate : moment(newDate); return dataFactory[scope.data.currentView](unixDate); } }; diff --git a/test/returnType.spec.js b/test/returnType.spec.js new file mode 100644 index 00000000..3ec560b2 --- /dev/null +++ b/test/returnType.spec.js @@ -0,0 +1,43 @@ +/*globals describe, beforeEach, it, expect, module, inject */ + +/** + * @license angular-bootstrap-datetimepicker + * (c) 2013 Knight Rider Consulting, Inc. http://www.knightrider.com + * License: MIT + */ + +/** + * + * @author A.M. Knight + * @since 1/29/14 + */ + +describe('returnType', function () { + 'use strict'; + var $rootScope, $compile; + beforeEach(module('ui.bootstrap.datetimepicker')); + beforeEach(inject(function (_$compile_, _$rootScope_) { + $compile = _$compile_; + $rootScope = _$rootScope_; + $rootScope.date = null; + })); + + describe('throws exception', function () { + it('if returnType is not a valid returnType', function () { + function compile() { + $compile('')($rootScope); + } + + expect(compile).toThrow("returnType must be either moment or date. Received: foo"); + }); + }); + describe('does NOT throw exception for valid values', function () { + it('if value is in valid return types', function () { + var validReturnTypes = ['moment', 'date']; + + for (var i = 0; i < validReturnTypes.length; i++) { + $compile('')($rootScope); + } + }); + }); +}); \ No newline at end of file