Skip to content

Commit

Permalink
Fixed #9635 - Globally configure p-calendar firstDayOfWeek and Fixed #…
Browse files Browse the repository at this point in the history
…10574 - calendar firstDayOfWeek won't change dinamically
  • Loading branch information
yigitfindikli committed Dec 28, 2021
1 parent 0be00b2 commit 9c6682c
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/app/components/api/primengconfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export class PrimeNGConfig {
monthNames: ["January","February","March","April","May","June","July","August","September","October","November","December"],
monthNamesShort: ["Jan", "Feb", "Mar", "Apr", "May", "Jun","Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
dateFormat: 'mm/dd/yy',
firstDayOfWeek: 0,
today: 'Today',
weekHeader: 'Wk',
weak: 'Weak',
Expand Down
1 change: 1 addition & 0 deletions src/app/components/api/translation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export interface Translation {
monthNames?: string[];
monthNamesShort?: string[];
dateFormat?: string;
firstDayOfWeek?: number;
today?: string;
weekHeader?: string;
weak?: string;
Expand Down
3 changes: 2 additions & 1 deletion src/app/components/api/translationkeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export class TranslationKeys {
public static readonly DAY_NAMES_MIN = 'dayNamesMin';
public static readonly MONTH_NAMES = 'monthNames';
public static readonly MONTH_NAMES_SHORT = 'monthNamesShort';
public static readonly FIRST_DAY_OF_WEEK = 'firstDayOfWeek';
public static readonly TODAY = 'today';
public static readonly WEEK_HEADER = 'weekHeader';
public static readonly WEAK = 'weak';
Expand All @@ -38,4 +39,4 @@ export class TranslationKeys {
public static readonly PASSWORD_PROMPT = 'passwordPrompt';
public static readonly EMPTY_MESSAGE = 'emptyMessage';
public static readonly EMPTY_FILTER_MESSAGE = 'emptyFilterMessage';
}
}
24 changes: 20 additions & 4 deletions src/app/components/calendar/calendar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,8 +300,6 @@ export class Calendar implements OnInit,OnDestroy,ControlValueAccessor {

@Input() focusTrap: boolean = true;

@Input() firstDayOfWeek: number = 0;

@Input() showTransitionOptions: string = '.12s cubic-bezier(0, 0, 0.2, 1)';

@Input() hideTransitionOptions: string = '.1s linear';
Expand Down Expand Up @@ -460,6 +458,8 @@ export class Calendar implements OnInit,OnDestroy,ControlValueAccessor {

_numberOfMonths: number = 1;

_firstDayOfWeek: number;

_view: string = 'date';

preventFocus: boolean;
Expand Down Expand Up @@ -596,6 +596,16 @@ export class Calendar implements OnInit,OnDestroy,ControlValueAccessor {
this.createResponsiveStyle();
}

@Input() get firstDayOfWeek(): number {
return this._numberOfMonths;
}

set firstDayOfWeek(firstDayOfWeek: number) {
this._firstDayOfWeek = firstDayOfWeek;

this.createWeekDays();
}

@Input()
set locale(newLocale: LocaleSettings) {
console.warn("Locale property has no effect, use new i18n API instead.");
Expand Down Expand Up @@ -682,7 +692,7 @@ export class Calendar implements OnInit,OnDestroy,ControlValueAccessor {

createWeekDays() {
this.weekDays = [];
let dayIndex = this.firstDayOfWeek;
let dayIndex = this.getFirstDateOfWeek();
let dayLabels = this.getTranslation(TranslationKeys.DAY_NAMES_MIN);
for (let i = 0; i < 7; i++) {
this.weekDays.push(dayLabels[dayIndex]);
Expand Down Expand Up @@ -1178,7 +1188,9 @@ export class Calendar implements OnInit,OnDestroy,ControlValueAccessor {
}

getSundayIndex() {
return this.firstDayOfWeek > 0 ? 7 - this.firstDayOfWeek : 0;
let firstDayOfWeek = this.getFirstDateOfWeek();

return firstDayOfWeek > 0 ? 7 - firstDayOfWeek : 0;
}

isSelected(dateMeta): boolean {
Expand Down Expand Up @@ -2503,6 +2515,10 @@ export class Calendar implements OnInit,OnDestroy,ControlValueAccessor {
return this.dateFormat||this.getTranslation('dateFormat');
}

getFirstDateOfWeek() {
return this._firstDayOfWeek||this.getTranslation(TranslationKeys.FIRST_DAY_OF_WEEK);
}

// Ported from jquery-ui datepicker formatDate
formatDate(date, format) {
if (!date) {
Expand Down
5 changes: 5 additions & 0 deletions src/app/showcase/components/i18n/i18n.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ <h5>en.json</h5>
"monthNames": ["January","February","March","April","May","June","July","August","September","October","November","December"],
"monthNamesShort": ["Jan", "Feb", "Mar", "Apr", "May", "Jun","Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
"dateFormat": "mm/dd/yy",
"firstDayOfWeek": 0,
"today": "Today",
"weekHeader": "Wk",
"weak": 'Weak',
Expand Down Expand Up @@ -264,6 +265,10 @@ <h5>Translation</h5>
<td>dateFormat</td>
<td>mm/dd/yy</td>
</tr>
<tr>
<td>firstDayOfWeek</td>
<td>0</td>
</tr>
<tr>
<td>today</td>
<td>Today</td>
Expand Down

0 comments on commit 9c6682c

Please sign in to comment.