2014-12-02 16:27:04 +00:00
|
|
|
/* Copyright (C) 2012-2013 Denver Gingerich,
|
2020-11-26 03:15:11 +00:00
|
|
|
** Copyright (C) 2013-2014, 2020 Bradley M. Kuhn,
|
2020-11-25 16:32:07 +00:00
|
|
|
** Copyright (C) 2016, 2020 Brett Smith.
|
2014-12-03 21:55:17 +00:00
|
|
|
** License: GPLv3-or-later
|
2014-12-02 16:27:04 +00:00
|
|
|
** Find a copy of GPL at https://sfconservancy.org/GPLv3
|
|
|
|
*/
|
|
|
|
|
2024-05-07 01:00:14 +00:00
|
|
|
"use strict";
|
|
|
|
|
2016-12-31 22:43:18 +00:00
|
|
|
var flipClass = function(elem, byeClass, hiClass) {
|
|
|
|
var classList = elem.classList;
|
|
|
|
classList.remove(byeClass);
|
|
|
|
classList.add(hiClass);
|
|
|
|
}
|
|
|
|
|
2017-01-08 23:23:51 +00:00
|
|
|
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;
|
2016-12-31 22:43:18 +00:00
|
|
|
}
|
|
|
|
|
2024-05-09 03:53:50 +00:00
|
|
|
/* 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";
|
|
|
|
}
|
2016-09-09 19:16:46 +00:00
|
|
|
|
2024-05-09 05:43:53 +00:00
|
|
|
var formCorrectionNeeded = qs('#form-correction-needed');
|
2024-05-09 04:10:10 +00:00
|
|
|
|
2024-05-09 12:12:35 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-09 04:06:39 +00:00
|
|
|
function init_sustainer_form_validation () {
|
Start errors in shown state for non-Javascript.
Since the error messages have important information, and since the
Javascript code is the only "enforcer" of the minimum donation, the
errors really should be displayed by default if the browser is not
Javascript-capable. This change does that, but also toggles the state
back so that errors are not shown until needed in Javascript-capable
browsers.
I believe this still fits graceful degradation, since browsers without
Javascript and CSS were already showing the errors anyway, so now the
only real change is that everyone sees the errors by default.
It *might* make sense to not show the errors in red in non-Javascript
browsers (i.e., make the default CSS color black for the form-error-show
class, and then change it to red in the Javascript). I didn't make that
so, because it's not clear to me that's right, and we *do* want to draw
attention to the errors lest people become a supporter below the
minimum (which has happened once already -- that precipitated this
change).
I'm still annoyed that PayPal doesn't provide a "minimum but no maximum"
variable donation box of its own, which would solve this problem
outright.
2014-12-05 15:07:27 +00:00
|
|
|
// Forms start in "invalid" form, with the errors shown, so that
|
|
|
|
// non-Javascript users see the errors by default and know what they must
|
2016-12-31 22:43:18 +00:00
|
|
|
// enter. Now we hide those for JavaScript users:
|
2024-05-09 05:43:53 +00:00
|
|
|
formCorrectionNeeded.classList.add('hidden');
|
Start errors in shown state for non-Javascript.
Since the error messages have important information, and since the
Javascript code is the only "enforcer" of the minimum donation, the
errors really should be displayed by default if the browser is not
Javascript-capable. This change does that, but also toggles the state
back so that errors are not shown until needed in Javascript-capable
browsers.
I believe this still fits graceful degradation, since browsers without
Javascript and CSS were already showing the errors anyway, so now the
only real change is that everyone sees the errors by default.
It *might* make sense to not show the errors in red in non-Javascript
browsers (i.e., make the default CSS color black for the form-error-show
class, and then change it to red in the Javascript). I didn't make that
so, because it's not clear to me that's right, and we *do* want to draw
attention to the errors lest people become a supporter below the
minimum (which has happened once already -- that precipitated this
change).
I'm still annoyed that PayPal doesn't provide a "minimum but no maximum"
variable donation box of its own, which would solve this problem
outright.
2014-12-05 15:07:27 +00:00
|
|
|
|
2024-05-09 05:43:53 +00:00
|
|
|
qsa('form.supporter-form').forEach(function(form) {
|
2024-05-09 12:12:35 +00:00
|
|
|
var amountInput = qs('input[type=number]', form);
|
2024-05-09 05:43:53 +00:00
|
|
|
var amountError = qs('.supporter-form-input .form-error', form);
|
2016-12-31 22:43:18 +00:00
|
|
|
|
2024-05-09 12:12:35 +00:00
|
|
|
function amount_change_handler () {
|
2017-01-08 23:23:51 +00:00
|
|
|
var amountData = buildAmountData(amountInput);
|
2024-05-09 12:12:35 +00:00
|
|
|
new_amount(amountData, amountInput, amountError);
|
2017-01-08 23:23:51 +00:00
|
|
|
amountInput.dataset.oldAmount = amountData.newAmount;
|
2024-05-09 12:12:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
amountInput.addEventListener('input', function() {
|
|
|
|
amountInput.classList.remove('invalid');
|
|
|
|
})
|
|
|
|
amountInput.addEventListener('focusout', amount_change_handler);
|
|
|
|
amount_change_handler();
|
2017-01-08 23:23:51 +00:00
|
|
|
|
2024-05-09 05:43:53 +00:00
|
|
|
form.addEventListener('submit', function(event) {
|
2024-05-09 12:12:35 +00:00
|
|
|
var amountData = buildAmountData(amountInput);
|
2017-01-08 23:23:51 +00:00
|
|
|
if (amountData.newAmount >= amountData.minAmount) {
|
2024-05-09 05:43:53 +00:00
|
|
|
formCorrectionNeeded.classList.add('hidden');
|
2017-01-08 23:23:51 +00:00
|
|
|
} else {
|
2024-05-09 05:43:53 +00:00
|
|
|
formCorrectionNeeded.classList.remove('hidden')
|
2017-01-08 23:23:51 +00:00
|
|
|
.css("font-weight", "bold").css("font-size", "150%");
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
});
|
2015-01-20 21:07:19 +00:00
|
|
|
});
|
2024-05-09 04:06:39 +00:00
|
|
|
}
|
2014-12-02 18:30:18 +00:00
|
|
|
|
2024-05-09 04:06:39 +00:00
|
|
|
function init_sustainer_type_switching () {
|
2024-05-09 12:12:35 +00:00
|
|
|
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));
|
2024-05-09 05:43:53 +00:00
|
|
|
formCorrectionNeeded.classList.add('hidden');
|
2016-09-09 18:35:15 +00:00
|
|
|
return false;
|
2016-09-09 16:10:41 +00:00
|
|
|
};
|
2024-05-09 12:12:35 +00:00
|
|
|
qsa(".supporter-type-selector a").forEach(function(el) {
|
|
|
|
el.addEventListener("click", () => selectSupportType(el));
|
|
|
|
});
|
2016-09-09 16:10:41 +00:00
|
|
|
|
2024-05-09 12:12:35 +00:00
|
|
|
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);
|
|
|
|
});
|
2024-05-09 03:53:50 +00:00
|
|
|
};
|
2020-11-26 17:21:44 +00:00
|
|
|
|
2024-05-09 03:53:50 +00:00
|
|
|
function init_expanders () {
|
2024-05-09 04:06:39 +00:00
|
|
|
// 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.
|
2024-05-09 03:53:50 +00:00
|
|
|
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;
|
2020-11-25 16:32:07 +00:00
|
|
|
});
|
2023-11-22 11:15:10 +00:00
|
|
|
});
|
2020-11-26 03:15:11 +00:00
|
|
|
});
|
2024-05-09 03:53:50 +00:00
|
|
|
}
|
|
|
|
|
2024-05-09 04:06:39 +00:00
|
|
|
init_sustainer_form_validation();
|
|
|
|
init_sustainer_type_switching();
|
2024-05-09 03:53:50 +00:00
|
|
|
init_expanders();
|