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

Commit 18371ab

Browse files
committed
fix(datepicker): make deprecated controller work with 1.3.x
Closes #4586
1 parent a09275a commit 18371ab

File tree

1 file changed

+186
-4
lines changed

1 file changed

+186
-4
lines changed

src/datepicker/datepicker.js

+186-4
Original file line numberDiff line numberDiff line change
@@ -870,15 +870,197 @@ angular.module('ui.bootstrap.datepicker')
870870

871871
.value('$datepickerSuppressWarning', false)
872872

873-
.controller('DatepickerController', ['$scope', '$attrs', '$log', '$datepickerSuppressWarning', '$controller', function($scope, $attrs, $log, $datepickerSuppressWarning, $controller) {
873+
.controller('DatepickerController', ['$scope', '$attrs', '$parse', '$interpolate', '$log', 'dateFilter', 'uibDatepickerConfig', '$datepickerSuppressError', '$datepickerSuppressWarning', function($scope, $attrs, $parse, $interpolate, $log, dateFilter, datepickerConfig, $datepickerSuppressError, $datepickerSuppressWarning) {
874874
if (!$datepickerSuppressWarning) {
875875
$log.warn('DatepickerController is now deprecated. Use UibDatepickerController instead.');
876876
}
877877

878-
return $controller('UibDatepickerController', {
879-
$scope: $scope,
880-
$attrs: $attrs
878+
var self = this,
879+
ngModelCtrl = { $setViewValue: angular.noop }; // nullModelCtrl;
880+
881+
this.modes = ['day', 'month', 'year'];
882+
883+
angular.forEach(['formatDay', 'formatMonth', 'formatYear', 'formatDayHeader', 'formatDayTitle', 'formatMonthTitle',
884+
'showWeeks', 'startingDay', 'yearRange', 'shortcutPropagation'], function(key, index) {
885+
self[key] = angular.isDefined($attrs[key]) ? (index < 6 ? $interpolate($attrs[key])($scope.$parent) : $scope.$parent.$eval($attrs[key])) : datepickerConfig[key];
881886
});
887+
888+
angular.forEach(['minDate', 'maxDate'], function(key) {
889+
if ($attrs[key]) {
890+
$scope.$parent.$watch($parse($attrs[key]), function(value) {
891+
self[key] = value ? new Date(value) : null;
892+
self.refreshView();
893+
});
894+
} else {
895+
self[key] = datepickerConfig[key] ? new Date(datepickerConfig[key]) : null;
896+
}
897+
});
898+
899+
angular.forEach(['minMode', 'maxMode'], function(key) {
900+
if ($attrs[key]) {
901+
$scope.$parent.$watch($parse($attrs[key]), function(value) {
902+
self[key] = angular.isDefined(value) ? value : $attrs[key];
903+
$scope[key] = self[key];
904+
if ((key == 'minMode' && self.modes.indexOf($scope.datepickerMode) < self.modes.indexOf(self[key])) || (key == 'maxMode' && self.modes.indexOf($scope.datepickerMode) > self.modes.indexOf(self[key]))) {
905+
$scope.datepickerMode = self[key];
906+
}
907+
});
908+
} else {
909+
self[key] = datepickerConfig[key] || null;
910+
$scope[key] = self[key];
911+
}
912+
});
913+
914+
$scope.datepickerMode = $scope.datepickerMode || datepickerConfig.datepickerMode;
915+
$scope.uniqueId = 'datepicker-' + $scope.$id + '-' + Math.floor(Math.random() * 10000);
916+
917+
if (angular.isDefined($attrs.initDate)) {
918+
this.activeDate = $scope.$parent.$eval($attrs.initDate) || new Date();
919+
$scope.$parent.$watch($attrs.initDate, function(initDate) {
920+
if (initDate && (ngModelCtrl.$isEmpty(ngModelCtrl.$modelValue) || ngModelCtrl.$invalid)) {
921+
self.activeDate = initDate;
922+
self.refreshView();
923+
}
924+
});
925+
} else {
926+
this.activeDate = new Date();
927+
}
928+
929+
$scope.isActive = function(dateObject) {
930+
if (self.compare(dateObject.date, self.activeDate) === 0) {
931+
$scope.activeDateId = dateObject.uid;
932+
return true;
933+
}
934+
return false;
935+
};
936+
937+
this.init = function(ngModelCtrl_) {
938+
ngModelCtrl = ngModelCtrl_;
939+
940+
ngModelCtrl.$render = function() {
941+
self.render();
942+
};
943+
};
944+
945+
this.render = function() {
946+
if (ngModelCtrl.$viewValue) {
947+
var date = new Date(ngModelCtrl.$viewValue),
948+
isValid = !isNaN(date);
949+
950+
if (isValid) {
951+
this.activeDate = date;
952+
} else if (!$datepickerSuppressError) {
953+
$log.error('Datepicker directive: "ng-model" value must be a Date object, a number of milliseconds since 01.01.1970 or a string representing an RFC2822 or ISO 8601 date.');
954+
}
955+
}
956+
this.refreshView();
957+
};
958+
959+
this.refreshView = function() {
960+
if (this.element) {
961+
this._refreshView();
962+
963+
var date = ngModelCtrl.$viewValue ? new Date(ngModelCtrl.$viewValue) : null;
964+
ngModelCtrl.$setValidity('dateDisabled', !date || (this.element && !this.isDisabled(date)));
965+
}
966+
};
967+
968+
this.createDateObject = function(date, format) {
969+
var model = ngModelCtrl.$viewValue ? new Date(ngModelCtrl.$viewValue) : null;
970+
return {
971+
date: date,
972+
label: dateFilter(date, format),
973+
selected: model && this.compare(date, model) === 0,
974+
disabled: this.isDisabled(date),
975+
current: this.compare(date, new Date()) === 0,
976+
customClass: this.customClass(date)
977+
};
978+
};
979+
980+
this.isDisabled = function(date) {
981+
return ((this.minDate && this.compare(date, this.minDate) < 0) || (this.maxDate && this.compare(date, this.maxDate) > 0) || ($attrs.dateDisabled && $scope.dateDisabled({date: date, mode: $scope.datepickerMode})));
982+
};
983+
984+
this.customClass = function(date) {
985+
return $scope.customClass({date: date, mode: $scope.datepickerMode});
986+
};
987+
988+
// Split array into smaller arrays
989+
this.split = function(arr, size) {
990+
var arrays = [];
991+
while (arr.length > 0) {
992+
arrays.push(arr.splice(0, size));
993+
}
994+
return arrays;
995+
};
996+
997+
this.fixTimeZone = function(date) {
998+
var hours = date.getHours();
999+
date.setHours(hours === 23 ? hours + 2 : 0);
1000+
};
1001+
1002+
$scope.select = function(date) {
1003+
if ($scope.datepickerMode === self.minMode) {
1004+
var dt = ngModelCtrl.$viewValue ? new Date(ngModelCtrl.$viewValue) : new Date(0, 0, 0, 0, 0, 0, 0);
1005+
dt.setFullYear(date.getFullYear(), date.getMonth(), date.getDate());
1006+
ngModelCtrl.$setViewValue(dt);
1007+
ngModelCtrl.$render();
1008+
} else {
1009+
self.activeDate = date;
1010+
$scope.datepickerMode = self.modes[self.modes.indexOf($scope.datepickerMode) - 1];
1011+
}
1012+
};
1013+
1014+
$scope.move = function(direction) {
1015+
var year = self.activeDate.getFullYear() + direction * (self.step.years || 0),
1016+
month = self.activeDate.getMonth() + direction * (self.step.months || 0);
1017+
self.activeDate.setFullYear(year, month, 1);
1018+
self.refreshView();
1019+
};
1020+
1021+
$scope.toggleMode = function(direction) {
1022+
direction = direction || 1;
1023+
1024+
if (($scope.datepickerMode === self.maxMode && direction === 1) || ($scope.datepickerMode === self.minMode && direction === -1)) {
1025+
return;
1026+
}
1027+
1028+
$scope.datepickerMode = self.modes[self.modes.indexOf($scope.datepickerMode) + direction];
1029+
};
1030+
1031+
// Key event mapper
1032+
$scope.keys = { 13: 'enter', 32: 'space', 33: 'pageup', 34: 'pagedown', 35: 'end', 36: 'home', 37: 'left', 38: 'up', 39: 'right', 40: 'down' };
1033+
1034+
var focusElement = function() {
1035+
self.element[0].focus();
1036+
};
1037+
1038+
$scope.$on('uib:datepicker.focus', focusElement);
1039+
1040+
$scope.keydown = function(evt) {
1041+
var key = $scope.keys[evt.which];
1042+
1043+
if (!key || evt.shiftKey || evt.altKey) {
1044+
return;
1045+
}
1046+
1047+
evt.preventDefault();
1048+
if (!self.shortcutPropagation) {
1049+
evt.stopPropagation();
1050+
}
1051+
1052+
if (key === 'enter' || key === 'space') {
1053+
if (self.isDisabled(self.activeDate)) {
1054+
return; // do nothing
1055+
}
1056+
$scope.select(self.activeDate);
1057+
} else if (evt.ctrlKey && (key === 'up' || key === 'down')) {
1058+
$scope.toggleMode(key === 'up' ? 1 : -1);
1059+
} else {
1060+
self.handleKeyDown(key, evt);
1061+
self.refreshView();
1062+
}
1063+
};
8821064
}])
8831065

8841066
.directive('datepicker', ['$log', '$datepickerSuppressWarning', function($log, $datepickerSuppressWarning) {

0 commit comments

Comments
 (0)