>From 2b9542cfcc180a6b5fc3b0ed28217eb0896c0d2b Mon Sep 17 00:00:00 2001 From: Alex Sassmannshausen Date: Sun, 11 Aug 2013 21:40:29 +0200 Subject: [PATCH 3/3] list-packages.scm: cause description to be shown, and 'void' links to not be shown when JS is disabled. * build-aux/list-packages.scm (package->sxml package): no longer use package-synopsis as link as we cannot assume the JavaScript in the link will work (no JS safety net); instead use a span. (package->sxml package): remove default invisibility of package descriptions. (package->sxml package): call (show_hide-grouper description-id) after each package is processed to collect the package description-ids to be made invisible. (show_hide-grouper id-or-force): new function; collect n package IDs, and to insert a JS call to bulk_show_hide with the package IDs to be processed into the xml. (packages->sxml packages): call (show_hide-grouper #f) after (map package->sxml packages) returns to force the remaining package IDs to be processed, even if n have not been reached yet. (insert-js): (show_hide(idThing)): add general clause to check whether working, DOM conforming JS exists; introduce thingLink variable to allow its text to be modified depending on the show/hide package description state of a given package. (insert-js): (prep(idThing)): new JS function that generates a link in the span containing package-synopsis. New link provides show/hide package-description functionality. (insert-js): (bulk_show_hide()): new JS function that is able to process (first call prep, then show_hide on a package-ID) any number of package-IDs sequentially (for now defaults to 15, as provided by (show_hide-grouper id-or-force)). --- build-aux/list-packages.scm | 72 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 63 insertions(+), 9 deletions(-) diff --git a/build-aux/list-packages.scm b/build-aux/list-packages.scm index b598b97..2aa6194 100755 --- a/build-aux/list-packages.scm +++ b/build-aux/list-packages.scm @@ -102,14 +102,8 @@ exec guile -l "$0" \ (title "Link to the Guix package source code")) ,(package-name package) " " ,(package-version package))) - (td (@ (colspan "2") (height "0")) - (a (@ (href "javascript:void(0)") - (title "show/hide package description") - (onClick ,(format #f "javascript:show_hide('~a')" - description-id))) - ,(package-synopsis package)) - (div (@ (id ,description-id) - (style "display: none;")) + (td (span (strong ,(package-synopsis package))) + (div (@ (id ,description-id)) ,(match (package-logo (package-name package)) ((? string? url) `(img (@ (src ,url) @@ -121,7 +115,35 @@ exec guile -l "$0" \ (a (@ (href ,(package-home-page package)) (title "Link to the package's website")) ,(package-home-page package)) - ,(status package)))))) + ,(status package)) + ,(show_hide-grouper description-id))))) + + (define show_hide-grouper + (let ((lid '()) + (group-counter 15)) + (lambda (id-or-force) + "Collect GROUP-COUNTER element IDs, then apply them to +bulk_show_hide. Pass ID-OR-FORCE #f to apply collected IDs to bulk_show_hide +even when GROUP-COUNTER IDs have not been collected." + (letrec + ((lid->js-argl + (lambda (l separator) + "Parse a list into JavaScript style arguments." + (if (null? l) + "" + (string-append separator "'" (car l) "'" + (lid->js-argl (cdr l) ", ")))))) + (cond ((or (not id-or-force) + (> (length lid) group-counter)) + (let ((t lid)) + (if id-or-force + (set! lid (list id-or-force)) + (set! lid '())) + `(script (@ (type "text/javascript")) + ,(format #f "bulk_show_hide(~a)" + (lid->js-argl t ""))))) + (else (set! lid (cons id-or-force lid)) + "")))))) (define (packages->sxml packages) "Return an HTML page as SXML describing PACKAGES." @@ -145,6 +167,7 @@ exec guile -l "$0" \ (th "Package version") (th "Package details")) ,@(map package->sxml packages)) + ,(show_hide-grouper #f) (a (@ (href "#intro") (title "Back to top.") (id "top")) @@ -209,14 +232,45 @@ color:#fff; // license: CC0 function show_hide(idThing) { + if(document.getElementById && document.createTextNode) { var thing = document.getElementById(idThing); + /* Used to change the link text, depending on whether description is + collapsed or expanded */ + var thingLink = thing.previousSibling.lastChild.firstChild; if (thing) { if (thing.style.display == \"none\") { thing.style.display = \"\"; + thingLink.data = 'Collapse'; } else { thing.style.display = \"none\"; + thingLink.data = 'Expand'; } } + } +} +/* Add controllers used for collapse/expansion of package descriptions */ +function prep(idThing) +{ + var tdThing = document.getElementById(idThing).parentNode; + if (tdThing) { + var aThing = tdThing.firstChild.appendChild(document.createElement('a')); + aThing.setAttribute('href', 'javascript:void(0)'); + aThing.setAttribute('title', 'show/hide package description'); + aThing.appendChild(document.createTextNode('Expand')); + aThing.onclick=function(){show_hide(idThing);}; + /* aThing.onkeypress=function(){show_hide(idThing);}; */ + } +} +/* Take n element IDs, prepare them for javascript enhanced +display and hide the IDs by default. */ +function bulk_show_hide() +{ + if(document.getElementById && document.createTextNode) { + for(var i=0; i")) -- 1.7.10.4