Files
@ c5289f39bb3c
Branch filter:
Location: website/conservancy/static/js/supporter-page.js - annotation
c5289f39bb3c
5.0 KiB
application/javascript
Fix flake8 warnings
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 | 01eb8c80c8c1 6b649e2f48e0 e397501bfa55 342590123ff5 01eb8c80c8c1 01eb8c80c8c1 01eb8c80c8c1 7b1ffebcfb10 7b1ffebcfb10 6392ae3a4313 6392ae3a4313 6392ae3a4313 6392ae3a4313 6392ae3a4313 6392ae3a4313 cb5b5ec23fc2 cb5b5ec23fc2 cb5b5ec23fc2 cb5b5ec23fc2 cb5b5ec23fc2 cb5b5ec23fc2 cb5b5ec23fc2 cb5b5ec23fc2 cb5b5ec23fc2 6392ae3a4313 6392ae3a4313 8a9f796eb1f7 8a9f796eb1f7 8a9f796eb1f7 8a9f796eb1f7 8a9f796eb1f7 ab29d4563909 10dfdb617be0 ccc036d631c2 44c0a9db3247 44c0a9db3247 44c0a9db3247 44c0a9db3247 44c0a9db3247 44c0a9db3247 44c0a9db3247 44c0a9db3247 44c0a9db3247 44c0a9db3247 44c0a9db3247 44c0a9db3247 44c0a9db3247 44c0a9db3247 44c0a9db3247 44c0a9db3247 44c0a9db3247 44c0a9db3247 73e30c30e844 674261e0f0c2 674261e0f0c2 6392ae3a4313 10dfdb617be0 674261e0f0c2 10dfdb617be0 44c0a9db3247 10dfdb617be0 6392ae3a4313 44c0a9db3247 cb5b5ec23fc2 44c0a9db3247 cb5b5ec23fc2 44c0a9db3247 44c0a9db3247 44c0a9db3247 44c0a9db3247 44c0a9db3247 44c0a9db3247 44c0a9db3247 cb5b5ec23fc2 10dfdb617be0 44c0a9db3247 cb5b5ec23fc2 10dfdb617be0 cb5b5ec23fc2 10dfdb617be0 cb5b5ec23fc2 cb5b5ec23fc2 cb5b5ec23fc2 cb5b5ec23fc2 86e780340a2b 73e30c30e844 b90413809f26 73e30c30e844 44c0a9db3247 44c0a9db3247 44c0a9db3247 44c0a9db3247 44c0a9db3247 44c0a9db3247 44c0a9db3247 44c0a9db3247 10dfdb617be0 9d30d7431a1e 86ef51db5dff 44c0a9db3247 44c0a9db3247 44c0a9db3247 86ef51db5dff 44c0a9db3247 44c0a9db3247 44c0a9db3247 44c0a9db3247 44c0a9db3247 44c0a9db3247 44c0a9db3247 8a9f796eb1f7 2ba369aa5c5a 8a9f796eb1f7 73e30c30e844 73e30c30e844 73e30c30e844 73e30c30e844 8a9f796eb1f7 8a9f796eb1f7 8a9f796eb1f7 8a9f796eb1f7 8a9f796eb1f7 8a9f796eb1f7 8a9f796eb1f7 8a9f796eb1f7 8a9f796eb1f7 8a9f796eb1f7 8a9f796eb1f7 8a9f796eb1f7 8a9f796eb1f7 e397501bfa55 e3a8c4bd708d 6b649e2f48e0 8a9f796eb1f7 8a9f796eb1f7 73e30c30e844 73e30c30e844 8a9f796eb1f7 | /* Copyright (C) 2012-2013 Denver Gingerich,
** Copyright (C) 2013-2014, 2020 Bradley M. Kuhn,
** Copyright (C) 2016, 2020 Brett Smith.
** License: GPLv3-or-later
** Find a copy of GPL at https://sfconservancy.org/GPLv3
*/
"use strict";
var flipClass = function(elem, byeClass, hiClass) {
var classList = elem.classList;
classList.remove(byeClass);
classList.add(hiClass);
}
var buildAmountData = function(amountInput) {
var amountData = {
minAmount: parseFloat(amountInput.min),
newAmount: parseFloat(amountInput.value),
}
if (amountInput.dataset.oldAmount !== undefined) {
amountData.oldAmount = parseFloat(amountInput.dataset.oldAmount);
}
return amountData;
}
/* We've sometimes published links that say #renew instead of #renewal.
Rewrite that to work as intended. */
if (window.location.hash === "#renew") {
window.location.hash = "#renewal";
}
var formCorrectionNeeded = qs('#form-correction-needed');
function new_amount(amountData, amountInput, amountError) {
var isValid = amountData.newAmount >= amountData.minAmount;
if (amountData.oldAmount === undefined) {
if (isValid) {
hide(amountError);
} else {
flipClass(amountInput, 'valid', 'invalid');
show(amountError);
}
} else if (isValid) {
flipClass(amountInput, 'invalid', 'valid');
hide(amountError);
} else if (amountData.oldAmount >= amountData.minAmount) {
flipClass(amountInput, 'valid', 'invalid');
show(amountError);
}
}
function init_sustainer_form_validation () {
// Forms start in "invalid" form, with the errors shown, so that
// non-Javascript users see the errors by default and know what they must
// enter. Now we hide those for JavaScript users:
formCorrectionNeeded.classList.add('hidden');
qsa('form.supporter-form').forEach(function(form) {
var amountInput = qs('input[type=number]', form);
var amountError = qs('.supporter-form-input .form-error', form);
function amount_change_handler () {
var amountData = buildAmountData(amountInput);
new_amount(amountData, amountInput, amountError);
amountInput.dataset.oldAmount = amountData.newAmount;
}
amountInput.addEventListener('input', function() {
amountInput.classList.remove('invalid');
})
amountInput.addEventListener('focusout', amount_change_handler);
amount_change_handler();
form.addEventListener('submit', function(event) {
var amountData = buildAmountData(amountInput);
if (amountData.newAmount >= amountData.minAmount) {
formCorrectionNeeded.classList.add('hidden');
} else {
formCorrectionNeeded.classList.remove('hidden')
.css("font-weight", "bold").css("font-size", "150%");
event.preventDefault();
}
});
});
}
function init_sustainer_type_switching () {
function selectSupportType(selectedLink) {
qsa(".supporter-type-selector a").forEach(function(el) {
el.classList.remove("supporter-type-selector-selected");
});
selectedLink.classList.add("supporter-type-selector-selected");
qsa(".supporter-type-selection").forEach(function(el) { hide(el); });
let hash = window.location.hash !== '' ? window.location.hash : '#annual';
show(qs(hash));
formCorrectionNeeded.classList.add('hidden');
return false;
};
qsa(".supporter-type-selector a").forEach(function(el) {
el.addEventListener("click", () => selectSupportType(el));
});
let el = qs(window.location.hash !== '' ? window.location.hash + 'Selector' : '#annualSelector');
selectSupportType(el);
window.addEventListener("hashchange", function () {
let el = qs(window.location.hash !== '' ? window.location.hash + 'Selector' : '#annualSelector');
selectSupportType(el);
});
};
function init_expanders () {
// To avoid hitting visitors with a wall of text, we initially hide the
// "Year in Review" and similar text inside expandable <details>
// sections. If the URL fragment references one of these sections, we open
// that section.
let details = qs('details' + window.location.hash) // eg. #WritingAndSpeaking
if (window.location.hash && details) {
details.open = true;
details.scrollIntoView();
}
// Exable convenient "expand all" link to expand/hide all sections at once.
qsa('.expander').forEach(function(expander) {
expander.innerHTML = expander.dataset['expandLinkText'];
expander.addEventListener('click', function() {
let details_elements = qsa('.expandable-section details');
let some_closed = Array.from(details_elements).some(el => !el.open);
details_elements.forEach(function(el) {
el.open = some_closed;
});
});
});
}
init_sustainer_form_validation();
init_sustainer_type_switching();
init_expanders();
|