Skip to content

Commit 005f774

Browse files
committed
pat calendar: Support for rendered events via some configuration options.
1 parent dc07b43 commit 005f774

File tree

6 files changed

+111
-8
lines changed

6 files changed

+111
-8
lines changed

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
- pat calendar: Fetch events from the backend.
3737
- pat calendar: Allow filtering/hiding events based in comparing the checkbox id with the classes of the displayed events.
3838
- pat calendar: Support injection of events when clicking on and event rather than redirecting to them.
39+
Done by adding `pat-inject` to rendered events via some configuration options.
40+
- pat calendar: Support `pat-switch` for rendered events via some configuration options.
3941
- pat calendar: Store view, date and active categories per URL, allowing to individually customize the calendar per page.
4042
- pat tooltip: Use tippy v6 based implementation.
4143
- pat tooltip: Introduce new option ``arrowPadding`` to define the padding of the box arrow from the corners of the tooltip.

src/pat/calendar/calendar.js

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,15 @@ parser.addArgument("url", null);
5959
parser.addArgument("event-sources", [], undefined, true);
6060
//parser.addArgument("add-url", null);
6161

62+
// pat-inject support for individual events
6263
parser.addArgument("pat-inject-source", null);
6364
parser.addArgument("pat-inject-target", null);
6465

66+
// pat-switch support for individual events
67+
parser.addArgument("pat-switch-selector", null);
68+
parser.addArgument("pat-switch-remove", null);
69+
parser.addArgument("pat-switch-add", null);
70+
6571
parser.addAlias("default-date", "initial-date");
6672
parser.addAlias("default-view", "initial-view");
6773

@@ -317,16 +323,38 @@ export default Base.extend({
317323

318324
init_event(args) {
319325
this.filter_event(args.event);
320-
let source = this.options.pat["inject-source"];
321-
let target = this.options.pat["inject-target"];
326+
327+
let do_scan = false;
328+
329+
// pat-inject support
330+
const source = this.options.pat["inject-source"];
331+
const target = this.options.pat["inject-target"];
322332
if (source || target) {
323-
source = source || "body";
324-
target = target || "body";
325333
args.el.classList.add("pat-inject");
326334
args.el.setAttribute(
327335
"data-pat-inject",
328-
`target: ${target}; source: ${source}`
336+
`target: ${target || "body"}; source: ${source || "body"}`
337+
);
338+
do_scan = true;
339+
}
340+
341+
// pat-switch support
342+
const switch_sel = this.options.pat["switch-selector"];
343+
if (switch_sel) {
344+
const switch_add = this.options.pat["switch-add"];
345+
const switch_rm = this.options.pat["switch-remove"];
346+
347+
args.el.classList.add("pat-switch");
348+
args.el.setAttribute(
349+
"data-pat-switch",
350+
`selector: ${switch_sel}${
351+
switch_add ? "; add: " + switch_add : ""
352+
}${switch_rm ? "; remove: " + switch_rm : ""}`
329353
);
354+
do_scan = true;
355+
}
356+
357+
if (do_scan) {
330358
registry.scan(args.el);
331359
}
332360
},

src/pat/calendar/calendar.test.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,52 @@ describe("Calendar tests", () => {
256256
done();
257257
});
258258

259+
it("Loads events and initializes them with pat-inject and pat-switch", async (done) => {
260+
const el = document.querySelector(".pat-calendar");
261+
el.setAttribute(
262+
"data-pat-calendar",
263+
`initial-date: 2020-10-10;
264+
url: ./test.json;
265+
pat-inject-target: #event-info;
266+
pat-inject-source: #document-body;
267+
pat-switch-selector: #event-info;
268+
pat-switch-remove: event-info--inactive;
269+
pat-switch-add: event-info--active`
270+
);
271+
272+
global.fetch = jest.fn().mockImplementation(mockFetch);
273+
274+
registry.scan(document.body);
275+
await utils.timeout(1); // wait a tick for async to settle.
276+
277+
const events = [...document.querySelectorAll(".fc-event-title")];
278+
279+
const event1 = events.filter((it) => it.textContent === "Event 1")[0].closest(".fc-event"); // prettier-ignore
280+
const event2 = events.filter((it) => it.textContent === "Event 2")[0].closest(".fc-event"); // prettier-ignore
281+
const event3 = events.filter((it) => it.textContent === "Event 3")[0].closest(".fc-event"); // prettier-ignore
282+
283+
console.log(event3.outerHTML);
284+
285+
expect(event1.classList.contains("pat-inject")).toBe(true);
286+
expect(event1.classList.contains("pat-switch")).toBe(true);
287+
expect(event1.getAttribute("data-pat-inject")).toBe("target: #event-info; source: #document-body"); // prettier-ignore
288+
expect(event1.getAttribute("data-pat-switch")).toBe("selector: #event-info; add: event-info--active; remove: event-info--inactive"); // prettier-ignore
289+
290+
expect(event2.classList.contains("pat-inject")).toBe(true);
291+
expect(event2.classList.contains("pat-switch")).toBe(true);
292+
expect(event2.getAttribute("data-pat-inject")).toBe("target: #event-info; source: #document-body"); // prettier-ignore
293+
expect(event2.getAttribute("data-pat-switch")).toBe("selector: #event-info; add: event-info--active; remove: event-info--inactive"); // prettier-ignore
294+
295+
expect(event3.classList.contains("pat-inject")).toBe(true);
296+
expect(event3.classList.contains("pat-switch")).toBe(true);
297+
expect(event3.getAttribute("data-pat-inject")).toBe("target: #event-info; source: #document-body"); // prettier-ignore
298+
expect(event3.getAttribute("data-pat-switch")).toBe("selector: #event-info; add: event-info--active; remove: event-info--inactive"); // prettier-ignore
299+
300+
global.fetch.mockClear();
301+
delete global.fetch;
302+
done();
303+
});
304+
259305
it("Loads correct date if set in query string", async (done) => {
260306
const el = document.querySelector(".pat-calendar");
261307
el.setAttribute("data-pat-calendar", "timezone: Europe/Berlin");

src/pat/calendar/documentation.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,5 +61,9 @@ The calendar can be configured through a `data-pat-calendar` attribute. The avai
6161
| `title-week` | MMM D YYYY | |
6262
| `url` | | | URL to an event source as JSON feed.
6363
| `event-color` | blue | Any CSS color value | Default color of events.
64-
| `pat-inject-source` | | CSS selector | If clicking on an event this selector identifies which section of the loaded event to inject.
65-
| `pat-inject-target` | | CSS selector | If clicking on an event this selector identifies where to inject the loaded content.
64+
| `pat-inject-source` | | CSS selector | If clicking on an event this selector identifies which section of the loaded event to inject. | string |
65+
| `pat-inject-target` | | CSS selector | If clicking on an event this selector identifies where to inject the loaded content. | string |
66+
| `pat-switch-selector` | | CSS selector | Defines the element on which pat-select should operate on. | string |
67+
| `pat-switch-add` | | CSS class name | Defines the class name to be added. | string |
68+
| `pat-switch-remove` | | CSS class name | Defines the class name to be removed. | string |
69+

src/pat/calendar/index.html

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,19 @@
1212
</head>
1313
<body>
1414
<div class="calendar-main">
15+
<div id="event-info" class="event-info--inactive">event info here!</div>
1516

1617
<div class="pat-calendar"
1718
data-pat-calendar="category-controls: .cal-categories;
1819
event-sources: ./test_event_source.json,./test_event_source2.json;
19-
event-sources-classes: internal-calendar, external-calendar;
2020
add-url: ./test_add_event.html;
2121
initial-date: 2020-10-10;
2222
event-color: fuchsia;
23+
pat-inject-target: #event-info;
24+
pat-inject-source: #document-body;
25+
pat-switch-selector: #event-info;
26+
pat-switch-remove: event-info--inactive;
27+
pat-switch-add: event-info--active;
2328
lang: en;
2429
store: local;">
2530
<form class="pat-toolbar cal-toolbar pat-form"
@@ -121,6 +126,12 @@
121126
.calendar-hidden {
122127
display: none !important;
123128
}
129+
.event-info--inactive {
130+
color: grey;
131+
}
132+
.event-info--active {
133+
background: lightblue;
134+
}
124135
</style>
125136

126137
</body>

src/pat/calendar/test_event.html

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8" />
5+
<title>Test event</title>
6+
</head>
7+
<body>
8+
<div id="document-body">
9+
hello i'm some detail infor for a test event
10+
</div>
11+
</body>
12+
</html>

0 commit comments

Comments
 (0)