From 8a9f796eb1f7f0e929914fe5789534900ecdc7ca Mon Sep 17 00:00:00 2001 From: Ben Sturmfels Date: Thu, 9 May 2024 13:53:50 +1000 Subject: [PATCH] Replace complex expanding JS with native
elements Have retained the "expand all" behaviour - just with less code. --- conservancy/static/js/supporter-page.js | 97 +++++++--------------- conservancy/templates/supporter/index.html | 46 ++++------ 2 files changed, 46 insertions(+), 97 deletions(-) diff --git a/conservancy/static/js/supporter-page.js b/conservancy/static/js/supporter-page.js index dea7126d..ad0f27f0 100644 --- a/conservancy/static/js/supporter-page.js +++ b/conservancy/static/js/supporter-page.js @@ -30,21 +30,13 @@ var supportTypeSelector = function(supportTypeHash) { return $(supportTypeHash + "Selector"); }; -var $window = $(window); +/* 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"; +} -$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() { +function init_sustainer_forms () { // 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: @@ -108,63 +100,34 @@ $(document).ready(function() { var selectSupportTypeFromHash = function() { return supportTypeSelector(window.location.hash).click(); }; - $window.bind("hashchange", selectSupportTypeFromHash); + $(window).bind("hashchange", selectSupportTypeFromHash); if (selectSupportTypeFromHash().length === 0) { supportTypeSelector("#annual").click(); } - var want_id = window.location.hash.substr(1) || "do not match any id"; - 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; - }); - $('div[data-read-more]').each(function(index, readmore) { - var $readmore = $(readmore) - var $header = $readmore.prev('h3'); - if ($header.length && $header[0].id === want_id) { - // Do nothing, leave it alone - } else { - 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 - var $linkpara = $('

'); - var $readlink = $linkpara.children('a'); - $readlink.append($readmore.data('read-more')); - $readlink.on('click', function(event) { - // 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. - $linkpara.replaceWith($readmore); - $readmore.fadeIn('fast'); - $expandable_counts[$ourid]--; - if ($expandable_counts[$ourid] <= 0) { - $readmore.closest(".expandable-section" ) - .children('a.expander').each(function(index, element){ - $(element).fadeOut('slow'); - }) - } +function init_expanders () { + // Conservancy does a lot each year, so we initially hide the "Year in + // Review" text insideexpandable
sections to avoid a wall of + // text. If the URL fragment matches one of our expandable sections, we + // automatically expand it - that's a direct link. + 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; }); - $readmore.hide().replaceWith($linkpara); - $expandable_counts[$ourid]++; - } - }); - // Final two each's enable the "Expand All" link. - $('a[data-expand-link-text]').each(function(index, element) { - var $element = $(element); - $element.append($element.data('expand-link-text')); - }); - $('.expandable-section').each(function(index) { - var $expandlink = $(this).find('a.expander'); - var $ourexpandablesection = $(this); - $expandlink.on('click', function(event) { - $expandlink.fadeOut('slow'); - $ourexpandablesection.find('.read-more').each(function(index) { $(this).click(); }); }); }); -}); +} + +init_sustainer_forms(); +init_expanders(); diff --git a/conservancy/templates/supporter/index.html b/conservancy/templates/supporter/index.html index a35667b6..8cf7514e 100644 --- a/conservancy/templates/supporter/index.html +++ b/conservancy/templates/supporter/index.html @@ -80,10 +80,10 @@
  • Hiring team members to get projects like Reproducible Builds to continue pushing the forefront of software reproducibility
  • - +

    -

    Our Year in Review

    -
    +
    + Our Year in Review

    This has been a big year for Software Freedom Conservancy in our tireless efforts to promote ethical technology, increase diversity and inclusion in @@ -97,7 +97,7 @@ and while we finalize details for next year, we hope to see you there to join us in community!

    SFC lawyers posing outside at the courthouse“ -

    SFC lawyers after recent Vizio case- CC BY-SA 4.0

    +

    SFC lawyers after recent Vizio case- CC BY-SA 4.0

    Our lawsuit against Vizio— the first lawsuit of its kind to seek third party beneficiary rights to the @@ -150,12 +150,10 @@ account will be approved.

    Thumbnail of video showing a tree and Software Freedom
    - - - -

    New staff!

    -
    +
    +
    +New staff!

    SFC hired two additional employees this year! General Counsel Rick Sanders joins the team to help with our continued legal needs. Rick has over 20 years experience as a intellectual-property litigator. His expertise has been @@ -166,17 +164,15 @@ Linux and free software, Paul's belief in the power of free software to help people engage with technology in non-exploitative ways fits in perfectly to support our growing organizational needs and mission. Helping make sure we can provide solid FOSS replacements to proprietary technologies for all of us.

    +
    - - -

    Writing and Speaking

    -
    - +
    + Writing and Speaking

    Our staff has been presenting and speaking about software freedom all year. Our Executive Director Karen Sandler received an honorary doctorate from Katholieke Universiteit Leuven for her incredible work in FOSS leadership, and her advocacy and pursuit of software freedom and rights for all. -In November she spoke at SFSCON about "The History of, and Path forward for, Copyleft and the GPL".

    + In November she spoke at SFSCON about "The History of, and Path forward for, Copyleft and the GPL".

    -
    - -

    Highlights from some of our projects

    -
    - +
    + Highlights from some of our projects

    We've raised, administered and/or facilitated $1.8 million to improve software freedom directly! This includes contractors, interns and students, administrators, and grants for creation, distribution and maintenance of free @@ -273,12 +266,9 @@ code related and process related talks. The

    Help us Continue this Work

    @@ -298,11 +288,7 @@ reach for reproducibility.

    Software Freedom Conservancy continues to be the beacon of change for software freedom that the world needs. Please consider donating now!

    - - -
    -