Skip to content

Commit

Permalink
2.6.1
Browse files Browse the repository at this point in the history
Fixed #186
  • Loading branch information
Gillardo committed Feb 20, 2018
1 parent 32dec98 commit 6bac813
Showing 1 changed file with 39 additions and 17 deletions.
56 changes: 39 additions & 17 deletions datetime-picker.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ angular.module('ui.bootstrap.datetimepicker', ['ui.bootstrap.dateparser', 'ui.bo

this.init = function (_ngModel) {
ngModel = _ngModel;
ngModelOptions = ngModel.$options || uiDatetimePickerConfig.ngModelOptions;
ngModelOptions = extractOptions(ngModel);

$scope.buttonBar = angular.isDefined($attrs.buttonBar) ? $scope.$parent.$eval($attrs.buttonBar) : uiDatetimePickerConfig.buttonBar;

Expand Down Expand Up @@ -127,17 +127,9 @@ angular.module('ui.bootstrap.datetimepicker', ['ui.bootstrap.dateparser', 'ui.bo
'<div uib-timepicker style="margin:0 auto"></div>' +
'</div>');

$scope.ngModelOptions = angular.copy(ngModelOptions);

if ($scope.ngModelOptions.updateOnDefault === true) {
$scope.ngModelOptions.updateOn = $scope.ngModelOptions.updateOn ?
$scope.ngModelOptions.updateOn + ' default' : 'default';
}

// get attributes from directive
popupEl.attr({
'ng-model': 'date',
'ng-model-options': 'ngModelOptions',
'ng-change': 'dateSelection(date)'
});

Expand Down Expand Up @@ -210,7 +202,7 @@ angular.module('ui.bootstrap.datetimepicker', ['ui.bootstrap.dateparser', 'ui.bo
return value;
}

$scope.date = uibDateParser.fromTimezone(value, ngModelOptions.timezone);
$scope.date = uibDateParser.fromTimezone(value, ngModelOptions.getOption('timezone'));

dateFormat = dateFormat.replace(/M!/, 'MM')
.replace(/d!/, 'dd');
Expand All @@ -219,7 +211,7 @@ angular.module('ui.bootstrap.datetimepicker', ['ui.bootstrap.dateparser', 'ui.bo
});
} else {
ngModel.$formatters.push(function (value) {
$scope.date = uibDateParser.fromTimezone(value, ngModelOptions.timezone);
$scope.date = uibDateParser.fromTimezone(value, ngModelOptions.getOption('timezone'));
return value;
});
}
Expand Down Expand Up @@ -300,9 +292,9 @@ angular.module('ui.bootstrap.datetimepicker', ['ui.bootstrap.dateparser', 'ui.bo
if (!isHtml5DateInput) {
dateFormat = dateFormat.replace(/M!/, 'MM')
.replace(/d!/, 'dd');
return uibDateParser.filter(uibDateParser.fromTimezone(value, ngModelOptions.timezone), dateFormat);
return uibDateParser.filter(uibDateParser.fromTimezone(value, ngModelOptions.getOption('timezone')), dateFormat);
} else {
return uibDateParser.fromTimezone(value, ngModelOptions.timezone).toLocaleString();
return uibDateParser.fromTimezone(value, ngModelOptions.getOption('timezone')).toLocaleString();
}
}
};
Expand Down Expand Up @@ -443,14 +435,14 @@ angular.module('ui.bootstrap.datetimepicker', ['ui.bootstrap.dateparser', 'ui.bo

$scope.isDisabled = function (date) {
if (date === 'today' || date === 'now')
date = uibDateParser.fromTimezone(new Date(), ngModelOptions.timezone);
date = uibDateParser.fromTimezone(new Date(), ngModelOptions.getOption('timezone'));

var dates = {};
angular.forEach(['minDate', 'maxDate'], function (key) {
if (!$scope.datepickerOptions[key]) {
dates[key] = null;
} else if (angular.isDate($scope.datepickerOptions[key])) {
dates[key] = uibDateParser.fromTimezone(new Date($scope.datepickerOptions[key]), ngModelOptions.timezone);
dates[key] = uibDateParser.fromTimezone(new Date($scope.datepickerOptions[key]), ngModelOptions.getOption('timezone'));
} else {
dates[key] = new Date(dateFilter($scope.datepickerOptions[key], 'medium'));
}
Expand All @@ -474,7 +466,7 @@ angular.module('ui.bootstrap.datetimepicker', ['ui.bootstrap.dateparser', 'ui.bo
var date = null;
var isNow = opt === 'now';

if (opt === 'today' || opt == 'now') {
if (opt === 'today' || opt === 'now') {
var now = new Date();
if (angular.isDate($scope.date)) {
date = new Date($scope.date);
Expand Down Expand Up @@ -534,6 +526,14 @@ angular.module('ui.bootstrap.datetimepicker', ['ui.bootstrap.dateparser', 'ui.bo
if ($scope.enableDate && $scope.enableTime)
$scope.showPicker = $scope.reOpenDefault === false ? 'date' : $scope.reOpenDefault;

// manually trigger the blur event
if (ngModelOptions.getOption('updateOn') === 'blur') {
$element[0].focus();
$timeout(function() {
$element[0].blur();
}, 50);
}

// if a on-close-fn has been defined, lets call it
// we only call this if closePressed is defined!
if (angular.isDefined(closePressed)) {
Expand All @@ -560,6 +560,28 @@ angular.module('ui.bootstrap.datetimepicker', ['ui.bootstrap.dateparser', 'ui.bo
$document.unbind('click', documentClickBind);
});

function extractOptions(ngModelCtrl) {
var ngModelOptions;

if (angular.version.minor < 6) { // in angular < 1.6 $options could be missing
// guarantee a value
ngModelOptions = angular.isObject(ngModelCtrl.$options) ?
ngModelCtrl.$options :
{
timezone: null
};

// mimic 1.6+ api
ngModelOptions.getOption = function (key) {
return ngModelOptions[key];
};
} else { // in angular >=1.6 $options is always present
ngModelOptions = ngModelCtrl.$options;
}

return ngModelOptions;
}

function documentClickBind(evt) {
var popup = $popup[0];
var dpContainsTarget = $element[0].contains(evt.target);
Expand Down Expand Up @@ -628,7 +650,7 @@ angular.module('ui.bootstrap.datetimepicker', ['ui.bootstrap.dateparser', 'ui.bo
if (angular.isString(viewValue)) {
var date = parseDateString(viewValue);
if (!isNaN(date)) {
return uibDateParser.toTimezone(date, ngModelOptions.timezone);
return uibDateParser.toTimezone(date, ngModelOptions.getOption('timezone'));
}

return undefined;
Expand Down

0 comments on commit 6bac813

Please sign in to comment.