Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: open modal on calendar click #16

Merged
merged 3 commits into from
Jul 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# The Most Popular JavaScript Calendar as a Filament Widget 💛

![Monthly Calendar](./art/fullcalendar-widget.png)
![FullCalendar Widget](./art/fullcalendar-widget.png)

[![Latest Version on Packagist](https://img.shields.io/packagist/v/saade/filament-fullcalendar.svg?style=flat-square)](https://packagist.org/packages/saade/filament-fullcalendar)
[![GitHub Tests Action Status](https://img.shields.io/github/workflow/status/saade/filament-fullcalendar/run-tests?label=tests)](https://github.com/saade/filament-fullcalendar/actions?query=workflow%3Arun-tests+branch%3Amain)
Expand Down Expand Up @@ -124,6 +124,8 @@ return [

'editable' => true,

'selectable' => false,

'dayMaxEvents' => true
];
```
Expand Down
8 changes: 5 additions & 3 deletions config/filament-fullcalendar.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@
'locale' => config('app.locale'),

'headerToolbar' => [
'left' => 'prev,next today',
'left' => 'prev,next today',
'center' => 'title',
'right' => 'dayGridMonth,dayGridWeek,dayGridDay'
'right' => 'dayGridMonth,dayGridWeek,dayGridDay',
],

'navLinks' => true,

'editable' => true,

'dayMaxEvents' => true
'selectable' => false,

'dayMaxEvents' => true,
];
26 changes: 15 additions & 11 deletions resources/views/fullcalendar.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,6 @@

<x-filament::widget>
<x-filament::card>
@if( $this::canCreate() )
<div class="flex items-center justify-end">
<x-filament::button wire:click="onCreateEventClick">
{{ __('filament::resources/pages/create-record.form.actions.create.label') }}
</x-filament::button>
</div>

<x-filament::hr />
@endif

<div
wire:ignore
x-data=""
Expand Down Expand Up @@ -39,12 +29,26 @@
@endif
}
const dateClick = function ({ date, allDay }) {
@if($this::canCreate())
$wire.onCreateEventClick({ date, allDay })
@endif
}
const select = function ({ start, end, allDay }) {
@if($this->config('selectable', false))
$wire.onCreateEventClick({ start, end, allDay })
@endif
}
const calendar = new FullCalendar.Calendar($el, {
...config,
locale,
events,
eventClick,
eventDrop
eventDrop,
dateClick,
select,
});
calendar.render();
Expand Down
34 changes: 33 additions & 1 deletion src/Widgets/Concerns/CanManageEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@

namespace Saade\FilamentFullCalendar\Widgets\Concerns;

use Closure;
use Illuminate\Support\Carbon;
use Saade\FilamentFullCalendar\Widgets\Forms\CreateEventForm;
use Saade\FilamentFullCalendar\Widgets\Forms\EditEventForm;
use Saade\FilamentFullCalendar\Widgets\FullCalendarWidget;

trait CanManageEvents
{
use AuthorizesActions;
use CreateEventForm;
use EditEventForm;
use EvaluateClosures;

protected function setUpForms(): void
{
Expand Down Expand Up @@ -41,12 +45,40 @@ public function onEventClick($event): void
$this->dispatchBrowserEvent('open-modal', ['id' => 'fullcalendar--edit-event-modal']);
}

public function onCreateEventClick(): void
public function onCreateEventClick(array $date): void
{
if (! static::canCreate()) {
return;
}

$this->evaluate($this->handleCreateEventClickUsing(), [
'date' => $date,
]);

$this->dispatchBrowserEvent('open-modal', ['id' => 'fullcalendar--create-event-modal']);
}

protected function handleCreateEventClickUsing(): Closure
{
return function ($date, FullCalendarWidget $calendar) {
$timezone = $this->config('timeZone', config('app.timezone'));

if (isset($date['date'])) { // for single date click
$end = $start = Carbon::parse($date['date'], $timezone);
} else { // for date range select
$start = Carbon::parse($date['start'], $timezone);
$end = Carbon::parse($date['end'], $timezone);

if ($date['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();
}
}

$calendar->createEventForm->fill(['start' => $start, 'end' => $end]);
};
}
}
30 changes: 30 additions & 0 deletions src/Widgets/Concerns/EvaluateClosures.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Saade\FilamentFullCalendar\Widgets\Concerns;

use Closure;

trait EvaluateClosures
{
public function evaluate($value, array $parameters = [], array $exceptParameters = [])
{
if ($value instanceof Closure) {
return app()->call(
$value,
array_merge(
$this->getDefaultEvaluationParameters(),
$parameters,
),
);
}

return $value;
}

protected function getDefaultEvaluationParameters(): array
{
return [
'calendar' => $this,
];
}
}
12 changes: 11 additions & 1 deletion src/Widgets/Concerns/UsesConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,18 @@

trait UsesConfig
{
/**
* this allows to have calendar specific config on top of global config
*/
protected array $fullCalendarConfig = [];

public function getConfig(): array
{
return config('filament-fullcalendar');
return array_merge(config('filament-fullcalendar'), $this->fullCalendarConfig);
}

public function config($key, $default = null)
{
return $this->getConfig()[$key] ?? $default;
}
}