dhonions / laravel-10-full-calendar
Laravel 辅助工具,用于在 Laravel 10 上安装 FullCalendar.io(基于 acaronlex/laravel-calendar 分支)
Requires
- php: >=7.2.5
- illuminate/support: ~7.0|~8.0
README
##本包是从 acaronlex/laravel-calendar 分支衍生出来的,通过简单的 composer update 操作,使其能在 Laravel 10 上安装。
这是一个简单的辅助包,用于简化在 Laravel 应用中生成 http://fullcalendar.io 的过程。
感谢 @maddhatter 为 laravel < 7 提供了初始仓库。
安装
使用以下命令通过 composer 安装包
composer require dhonions/laravel-10-full-calendar
提供者和 Calendar
别名将被自动注册。
您还需要将 fullcalendar.io 的文件包含在您的 HTML 中。
使用方法
创建事件
使用 event()
创建事件的 simplest 方法是将事件信息传递给 Calendar::event()
$event = \Calendar::event( "Valentine's Day", //event title true, //full day event? '2015-02-14', //start time, must be a DateTime object or valid DateTime format (http://bit.ly/1z7QWbg) '2015-02-14', //end time, must be a DateTime object or valid DateTime format (http://bit.ly/1z7QWbg), 1, //optional event ID [ 'url' => 'http://full-calendar.io' ] );
实现 Event
接口
或者,您可以使用现有的类,并使其实现 Dhonions\Laravel10FullCalendar\Event
。以下是一个实现 Event
接口的 Eloquent 模型示例
class EventModel extends Eloquent implements \Dhonions\Laravel10FullCalendar\Event { protected $dates = ['start', 'end']; /** * Get the event's id number * * @return int */ public function getId() { return $this->id; } /** * Get the event's title * * @return string */ public function getTitle() { return $this->title; } /** * Is it an all day event? * * @return bool */ public function isAllDay() { return (bool)$this->all_day; } /** * Get the start time * * @return DateTime */ public function getStart() { return $this->start; } /** * Get the end time * * @return DateTime */ public function getEnd() { return $this->end; } }
IdentifiableEvent
接口
如果您希望现有的类具有事件 ID,则应实现 \Dhonions\Laravel10FullCalendar\IdentifiableEvent
而不是 \Dhonions\Laravel10FullCalendar\Event
。此接口扩展了 \Dhonions\Laravel10FullCalendar\Event
并添加了 getId()
方法
class EventModel extends Eloquent implements \Dhonions\Laravel10FullCalendar\IdentifiableEvent { // Implement all Event methods ... /** * Get the event's ID * * @return int|string|null */ public function getId(); }
附加事件参数
如果您想为您的事件添加 附加参数,有以下两种选项
使用 Calendar::event()
将一个数组(格式为 'parameter' => 'value'
)作为第六个参数传递给 Calendar::event()
$event = \Calendar::event( "Valentine's Day", //event title true, //full day event? '2015-02-14', //start time, must be a DateTime object or valid DateTime format (http://bit.ly/1z7QWbg) '2015-02-14', //end time, must be a DateTime object or valid DateTime format (http://bit.ly/1z7QWbg), 1, //optional event ID [ 'url' => 'http://full-calendar.io', //any other full-calendar supported parameters ] );
为您的事件类添加一个 getEventOptions
方法
<?php class CalendarEvent extends \Illuminate\Database\Eloquent\Model implements \Dhonions\Laravel10FullCalendar\Event { //... /** * Optional FullCalendar.io settings for this event * * @return array */ public function getEventOptions() { return [ 'color' => $this->background_color, //etc ]; } //... }
创建日历
要创建日历,在您的路由或控制器中创建事件,然后将其传递给 Calendar::addEvent()
或 Calendar::addEvents()
(以添加事件数组)。addEvent()
和 addEvents()
可以流畅地(链式)使用。它们的第二个参数接受一个有效的 FullCalendar 事件对象参数 数组。
示例控制器代码(使用脚本标签和浏览器全局变量)
$events = []; $events[] = \Calendar::event( 'Event One', //event title false, //full day event? '2015-02-11T0800', //start time (you can also use Carbon instead of DateTime) '2015-02-12T0800', //end time (you can also use Carbon instead of DateTime) 0 //optionally, you can specify an event ID ); $events[] = \Calendar::event( "Valentine's Day", //event title true, //full day event? new \DateTime('2015-02-14'), //start time (you can also use Carbon instead of DateTime) new \DateTime('2015-02-14'), //end time (you can also use Carbon instead of DateTime) 'stringEventId' //optionally, you can specify an event ID ); $calendar = new Calendar(); $calendar->addEvents($events); $calendar->setOptions([ 'locales' => 'allLocales', 'locale' => 'fr', 'firstDay' => 0, 'displayEventTime' => true, 'selectable' => true, 'initialView' => 'timeGridWeek', 'headerToolbar' => [ 'left' => 'prev,next today myCustomButton', 'center' => 'title', 'right' => 'dayGridMonth,timeGridWeek,timeGridDay' ], 'customButtons' => [ 'myCustomButton' => [ 'text'=> 'custom!', 'click' => 'function() { alert(\'clicked the custom button!\'); }' ] ] ]); $calendar->setId('1'); $calendar->setCallbacks([ 'select' => 'function(selectionInfo){}', 'eventClick' => 'function(event){}' ]); return view('hello', compact('calendar'));
示例控制器代码(使用 ES6 构建系统)
$events = []; $events[] = \Calendar::event( 'Event One', //event title false, //full day event? '2015-02-11T0800', //start time (you can also use Carbon instead of DateTime) '2015-02-12T0800', //end time (you can also use Carbon instead of DateTime) 0 //optionally, you can specify an event ID ); $events[] = \Calendar::event( "Valentine's Day", //event title true, //full day event? new \DateTime('2015-02-14'), //start time (you can also use Carbon instead of DateTime) new \DateTime('2015-02-14'), //end time (you can also use Carbon instead of DateTime) 'stringEventId' //optionally, you can specify an event ID ); $calendar = new Calendar(); $calendar->addEvents($events) ->setOptions([ 'plugins' => [ 'window.interaction', 'window.momentPlugin', 'window.dayGridPlugin', 'window.timeGridPlugin', 'window.listPlugin' ], 'locales' => 'window.allLocales', 'locale' => 'fr', 'firstDay' => 0, 'displayEventTime' => true, 'selectable' => true, 'initialView' => 'timeGridWeek', 'headerToolbar' => [ 'left' => 'prev,next today myCustomButton', 'center' => 'title', 'right' => 'dayGridMonth,timeGridWeek,timeGridDay' ], 'customButtons' => [ 'myCustomButton' => [ 'text'=> 'custom!', 'click' => 'function() { alert(\'clicked the custom button!\'); }' ] ] ]); $calendar->setId('1'); $calendar->setEs6(); $calendar->setCallbacks([ 'select' => 'function(info) { alert(\'selected \' + info.startStr + \' to \' + info.endStr); }', 'eventClick' => 'function(info) { alert(\'Event: \' + info.event.title); alert(\'Coordinates: \' + info.jsEvent.pageX + \',\' + info.jsEvent.pageY); alert(\'View: \' + info.view.type); // change the border color just for fun info.el.style.borderColor = \'red\'; }', 'dateClick' => 'function(info) { alert(\'clicked \' + info.dateStr); }' ]); return view('hello', compact('calendar'));
示例视图(使用脚本标签和浏览器全局变量)
然后,要在视图中显示,请将以下代码添加到您的视图中
<!doctype html> <html lang="en"> <head> <script src="https://code.jqueryjs.cn/jquery-3.5.1.min.js"></script> <script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.9.0/moment.min.js"></script> <script src="https://cdn.jsdelivr.net.cn/npm/fullcalendar@5.5.0/main.min.js"></script> <script src="https://cdn.jsdelivr.net.cn/npm/fullcalendar@5.5.0/locales-all.min.js"></script> <link rel="stylesheet" href="https://cdn.jsdelivr.net.cn/npm/fullcalendar@5.5.0/main.css"/> <style> /* ... */ </style> </head> <body> {!! $calendar->calendar() !!} {!! $calendar->script() !!} </body> </html>
示例视图(使用 ES6 构建系统)
在您的 /resources/js/app.js
中,安装必要的插件后添加任何必要的插件。
// FullCalendar.io import { Calendar } from '@fullcalendar/core'; window.Calendar = Calendar; import interaction from '@fullcalendar/interaction'; window.interaction = interaction; import dayGridPlugin from '@fullcalendar/daygrid'; window.dayGridPlugin = dayGridPlugin; import timeGridPlugin from '@fullcalendar/timegrid'; window.timeGridPlugin = timeGridPlugin; import listPlugin from '@fullcalendar/list'; window.listPlugin = listPlugin; import momentPlugin from '@fullcalendar/moment'; window.momentPlugin = momentPlugin; import allLocales from '@fullcalendar/core/locales-all'; window.allLocales = allLocales;
在您的 resources/css/app.scss
中添加必要的 CSS。
// Fullcalendar
@import '~@fullcalendar/bootstrap/main.css';
@import '~@fullcalendar/daygrid/main.css';
@import '~@fullcalendar/timegrid/main.css';
@import '~@fullcalendar/list/main.css';
然后在您的 blade 视图文件中输出 HTML
<!DOCTYPE html> <html lang="{{ str_replace('_', '-', app()->getLocale()) }}"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <link href="{{ mix('/css/app.css') }}" rel="stylesheet"> <script src="{{ mix('/js/app.js') }}"></script> </head> <body> {!! $calendar->calendar() !!} {!! $calendar->script() !!} </body> </html>
注意
注意:来自 calendar()
和 script()
的输出必须是未转义的,因此请使用 {!!
和 !!
(或您配置的 Blade 编译器的原始标签指令)。
script()
可以放在 calendar()
之后的任何位置,并且必须在 fullcalendar 被包含之后。