>From 145d2c1dfd05c8c7182df4d6383629bfee070ba9 Mon Sep 17 00:00:00 2001 From: Alex Sassmannshausen Date: Tue, 20 Aug 2013 00:26:37 +0200 Subject: [PATCH 1/3] list-packages: Progressive Enhancement approach to JS. * build-aux/list-packages.scm (package->sxml): Remove elements and JS function calls. These are created through JS (prep_pkg_descs). Add call to insert-prep_pkg_descs for every package in the table. (insert-prep_pkg_descs): New function. (packages->sxml): Add final call to insert-prep_pkg_descs. (insert-js): show_hide: add compatibility check, introduce, use thingLink. prep: new JS function. bulk_show_hide: new JS function. --- build-aux/list-packages.scm | 80 ++++++++++++++++++++++++++++++++++++++----- 1 file changed, 72 insertions(+), 8 deletions(-) diff --git a/build-aux/list-packages.scm b/build-aux/list-packages.scm index 9cb07c1..1964c82 100755 --- a/build-aux/list-packages.scm +++ b/build-aux/list-packages.scm @@ -103,13 +103,8 @@ exec guile -l "$0" \ (title "Link to the Guix package source code")) ,(package-name package) " " ,(package-version package))) - (td (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 ,(package-synopsis package)) + (div (@ (id ,description-id)) ,(match (package-logo (package-name package)) ((? string? url) `(img (@ (src ,url) @@ -122,7 +117,44 @@ 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)) + ,(insert-prep_pkg_descs description-id))))) + +(define insert-prep_pkg_descs + (let ((lid '()) + (group-counter 15)) + (lambda (id) + "Collect GROUP-COUNTER element IDs, then apply them to +prep_pkg_descs. If ID is #f, force the application of collected IDs to +prep_pkg_descs even when the number of IDs is smaller than GROUP-COUNTER." + (define (insert-js-call) + "Return an sxml call to prep_pkg_descs." + (define (lid->js-argl) + "Parse a Scheme list into a JavaScript style arguments list." + (define (helper l) + (if (null? l) + (begin + (set! lid '()) ; lid, now processed, safe to reset. + "") + (string-append ", '" (car l) "'" ; further args. + (helper (cdr l))))) + (string-append "'" (car lid) "'" ; initial arg. + (helper (cdr lid)))) + `(script (@ (type "text/javascript")) + ,(format #f "prep_pkg_descs(~a)" + (lid->js-argl)))) + (if id + (begin + ;; If ID is not false, then we add ID to LID. + (set! lid (cons id lid)) + ;; If LID is now equal to GROUP-COUNTER it is time to insert a + ;; call to prep_pkg_descs in the HTML. + (if (= (length lid) group-counter) + (insert-js-call) + "")) + ;; if ID is false then we force the insert of a call to + ;; prep_pkg_descs for (< n GROUP-COUNTER) packages in the HTML. + (insert-js-call))))) (define (packages->sxml packages) "Return an HTML page as SXML describing PACKAGES." @@ -146,6 +178,7 @@ exec guile -l "$0" \ (th "Package version") (th "Package details")) ,@(map package->sxml packages)) + ,(insert-prep_pkg_descs #f) (a (@ (href "#intro") (title "Back to top.") (id "top")) @@ -210,14 +243,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 prep_pkg_descs() +{ + if(document.getElementById && document.createTextNode) { + for(var i=0; i")) -- 1.7.10.4