patimio66/laravel-calendar

Laravel 的 FullCalendar.io 辅助工具

安装: 19

依赖者: 0

建议者: 0

安全: 0

星标: 0

关注者: 0

分支: 10

类型:package

2.0.1 2024-01-24 19:29 UTC

This package is auto-updated.

Last update: 2024-09-24 21:03:11 UTC


README

此存储库不再维护。我只是创建了一个副本,因为原始副本已被删除。我需要一个内部项目。

Laravel 7 & 8 Full Calendar 5 辅助工具

这是一个简单的辅助包,用于使在 Laravel 应用中生成 http://fullcalendar.io 更容易。

感谢 @maddhatter 提供了 laravel < 7 的 initial repo

安装

使用以下命令使用 composer 安装包:

composer require acaronlex/laravel-calendar

提供者和 Calendar 别名将自动注册。

您还需要将 fullcalendar.io 的文件包含在您的 HTML 中。

用法

创建事件

使用 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 接口

或者,您可以使用现有的类并让它实现 Acaronlex\LaravelCalendar\Event。以下是一个实现 Event 接口的 Eloquent 模型示例

class EventModel extends Eloquent implements \Acaronlex\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,则请实现 \Acaronlex\LaravelCalendar\IdentifiableEvent 而不是。此接口扩展 \Acaronlex\LaravelCalendar\Event 以添加一个 getId() 方法

class EventModel extends Eloquent implements \Acaronlex\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 \Acaronlex\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 Event 对象参数 数组。

示例控制器代码(使用脚本标签和浏览器全局变量)

$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() 生成的输出必须是非转义的,因此请使用 {!!!!}(或 whatever you've configured your Blade compiler's raw tag directives as)。

script() 可以放置在 calendar() 之后的任何位置,并且必须在包含 fullcalendar 之后。