Skip to main content

max / makenotwork

2.2 KB · 47 lines History Blame Raw
1 // Delegated handlers extracted from inline on* attributes for CSP compliance.
2 // These are the named wrappers referenced by data-action / data-change /
3 // data-input / data-submit in the pages templates. The action dispatcher in
4 // the core module invokes each with `this` bound to the dispatching element, so the
5 // bodies read args from the element (this.value, this.dataset, this itself)
6 // exactly as the original inline handlers did.
7
8 // cart.html: onclick="removeCartGroup(this)"
9 window.onRemoveCartGroup = function () {
10 removeCartGroup(this);
11 };
12
13 // discover.html: onclick="document.querySelector('.discover-sidebar').classList.toggle('show'); this.classList.toggle('active');"
14 window.toggleDiscoverFilters = function () {
15 // The state lives on .discover-layout, not on the sidebar itself: the
16 // sidebar is replaced wholesale by an out-of-band swap on every filter
17 // change, which would drop a class set on it and snap the mobile panel
18 // shut mid-interaction.
19 var layout = document.querySelector('.discover-layout');
20 if (!layout) return;
21 var open = layout.classList.toggle('filters-open');
22 this.classList.toggle('active', open);
23 this.setAttribute('aria-expanded', open ? 'true' : 'false');
24 };
25
26 // index.html: onsubmit="return submitNotify(event)" (data-submit auto-prevents;
27 // submitNotify only uses the event for preventDefault).
28 window.onNotifySubmit = function () {
29 submitNotify({ preventDefault: function () {} });
30 };
31
32 // item.html / library_downloads.html / project.html:
33 // onclick="switchSectionTab(this, 'section-{{ section.slug }}')"
34 window.onSwitchSectionTab = function () {
35 switchSectionTab(this, this.getAttribute('data-arg'));
36 };
37
38 // purchase.html: oninput="updatePwywButton(this.value)"
39 window.onPwywInput = function () {
40 updatePwywButton(this.value);
41 };
42
43 // purchase.html: onclick="fetch('/api/cart/{{ item.id }}',{...}).then(...);return false;"
44 window.onAddToCartLink = function () {
45 fetch('/api/cart/' + this.getAttribute('data-arg'), {method:'POST',headers:csrfHeaders()}).then(function(r){if(!r.ok)throw new Error('Failed');return r.json()}).then(function(){window.location.href='/cart'}).catch(function(){alert('Could not add to cart. Please try again.')});
46 };
47