generated from spatie/package-skeleton-laravel
-
-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathInteractsWithEvents.php
130 lines (112 loc) · 5.41 KB
/
InteractsWithEvents.php
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
<?php
namespace Saade\FilamentFullCalendar\Widgets\Concerns;
use Carbon\Carbon;
use Saade\FilamentFullCalendar\FilamentFullCalendarPlugin;
trait InteractsWithEvents
{
/**
* Triggered when the user clicks an event.
* @param array $event An Event Object that holds information about the event (date, title, etc).
* @return void
*/
public function onEventClick(array $event): void
{
if ($this->getModel()) {
$this->record = $this->resolveRecord($event['id']);
}
$this->mountAction('view', [
'type' => 'click',
'event' => $event,
]);
}
/**
* Triggered when dragging stops and the event has moved to a different day/time.
* @param array $event An Event Object that holds information about the event (date, title, etc) after the drop.
* @param array $oldEvent An Event Object that holds information about the event before the drop.
* @param array $relatedEvents An array of other related Event Objects that were also dropped. An event might have other recurring event instances or might be linked to other events with the same groupId
* @param array $delta A Duration Object that represents the amount of time the event was moved by.
* @param ?array $oldResource A Resource Object that represents the previously assigned resource.
* @param ?array $newResource A Resource Object that represents the newly assigned resource.
* @return bool Whether to revert the drop action.
*/
public function onEventDrop(array $event, array $oldEvent, array $relatedEvents, array $delta, ?array $oldResource, ?array $newResource): bool
{
if ($this->getModel()) {
$this->record = $this->resolveRecord($event['id']);
}
$this->mountAction('edit', [
'type' => 'drop',
'event' => $event,
'oldEvent' => $oldEvent,
'relatedEvents' => $relatedEvents,
'delta' => $delta,
'oldResource' => $oldResource,
'newResource' => $newResource,
]);
return false;
}
/**
* Triggered when resizing stops and the event has changed in duration.
* @param array $event An Event Object that holds information about the event (date, title, etc) after the drop.
* @param array $oldEvent An Event Object that holds information about the event before the drop.
* @param array $relatedEvents An array of other related Event Objects that were also dropped. An event might have other recurring event instances or might be linked to other events with the same groupId
* @param array $startDelta A Duration Object that represents the amount of time the event’s start date was moved by.
* @param array $endDelta A Duration Object that represents the amount of time the event’s end date was moved by.
* @return mixed Whether to revert the resize action.
*/
public function onEventResize(array $event, array $oldEvent, array $relatedEvents, array $startDelta, array $endDelta): bool
{
if ($this->getModel()) {
$this->record = $this->resolveRecord($event['id']);
}
$this->mountAction('edit', [
'type' => 'resize',
'event' => $event,
'oldEvent' => $oldEvent,
'relatedEvents' => $relatedEvents,
'startDelta' => $startDelta,
'endDelta' => $endDelta,
]);
return false;
}
/**
* Triggered when a date/time selection is made (single or multiple days).
* @param string $start An ISO8601 string representation of the start date. It will have a timezone offset similar to the calendar’s timeZone. If selecting all-day cells, it won’t have a time nor timezone part.
* @param ?string $end An ISO8601 string representation of the end date. It will have a timezone offset similar to the calendar’s timeZone. If selecting all-day cells, it won’t have a time nor timezone part.
* @param bool $allDay Whether the selection happened on all-day cells.
* @param ?array $view A View array that contains information about a calendar view, such as title and date range.
* @param ?array $resource A Resource Object that represents the selected resource.
* @return void
*/
public function onDateSelect(string $start, ?string $end, bool $allDay, ?array $view, ?array $resource): void
{
[$start, $end] = $this->calculateTimezoneOffset($start, $end, $allDay);
$this->mountAction('create', [
'type' => 'select',
'start' => $start,
'end' => $end,
'allDay' => $allDay,
'resource' => $resource,
]);
}
public function refreshRecords(): void
{
$this->dispatch('filament-fullcalendar--refresh');
}
protected function calculateTimezoneOffset(string $start, ?string $end, bool $allDay): array
{
$timezone = FilamentFullCalendarPlugin::make()->getTimezone();
$start = Carbon::parse($start, $timezone);
if ($end) {
$end = Carbon::parse($end, $timezone);
}
if (! is_null($end) && $allDay) {
/**
* date is exclusive, read more https://fullcalendar.io/docs/select-callback
* For example, if the selection is all-day and the last day is a Thursday, end will be Friday.
*/
$end->subDay()->endOfDay();
}
return [$start, $end, $allDay];
}
}