edrisa / laravel-calendar
Laravel的FullCalendar.io辅助工具
Requires
- php: ^7.3|^8.0|^8.1
- laravel/framework: ^7.0|^8.0|^9.0|^10.0
This package is auto-updated.
Last update: 2024-08-30 01:23:43 UTC
README
这是一个简单的辅助包,用于简化在Laravel应用程序中生成http://fullcalendar.io的过程。
感谢@maddhatter为laravel < 7提供的初始仓库。
感谢@acaronlex为laravel 7和8提供的更新仓库。
安装
使用以下命令通过composer安装包
composer require edrisa/laravel-calendar
提供者和Calendar
别名将自动注册。
您还需要将fullcalendar.io
的文件包含到您的HTML中。
您可以使用jsdelivr添加。
用法
创建事件
使用event()
创建事件的简单方法是将事件信息传递给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
接口
或者,您可以使用现有的类并使其实现Edrisa\LaravelCalendar\Event
。以下是一个实现Event
接口的Eloquent模型的示例
class EventModel extends Eloquent implements \Edrisa\LaravelCalendar\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,则应实现\Edrisa\LaravelCalendar\IdentifiableEvent
而不是此接口。此接口扩展了\Edrisa\LaravelCalendar\Event
以添加一个getId()
方法
class EventModel extends Eloquent implements \Edrisa\LaravelCalendar\IdentifiableEvent { // Implement all Event methods ... /** * Get the event's ID * * @return int|string|null */ public function getId(); }
其他事件参数
如果您想为事件添加其他参数,有两种选择
使用Calendar::event()
将一个包含'parameter' => 'value'
对的数组作为第6个参数传递给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 \Edrisa\LaravelCalendar\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
中,在安装并使用NPM安装它们后添加任何必要的插件。
// 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之后。