Verified Commit 74b566d7 authored by Adrian Pascu's avatar Adrian Pascu
Browse files

Show "Coming up" and "Every week" in one shared `agenda.html` partial that...

Show "Coming up" and "Every week" in one shared `agenda.html` partial that `agenda.js` keeps current in the browser
parent a0133ba0
Loading
Loading
Loading
Loading
Loading
+1
Changes for .prettierignore: 1 added line, 0 removed lines.
Original line number Diff line number Diff line
@@ -4,3 +4,4 @@
layouts/_default/_markup/
layouts/partials/agenda.html
layouts/partials/agenda-data.html
layouts/partials/agenda-moment.html
+4 −2
Changes for README.md: 4 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -12,9 +12,11 @@ This site is built using the [Hugo static site generator](https://gohugo.io/).
   - Refer to [this example](https://gitlab.com/hsbxl/agenda/raw/master/content/events/panpanpan.md) for guidance.
3. If the event has an `image:` at the top and the image carries information (a poster with a date, a map), add `image_alt:` with a short description of what it shows. Leave it out for a logo or a decorative picture.

### Regular series in the Agenda
### The Agenda

The Agenda in the sidebar, and on phones in the home page itself, shows each regular series with its rhythm and at most its next three dates, and lists the other events separately, so one-off events stay visible. A series is regular when the `_index.md` of its folder has a `recurrence:` line, for example `recurrence: "open evening for everyone, every Tuesday, 19:00"` in `content/events/techtuesday/_index.md`. That text appears next to the series name. To cancel one date of a series, add `cancelled: true` to that event: it then appears with the other events, and the series shows the next three dates that still take place. The events page and the calendar file always list every date.
The Agenda appears in the sidebar, in the home page itself on phones, and at the top of the events page. "Coming up" lists every event of the next two weeks (six weeks on the events page) in start order, with the weekly ones tagged "weekly" and the others in bold, at most 8 rows in the sidebar and on the home page. "Every week" names each weekly series once, with its day and time, and links to the series page. A series is weekly when the `_index.md` of its folder has a `recurrence:` line, for example `recurrence: "open evening for everyone, every Tuesday, 19:00"` in `content/events/techtuesday/_index.md`. To cancel one date, add `cancelled: true` to that event: it leaves "Coming up", and its series stays under "Every week". The events page and the calendar file always list every date.

The site is rebuilt every night, and the build writes the next two days beyond the window too. `static/js/agenda.js` then compares each event's start and end with the visitor's clock, on load and once a minute: it hides events that have ended, puts the ones in progress at the top with a "now" badge and their end time, and keeps the row limit. Without JavaScript, the list shows as it was at build time.

### Adding a Project

+1 −1
Changes for content/events/_index.md: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -6,7 +6,7 @@ image: 8240276852_f190132576_w.jpg
description: "Upcoming and past events at Hackerspace Brussels: TechTuesdays, open coworking days, workshops, hack nights and community meetups."
---

## Upcoming events
## All upcoming events

{{< events when="upcoming" series="all" >}}

+62 −31
Changes for e2e/structure.spec.ts: 62 added lines, 31 removed lines.
Original line number Diff line number Diff line
@@ -161,49 +161,80 @@ test("shows a table of contents only when the page has headings", async ({
  ).toBeVisible();
});

for (const { path, headings } of [
  { path: "/", headings: ".home-agenda h3" },
  { path: "/contact/", headings: "#last-events h4" },
for (const { path, agenda } of [
  { path: "/", agenda: ".home-agenda .agenda" },
  { path: "/contact/", agenda: "#last-events .agenda" },
]) {
  test(`lists at most the next three dates of each regular series in the Agenda on ${path}, apart from one-off events`, async ({
  test(`lists what is coming up, then each weekly series once, in the Agenda on ${path}`, async ({
    page,
  }) => {
    await page.goto(path);
    const agenda = await page.evaluate((selector) => {
      const list = (heading: string) => {
        const title = [...document.querySelectorAll(selector)].find(
          (element) => element.textContent === heading,
        );
        const items = title?.nextElementSibling?.querySelectorAll("a") ?? [];
        return [...items].map((link) => ({
          path: new URL(link.href).pathname,
          series: new URL(link.href).pathname.split("/")[2] ?? "",
          text: link.textContent?.replace(/\s+/g, " ").trim() ?? "",
        }));
    const content = await page.evaluate((selector) => {
      const root = document.querySelector(selector)!;
      const entries = [
        ...root.querySelectorAll<HTMLElement>(".agenda-entry:not([hidden])"),
      ];
      return {
        headings: [...root.querySelectorAll("h3, h4")].map(
          (heading) => heading.textContent,
        ),
        entries: entries.map((entry) => ({
          datetime: entry.querySelector("time")?.getAttribute("datetime"),
          weekly: entry.querySelector(".agenda-tag")?.textContent === "weekly",
          series: new URL(entry.querySelector("a")!.href).pathname.split(
            "/",
          )[2],
        })),
        series: [...root.querySelectorAll(".agenda-series a")].map(
          (link) => new URL((link as HTMLAnchorElement).href).pathname,
        ),
        subscribe: root.querySelector<HTMLAnchorElement>(".agenda-subscribe a")
          ?.href,
      };
      return { regular: list("Regular"), upcoming: list("Upcoming events") };
    }, headings);
    const datesPerSeries = new Map<string, Set<string>>();
    for (const item of agenda.regular) {
      const dates = datesPerSeries.get(item.series) ?? new Set();
      datesPerSeries.set(item.series, dates.add(item.path));
    }
    const regularSeries = [...datesPerSeries.keys()];
    expect(regularSeries.length).toBeGreaterThan(0);
    for (const dates of datesPerSeries.values()) {
      expect(dates.size).toBeLessThanOrEqual(3);
    }, agenda);
    expect(content.headings).toEqual(["Coming up", "Every week"]);
    expect(content.entries.length).toBeLessThanOrEqual(8);
    for (const entry of content.entries) {
      expect(entry.datetime).toMatch(
        /^\d{4}-\d\d-\d\dT\d\d:\d\d:00[+-]\d\d:\d\d$/,
      );
    }
    expect(datesPerSeries.get("techtuesday")?.size).toBe(3);
    expect(agenda.upcoming.length).toBeLessThanOrEqual(8);
    expect(content.series).toEqual(
      expect.arrayContaining([
        "/events/techtuesday/",
        "/events/open-coworking-day/",
        "/events/sunday-electronics/",
      ]),
    );
    expect(
      agenda.upcoming.filter(
        (item) =>
          regularSeries.includes(item.series) && !/CANCELLED/i.test(item.text),
      content.entries.filter(
        (entry) =>
          entry.weekly !== content.series.includes(`/events/${entry.series}/`),
      ),
    ).toEqual([]);
    expect(content.subscribe).toBe("webcal://hsbxl.be/events/index.ics");
  });
}

test("marks an event in progress as now and puts it first in the Agenda", async ({
  page,
}) => {
  await page.goto("/");
  const start = await page
    .locator(".agenda:visible .agenda-entry")
    .nth(1)
    .getAttribute("data-start");
  await page.clock.setFixedTime(Date.parse(start!) + 60000);
  await page.reload();
  const entries = page.locator(".agenda:visible .agenda-entry:not([hidden])");
  const started = entries.filter({
    has: page.locator(`time[datetime="${start}"]`),
  });
  await expect(started.locator(".agenda-now")).toBeVisible();
  await expect(started.locator(".agenda-until")).toBeVisible();
  await expect(entries.first().locator(".agenda-now")).toBeVisible();
});

test("shows the Agenda once on the home page: in the sidebar on desktop, in the page on phones", async ({
  page,
}, info) => {
+49 −25
Changes for layouts/partials/agenda-data.html: 49 added lines, 25 removed lines.
Original line number Diff line number Diff line
{{- $today := now.Format "2006-01-02" -}}
{{- $horizon := (now.AddDate 0 0 60).Format "2006-01-02" -}}
{{- $regular := slice -}}
{{- $now := now -}}
{{- $shownUntil := $now.AddDate 0 0 .days -}}
{{- $emittedUntil := $now.AddDate 0 0 (add .days 2) -}}
{{- $live := slice -}}
{{- $later := slice -}}
{{- $buffer := slice -}}
{{- range site.RegularPages.ByParam "startdate" -}}
  {{- if and (eq .Type "events") (isset .Params "startdate") (not .Params.cancelled) -}}
    {{- $start := partial "agenda-moment.html" (dict "date" .Params.startdate "time" .Params.starttime) -}}
    {{- $endDate := .Params.enddate | default .Params.startdate -}}
    {{- $end := false -}}
    {{- with .Params.endtime -}}
      {{- $end = partial "agenda-moment.html" (dict "date" $endDate "time" .) -}}
    {{- else -}}
      {{- with partial "agenda-moment.html" (dict "date" $endDate) }}{{ $end = .AddDate 0 0 1 }}{{ end -}}
    {{- end -}}
    {{- if and $start $end (not ($end.After $start)) -}}
      {{- $end = $end.AddDate 0 0 1 -}}
    {{- end -}}
    {{- if and $start $end ($end.After $now) ($start.Before $emittedUntil) -}}
      {{- $entry := dict "page" . "start" $start "end" $end "weekly" (not (not .Parent.Params.recurrence)) "live" (not ($start.After $now)) -}}
      {{- if $entry.live -}}
        {{- $live = $live | append $entry -}}
      {{- else if $start.Before $shownUntil -}}
        {{- $later = $later | append $entry -}}
      {{- else -}}
        {{- $buffer = $buffer | append $entry -}}
      {{- end -}}
    {{- end -}}
  {{- end -}}
{{- end -}}
{{- $limit := .limit -}}
{{- $entries := slice -}}
{{- range $i, $entry := $live | append $later -}}
  {{- $entries = $entries | append (merge $entry (dict "shown" (or (eq $limit 0) (lt $i $limit)))) -}}
{{- end -}}
{{- range $buffer -}}
  {{- $entries = $entries | append (merge . (dict "shown" false)) -}}
{{- end -}}
{{- $series := slice -}}
{{- $today := $now.Format "2006-01-02" -}}
{{- with site.GetPage "/events" -}}
  {{- range .Sections -}}
    {{- $recurrence := .Params.recurrence -}}
    {{- if $recurrence -}}
      {{- $name := .Params.series | default .Title -}}
      {{- $label := printf "%s, %s" $name $recurrence -}}
    {{- if .Params.recurrence -}}
      {{- $section := . -}}
      {{- $next := slice -}}
      {{- range .RegularPages.ByParam "startdate" -}}
        {{- if and (lt (len $next) 3) (not .Params.cancelled) (isset .Params "startdate") (ge .Params.startdate $today) -}}
          {{- $next = $next | append . -}}
        {{- if and (not $next) (not .Params.cancelled) (isset .Params "startdate") (ge (dateFormat "2006-01-02" .Params.startdate) $today) -}}
          {{- $moment := partial "agenda-moment.html" (dict "date" .Params.startdate "time" .Params.starttime) -}}
          {{- if $moment }}{{ $next = slice (dict "moment" $moment "time" (printf "%v" .Params.starttime)) }}{{ end -}}
        {{- end -}}
      {{- end -}}
      {{- with $next -}}
        {{- $first := index . 0 -}}
        {{- $regular = $regular | append (dict "date" $first.Params.startdate "page" $first "pages" . "name" $name "label" $label) -}}
      {{- end -}}
    {{- end -}}
  {{- end -}}
{{- end -}}
{{- $oneOff := slice -}}
{{- range site.RegularPages.ByParam "startdate" -}}
  {{- if eq .Type "events" -}}
    {{- $upcoming := false -}}
    {{- if isset .Params "startdate" -}}
      {{- $upcoming = and (ge .Params.startdate $today) (le .Params.startdate $horizon) -}}
    {{- else if isset .Params "enddate" -}}
      {{- $upcoming = ge .Params.enddate $today -}}
        {{- $order := printf "%d %s" (mod (add (int $first.moment.Weekday) 6) 7) ($first.moment.Format "15:04") -}}
        {{- $series = $series | append (dict "page" $section "name" ($section.Params.series | default $section.Title) "day" ($first.moment.Format "Mon") "time" $first.time "order" $order) -}}
      {{- end -}}
    {{- if and $upcoming (or (not .Parent.Params.recurrence) .Params.cancelled) -}}
      {{- $oneOff = $oneOff | append (dict "page" . "label" .Params.linktitle) -}}
    {{- end -}}
  {{- end -}}
{{- end -}}
{{- return dict "regular" (sort $regular "date") "upcoming" (first 8 $oneOff) -}}
{{- return dict "entries" $entries "series" (sort $series "order") -}}
Loading