Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
- pat calendar: Fetch events from the backend.
- pat calendar: Allow filtering/hiding events based in comparing the checkbox id with the classes of the displayed events.
- pat calendar: Support injection of events when clicking on and event rather than redirecting to them.
Done by adding `pat-inject` to rendered events via some configuration options.
- pat calendar: Support `pat-switch` for rendered events via some configuration options.
- pat calendar: Store view, date and active categories per URL, allowing to individually customize the calendar per page.
- pat tooltip: Use tippy v6 based implementation.
- pat tooltip: Introduce new option ``arrowPadding`` to define the padding of the box arrow from the corners of the tooltip.
Expand Down
56 changes: 45 additions & 11 deletions src/pat/calendar/calendar.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,15 @@ parser.addArgument("url", null);
parser.addArgument("event-sources", [], undefined, true);
//parser.addArgument("add-url", null);

// pat-inject support for individual events
parser.addArgument("pat-inject-source", null);
parser.addArgument("pat-inject-target", null);

// pat-switch support for individual events
parser.addArgument("pat-switch-selector", null);
parser.addArgument("pat-switch-remove", null);
parser.addArgument("pat-switch-add", null);

parser.addAlias("default-date", "initial-date");
parser.addAlias("default-view", "initial-view");

Expand Down Expand Up @@ -147,8 +153,6 @@ export default Base.extend({
];
config.eventColor = opts.eventColor;

config.dateClick = this.addNewEvent.bind(this);

let lang =
opts.lang ||
document.querySelector("html").getAttribute("lang") ||
Expand Down Expand Up @@ -196,10 +200,13 @@ export default Base.extend({
cal_el.setAttribute("class", "pat-calendar__fc");
el.appendChild(cal_el);

// Create a element for modals/injections
this.mod_el = document.createElement("section");
this.mod_el.setAttribute("class", "pat-calendar__modal");
el.appendChild(this.mod_el);
if (opts.addUrl) {
config.dateClick = this.addNewEvent.bind(this);
// Create a element for modals/injections
this.mod_el = document.createElement("section");
this.mod_el.setAttribute("class", "pat-calendar__modal");
el.appendChild(this.mod_el);
}

let calendar = (this.calendar = new Calendar(cal_el, config));
calendar.render();
Expand Down Expand Up @@ -251,6 +258,11 @@ export default Base.extend({
contact_email: event.contact_email,
event_url: event.event_url,
};
for (const prop in ret) {
if (!ret[prop]) {
delete ret[prop];
}
}
return ret;
},

Expand Down Expand Up @@ -311,16 +323,38 @@ export default Base.extend({

init_event(args) {
this.filter_event(args.event);
let source = this.options.pat["inject-source"];
let target = this.options.pat["inject-target"];

let do_scan = false;

// pat-inject support
const source = this.options.pat["inject-source"];
const target = this.options.pat["inject-target"];
if (source || target) {
source = source || "body";
target = target || "body";
args.el.classList.add("pat-inject");
args.el.setAttribute(
"data-pat-inject",
`target: ${target}; source: ${source}`
`target: ${target || "body"}; source: ${source || "body"}`
);
do_scan = true;
}

// pat-switch support
const switch_sel = this.options.pat["switch-selector"];
if (switch_sel) {
const switch_add = this.options.pat["switch-add"];
const switch_rm = this.options.pat["switch-remove"];

args.el.classList.add("pat-switch");
args.el.setAttribute(
"data-pat-switch",
`selector: ${switch_sel}${
switch_add ? "; add: " + switch_add : ""
}${switch_rm ? "; remove: " + switch_rm : ""}`
);
do_scan = true;
}

if (do_scan) {
registry.scan(args.el);
}
},
Expand Down
80 changes: 77 additions & 3 deletions src/pat/calendar/calendar.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ const mockFetch = () =>
end: "2020-10-10T12:00:00Z",
},
{
title: "Event 2",
start: "2020-10-12",
end: "2020-10-12",
"title": "Event 2",
"start": "2020-10-12",
"end": "2020-10-12",
"@id": "./test_event.html",
},
{
title: "Event 3",
Expand Down Expand Up @@ -228,6 +229,79 @@ describe("Calendar tests", () => {
done();
});

it("Loads events and does not set the href if not present", async (done) => {
const el = document.querySelector(".pat-calendar");
el.setAttribute(
"data-pat-calendar",
"initial-date: 2020-10-10; url: ./test.json;"
);

global.fetch = jest.fn().mockImplementation(mockFetch);

registry.scan(document.body);
await utils.timeout(1); // wait a tick for async to settle.

const events = [...document.querySelectorAll(".fc-event-title")];

const event1 = events.filter((it) => it.textContent === "Event 1")[0].closest(".fc-event"); // prettier-ignore
const event2 = events.filter((it) => it.textContent === "Event 2")[0].closest(".fc-event"); // prettier-ignore
const event3 = events.filter((it) => it.textContent === "Event 3")[0].closest(".fc-event"); // prettier-ignore

expect(event1.href).toBeFalsy();
expect(event2.href).toBe("http://localhost/test_event.html");
expect(event3.href).toBeFalsy();

global.fetch.mockClear();
delete global.fetch;
done();
});

it("Loads events and initializes them with pat-inject and pat-switch", async (done) => {
const el = document.querySelector(".pat-calendar");
el.setAttribute(
"data-pat-calendar",
`initial-date: 2020-10-10;
url: ./test.json;
pat-inject-target: #event-info;
pat-inject-source: #document-body;
pat-switch-selector: #event-info;
pat-switch-remove: event-info--inactive;
pat-switch-add: event-info--active`
);

global.fetch = jest.fn().mockImplementation(mockFetch);

registry.scan(document.body);
await utils.timeout(1); // wait a tick for async to settle.

const events = [...document.querySelectorAll(".fc-event-title")];

const event1 = events.filter((it) => it.textContent === "Event 1")[0].closest(".fc-event"); // prettier-ignore
const event2 = events.filter((it) => it.textContent === "Event 2")[0].closest(".fc-event"); // prettier-ignore
const event3 = events.filter((it) => it.textContent === "Event 3")[0].closest(".fc-event"); // prettier-ignore

console.log(event3.outerHTML);

expect(event1.classList.contains("pat-inject")).toBe(true);
expect(event1.classList.contains("pat-switch")).toBe(true);
expect(event1.getAttribute("data-pat-inject")).toBe("target: #event-info; source: #document-body"); // prettier-ignore
expect(event1.getAttribute("data-pat-switch")).toBe("selector: #event-info; add: event-info--active; remove: event-info--inactive"); // prettier-ignore

expect(event2.classList.contains("pat-inject")).toBe(true);
expect(event2.classList.contains("pat-switch")).toBe(true);
expect(event2.getAttribute("data-pat-inject")).toBe("target: #event-info; source: #document-body"); // prettier-ignore
expect(event2.getAttribute("data-pat-switch")).toBe("selector: #event-info; add: event-info--active; remove: event-info--inactive"); // prettier-ignore

expect(event3.classList.contains("pat-inject")).toBe(true);
expect(event3.classList.contains("pat-switch")).toBe(true);
expect(event3.getAttribute("data-pat-inject")).toBe("target: #event-info; source: #document-body"); // prettier-ignore
expect(event3.getAttribute("data-pat-switch")).toBe("selector: #event-info; add: event-info--active; remove: event-info--inactive"); // prettier-ignore

global.fetch.mockClear();
delete global.fetch;
done();
});

it("Loads correct date if set in query string", async (done) => {
const el = document.querySelector(".pat-calendar");
el.setAttribute("data-pat-calendar", "timezone: Europe/Berlin");
Expand Down
8 changes: 6 additions & 2 deletions src/pat/calendar/documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,9 @@ The calendar can be configured through a `data-pat-calendar` attribute. The avai
| `title-week` | MMM D YYYY | |
| `url` | | | URL to an event source as JSON feed.
| `event-color` | blue | Any CSS color value | Default color of events.
| `pat-inject-source` | | CSS selector | If clicking on an event this selector identifies which section of the loaded event to inject.
| `pat-inject-target` | | CSS selector | If clicking on an event this selector identifies where to inject the loaded content.
| `pat-inject-source` | | CSS selector | If clicking on an event this selector identifies which section of the loaded event to inject. | string |
| `pat-inject-target` | | CSS selector | If clicking on an event this selector identifies where to inject the loaded content. | string |
| `pat-switch-selector` | | CSS selector | Defines the element on which pat-select should operate on. | string |
| `pat-switch-add` | | CSS class name | Defines the class name to be added. | string |
| `pat-switch-remove` | | CSS class name | Defines the class name to be removed. | string |

13 changes: 12 additions & 1 deletion src/pat/calendar/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,19 @@
</head>
<body>
<div class="calendar-main">
<div id="event-info" class="event-info--inactive">event info here!</div>

<div class="pat-calendar"
data-pat-calendar="category-controls: .cal-categories;
event-sources: ./test_event_source.json,./test_event_source2.json;
event-sources-classes: internal-calendar, external-calendar;
add-url: ./test_add_event.html;
initial-date: 2020-10-10;
event-color: fuchsia;
pat-inject-target: #event-info;
pat-inject-source: #document-body;
pat-switch-selector: #event-info;
pat-switch-remove: event-info--inactive;
pat-switch-add: event-info--active;
lang: en;
store: local;">
<form class="pat-toolbar cal-toolbar pat-form"
Expand Down Expand Up @@ -121,6 +126,12 @@
.calendar-hidden {
display: none !important;
}
.event-info--inactive {
color: grey;
}
.event-info--active {
background: lightblue;
}
</style>

</body>
Expand Down
12 changes: 12 additions & 0 deletions src/pat/calendar/test_event.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Test event</title>
</head>
<body>
<div id="document-body">
hello i'm some detail infor for a test event
</div>
</body>
</html>
6 changes: 4 additions & 2 deletions src/pat/calendar/test_event_source.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
"title": "Event 1",
"start": "2020-10-10T10:00:00",
"end": "2020-10-10T12:00:00",
"class": "hello-class-1 hello-class-2 calendar-section1-category1"
"class": "hello-class-1 hello-class-2 calendar-section1-category1",
"@id": "./test_event.html"
},
{
"title": "Event 2",
"start": "2020-10-12",
"end": "2020-10-12",
"class": "calendar-section1-category2"
"class": "calendar-section1-category2",
"@id": "./test_event.html"
},
{
"title": "Event 3",
Expand Down