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

Commit 46c7b18

Browse files
committed
feat(datepicker): allow changing first day of the week. Fixes #4316.
1 parent abb064a commit 46c7b18

File tree

4 files changed

+61
-3
lines changed

4 files changed

+61
-3
lines changed

src/components/datepicker/calendar.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -502,10 +502,13 @@
502502
* This should only need to be called once during initialization.
503503
*/
504504
CalendarCtrl.prototype.buildWeekHeader = function() {
505+
var firstDayOfWeek = this.dateLocale.firstDayOfWeek;
506+
var shortDays = this.dateLocale.shortDays;
507+
505508
var row = document.createElement('tr');
506509
for (var i = 0; i < 7; i++) {
507510
var th = document.createElement('th');
508-
th.textContent = this.dateLocale.shortDays[i];
511+
th.textContent = shortDays[(i + firstDayOfWeek) % 7];
509512
row.appendChild(th);
510513
}
511514

src/components/datepicker/calendar.spec.js

+37
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,19 @@ describe('md-calendar', function() {
194194
expect(extractRowText(header)).toEqual(['SZ', 'MZ', 'TZ', 'WZ', 'TZ', 'FZ','SZ']);
195195
dateLocale.shortDays = oldShortDays;
196196
});
197+
198+
it('should allow changing the first day of the week to Monday', function() {
199+
var oldShortDays = dateLocale.shortDays;
200+
dateLocale.shortDays = ['SZ', 'MZ', 'TZ', 'WZ', 'TZ', 'FZ', 'SZ'];
201+
dateLocale.firstDayOfWeek = 1;
202+
203+
var newElement = createElement()[0];
204+
var header = newElement.querySelector('.md-calendar-day-header tr');
205+
206+
expect(extractRowText(header)).toEqual(['MZ', 'TZ', 'WZ', 'TZ', 'FZ','SZ', 'SZ']);
207+
dateLocale.shortDays = oldShortDays;
208+
dateLocale.firstDayOfWeek = 0;
209+
});
197210
});
198211

199212
describe('#buildCalendarForMonth', function() {
@@ -226,6 +239,30 @@ describe('md-calendar', function() {
226239
expect(calendarDates).toEqual(expectedDates);
227240
});
228241

242+
it('should render a month correctly when the first day of the week is Monday', function() {
243+
dateLocale.firstDayOfWeek = 1;
244+
var date = new Date(2014, MAY, 30);
245+
var monthElement = monthCtrl.buildCalendarForMonth(date);
246+
247+
var calendarRows = monthElement.querySelectorAll('tr');
248+
var calendarDates = [];
249+
250+
angular.forEach(calendarRows, function(tr) {
251+
calendarDates.push(extractRowText(tr));
252+
});
253+
254+
var expectedDates = [
255+
['May 2014', '', '1', '2', '3', '4'],
256+
['5', '6', '7', '8', '9', '10', '11'],
257+
['12', '13', '14', '15', '16', '17', '18'],
258+
['19', '20', '21', '22', '23', '24', '25'],
259+
['26', '27', '28', '29', '30', '31', ''],
260+
['', '', '', '', '', '', ''],
261+
];
262+
expect(calendarDates).toEqual(expectedDates);
263+
dateLocale.firstDayOfWeek = 0;
264+
});
265+
229266
it('should show the month on its own row if the first day is before Tuesday', function() {
230267
var date = new Date(2014, JUN, 30); // 1st on Sunday
231268
var monthElement = monthCtrl.buildCalendarForMonth(date);

src/components/datepicker/calendarMonth.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@
172172
var date = this.dateUtil.isValidDate(opt_dateInMonth) ? opt_dateInMonth : new Date();
173173

174174
var firstDayOfMonth = this.dateUtil.getFirstDateOfMonth(date);
175-
var firstDayOfTheWeek = firstDayOfMonth.getDay();
175+
var firstDayOfTheWeek = this.getLocaleDay_(firstDayOfMonth);
176176
var numberOfDaysInMonth = this.dateUtil.getNumberOfDaysInMonth(date);
177177

178178
// Store rows for the month in a document fragment so that we can append them all at once.
@@ -263,4 +263,13 @@
263263
return monthBody;
264264
};
265265

266+
/**
267+
* Gets the day-of-the-week index for a date for the current locale.
268+
* @private
269+
* @param {Date} date
270+
* @returns {number} The column index of the date in the calendar.
271+
*/
272+
CalendarMonthCtrl.prototype.getLocaleDay_ = function(date) {
273+
return (date.getDay() + (7 - this.dateLocale.firstDayOfWeek)) % 7
274+
};
266275
})();

src/components/datepicker/dateLocaleProvider.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
* @property {(Array<string>)=} shortDays Array of abbreviated dayes of the week.
1919
* @property {(Array<string>)=} dates Array of dates of the month. Only necessary for locales
2020
* using a numeral system other than [1, 2, 3...].
21+
* @property {(Array<string>)=} firstDayOfWeek The first day of the week. Sunday = 0, Monday = 1,
22+
* etc.
2123
* @property {(function(string): Date)=} parseDate Function to parse a date object from a string.
2224
* @property {(function(Date): string)=} formatDate Function to format a date object to a string.
2325
* @property {(function(Date): string)=} monthHeaderFormatter Function that returns the label for
@@ -38,6 +40,9 @@
3840
* $mdDateLocaleProvider.days = ['dimanche', 'lundi', 'mardi', ...];
3941
* $mdDateLocaleProvider.shortDays = ['Di', 'Lu', 'Ma', ...];
4042
*
43+
* // Can change week display to start on Monday.
44+
* $mdDateLocaleProvider.firstDayOfWeek = 1;
45+
*
4146
* // Optional.
4247
* $mdDateLocaleProvider.dates = [1, 2, 3, 4, 5, 6, ...];
4348
*
@@ -51,7 +56,7 @@
5156
* };
5257
*
5358
* $mdDateLocaleProvider.monthHeaderFormatter = function(date) {
54-
* myShortMonths[date.getMonth()] + ' ' + date.getFullYear();
59+
* return myShortMonths[date.getMonth()] + ' ' + date.getFullYear();
5560
* };
5661
*
5762
* // In addition to date display, date components also need localized messages
@@ -89,6 +94,9 @@
8994
/** Array of dates of a month (1 - 31). Characters might be different in some locales. */
9095
this.dates = null;
9196

97+
/** Index of the first day of the week. 0 = Sunday, 1 = Monday, etc. */
98+
this.firstDayOfWeek = 0;
99+
92100
/**
93101
* Function that converts the date portion of a Date to a string.
94102
* @type {(function(Date): string)}
@@ -231,6 +239,7 @@
231239
days: this.days || $locale.DATETIME_FORMATS.DAY,
232240
shortDays: this.shortDays || defaultShortDays,
233241
dates: this.dates || defaultDates,
242+
firstDayOfWeek: this.firstDayOfWeek || 0,
234243
formatDate: this.formatDate || defaultFormatDate,
235244
parseDate: this.parseDate || defaultParseDate,
236245
isDateComplete: this.isDateComplete || defaultIsDateComplete,

0 commit comments

Comments
 (0)