31 |
32 | @for (var i = 0; i < 7; i++)
33 | {
34 | var d = Dates.GetNumOfDay(i);
35 |
36 |
39 | }
40 |
41 | @for ( var i = 0; i < 40; i++ )
42 | {
43 | if (State == StateCase.Before)
44 | {
45 | if (firstDayWeek == Dates.GetNumOfDay(i)) // Cell is first day?
46 | {
47 | State = StateCase.InMonth; // Start filling days
48 | var dayClick = DayCounter;
49 | CSSbackground = GetBackground(dayClick);
50 |
ClickDayInternal(e, dayClick)"
54 | @ondrop="() => HandleDayOnDrop(dayClick)">
55 | @(DayCounter.Day.ToString())
56 |
57 | DayCounter = DayCounter.AddDays(1);
58 | OffsetCell = i;
59 | }
60 | else
61 | {
62 | // Still empty cells
63 |
HandleClickOutsideCurrentMonthClick(-1)">
65 |
66 | }
67 | }
68 | if (State == StateCase.InMonth)
69 | {
70 | if (DayCounter >= LastDay) // Stop at last day
71 | {
72 | State = StateCase.After;
73 | // Again empty cells from here on
74 |
HandleClickOutsideCurrentMonthClick(1)">
76 |
77 | }
78 | else
79 | {
80 | DateTime _dayClick = DayCounter;
81 | CSSbackground = GetBackground(_dayClick);
82 | if (HighlightToday)
83 | {
84 | CSSToday = DayCounter == Today ? "monthly-today" : null;
85 | }
86 |
87 |
ClickDayInternal(e, _dayClick)"
91 | @ondrop="() => HandleDayOnDrop(_dayClick)">
92 | @(DayCounter.Day.ToString())
93 |
94 | DayCounter = DayCounter.AddDays(1);
95 | }
96 | }
97 | if (State == StateCase.After)
98 | {
99 |
HandleClickOutsideCurrentMonthClick(1)">
101 |
102 | }
103 | }
104 |
105 | @if ( TasksList is not null )
106 | {
107 | // occupiedPosition accumulates the number of tasks in a cell
108 | var occupiedPosition = new TaskPosition[32]; // If one day, there are 32 days in a month, it will crash :P
109 | for (int i = 0; i < 32; ++i)
110 | {
111 | occupiedPosition[i] = new TaskPosition();
112 | }
113 |
114 | string? classPosition;
115 | string taskContent = string.Empty;
116 | string? taskComment = null;
117 | bool onmMultiLine = false;
118 | bool draggable = false;
119 |
120 | for (var k = 0; k < TasksList.Length; k++)
121 | {
122 | Tasks t = TasksList[k];
123 |
124 | if (( t.DateStart.Date <= FirstDate && FirstDate <= t.DateEnd.Date ) ||
125 | ( t.DateStart.Date > FirstDate && LastDay > t.DateEnd.Date ) ||
126 | ( t.DateStart.Date < LastDay && LastDay <= t.DateEnd.Date ))
127 | {
128 | draggable = t.NotBeDraggable ? false : Draggable;
129 |
130 | // Reframes dates in the month
131 | DateTime Start = t.DateStart.Date < FirstDate ? FirstDate : t.DateStart.Date;
132 | DateTime End = t.DateEnd.Date >= LastDay ? LastDay.AddDays(-1) : t.DateEnd.Date;
133 |
134 | // 7 => num of colum (a week..)
135 | // + 2 => the 1st row is the week name
136 | int x = (Start.Day + OffsetCell - 1) % 7 + 1;
137 | int y = (Start.Day + OffsetCell - 1) / 7 + 2;
138 | int s = (int)(End.Date - Start.Date).TotalDays + 1;
139 |
140 | classPosition = null;
141 |
142 | TaskPosition position = occupiedPosition[Start.Day];
143 |
144 | if ( position.Top == false )
145 | {
146 | for (int i = Start.Day; i < Start.Day + s; ++i)
147 | {
148 | occupiedPosition[i].Top = true;
149 | }
150 | classPosition = "monthly-task-first";
151 | }
152 | else if ( position.Center == false )
153 | {
154 | for (int i = Start.Day; i < Start.Day + s; ++i)
155 | {
156 | occupiedPosition[i].Center = true;
157 | }
158 | classPosition = "monthly-task-second";
159 | }
160 | else if (position.Bottom == false)
161 | {
162 | for (int i = Start.Day; i < Start.Day + s; ++i)
163 | {
164 | occupiedPosition[i].Bottom = true;
165 | }
166 | classPosition = "monthly-task-bottom";
167 | }
168 |
169 | string borderClass = "border-start";
170 | do
171 | {
172 | string row = $"grid-column:{x} / span {s}; grid-row:{y};";
173 |
174 | if ( classPosition is not null )
175 | {
176 | if ( PriorityDisplay == PriorityLabel.Code )
177 | {
178 | taskContent = string.IsNullOrWhiteSpace(t.Code) ? t.Caption : t.Code;
179 | }
180 | else
181 | {
182 | taskContent = string.IsNullOrWhiteSpace(t.Caption) ? t.Code : t.Caption;
183 | }
184 |
185 | // If there is time, add start time at begening
186 | if ( t.DateStart.Hour + t.DateStart.Minute > 0 )
187 | {
188 | taskContent = $"{t.DateStart.ToString("t")} {taskContent}" ;
189 | }
190 |
191 | taskComment = string.IsNullOrWhiteSpace(t.Comment) ? null : t.Comment;
192 |
193 | string taskColor = Colors.GetHatching(t.FillStyle, t.Color);
194 | if (!String.IsNullOrEmpty(t.ForeColor))
195 | {
196 | taskColor = taskColor + $"color:{t.ForeColor}";
197 | }
198 |
199 |
ClickTaskInternal(e, t.ID, Start)"
205 | @ondragstart="() => HandleDragStart(t.ID)">
206 | @taskContent
207 |
208 | }
209 | else
210 | {
211 | // Mode 2 tasks in cell => we display "more..."
212 |
ClickAllDayInternal(e, Start)">
215 | @($"+ {occupiedPosition[Start.Day].Counter - 2} ...")
216 |
217 | }
218 |
219 | onmMultiLine = false;
220 | if (x + s > 8)
221 | {
222 | onmMultiLine = true;
223 |
224 | Start = Start.AddDays(8 - x);
225 | End = t.DateEnd.Date >= LastDay ? LastDay.AddDays(-1) : t.DateEnd.Date;
226 |
227 | x = (Start.Day + OffsetCell - 1) % 7 + 1;
228 | y = (Start.Day + OffsetCell - 1) / 7 + 2;
229 | s = (int)(End.Date - Start.Date).TotalDays + 1;
230 |
231 | borderClass = string.Empty;
232 | }
233 |
234 | } while ( onmMultiLine );
235 |
236 | // Start et End may have been modified, I redefine them for the whole month
237 | Start = t.DateStart.Date < FirstDate ? FirstDate : t.DateStart.Date;
238 | End = t.DateEnd.Date >= LastDay ? LastDay.AddDays(-1) : t.DateEnd.Date;
239 |
240 | for (int d = Start.Day; d <= End.Day; d++)
241 | {
242 | occupiedPosition[d].Counter++;
243 | }
244 |
245 | }
246 | }
247 | }
248 |
249 |
--------------------------------------------------------------------------------
/BlazorCalendar/MonthlyView.razor.cs:
--------------------------------------------------------------------------------
1 | namespace BlazorCalendar;
2 |
3 | using BlazorCalendar.Models;
4 | using Microsoft.AspNetCore.Components;
5 | using Microsoft.AspNetCore.Components.Web;
6 |
7 | partial class MonthlyView : CalendarBase
8 | {
9 | [CascadingParameter(Name = "SelectedView")]
10 | public DisplayedView DisplayedView { get; set; } = DisplayedView.Monthly;
11 |
12 | private DateTime _firstdate;
13 |
14 | [CascadingParameter(Name = "FirstDate")]
15 | public DateTime FirstDate
16 | {
17 | get
18 | {
19 | if (_firstdate == DateTime.MinValue) _firstdate = DateTime.Today;
20 | return _firstdate.Date;
21 | }
22 | set
23 | {
24 | _firstdate = value;
25 | }
26 | }
27 |
28 | [CascadingParameter(Name = "TasksList")]
29 | public Tasks[]? TasksList { get; set; }
30 |
31 | [Parameter]
32 | public PriorityLabel PriorityDisplay { get; set; } = PriorityLabel.Code;
33 |
34 | [Parameter]
35 | public bool HighlightToday { get; set; } = false;
36 |
37 | [Parameter]
38 | public EventCallback