File size: 14,844 Bytes
eefeb99
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/* Capterra offline study - authored behavior (frontend fixture stage).
   Same-origin only; no remote requests, telemetry, or live chat.
   Contract: help overlay closes with Escape and restores focus; navigation
   uses ordinary local URLs and history (popstate) so refresh and
   back/forward stay deterministic. */

(function () {
  "use strict";

  function ready(fn) {
    if (document.readyState !== "loading") { fn(); return; }
    document.addEventListener("DOMContentLoaded", fn);
  }

  ready(function () {
    // Keep Enter equivalent to the visible Search button in every supported
    // browser and automation environment.
    document.querySelectorAll(".hero-search").forEach(function (form) {
      form.addEventListener("keydown", function (event) {
        if (event.key === "Enter" && event.target.matches("input")) {
          event.preventDefault();
          form.requestSubmit();
        }
      });
    });

    // Mobile navigation toggle.
    var toggle = document.querySelector(".nav-toggle");
    var nav = document.getElementById("main-nav");
    if (toggle && nav) {
      toggle.addEventListener("click", function () {
        var expanded = toggle.getAttribute("aria-expanded") === "true";
        toggle.setAttribute("aria-expanded", String(!expanded));
        nav.classList.toggle("open", !expanded);
      });
      nav.addEventListener("click", function (event) {
        if (event.target.closest("a")) {
          toggle.setAttribute("aria-expanded", "false");
          nav.classList.remove("open");
        }
      });
      document.addEventListener("keydown", function (event) {
        if (event.key === "Escape" && nav.classList.contains("open")) {
          nav.classList.remove("open");
          toggle.setAttribute("aria-expanded", "false");
          toggle.focus();
        }
      });
    }

    // Help overlay: open via trigger, close via button or Escape,
    // and restore focus to the opener on close.
    var trigger = document.querySelector(".help-trigger");
    var dialog = document.getElementById("help-dialog");
    if (trigger && dialog) {
      var close = dialog.querySelector(".help-close");
      var lastFocus = null;
      var openDialog = function () {
        if (dialog.hidden) {
          lastFocus = document.activeElement;
        }
        dialog.hidden = false;
        document.body.classList.add("help-simulation-state");
        (close || dialog).focus();
      };
      var closeDialog = function () {
        dialog.hidden = true;
        document.body.classList.remove("help-simulation-state");
        if (lastFocus && lastFocus.focus) { lastFocus.focus(); }
      };
      trigger.addEventListener("click", openDialog);
      if (close) { close.addEventListener("click", closeDialog); }
      document.addEventListener("keydown", function (event) {
        if (event.key === "Escape" && !dialog.hidden) { closeDialog(); }
        if (event.key === "Tab" && !dialog.hidden) {
          var focusable = Array.prototype.slice.call(
            dialog.querySelectorAll(
              'a[href], button:not([disabled]), [tabindex]:not([tabindex="-1"])'
            )
          );
          if (!focusable.length) {
            event.preventDefault();
            dialog.focus();
            return;
          }
          var first = focusable[0];
          var last = focusable[focusable.length - 1];
          if (event.shiftKey && document.activeElement === first) {
            event.preventDefault();
            last.focus();
          } else if (!event.shiftKey && document.activeElement === last) {
            event.preventDefault();
            first.focus();
          }
        }
      });
      var openFromFragment = function () {
        if (window.location.hash === "#local-help") { openDialog(); }
      };
      window.addEventListener("hashchange", openFromFragment);
      openFromFragment();
    }

    // History support: fixture states are plain local URLs, so browser
    // back/forward (popstate) always lands on a deterministic server render.
    window.addEventListener("popstate", function () {
      // Server-rendered pages are the source of truth; nothing to hydrate.
      // The listener documents deterministic back/forward support.
    });

    // Home category tabs: local, display-only selection.
    var tabs = document.querySelectorAll(".category-tab");
    tabs.forEach(function (tab) {
      tab.addEventListener("click", function () {
        tabs.forEach(function (other) {
          other.classList.toggle("active", other === tab);
          other.setAttribute("aria-selected", String(other === tab));
        });
      });
    });

    // Review-search page: industry and job function are two real steps.
    var chips = document.querySelectorAll("[data-industry]");
    var jobs = document.querySelectorAll("[data-job]");
    var jobFunctions = document.querySelector("[data-job-functions]");
    var showProducts = document.querySelector("[data-show-products]");
    var jobHint = document.querySelector("[data-job-hint]");
    var productInput = document.getElementById("review-product-query");
    var productStatus = document.getElementById("review-product-status");
    if (chips.length && showProducts) {
      var isSupportedProduct = function () {
        if (!productInput) { return true; }
        var normalized = productInput.value.trim().toLowerCase();
        // The captured offline product set contains Notion only. A blank
        // field preserves the source-like mobile path where the input is
        // visually replaced by its Search action.
        return normalized === "" || normalized === "notion";
      };
      var selected = function (controls) {
        return Array.prototype.some.call(controls, function (control) {
          return control.getAttribute("aria-pressed") === "true";
        });
      };
      var updateProductState = function (announceUnsupported) {
        var supported = isSupportedProduct();
        var readyToContinue = supported && selected(chips) && selected(jobs);
        showProducts.disabled = !readyToContinue;
        showProducts.setAttribute("aria-disabled", String(!readyToContinue));
        if (productInput) {
          productInput.setAttribute("aria-invalid", String(!supported));
        }
        if (productStatus) {
          productStatus.hidden = supported || !announceUnsupported;
          productStatus.textContent = supported
            ? ""
            : "That product isn’t available. Search for Notion to continue.";
        }
      };
      var addEnterActivation = function (control) {
        control.addEventListener("keydown", function (event) {
          if (event.key === "Enter") {
            event.preventDefault();
            control.click();
          }
        });
      };
      chips.forEach(function (chip) {
        chip.setAttribute("aria-pressed", "false");
        addEnterActivation(chip);
        chip.addEventListener("click", function () {
          chips.forEach(function (other) {
            other.setAttribute("aria-pressed", String(other === chip));
          });
          jobs.forEach(function (job) {
            job.setAttribute("aria-pressed", "false");
          });
          if (jobFunctions) { jobFunctions.hidden = false; }
          if (jobHint) {
            jobHint.textContent =
              "Now choose the job function that best matches your work.";
          }
          updateProductState(false);
        });
      });
      jobs.forEach(function (job) {
        job.setAttribute("aria-pressed", "false");
        addEnterActivation(job);
        job.addEventListener("click", function () {
          jobs.forEach(function (other) {
            other.setAttribute("aria-pressed", String(other === job));
          });
          if (jobHint) {
            jobHint.textContent =
              "Industry and job function selected. Show matching products.";
          }
          updateProductState(true);
        });
      });
      if (productInput) {
        productInput.addEventListener("input", function () {
          updateProductState(productInput.value.trim() !== "");
        });
      }
      showProducts.addEventListener("click", function () {
        if (!isSupportedProduct()) {
          updateProductState(true);
          if (productInput) { productInput.focus(); }
        } else if (!showProducts.disabled) {
          window.location.assign("/reviews/notion/write");
        }
      });
      updateProductState(false);
    }

    // Rating picker: selecting four stars visibly fills stars one through
    // four. This avoids the misleading single-filled-star radio default.
    var ratingInputs = document.querySelectorAll(
      '.star-picker input[name="rating"]'
    );
    var ratingOutput = document.querySelector("[data-rating-output]");
    if (ratingInputs.length) {
      var paintRating = function (selected) {
        ratingInputs.forEach(function (input) {
          var option = input.closest(".star-option");
          if (option) {
            option.classList.toggle(
              "is-filled", Number(input.value) <= selected
            );
          }
        });
        if (ratingOutput) {
          ratingOutput.textContent = selected
            ? selected + " of 5 stars selected"
            : "No rating selected";
        }
      };
      ratingInputs.forEach(function (input) {
        input.addEventListener("change", function () {
          paintRating(Number(input.value));
        });
      });
      var checkedRating = document.querySelector(
        '.star-picker input[name="rating"]:checked'
      );
      paintRating(checkedRating ? Number(checkedRating.value) : 0);

      // A server-rendered missing-rating response returns keyboard and
      // assistive-technology users to the first radio while retaining the
      // submitted review text and keeping the alert available by reference.
      var ratingError = document.querySelector(
        '.rating-fieldset[data-focus-error="rating"]'
      );
      if (ratingError) {
        var firstRating = ratingError.querySelector(
          'input[name="rating"]'
        );
        if (firstRating) { firstRating.focus(); }
      }
    }

    // Every source-visible unavailable external action terminates in one
    // honest local disclosure. Public controls expose only their stable
    // product identity and a generic boundary kind; the scope assertion
    // manifest remains server-side. The protected contract-audit surface may
    // additionally provide an assertion payload for machine inspection.
    var boundaryDialog = document.getElementById("effect-boundary-dialog");
    var boundaryControls = document.querySelectorAll(
      "[data-effect-boundary], [data-affordance-id][data-effect-payload]"
    );
    if (boundaryDialog && boundaryControls.length) {
      var boundaryClose = boundaryDialog.querySelector(
        ".effect-boundary-close"
      );
      var boundarySummary = boundaryDialog.querySelector(
        ".effect-boundary-summary"
      );
      var boundaryEffects = boundaryDialog.querySelector(
        ".effect-boundary-effects"
      );
      var boundaryLastFocus = null;
      var closeBoundary = function () {
        boundaryDialog.hidden = true;
        if (boundaryLastFocus && boundaryLastFocus.focus) {
          boundaryLastFocus.focus();
        }
      };
      boundaryControls.forEach(function (control) {
        control.addEventListener("click", function (event) {
          if (control.hasAttribute("data-effect-boundary")) {
            event.preventDefault();
          }
          var assertions = [];
          try {
            assertions = JSON.parse(
              control.getAttribute("data-effect-payload") || "[]"
            );
          } catch (_error) {
            assertions = [];
          }
          boundaryLastFocus = control;
          boundarySummary.textContent =
            control.getAttribute("data-boundary-message") ||
            control.textContent.trim() + " isn’t available from this page.";
          boundaryEffects.replaceChildren();
          assertions.forEach(function (assertion) {
            var item = document.createElement("li");
            item.setAttribute("data-effect-id", assertion.id);
            item.setAttribute(
              "data-effect-coverage-id",
              assertion.coverage_id
            );
            item.hidden = true;
            boundaryEffects.appendChild(item);
          });
          boundaryDialog.hidden = false;
          (boundaryClose || boundaryDialog).focus();
        });
      });
      if (boundaryClose) {
        boundaryClose.addEventListener("click", closeBoundary);
      }
      document.addEventListener("keydown", function (event) {
        if (event.key === "Escape" && !boundaryDialog.hidden) {
          closeBoundary();
        }
      });
    }

    // A genuine filter selection enters the frozen filter-display route
    // through the public GET form; no query-only identity flip is required.
    var reviewFilterForm = document.querySelector(
      "form[data-review-filter-form]"
    );
    var reviewFilterControls = document.querySelectorAll(
      "select[data-review-filter-control]"
    );
    if (reviewFilterForm && reviewFilterControls.length) {
      reviewFilterControls.forEach(function (control) {
        control.addEventListener("change", function () {
          reviewFilterForm.requestSubmit();
        });
      });
    }

    var quickPollDismiss = document.querySelector(
      "[data-quick-poll-dismiss]"
    );
    if (quickPollDismiss) {
      quickPollDismiss.addEventListener("click", function () {
        var poll = quickPollDismiss.closest(".reviews-quick-poll");
        if (poll) { poll.hidden = true; }
      });
    }

    // Source-visible "Show more" controls reveal local content in place and
    // preserve a stable expanded state for accessibility tooling.
    document.querySelectorAll(".reviews-show-more[aria-controls]").forEach(
      function (control) {
        var targetId = control.getAttribute("aria-controls");
        var target = document.getElementById(targetId);
        if (!target) { return; }
        var collapsedLabel = control.innerHTML;
        control.dataset.collapsedLabel = collapsedLabel;
        control.addEventListener("click", function () {
          var expand = target.hidden;
          target.hidden = !expand;
          document
            .querySelectorAll(
              '.reviews-show-more[aria-controls="' + targetId + '"]'
            )
            .forEach(function (peer) {
              peer.setAttribute("aria-expanded", String(expand));
              peer.innerHTML = expand
                ? "Show less&nbsp; &minus;"
                : peer.dataset.collapsedLabel;
            });
        });
      }
    );
  });
})();