Skip to content
This repository was archived by the owner on Sep 5, 2024. It is now read-only.

Commit 8aa5d58

Browse files
Splaktarmmalerba
authored andcommitted
feat(datepicker,calendar): md-date-filter disables months in month mode (#11526)
only the first day of the month is checked to disable the month fix typos in docs fix JSDoc Closes #11525
1 parent 705c54e commit 8aa5d58

File tree

4 files changed

+37
-12
lines changed

4 files changed

+37
-12
lines changed

src/components/datepicker/demoValidations/index.html

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,11 @@ <h4>Inside a md-input-container</h4>
3737
</md-input-container>
3838
</form>
3939
</div>
40+
<div layout-gt-xs="row">
41+
<div flex-gt-xs>
42+
<h4>Date-picker that only allows for certain months to be selected</h4>
43+
<md-datepicker ng-model="ctrl.myDate" md-placeholder="Enter date" md-mode="month"
44+
md-date-filter="ctrl.filter"></md-datepicker>
45+
</div>
46+
</div>
4047
</md-content>

src/components/datepicker/demoValidations/script.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
angular.module('datepickerValidations', ['ngMaterial', 'ngMessages']).controller('AppCtrl', function() {
1+
angular.module('datepickerValidations', ['ngMaterial', 'ngMessages'])
2+
.controller('AppCtrl', function() {
23
this.myDate = new Date();
34

45
this.minDate = new Date(
@@ -13,8 +14,20 @@ angular.module('datepickerValidations', ['ngMaterial', 'ngMessages']).controller
1314
this.myDate.getDate()
1415
);
1516

17+
/**
18+
* @param {Date} date
19+
* @returns {boolean}
20+
*/
1621
this.onlyWeekendsPredicate = function(date) {
1722
var day = date.getDay();
1823
return day === 0 || day === 6;
1924
};
25+
26+
/**
27+
* @param {Date} date
28+
* @returns {boolean} return false to disable all odd numbered months, true for even months
29+
*/
30+
this.filter = function(date) {
31+
return date.getMonth() % 2;
32+
}
2033
});

src/components/datepicker/js/calendar.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@
99
* @param {Date} ng-model The component's model. Should be a Date object.
1010
* @param {Date=} md-min-date Expression representing the minimum date.
1111
* @param {Date=} md-max-date Expression representing the maximum date.
12-
* @param {(function(Date): boolean)=} md-date-filter Function expecting a date and returning a boolean whether it can be selected or not.
12+
* @param {(function(Date): boolean)=} md-date-filter Function expecting a date and returning a
13+
* boolean whether it can be selected or not.
1314
* @param {String=} md-current-view Current view of the calendar. Can be either "month" or "year".
14-
* @param {String=} md-mode Restricts the user to only selecting a value from a particular view. This option can
15-
* be used if the user is only supposed to choose from a certain date type (e.g. only selecting the month).
16-
* Can be either "month" or "day". **Note** that this will ovewrite the `md-current-view` value.
15+
* @param {String=} md-mode Restricts the user to only selecting a value from a particular view.
16+
* This option can be used if the user is only supposed to choose from a certain date type
17+
* (e.g. only selecting the month). Can be either "month" or "day". **Note** that this will
18+
* overwrite the `md-current-view` value.
1719
*
1820
* @description
1921
* `<md-calendar>` is a component that renders a calendar that can be used to select a date.

src/components/datepicker/js/calendarYearBody.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@
8383

8484
/**
8585
* Creates a single cell to contain a year in the calendar.
86-
* @param {number} opt_year Four-digit year.
87-
* @param {number} opt_month Zero-indexed month.
86+
* @param {number} year Four-digit year.
87+
* @param {number} month Zero-indexed month.
8888
* @returns {HTMLElement}
8989
*/
9090
CalendarYearBodyCtrl.prototype.buildMonthCell = function(year, month) {
@@ -98,7 +98,7 @@
9898
cell.id = calendarCtrl.getDateId(firstOfMonth, 'year');
9999

100100
// Use `data-timestamp` attribute because IE10 does not support the `dataset` property.
101-
cell.setAttribute('data-timestamp', firstOfMonth.getTime());
101+
cell.setAttribute('data-timestamp', String(firstOfMonth.getTime()));
102102

103103
if (this.dateUtil.isSameMonthAndYear(firstOfMonth, calendarCtrl.today)) {
104104
cell.classList.add(calendarCtrl.TODAY_CLASS);
@@ -112,15 +112,18 @@
112112

113113
var cellText = this.dateLocale.shortMonths[month];
114114

115-
if (this.dateUtil.isMonthWithinRange(firstOfMonth,
116-
calendarCtrl.minDate, calendarCtrl.maxDate)) {
115+
if (this.dateUtil.isMonthWithinRange(
116+
firstOfMonth, calendarCtrl.minDate, calendarCtrl.maxDate) &&
117+
(!angular.isFunction(this.calendarCtrl.dateFilter) ||
118+
this.calendarCtrl.dateFilter(firstOfMonth))) {
117119
var selectionIndicator = document.createElement('span');
118120
selectionIndicator.classList.add('md-calendar-date-selection-indicator');
119121
selectionIndicator.textContent = cellText;
120122
cell.appendChild(selectionIndicator);
121123
cell.addEventListener('click', yearCtrl.cellClickHandler);
122124

123-
if (calendarCtrl.displayDate && this.dateUtil.isSameMonthAndYear(firstOfMonth, calendarCtrl.displayDate)) {
125+
if (calendarCtrl.displayDate &&
126+
this.dateUtil.isSameMonthAndYear(firstOfMonth, calendarCtrl.displayDate)) {
124127
this.focusAfterAppend = cell;
125128
}
126129
} else {
@@ -133,7 +136,7 @@
133136

134137
/**
135138
* Builds a blank cell.
136-
* @return {HTMLTableCellElement}
139+
* @return {HTMLElement}
137140
*/
138141
CalendarYearBodyCtrl.prototype.buildBlankCell = function() {
139142
var cell = document.createElement('td');

0 commit comments

Comments
 (0)