Skip to main content

max / makenotwork

2.9 KB · 72 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 // No-op verb: used where the original inline handler only stopped propagation
9 // (the data-stop modifier does the actual work; this satisfies the dispatcher's
10 // requirement for a data-action value).
11 window.noop = function () {};
12
13 // cart.html: onclick="removeCartGroup(this)"
14 window.onRemoveCartGroup = function () {
15 removeCartGroup(this);
16 };
17
18 // discover.html: onclick="document.querySelector('.discover-sidebar').classList.toggle('show'); this.classList.toggle('active');"
19 window.toggleDiscoverFilters = function () {
20 // The state lives on .discover-layout, not on the sidebar itself: the
21 // sidebar is replaced wholesale by an out-of-band swap on every filter
22 // change, which would drop a class set on it and snap the mobile panel
23 // shut mid-interaction.
24 var layout = document.querySelector('.discover-layout');
25 if (!layout) return;
26 var open = layout.classList.toggle('filters-open');
27 this.classList.toggle('active', open);
28 this.setAttribute('aria-expanded', open ? 'true' : 'false');
29 };
30
31 // index.html: onsubmit="return submitNotify(event)" (data-submit auto-prevents;
32 // submitNotify only uses the event for preventDefault).
33 window.onNotifySubmit = function () {
34 submitNotify({ preventDefault: function () {} });
35 };
36
37 // item.html: onclick="openCollectionPicker('{{ item.id }}', this)"
38 window.onOpenCollectionPicker = function () {
39 openCollectionPicker(this.getAttribute('data-arg'), this);
40 };
41
42 // item.html / library_downloads.html / project.html:
43 // onclick="switchSectionTab(this, 'section-{{ section.slug }}')"
44 window.onSwitchSectionTab = function () {
45 switchSectionTab(this, this.getAttribute('data-arg'));
46 };
47
48 // library.html: onclick="setActiveTab(this)"
49 window.onSetActiveTab = function () {
50 setActiveTab(this);
51 };
52
53 // purchase.html: oninput="updatePwywButton(this.value)"
54 window.onPwywInput = function () {
55 updatePwywButton(this.value);
56 };
57
58 // purchase.html: onclick="fetch('/api/cart/{{ item.id }}',{...}).then(...);return false;"
59 window.onAddToCartLink = function () {
60 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.')});
61 };
62
63 // purchase.html: onclick="window.history.back()"
64 window.historyBack = function () {
65 window.history.back();
66 };
67
68 // receipt.html: onclick="window.print()"
69 window.printPage = function () {
70 window.print();
71 };
72