Skip to content

Commit

Permalink
Working on #7: adjustment of the selected day of target month to the …
Browse files Browse the repository at this point in the history
…available days of target month OK. Last thing to fix: the available months should change when the year changes (added a test case for this).
  • Loading branch information
Barbara Post committed Jun 14, 2017
1 parent b969fe0 commit 3b7229c
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 8 deletions.
2 changes: 1 addition & 1 deletion dist/ionic-datepicker.bundle.min.js

Large diffs are not rendered by default.

59 changes: 54 additions & 5 deletions src/ionic-datepicker.provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,19 +245,68 @@ angular.module('ionic-datepicker.provider', [])
var newDay = getAcceptableDay(selectedDate.getDate(),
monthNumber,
selectedDate.getFullYear());
$scope.currentDate = resetHMSM(new Date($scope.currentDate.getFullYear(), monthNumber, newDay));

var newDate = resetHMSM(new Date($scope.currentDate.getFullYear(), monthNumber, newDay));

var toDateTmp = new Date($scope.toDate);
if (newDate.getFullYear() > toDateTmp.getFullYear()
|| (newDate.getFullYear() == toDateTmp.getFullYear() && newDate.getMonth() > toDateTmp.getMonth()))
return;

if((newDate.getFullYear() == toDateTmp.getFullYear() && newDate.getMonth() == toDateTmp.getMonth())) {
if(newDate.getDate() > toDateTmp.getDate()) {
newDate.setDate(toDateTmp.getDate());
}
}

var fromDateTmp = new Date($scope.fromDate);
if (newDate.getFullYear() < fromDateTmp.getFullYear()
|| (newDate.getFullYear() == fromDateTmp.getFullYear() && newDate.getMonth() < fromDateTmp.getMonth()))
return;

if((newDate.getFullYear() == fromDateTmp.getFullYear() && newDate.getMonth() == fromDateTmp.getMonth())) {
if(newDate.getDate() < fromDateTmp.getDate()) {
newDate.setDate(fromDateTmp.getDate());
}
}

$scope.currentDate = newDate;
$scope.adjustSelctedDateEpoch($scope.currentDate, true);
refreshDateList($scope.currentDate);
};

//Year changed
$scope.yearChanged = function (year) {
var selectedDate = new Date($scope.selctedDateEpoch);
var newDay = getAcceptableDay(selectedDate.getDate(),
var selectedDate = new Date($scope.selctedDateEpoch);
var newDay = getAcceptableDay(selectedDate.getDate(),
selectedDate.getMonth(),
year);
$scope.currentDate = resetHMSM(new Date(year, $scope.currentDate.getMonth(), newDay));
$scope.adjustSelctedDateEpoch($scope.currentDate, true);

var newDate = resetHMSM(new Date(year, $scope.currentDate.getMonth(), newDay));

var toDateTmp = new Date($scope.toDate);
if (newDate.getFullYear() > toDateTmp.getFullYear()
|| (newDate.getFullYear() == toDateTmp.getFullYear() && newDate.getMonth() > toDateTmp.getMonth()))
return;

if((newDate.getFullYear() == toDateTmp.getFullYear() && newDate.getMonth() == toDateTmp.getMonth())) {
if(newDate.getDate() > toDateTmp.getDate()) {
newDate.setDate(toDateTmp.getDate());
}
}

var fromDateTmp = new Date($scope.fromDate);
if (newDate.getFullYear() < fromDateTmp.getFullYear()
|| (newDate.getFullYear() == fromDateTmp.getFullYear() && newDate.getMonth() < fromDateTmp.getMonth()))
return;

if((newDate.getFullYear() == fromDateTmp.getFullYear() && newDate.getMonth() == fromDateTmp.getMonth())) {
if(newDate.getDate() < fromDateTmp.getDate()) {
newDate.setDate(fromDateTmp.getDate());
}
}

$scope.currentDate = newDate;$scope.adjustSelctedDateEpoch($scope.currentDate, true);
refreshDateList($scope.currentDate);
};

Expand Down
3 changes: 3 additions & 0 deletions test/www/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ <h1 class="title">Test of ionic date picker</h1>
<div class="row">
<input type="radio" name="disableDates" value="special" ng-model="options.disableDates">Enable from March 12, 2017 to October 18, 2017
</div>
<div class="row">
<input type="radio" name="disableDates" value="special2" ng-model="options.disableDates">Enable from March 12, 2017 to October 18, 2018
</div>
</div>
</div>
</div>
Expand Down
6 changes: 5 additions & 1 deletion test/www/js/controllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,14 @@ angular.module('starter.controllers', [])
// from 03-01-2017 to 08-31-2017
ipObj1.from = new Date(2017, 2, 1);
ipObj1.to = new Date(2017, 7, 31);
} else {
} else if($scope.options.disableDates == "special") {
// from 03-12-2017 to 10-18-2017
ipObj1.from = new Date(2017, 2, 12);
ipObj1.to = new Date(2017, 9, 18);
} else if($scope.options.disableDates == "special2") {
// from 03-12-2017 to 10-18-2018
ipObj1.from = new Date(2017, 2, 12);
ipObj1.to = new Date(2018, 9, 18);
}

ionicDatePicker.openDatePicker(ipObj1);
Expand Down
3 changes: 3 additions & 0 deletions test_original_datepicker/www/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ <h1 class="title">Test of original project's ionic date picker</h1>
<div class="row">
<input type="radio" name="disableDates" value="special" ng-model="options.disableDates">Enable from March 12, 2017 to October 18, 2017
</div>
<div class="row">
<input type="radio" name="disableDates" value="special2" ng-model="options.disableDates">Enable from March 12, 2017 to October 18, 2018
</div>
</div>
</div>
</div>
Expand Down
6 changes: 5 additions & 1 deletion test_original_datepicker/www/js/controllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,14 @@ angular.module('starter.controllers', [])
// from 03-01-2017 to 08-31-2017
ipObj1.from = new Date(2017, 2, 1);
ipObj1.to = new Date(2017, 7, 31);
} else {
} else if($scope.options.disableDates == "special") {
// from 03-12-2017 to 10-18-2017
ipObj1.from = new Date(2017, 2, 12);
ipObj1.to = new Date(2017, 9, 18);
} else if($scope.options.disableDates == "special2") {
// from 03-12-2017 to 10-18-2018
ipObj1.from = new Date(2017, 2, 12);
ipObj1.to = new Date(2018, 9, 18);
}

ionicDatePicker.openDatePicker(ipObj1);
Expand Down

0 comments on commit 3b7229c

Please sign in to comment.