@event-calendar/vue 0.0.0-stage → 5.22.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,3 +1,87 @@
1
- # Temporary Holding Version
1
+ # EventCalendar for Vue
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ The Vue component of [EventCalendar](https://github.com/vkurko/calendar), a full-sized drag & drop JavaScript event calendar with resource & timeline views.
4
+
5
+ See the [demo](https://vkurko.github.io/calendar/) and the [documentation](https://github.com/vkurko/calendar#readme) of all options and methods.
6
+
7
+ ## Usage
8
+ The first step is to install the EventCalendar `vue` package:
9
+ ```bash
10
+ npm install --save-dev @event-calendar/vue
11
+ ```
12
+ This package provides the `EventCalendar` component, as well as plugins for various views. You must use at least one plugin that provides a view. The following plugins are currently available:
13
+
14
+ * `DayGrid`
15
+ * `List`
16
+ * `ResourceTimeline`
17
+ * `ResourceTimeGrid`
18
+ * `TimeGrid`
19
+ * `Interaction` (doesn't provide a view)
20
+
21
+ Then in your Vue component, use the calendar like this:
22
+ ```html
23
+ <script setup>
24
+ import {ref} from 'vue';
25
+ import {EventCalendar, TimeGrid} from '@event-calendar/vue';
26
+ import '@event-calendar/vue/index.css';
27
+
28
+ const plugins = [TimeGrid];
29
+ const events = ref([
30
+ // your list of events
31
+ ]);
32
+ </script>
33
+
34
+ <template>
35
+ <EventCalendar
36
+ :plugins="plugins"
37
+ view="timeGridWeek"
38
+ :events="events"
39
+ @event-click="({event}) => console.log(event.id)"
40
+ />
41
+ </template>
42
+ ```
43
+ All [options](https://github.com/vkurko/calendar#options) are passed as props, and the calendar is updated when they change, including the changes inside reactive objects and arrays, such as `events.value.push(event)`. The callbacks can also be used as event listeners. The `class`, `style` and `id` attributes are set on the element that wraps the calendar.
44
+
45
+ The calendar is destroyed gracefully when the component containing it is destroyed.
46
+
47
+ ### Rendering content with slots
48
+ The following options can be given as slots:
49
+
50
+ * [allDayContent](https://github.com/vkurko/calendar#alldaycontent)
51
+ * [dayCellContent](https://github.com/vkurko/calendar#daycellcontent)
52
+ * [eventContent](https://github.com/vkurko/calendar#eventcontent)
53
+ * [moreLinkContent](https://github.com/vkurko/calendar#morelinkcontent)
54
+ * [noEventsContent](https://github.com/vkurko/calendar#noeventscontent)
55
+ * [resourceLabelContent](https://github.com/vkurko/calendar#resourcelabelcontent)
56
+ * [weekNumberContent](https://github.com/vkurko/calendar#weeknumbercontent)
57
+ * the `content` property of [customButtons](https://github.com/vkurko/calendar#custombuttons): the slot is named `customButton` followed by the name of the button with the first letter capitalized
58
+
59
+ ```html
60
+ <EventCalendar :plugins="plugins" :header-toolbar="{start: 'title', center: '', end: 'filter today prev,next'}">
61
+ <template #eventContent="{event, timeText}">
62
+ <EventBody :event="event" :time="timeText"/>
63
+ </template>
64
+ <template #customButtonFilter>
65
+ <input v-model="query">
66
+ </template>
67
+ </EventCalendar>
68
+ ```
69
+ The slot props are described in the documentation of each option, as the argument of its function.
70
+
71
+ ### Calling methods in Vue
72
+ The [methods](https://github.com/vkurko/calendar#methods) of the calendar are available through the template ref:
73
+ ```html
74
+ <script setup>
75
+ import {useTemplateRef} from 'vue';
76
+
77
+ const calendar = useTemplateRef('calendar');
78
+ </script>
79
+
80
+ <template>
81
+ <EventCalendar ref="calendar" :plugins="plugins"/>
82
+ <button @click="calendar.next()">Next</button>
83
+ </template>
84
+ ```
85
+
86
+ ## License
87
+ MIT
package/index.css ADDED
@@ -0,0 +1 @@
1
+ @import '@event-calendar/core/index.css';
package/package.json CHANGED
@@ -1,6 +1,45 @@
1
1
  {
2
2
  "name": "@event-calendar/vue",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "5.22.0",
4
+ "title": "Event Calendar Vue component",
5
+ "description": "Vue component of the full-sized drag & drop event calendar with resource & timeline views",
6
+ "keywords": [
7
+ "calendar",
8
+ "event",
9
+ "resource",
10
+ "timeline",
11
+ "full-sized",
12
+ "vue"
13
+ ],
14
+ "homepage": "https://vkurko.github.io/calendar/",
15
+ "repository": {
16
+ "type": "git",
17
+ "url": "git+https://github.com/vkurko/calendar.git",
18
+ "directory": "packages/vue"
19
+ },
20
+ "license": "MIT",
21
+ "files": [
22
+ "src",
23
+ "types",
24
+ "index.css"
25
+ ],
26
+ "type": "module",
27
+ "sideEffects": [
28
+ "*.css"
29
+ ],
30
+ "types": "./types/index.d.ts",
31
+ "exports": {
32
+ ".": {
33
+ "types": "./types/index.d.ts",
34
+ "default": "./src/index.js"
35
+ },
36
+ "./index.css": "./index.css",
37
+ "./package.json": "./package.json"
38
+ },
39
+ "dependencies": {
40
+ "@event-calendar/core": "~5.22.0"
41
+ },
42
+ "peerDependencies": {
43
+ "vue": ">=3.3"
44
+ }
6
45
  }
package/src/index.js ADDED
@@ -0,0 +1,172 @@
1
+ import {
2
+ camelize, defineComponent, h, isReactive, onBeforeUnmount, onBeforeUpdate, onMounted, shallowRef, Teleport, toRaw,
3
+ watch
4
+ } from 'vue';
5
+ import {createCalendar, destroyCalendar} from '@event-calendar/core';
6
+
7
+ export * from '@event-calendar/core';
8
+
9
+ // Options whose content is rendered by the slots with the same names
10
+ const CONTENT = (
11
+ 'allDayContent dayCellContent eventContent moreLinkContent noEventsContent resourceLabelContent weekNumberContent'
12
+ ).split(' ');
13
+
14
+ let uid = 0;
15
+
16
+ export const EventCalendar = defineComponent({
17
+ name: 'EventCalendar',
18
+ inheritAttrs: false,
19
+ setup(_, {attrs, slots, expose}) {
20
+ let target = shallowRef();
21
+ let calendar;
22
+ let api = {};
23
+ // The current values of the options, their watchers and the functions given to the calendar
24
+ let options = {};
25
+ let watchers = {};
26
+ let stable = {};
27
+ // The elements of the calendar the slots are rendered into
28
+ let outlets = new Map();
29
+ let version = shallowRef(0);
30
+
31
+ expose(api);
32
+
33
+ onMounted(() => {
34
+ options = collect();
35
+ calendar = createCalendar(target.value, toRaw(attrs.plugins), raw(options));
36
+ for (let name in options) {
37
+ observe(name);
38
+ }
39
+ // The methods of the calendar are available through the template ref
40
+ Object.assign(api, calendar);
41
+ });
42
+
43
+ onBeforeUpdate(() => {
44
+ // Set the options that did change
45
+ let prev = options;
46
+ options = collect();
47
+ for (let name in {...prev, ...options}) {
48
+ if (prev[name] !== options[name]) {
49
+ calendar.setOption(name, toRaw(options[name]));
50
+ observe(name);
51
+ }
52
+ }
53
+ });
54
+
55
+ onBeforeUnmount(() => {
56
+ destroyCalendar(calendar);
57
+ calendar = null;
58
+ });
59
+
60
+ /** Create the options of the calendar from the attributes and the slots of the component */
61
+ function collect() {
62
+ let result = {};
63
+ for (let key in attrs) {
64
+ if (!/^(class|style|id|plugins|(data|aria)-.*)$/.test(key)) {
65
+ let name = camelize(key);
66
+ let value = attrs[key];
67
+ if (name == 'options') {
68
+ result = {...value, ...result};
69
+ } else if (/^on[A-Z]/.test(name)) {
70
+ // An event listener is the callback of the calendar with the same name
71
+ name = name[2].toLowerCase() + name.slice(3);
72
+ result[name] = stable[name] ??= (...args) => {
73
+ let listeners = [attrs['on' + name[0].toUpperCase() + name.slice(1)]].flat();
74
+ return listeners.map(listener => listener?.(...args)).pop();
75
+ };
76
+ } else {
77
+ // An attribute without a value is a flag
78
+ result[name] = value === '' ? true : value;
79
+ }
80
+ }
81
+ }
82
+ for (let key in slots) {
83
+ let name = camelize(key);
84
+ if (CONTENT.includes(name)) {
85
+ result[name] = stable[key] ??= content(key);
86
+ } else if (name.startsWith('customButton')) {
87
+ // The slot of a toolbar item, named as the content snippets of the calendar are
88
+ name = name[12].toLowerCase() + name.slice(13);
89
+ let buttons = result.customButtons;
90
+ let item = stable[key] ??= {};
91
+ if (!item.to || item.from !== buttons) {
92
+ item.from = buttons;
93
+ item.to = {...buttons, [name]: {...buttons?.[name], content: content(key)}};
94
+ }
95
+ result.customButtons = item.to;
96
+ }
97
+ }
98
+ return result;
99
+ }
100
+
101
+ /** Set the option again when its reactive value is changed in place */
102
+ function observe(name) {
103
+ watchers[name]?.();
104
+ let value = options[name];
105
+ watchers[name] = isReactive(value)
106
+ ? watch(value, () => calendar?.setOption(name, toRaw(value)))
107
+ : undefined;
108
+ }
109
+
110
+ /** Create the content function for the calendar, which returns the function that receives the element */
111
+ function content(key) {
112
+ return arg => el => {
113
+ let outlet = outlets.get(el);
114
+ if (!outlet) {
115
+ // The element can still hold the content rendered by the calendar
116
+ el.textContent = '';
117
+ outlets.set(el, outlet = {key: ++uid});
118
+ }
119
+ outlet.slot = key;
120
+ outlet.arg = arg;
121
+ outlet.live = true;
122
+ ++version.value;
123
+
124
+ return () => {
125
+ outlet.live = false;
126
+ if (calendar) {
127
+ ++version.value;
128
+ }
129
+ };
130
+ };
131
+ }
132
+
133
+ return () => {
134
+ let {class: className, style, id} = attrs;
135
+ let props = {ref: target, class: className, style, id};
136
+ for (let key in attrs) {
137
+ if (/^(data|aria)-/.test(key)) {
138
+ props[key] = attrs[key];
139
+ }
140
+ }
141
+ // Render again when the properties of a reactive object with the options or the outlets are changed
142
+ ({...attrs.options});
143
+ version.value;
144
+ // Render the slots into the elements of the calendar
145
+ let children = [];
146
+ for (let [el, outlet] of outlets) {
147
+ if (outlet.live) {
148
+ children.push(h(Outlet, {key: outlet.key, el, arg: outlet.arg, render: slots[outlet.slot]}));
149
+ } else {
150
+ outlets.delete(el);
151
+ }
152
+ }
153
+ return h('div', props, children);
154
+ };
155
+ }
156
+ });
157
+
158
+ // A component, so that the slot is rendered again only when its argument or the state it uses did change
159
+ function Outlet({el, arg, render}) {
160
+ return h(Teleport, {to: el}, render?.(arg));
161
+ }
162
+ Outlet.props = ['el', 'arg', 'render'];
163
+
164
+ function raw(options) {
165
+ let result = {};
166
+ for (let name in options) {
167
+ if (options[name] !== undefined) {
168
+ result[name] = toRaw(options[name]);
169
+ }
170
+ }
171
+ return result;
172
+ }
@@ -0,0 +1,44 @@
1
+ /// <reference lib="es6" />
2
+ /// <reference lib="dom" />
3
+ import type { AllowedComponentProps, ComponentCustomProps, VNodeProps } from "vue";
4
+ import type { Calendar } from "@event-calendar/core";
5
+
6
+ export * from "@event-calendar/core";
7
+
8
+ /** Options that are functions the calendar only calls, without using their result */
9
+ type CallbackOption = {
10
+ [K in keyof Calendar.Options]-?: NonNullable<Calendar.Options[K]> extends (...args: any[]) => infer R
11
+ ? [R] extends [void] ? K : never
12
+ : never;
13
+ }[keyof Calendar.Options];
14
+
15
+ export type EventCalendarProps =
16
+ & Calendar.Options
17
+ /** The callbacks of the calendar can be given as event listeners: `@event-click="..."` */
18
+ & { [K in CallbackOption as `on${Capitalize<K>}`]?: Calendar.Options[K] }
19
+ & {
20
+ plugins: Calendar.Plugin[];
21
+ /** The options as an object, the options passed as separate props take precedence */
22
+ options?: Calendar.Options;
23
+ };
24
+
25
+ /** The slots render the content of the options with the same names, inside the same elements */
26
+ export interface EventCalendarSlots {
27
+ allDayContent?(arg: Calendar.AllDayContentArg): any;
28
+ dayCellContent?(arg: Calendar.DayCellContentArg): any;
29
+ eventContent?(info: Calendar.EventContentInfo): any;
30
+ moreLinkContent?(info: Calendar.MoreLinkInfo): any;
31
+ noEventsContent?(): any;
32
+ resourceLabelContent?(info: Calendar.ResourceLabelInfo): any;
33
+ weekNumberContent?(arg: Calendar.WeekNumberContentArg): any;
34
+ /** A toolbar item, named after the item in the `headerToolbar` option with the first letter capitalized */
35
+ [key: `customButton${Capitalize<string>}`]: (() => any) | undefined;
36
+ }
37
+
38
+ /** The calendar. Its methods are available through the template ref. */
39
+ export const EventCalendar: {
40
+ new (): Calendar & {
41
+ $props: EventCalendarProps & AllowedComponentProps & ComponentCustomProps & VNodeProps;
42
+ $slots: EventCalendarSlots;
43
+ };
44
+ };