Skip to content

Commit b82a97c

Browse files
authored
Merge pull request #8057 from IgniteUI/calendar-week-numbers
Calendar week numbers
2 parents bc970ff + b3c7a66 commit b82a97c

File tree

14 files changed

+286
-17
lines changed

14 files changed

+286
-17
lines changed

projects/igniteui-angular/src/lib/calendar/calendar-helper-utils.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ export class HelperTestFunctions {
99
public static MODAL_OVERLAY_CSSCLASS = 'igx-overlay__wrapper--modal';
1010

1111
public static CALENDAR_CSSCLASS = '.igx-calendar';
12+
public static CALENDAR_WEEK_NUMBER_CLASS = '.igx-calendar__date--week-number';
13+
public static CALENDAR_WEEK_NUMBER_ITEM_CLASS = '.igx-calendar__date-content--week-number';
14+
public static CALENDAR_WEEK_NUMBER_LABEL_CLASS = '.igx-calendar__label--week-number';
1215
public static CALENDAR_HEADER_CSSCLASS = '.igx-calendar__header';
1316
public static CALENDAR_HEADER_YEAR_CSSCLASS = '.igx-calendar__header-year';
1417
public static CALENDAR_HEADER_DATE_CSSCLASS = '.igx-calendar__header-date';

projects/igniteui-angular/src/lib/calendar/calendar.component.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ <h2 class="igx-calendar__header-date">
5757
[disabledDates]="disabledDates"
5858
[specialDates]="specialDates"
5959
[hideOutsideDays]="hideOutsideDays"
60+
[showWeekNumbers]="showWeekNumbers"
6061
(onViewChanging)="viewChanging($event)"
6162
(onDateSelection)="childClicked($event)">
6263
</igx-days-view>

projects/igniteui-angular/src/lib/calendar/calendar.component.spec.ts

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ describe('IgxCalendar - ', () => {
337337
// 6 weeks + week header
338338
expect(calendarRows.length).toEqual(7);
339339

340-
// 7 calendar rows * 7 elements in each
340+
// 6 calendar rows * 7 elements in each
341341
expect(
342342
dom.queryAll(By.css(`${HelperTestFunctions.CALENDAR_ROW_CSSCLASS} > igx-day-item`)).length
343343
).toEqual(42);
@@ -360,6 +360,45 @@ describe('IgxCalendar - ', () => {
360360
expect(calendarHeader).toBeFalsy();
361361
});
362362

363+
it('Should properly render calendar DOM with week numbers enabled', () => {
364+
const today = new Date(Date.now());
365+
calendar.viewDate = today;
366+
calendar.showWeekNumbers = true;
367+
fixture.detectChanges();
368+
369+
const calendarRows = dom.queryAll(By.css(HelperTestFunctions.CALENDAR_ROW_CSSCLASS));
370+
expect(calendarRows.length).toEqual(7);
371+
372+
// 6 calendar rows * 8 elements in each
373+
expect(
374+
dom.queryAll(By.css(`${HelperTestFunctions.CALENDAR_ROW_CSSCLASS} > igx-day-item`)).length +
375+
dom.queryAll(By.css(`${HelperTestFunctions.CALENDAR_ROW_CSSCLASS} >
376+
${HelperTestFunctions.CALENDAR_WEEK_NUMBER_CLASS}`)).length
377+
).toEqual(48);
378+
expect(
379+
dom.queryAll(By.css(`${HelperTestFunctions.CALENDAR_ROW_CSSCLASS} > span`)).length +
380+
dom.queryAll(By.css(`${HelperTestFunctions.CALENDAR_ROW_CSSCLASS} > ${HelperTestFunctions.CALENDAR_WEEK_NUMBER_LABEL_CLASS}`)).length
381+
).toEqual(8);
382+
383+
});
384+
385+
it('Week numbers should appear as first column', () => {
386+
const firstWeekOfTheYear = new Date(2020, 0, 5);
387+
calendar.viewDate = firstWeekOfTheYear;
388+
calendar.showWeekNumbers = true;
389+
fixture.detectChanges();
390+
391+
const calendarRows = dom.queryAll(By.css(`${HelperTestFunctions.CALENDAR_ROW_CSSCLASS}`));
392+
393+
const maxWeeks = 52;
394+
calendarRows.forEach((row, idx) => {
395+
const firstRowItem = row.nativeElement.children[0];
396+
idx === 0 ?
397+
expect(firstRowItem.firstChild.innerText).toEqual('Wk') :
398+
expect(firstRowItem.firstChild.innerText).toEqual((idx === 1 ? maxWeeks : idx - 1).toString());
399+
});
400+
});
401+
363402
it('Calendar DOM structure - year view | month view', () => {
364403
dom.queryAll(By.css(HelperTestFunctions.CALENDAR_DATE_CSSCLASS))[0].nativeElement.click();
365404
fixture.detectChanges();

projects/igniteui-angular/src/lib/calendar/calendar.component.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,17 @@ export class IgxCalendarComponent extends IgxMonthPickerBaseDirective implements
150150
this._monthsViewNumber = val;
151151
}
152152

153+
/**
154+
* Show/hide week numbers
155+
*
156+
* @example
157+
* ```html
158+
* <igx-calendar [showWeekNumbers]="true"></igx-calendar>
159+
* ``
160+
*/
161+
@Input()
162+
public showWeekNumbers = false;
163+
153164
/**
154165
* Apply the different states for the transitions of animateChange
155166
* @hidden

projects/igniteui-angular/src/lib/calendar/calendar.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,14 @@ export class Calendar {
354354
return this.timedelta(date, TimeDeltaInterval.Year, -1);
355355
}
356356

357+
public getWeekNumber(date: Date) {
358+
const firstJan = new Date(date.getFullYear(), 0, 1).getTime();
359+
const today = new Date(date.getFullYear(), date.getMonth(), date.getDate()).getTime();
360+
const dayInMilSeconds = 86400000;
361+
const dayOfYear = ((today - firstJan + 1) / dayInMilSeconds);
362+
return Math.ceil(dayOfYear / 7);
363+
}
364+
357365
private generateICalendarDate(date: Date, year: number, month: number): ICalendarDate {
358366
return {
359367
date,

projects/igniteui-angular/src/lib/calendar/days-view/days-view.component.html

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
<div class="igx-calendar__body-row">
2+
<div *ngIf="showWeekNumbers" class="igx-calendar__label igx-calendar__label--week-number">
3+
<span>Wk</span>
4+
</div>
25
<span *ngFor="let dayName of generateWeekHeader()" class="igx-calendar__label">
36
{{ dayName | titlecase }}
47
</span>
58
</div>
69

710
<div *ngFor="let week of getCalendarMonth; last as isLast; index as i; trackBy: rowTracker"
811
class="igx-calendar__body-row">
12+
<div class="igx-calendar__date igx-calendar__date--week-number" *ngIf="showWeekNumbers">
13+
<span class="igx-calendar__date-content igx-calendar__date-content--week-number">
14+
{{getWeekNumber(week[0].date)}}
15+
</span>
16+
</div>
17+
<!-- <igx-week-number-item *ngIf="showWeekNumbers">{{getWeekNumber(week[0].date)}}</igx-week-number-item> -->
918
<igx-day-item
1019
*ngFor="let day of week; trackBy: dateTracker"
1120
[date]="day"

projects/igniteui-angular/src/lib/calendar/days-view/days-view.component.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,17 @@ export class IgxDaysViewComponent extends IgxCalendarBaseDirective implements Do
5454
@Input()
5555
public changeDaysView = false;
5656

57+
/**
58+
* Show/hide week numbers
59+
*
60+
* @example
61+
* ```html
62+
* <igx-days-view [showWeekNumbers]="true"></igx-days-view>
63+
* ``
64+
*/
65+
@Input()
66+
public showWeekNumbers: boolean;
67+
5768
/**
5869
* @hidden
5970
*/
@@ -125,6 +136,15 @@ export class IgxDaysViewComponent extends IgxCalendarBaseDirective implements Do
125136
}
126137
}
127138

139+
/**
140+
* Returns the week number by date
141+
*
142+
* @hidden
143+
*/
144+
public getWeekNumber(date): number {
145+
return this.calendarModel.getWeekNumber(date);
146+
}
147+
128148
/**
129149
* Returns the locale representation of the date in the days view.
130150
*

projects/igniteui-angular/src/lib/core/styles/components/calendar/_calendar-component.scss

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@
4545
@extend %cal-value--label !optional;
4646
}
4747

48+
@include e(label, 'week-number') {
49+
@extend %label--week-number !optional;
50+
}
51+
4852
@include e(date) {
4953
@extend %cal-value !optional;
5054

@@ -54,6 +58,10 @@
5458
}
5559
}
5660

61+
@include e(date, 'week-number') {
62+
@extend %cal-value-date--week-number !optional;
63+
}
64+
5765
@include e(date, 'inactive') {
5866
@extend %cal-value !optional;
5967
@extend %cal-value--inactive !optional;
@@ -143,6 +151,10 @@
143151
@extend %cal-value-content !optional;
144152
}
145153

154+
@include e(date-content, 'week-number') {
155+
@extend %cal-value-content--week-number !optional;
156+
}
157+
146158
@include e(year) {
147159
@extend %cal-value !optional;
148160
@extend %cal-value--year !optional;

0 commit comments

Comments
 (0)