-
Notifications
You must be signed in to change notification settings - Fork 469
Expand file tree
/
Copy pathCalendarTitles.cs
More file actions
221 lines (193 loc) · 7.67 KB
/
CalendarTitles.cs
File metadata and controls
221 lines (193 loc) · 7.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// ------------------------------------------------------------------------
// This file is licensed to you under the MIT License.
// ------------------------------------------------------------------------
using Microsoft.FluentUI.AspNetCore.Components.Extensions;
namespace Microsoft.FluentUI.AspNetCore.Components.Calendar;
/// <summary>
/// Provides titles and navigation-related properties for a calendar, based on the current view and culture settings.
/// </summary>
/// <typeparam name="TValue">The type of value handled by the calendar.</typeparam>
internal class CalendarTitles<TValue>
{
private readonly FluentCalendar<TValue> _calendar;
/// <summary>
/// Initializes a new instance of the <see cref="CalendarTitles{TValue}"/> class.
/// </summary>
/// <param name="calendar"></param>
public CalendarTitles(FluentCalendar<TValue> calendar)
{
_calendar = calendar;
CalendarExtended = new CalendarExtended(calendar.Culture, calendar.PickerMonth.ConvertToRequiredDateTime());
View = calendar.View;
}
/// <summary>
/// Gets the CalendarExtended to use for the calendar.
/// </summary>
public CalendarExtended CalendarExtended { get; }
/// <summary>
/// Gets the currently View.
/// </summary>
public CalendarViews View { get; }
/// <summary>
/// Gets the current date.
/// </summary>
private DateTime Date => CalendarExtended.Date;
/// <summary>
/// Gets a value indicating whether the calendar Title is not clickable.
/// </summary>
public bool ReadOnly
{
get
{
if (_calendar.IsReadOnlyOrDisabled)
{
return true;
}
return View switch
{
CalendarViews.Days => false,
CalendarViews.Months => false,
CalendarViews.Years => true,
_ => true,
};
}
}
/// <summary>
/// Gets the main calendar title.
/// </summary>
public string Label
{
get
{
return View switch
{
#pragma warning disable MA0011
CalendarViews.Days => CalendarExtended.GetMonthNameAndYear(),
CalendarViews.Months => CalendarExtended.GetYear(),
CalendarViews.Years => CalendarExtended.GetYearsRangeLabel(Date.GetYear(_calendar.Culture) - CalendarExtended.YearShiftCentered),
#pragma warning restore MA0011
_ => string.Empty,
};
}
}
/// <summary>
/// Gets the title representing the previous period based on the current calendar view.
/// </summary>
public string PreviousTitle
{
get
{
return View switch
{
CalendarViews.Days => CalendarExtended.GetMonthName(Date.AddMonths(-1, _calendar.Culture)),
CalendarViews.Months => CalendarExtended.GetYear(Date.AddYears(-1, _calendar.Culture)),
CalendarViews.Years => CalendarExtended.GetYearsRangeLabel(Date.GetYear(_calendar.Culture) - 12 - CalendarExtended.YearShiftCentered),
_ => string.Empty,
};
}
}
/// <summary>
/// Gets a value indicating whether the "Previous" navigation button is disabled.
/// </summary>
public bool PreviousDisabled
{
get
{
#pragma warning disable MA0011
var minDate = _calendar.Culture.Calendar.MinSupportedDateTime.AddMonths(1);
#pragma warning restore MA0011
var rangeMinDate = _calendar.MinDate.ConvertToDateTime()?.Date;
var rangePreviousDisabled = false;
if (rangeMinDate.HasValue)
{
var candidate = View switch
{
CalendarViews.Days => Date.AddMonths(-1, _calendar.Culture),
CalendarViews.Months => Date.AddYears(-1, _calendar.Culture),
CalendarViews.Years => Date.AddYears(-12, _calendar.Culture),
_ => Date,
};
var candidatePeriodEnd = View switch
{
CalendarViews.Days
=> candidate.StartOfMonth(_calendar.Culture)
.AddMonths(1, _calendar.Culture)
.AddDays(-1),
CalendarViews.Months
=> candidate.StartOfYear(_calendar.Culture)
.AddYears(1, _calendar.Culture)
.AddDays(-1),
CalendarViews.Years
=> candidate.StartOfYear(_calendar.Culture)
.AddYears(12, _calendar.Culture)
.AddDays(-1),
_ => candidate,
};
rangePreviousDisabled = candidatePeriodEnd.Date < rangeMinDate.Value;
}
return View switch
{
CalendarViews.Days => (Date.Year == minDate.Year && Date.Month == minDate.Month) || rangePreviousDisabled,
CalendarViews.Months => Date.Year == minDate.Year || rangePreviousDisabled,
CalendarViews.Years => Date.Year - CalendarExtended.YearShiftCentered <= minDate.Year + 12 || rangePreviousDisabled,
_ => false,
};
}
}
/// <summary>
/// Gets the title representing the next time period based on the current calendar view.
/// </summary>
public string NextTitle
{
get
{
return View switch
{
CalendarViews.Days => CalendarExtended.GetMonthName(Date.AddMonths(+1, _calendar.Culture)),
CalendarViews.Months => CalendarExtended.GetYear(Date.AddYears(+1, _calendar.Culture)),
CalendarViews.Years => CalendarExtended.GetYearsRangeLabel(Date.GetYear(_calendar.Culture) + 12 - CalendarExtended.YearShiftCentered),
_ => string.Empty,
};
}
}
/// <summary>
/// Gets a value indicating whether the "Next" navigation option is disabled.
/// </summary>
public bool NextDisabled
{
get
{
var maxDate = _calendar.Culture.Calendar.MaxSupportedDateTime;
var rangeMaxDate = _calendar.MaxDate.ConvertToDateTime()?.Date;
var rangeNextDisabled = false;
if (rangeMaxDate.HasValue)
{
var candidate = View switch
{
CalendarViews.Days => Date.AddMonths(+1, _calendar.Culture),
CalendarViews.Months => Date.AddYears(+1, _calendar.Culture),
CalendarViews.Years => Date.AddYears(+12, _calendar.Culture),
_ => Date,
};
var candidatePeriodStart = View switch
{
CalendarViews.Days
=> candidate.StartOfMonth(_calendar.Culture),
CalendarViews.Months
=> candidate.StartOfYear(_calendar.Culture),
CalendarViews.Years
=> candidate.StartOfYear(_calendar.Culture),
_ => candidate,
};
rangeNextDisabled = candidatePeriodStart.Date > rangeMaxDate.Value;
}
return View switch
{
CalendarViews.Days => (Date.Year == maxDate.Year && Date.Month == maxDate.Month) || rangeNextDisabled,
CalendarViews.Months => Date.Year == maxDate.Year || rangeNextDisabled,
CalendarViews.Years => Date.Year + 12 - CalendarExtended.YearShiftCentered >= maxDate.Year || rangeNextDisabled,
_ => false,
};
}
}
}