website/www/conservancy/static/js/supporter-page.js
2017-01-01 10:24:34 -05:00

98 lines
3.6 KiB
JavaScript

/* Copyright (C) 2012-2013 Denver Gingerich,
** Copyright (C) 2013-2014 Bradley M. Kuhn,
** Copyright (C) 2016 Brett Smith.
** License: GPLv3-or-later
** Find a copy of GPL at https://sfconservancy.org/GPLv3
*/
var flipClass = function(elem, byeClass, hiClass) {
var classList = elem.classList;
classList.remove(byeClass);
classList.add(hiClass);
}
var amountIsValid = function(amountInput) {
var value = parseFloat(amountInput.value);
var min = parseFloat(amountInput.min);
/* Is the value is a valid float, it will stringify back to itself. */
return (String(value) === amountInput.value) && (value >= min);
}
var supportTypeSelector = function(supportTypeHash) {
return $(".supporter-type-selector a[href=" + supportTypeHash + "]");
};
var $window = $(window);
$window.load(function() {
/* 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 $selectorLink = supportTypeSelector(window.location.hash);
if ($selectorLink.length > 0) {
$window.scrollTop($selectorLink.offset().top);
}
});
$(document).ready(function() {
// 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:
$('.form-error').addClass('hidden');
var $formCorrectionNeeded = $('#form-correction-needed');
$('form.supporter-form input[type=number]').on('focusout', function(event) {
var amountInput = event.target;
var wasValid = !amountInput.classList.contains('invalid');
var isValid = amountIsValid(amountInput);
if (!wasValid && isValid) {
flipClass(amountInput, 'invalid', 'valid');
$('.form-error', amountInput.parentNode).addClass('hidden');
} else if (wasValid && !isValid) {
flipClass(amountInput, 'valid', 'invalid');
$('.form-error', amountInput.parentNode).removeClass('hidden');
}
});
$('form.supporter-form').on('submit', function(event) {
if (amountIsValid($('input[name=amount]', event.target)[0])) {
$formCorrectionNeeded.addClass('hidden');
} else {
$formCorrectionNeeded.removeClass('hidden')
.css("font-weight", "bold").css("font-size", "150%");
event.preventDefault();
}
});
var selectSupportType = function(event) {
var $selectedLink = $(event.target);
$(".supporter-type-selector a").removeClass("supporter-type-selector-selected");
$selectedLink.addClass("supporter-type-selector-selected");
$(".supporter-type-selection").each(function(index, element) {
var $element = $(element);
if (event.target.href.endsWith("#" + element.id)) {
$element.show();
} else {
$element.hide();
}
});
$formCorrectionNeeded.addClass('hidden');
return false;
};
$(".supporter-type-selector a").bind("click", selectSupportType);
var selectSupportTypeFromHash = function() {
return supportTypeSelector(window.location.hash).click();
};
$window.bind("hashchange", selectSupportTypeFromHash);
var $selectorLink = selectSupportTypeFromHash();
if (parseFloat($("form#annual").get(0).dataset.upgradeFromAmount) > 0) {
supportTypeSelector("#annual").click();
$(".supporter-type-selector").hide();
}
else if ($selectorLink.length === 0) {
supportTypeSelector("#annual").click();
}
});