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
|
|
|
|
*/
|
|
|
|
|
2017-01-08 23:23:51 +00:00
|
|
|
var NEW_AMOUNT_EVENT = 'conservancy:newamount';
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-09-09 19:16:46 +00:00
|
|
|
var supportTypeSelector = function(supportTypeHash) {
|
2017-01-01 15:53:58 +00:00
|
|
|
return $(supportTypeHash + "Selector");
|
2016-09-09 19:16:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
var $window = $(window);
|
|
|
|
|
|
|
|
$window.load(function() {
|
2016-12-29 15:33:22 +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
|
|
|
var $selectorLink = supportTypeSelector(window.location.hash);
|
|
|
|
if ($selectorLink.length > 0) {
|
|
|
|
$window.scrollTop($selectorLink.offset().top);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-12-02 16:27:04 +00:00
|
|
|
$(document).ready(function() {
|
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:
|
|
|
|
var $formCorrectionNeeded = $('#form-correction-needed');
|
2017-01-01 21:03:03 +00:00
|
|
|
$formCorrectionNeeded.addClass('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
|
|
|
|
2017-01-08 23:23:51 +00:00
|
|
|
$('form.supporter-form').each(function(index, form) {
|
|
|
|
var $amountInput = $('input[type=number]', form).first();
|
2017-01-09 21:56:42 +00:00
|
|
|
var $amountError = $('.form-error', $amountInput.parents('.supporter-form-input'));
|
2017-01-08 23:23:51 +00:00
|
|
|
|
|
|
|
$amountError.on(NEW_AMOUNT_EVENT, function(event, amountData) {
|
|
|
|
var isValid = amountData.newAmount >= amountData.minAmount;
|
|
|
|
if (amountData.oldAmount === undefined) {
|
|
|
|
if (isValid) {
|
|
|
|
$amountError.addClass('hidden');
|
|
|
|
} else {
|
|
|
|
flipClass($amountInput[0], 'valid', 'invalid');
|
|
|
|
$amountError.removeClass('hidden');
|
|
|
|
}
|
|
|
|
} else if (isValid) {
|
|
|
|
flipClass($amountInput[0], 'invalid', 'valid');
|
|
|
|
$amountError.fadeOut();
|
|
|
|
} else if (amountData.oldAmount >= amountData.minAmount) {
|
|
|
|
flipClass($amountInput[0], 'valid', 'invalid');
|
|
|
|
$amountError.fadeIn();
|
2017-01-01 21:03:03 +00:00
|
|
|
}
|
2017-01-08 23:23:51 +00:00
|
|
|
});
|
2016-12-31 22:43:18 +00:00
|
|
|
|
2017-01-08 23:23:51 +00:00
|
|
|
$amountInput.on('input', function(event) {
|
|
|
|
event.target.classList.remove('invalid');
|
|
|
|
}).on('focusout', function(event) {
|
|
|
|
var amountInput = event.target;
|
|
|
|
var amountData = buildAmountData(amountInput);
|
|
|
|
$amountError.trigger(NEW_AMOUNT_EVENT, amountData);
|
|
|
|
amountInput.dataset.oldAmount = amountData.newAmount;
|
|
|
|
}).trigger('focusout');
|
|
|
|
|
|
|
|
$(form).on('submit', function(event) {
|
|
|
|
var amountData = buildAmountData($amountInput[0]);
|
|
|
|
if (amountData.newAmount >= amountData.minAmount) {
|
|
|
|
$formCorrectionNeeded.addClass('hidden');
|
|
|
|
} else {
|
|
|
|
$formCorrectionNeeded.removeClass('hidden')
|
|
|
|
.css("font-weight", "bold").css("font-size", "150%");
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
});
|
2015-01-20 21:07:19 +00:00
|
|
|
});
|
2014-12-02 18:30:18 +00:00
|
|
|
|
2016-09-09 16:10:41 +00:00
|
|
|
var selectSupportType = function(event) {
|
|
|
|
var $selectedLink = $(event.target);
|
|
|
|
$(".supporter-type-selector a").removeClass("supporter-type-selector-selected");
|
|
|
|
$selectedLink.addClass("supporter-type-selector-selected");
|
2016-12-31 23:19:49 +00:00
|
|
|
$(".supporter-type-selection").hide();
|
2020-11-25 15:45:38 +00:00
|
|
|
$(event.target.hash).show();
|
2016-12-31 22:43:18 +00:00
|
|
|
$formCorrectionNeeded.addClass('hidden');
|
2016-09-09 18:35:15 +00:00
|
|
|
return false;
|
2016-09-09 16:10:41 +00:00
|
|
|
};
|
|
|
|
$(".supporter-type-selector a").bind("click", selectSupportType);
|
|
|
|
|
|
|
|
var selectSupportTypeFromHash = function() {
|
2016-09-09 18:35:15 +00:00
|
|
|
return supportTypeSelector(window.location.hash).click();
|
2016-09-09 16:10:41 +00:00
|
|
|
};
|
2016-09-09 18:35:15 +00:00
|
|
|
$window.bind("hashchange", selectSupportTypeFromHash);
|
2017-01-01 15:53:58 +00:00
|
|
|
if (selectSupportTypeFromHash().length === 0) {
|
2016-09-09 18:35:15 +00:00
|
|
|
supportTypeSelector("#annual").click();
|
|
|
|
}
|
2020-11-25 16:43:57 +00:00
|
|
|
var want_id = window.location.hash.substr(1) || "do not match any id";
|
2020-11-26 17:21:44 +00:00
|
|
|
var $expandable_counts = [];
|
|
|
|
// First, build a count of expandable div sections
|
|
|
|
$('div.expandable-section').each(function(index, section) {
|
|
|
|
var $ourid = $(section).attr('id');
|
|
|
|
if ($ourid == undefined) { $ourid = "__global"; }
|
|
|
|
$expandable_counts[$ourid] = 0;
|
|
|
|
});
|
2020-11-25 18:47:40 +00:00
|
|
|
$('div[data-read-more]').each(function(index, readmore) {
|
2020-11-25 16:32:07 +00:00
|
|
|
var $readmore = $(readmore)
|
2020-11-25 18:47:40 +00:00
|
|
|
var $header = $readmore.prev('h3');
|
|
|
|
if ($header.length && $header[0].id === want_id) {
|
|
|
|
// Do nothing, leave it alone
|
2020-11-25 16:32:07 +00:00
|
|
|
} else {
|
2020-11-26 17:21:44 +00:00
|
|
|
var $ourid = $readmore.closest(".expandable-section" ).attr('id');
|
|
|
|
if ($ourid == undefined) { $ourid = "__global"; }
|
|
|
|
|
|
|
|
// Set up the link for this specific section using the text from
|
|
|
|
// the data-read-more atrribute on the div specific to this section
|
2020-11-25 18:47:40 +00:00
|
|
|
var $linkpara = $('<p><a class="read-more"></a></p>');
|
|
|
|
var $readlink = $linkpara.children('a');
|
|
|
|
$readlink.append($readmore.data('read-more'));
|
|
|
|
$readlink.on('click', function(event) {
|
2020-11-26 17:21:44 +00:00
|
|
|
// When clicked, we'll restore the actual text and fade it in
|
|
|
|
// quickly, and also see if there are any remaining
|
|
|
|
// expandable sections left in this particular expandable
|
|
|
|
// section. If none are left, make the "Expand all" button
|
|
|
|
// (which lives in an 'a' anchor of the class 'expander') disappear.
|
2020-11-25 18:47:40 +00:00
|
|
|
$linkpara.replaceWith($readmore);
|
|
|
|
$readmore.fadeIn('fast');
|
2020-11-26 17:21:44 +00:00
|
|
|
$expandable_counts[$ourid]--;
|
|
|
|
if ($expandable_counts[$ourid] <= 0) {
|
|
|
|
$readmore.closest(".expandable-section" )
|
|
|
|
.children('a.expander').each(function(index, element){
|
|
|
|
$(element).fadeOut('slow');
|
|
|
|
})
|
|
|
|
}
|
2020-11-25 16:32:07 +00:00
|
|
|
});
|
2020-11-25 18:47:40 +00:00
|
|
|
$readmore.hide().replaceWith($linkpara);
|
2020-11-26 17:21:44 +00:00
|
|
|
$expandable_counts[$ourid]++;
|
2020-11-25 16:32:07 +00:00
|
|
|
}
|
|
|
|
});
|
2020-11-26 17:21:44 +00:00
|
|
|
// Final two each's enable the "Expand All" link.
|
2020-11-26 03:15:11 +00:00
|
|
|
$('a[data-expand-link-text]').each(function(index, element) {
|
|
|
|
var $element = $(element);
|
|
|
|
$element.append($element.data('expand-link-text'));
|
|
|
|
});
|
|
|
|
$('.expandable-section').each(function(index) {
|
2023-11-21 23:38:37 +00:00
|
|
|
var $expandlink = $(this).find('a.expander');
|
2020-11-26 03:15:11 +00:00
|
|
|
var $ourexpandablesection = $(this);
|
|
|
|
$expandlink.on('click', function(event) {
|
|
|
|
$expandlink.fadeOut('slow');
|
2023-11-22 11:15:10 +00:00
|
|
|
$ourexpandablesection.find('.read-more').each(function(index) { $(this).click(); });
|
|
|
|
});
|
2020-11-26 03:15:11 +00:00
|
|
|
});
|
2014-12-02 23:25:42 +00:00
|
|
|
});
|