Skip to content
This repository has been archived by the owner on May 29, 2019. It is now read-only.

Commit

Permalink
feat(datepicker): deprecate literal usage
Browse files Browse the repository at this point in the history
- Deprecate support for non-date objects with the datepicker

Closes #5658
Closes #5732
  • Loading branch information
wesleycho committed Apr 4, 2016
1 parent 4fe5d99 commit fc02fd1
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 6 deletions.
10 changes: 8 additions & 2 deletions src/datepicker/datepicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ angular.module('ui.bootstrap.datepicker', ['ui.bootstrap.dateparser', 'ui.bootst

.value('$datepickerSuppressError', false)

.value('$datepickerLiteralWarning', true)

.constant('uibDatepickerConfig', {
datepickerMode: 'day',
formatDay: 'dd',
Expand All @@ -21,8 +23,8 @@ angular.module('ui.bootstrap.datepicker', ['ui.bootstrap.dateparser', 'ui.bootst
yearRows: 4
})

.controller('UibDatepickerController', ['$scope', '$attrs', '$parse', '$interpolate', '$locale', '$log', 'dateFilter', 'uibDatepickerConfig', '$datepickerSuppressError', 'uibDateParser',
function($scope, $attrs, $parse, $interpolate, $locale, $log, dateFilter, datepickerConfig, $datepickerSuppressError, dateParser) {
.controller('UibDatepickerController', ['$scope', '$attrs', '$parse', '$interpolate', '$locale', '$log', 'dateFilter', 'uibDatepickerConfig', '$datepickerLiteralWarning', '$datepickerSuppressError', 'uibDateParser',
function($scope, $attrs, $parse, $interpolate, $locale, $log, dateFilter, datepickerConfig, $datepickerLiteralWarning, $datepickerSuppressError, dateParser) {
var self = this,
ngModelCtrl = { $setViewValue: angular.noop }, // nullModelCtrl;
ngModelOptions = {},
Expand Down Expand Up @@ -99,6 +101,10 @@ angular.module('ui.bootstrap.datepicker', ['ui.bootstrap.dateparser', 'ui.bootst
if (angular.isDate(value)) {
self[key] = dateParser.fromTimezone(new Date(value), ngModelOptions.timezone);
} else {
if ($datepickerLiteralWarning) {
$log.warn('Literal date support has been deprecated, please switch to date object usage');
}

self[key] = new Date(dateFilter(value, 'medium'));
}
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/datepicker/docs/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ Apart from the previous settings, to configure the uib-datepicker you need to cr
<small class="badge">C</small>
<i class="glyphicon glyphicon-eye-open"></i>
_(Default: `null`)_ -
Defines the maximum available date.
Defines the maximum available date. Requires a Javascript Date object.

* `maxMode`
<small class="badge">C</small>
Expand All @@ -96,7 +96,7 @@ Apart from the previous settings, to configure the uib-datepicker you need to cr
<small class="badge">C</small>
<i class="glyphicon glyphicon-eye-open"></i>
_(Default: `null`)_ -
Defines the minimum available date.
Defines the minimum available date. Requires a Javascript Date object.

* `minMode`
<small class="badge">C</small>
Expand Down
80 changes: 80 additions & 0 deletions src/datepicker/test/datepicker.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,86 @@ describe('datepicker', function() {
element.trigger(e);
}

describe('$datepickerLiteralWarning', function() {
var $compile,
$log,
$scope;

it('should warn when using literals for min date by default', function() {
inject(function(_$log_, _$rootScope_, _$compile_) {
$log = _$log_;
$scope = _$rootScope_.$new();
$compile = _$compile_;
});

spyOn($log, 'warn');
$scope.options = {
minDate: '1984-01-01'
};
element = $compile('<uib-datepicker ng-model="locals.date" datepicker-options="options"></uib-datepicker>')($scope);
$scope.$digest();

expect($log.warn).toHaveBeenCalledWith('Literal date support has been deprecated, please switch to date object usage');
});

it('should suppress warning when using literals for min date', function() {
module(function($provide) {
$provide.value('$datepickerLiteralWarning', false);
});
inject(function(_$log_, _$rootScope_, _$compile_) {
$log = _$log_;
$scope = _$rootScope_.$new();
$compile = _$compile_;
});

spyOn($log, 'warn');
$scope.options = {
minDate: '1984-01-01'
};
element = $compile('<uib-datepicker ng-model="locals.date" datepicker-options="options"></uib-datepicker>')($scope);
$scope.$digest();

expect($log.warn).not.toHaveBeenCalled();
});

it('should warn when using literals for max date by default', function() {
inject(function(_$log_, _$rootScope_, _$compile_) {
$log = _$log_;
$scope = _$rootScope_.$new();
$compile = _$compile_;
});

spyOn($log, 'warn');
$scope.options = {
maxDate: '1984-01-01'
};
element = $compile('<uib-datepicker ng-model="locals.date" datepicker-options="options"></uib-datepicker>')($scope);
$scope.$digest();

expect($log.warn).toHaveBeenCalledWith('Literal date support has been deprecated, please switch to date object usage');
});

it('should suppress warning when using literals for max date', function() {
module(function($provide) {
$provide.value('$datepickerLiteralWarning', false);
});
inject(function(_$log_, _$rootScope_, _$compile_) {
$log = _$log_;
$scope = _$rootScope_.$new();
$compile = _$compile_;
});

spyOn($log, 'warn');
$scope.options = {
maxDate: '1984-01-01'
};
element = $compile('<uib-datepicker ng-model="locals.date" datepicker-options="options"></uib-datepicker>')($scope);
$scope.$digest();

expect($log.warn).not.toHaveBeenCalled();
});
});

describe('$datepickerSuppressError', function() {
var $compile,
$log,
Expand Down
10 changes: 8 additions & 2 deletions src/datepickerPopup/popup.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
angular.module('ui.bootstrap.datepickerPopup', ['ui.bootstrap.datepicker', 'ui.bootstrap.position'])

.value('$datepickerPopupLiteralWarning', true)

.constant('uibDatepickerPopupConfig', {
altInputFormats: [],
appendToBody: false,
Expand All @@ -20,8 +22,8 @@ angular.module('ui.bootstrap.datepickerPopup', ['ui.bootstrap.datepicker', 'ui.b
placement: 'auto bottom-left'
})

.controller('UibDatepickerPopupController', ['$scope', '$element', '$attrs', '$compile', '$log', '$parse', '$window', '$document', '$rootScope', '$uibPosition', 'dateFilter', 'uibDateParser', 'uibDatepickerPopupConfig', '$timeout', 'uibDatepickerConfig',
function($scope, $element, $attrs, $compile, $log, $parse, $window, $document, $rootScope, $position, dateFilter, dateParser, datepickerPopupConfig, $timeout, datepickerConfig) {
.controller('UibDatepickerPopupController', ['$scope', '$element', '$attrs', '$compile', '$log', '$parse', '$window', '$document', '$rootScope', '$uibPosition', 'dateFilter', 'uibDateParser', 'uibDatepickerPopupConfig', '$timeout', 'uibDatepickerConfig', '$datepickerPopupLiteralWarning',
function($scope, $element, $attrs, $compile, $log, $parse, $window, $document, $rootScope, $position, dateFilter, dateParser, datepickerPopupConfig, $timeout, datepickerConfig, $datepickerPopupLiteralWarning) {
var cache = {},
isHtml5DateInput = false;
var dateFormat, closeOnDateSelection, appendToBody, onOpenFocus,
Expand Down Expand Up @@ -203,6 +205,10 @@ function($scope, $element, $attrs, $compile, $log, $parse, $window, $document, $
} else if (angular.isDate($scope.datepickerOptions[key])) {
dates[key] = dateParser.fromTimezone(new Date($scope.datepickerOptions[key]), timezone);
} else {
if ($datepickerPopupLiteralWarning) {
$log.warn('Literal date support has been deprecated, please switch to date object usage');
}

dates[key] = new Date(dateFilter($scope.datepickerOptions[key], 'medium'));
}
});
Expand Down

0 comments on commit fc02fd1

Please sign in to comment.