>From ed5771c1d5a18c31634ef075d6fe1eee70b32d6b Mon Sep 17 00:00:00 2001 From: Alex Sassmannshausen Date: Mon, 26 Aug 2013 15:55:28 +0200 Subject: [PATCH 2/2] list-packages: Progressive Enhancement approach to JS. * build-aux/list-packages.scm (package->sxml): Modify formal params, docstring. Introduce logic for fold-values process. (insert-tr): Moved sxml package table-row generation to new function; remove elements and JS function calls. These are created through JS (prep_pkg_descs). Add insert-js-call for every 15th package, and the last. (insert-js-call): New function. (packages->sxml): Change map to fold values; add init params. (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 | 133 ++++++++++++++++++++++++++++++++----------- 1 file changed, 101 insertions(+), 32 deletions(-) diff --git a/build-aux/list-packages.scm b/build-aux/list-packages.scm index 3e798fc..b32b595 100755 --- a/build-aux/list-packages.scm +++ b/build-aux/list-packages.scm @@ -29,6 +29,7 @@ exec guile -l "$0" \ #:use-module (guix gnu-maintenance) #:use-module (gnu packages) #:use-module (sxml simple) + #:use-module (sxml fold) #:use-module (web uri) #:use-module (ice-9 match) #:use-module (srfi srfi-1) @@ -48,8 +49,10 @@ exec guile -l "$0" \ (equal? (gnu-package-name package) name)) gnu)))) -(define (package->sxml package) - "Return HTML-as-SXML representing PACKAGE." +(define (package->sxml package previous lid l) + "Return built HTML-as-SXML representing PACKAGEs, collected in +PREVIOUS. Include a call to the JavaScript prep_pkg_descs function, every time +the length of LID (increasing) is 15 or when L (decreasing) is 1." (define (source-url package) (let ((loc (package-location package))) (and loc @@ -92,37 +95,72 @@ exec guile -l "$0" \ (and=> (lookup-gnu-package name) gnu-package-logo)) + (define (insert-tr description-id js?) + (define (insert-js-call lid) + "Return an sxml call to prep_pkg_descs, with up to 15 elements of lid as +formal parameters." + (define (lid->js-argl) + "Parse a Scheme list into a JavaScript style arguments list." + (define (helper l) + (if (null? l) + "" ; No more args, done with list. + (string-append ", '" (car l) "'" ; Append a further arg. + (helper (cdr l))))) + + (string-append "'" (car lid) "'" ; Initial arg. + (helper (cdr lid)))) + + `(script (@ (type "text/javascript")) ; Insert JS call + ,(format #f "prep_pkg_descs(~a)" + (lid->js-argl)))) + + (let ((lid (cons description-id lid))) + `(tr (td ,(if (gnu-package? package) + `(img (@ (src "/graphics/gnu-head-mini.png") + (alt "Part of GNU") + (title "Part of GNU"))) + "")) + (td (a (@ (href ,(source-url package)) + (title "Link to the Guix package source code")) + ,(package-name package) " " + ,(package-version package))) + (td (span ,(package-synopsis package)) + (div (@ (id ,description-id)) + ,(match (package-logo (package-name package)) + ((? string? url) + `(img (@ (src ,url) + (height "35") + (class "package-logo") + (alt ("Logo of " ,(package-name package)))))) + (_ #f)) + (p ,(package-description package)) + ,(license package) + (a (@ (href ,(package-home-page package)) + (title "Link to the package's website")) + ,(package-home-page package)) + ,(status package) + ,(if js? + (insert-js-call lid) + "")))))) + (let ((description-id (symbol->string (gensym (package-name package))))) - `(tr (td ,(if (gnu-package? package) - `(img (@ (src "/graphics/gnu-head-mini.png") - (alt "Part of GNU") - (title "Part of GNU"))) - "")) - (td (a (@ (href ,(source-url package)) - (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;")) - ,(match (package-logo (package-name package)) - ((? string? url) - `(img (@ (src ,url) - (height "35") - (class "package-logo") - (alt ("Logo of " ,(package-name package)))))) - (_ #f)) - (p ,(package-description package)) - ,(license package) - (a (@ (href ,(package-home-page package)) - (title "Link to the package's website")) - ,(package-home-page package)) - ,(status package)))))) + (cond ((= l 1) ; Last package in packages + (reverse ; Fold has reversed packages + (cons (insert-tr description-id 'js) ; Only return sxml + previous))) + ((= (length lid) 15) ; Time for a JS call + (values + (cons (insert-tr description-id 'js) ; Prefix new sxml + previous) + '() ; Reset lid + (1- l))) ; Reduce l + (else ; Insert another row, and build lid + (values + (cons (insert-tr description-id #f) ; Prefix new sxml + previous) + (cons description-id lid) ; Pass along updated lid + (1- l)))))) ; Reduce l (define (packages->sxml packages) "Return an HTML page as SXML describing PACKAGES." @@ -145,7 +183,7 @@ exec guile -l "$0" \ (tr (th "GNU?") (th "Package version") (th "Package details")) - ,@(map package->sxml packages)) + ,@(fold-values package->sxml packages '() '() (length packages))) (a (@ (href "#intro") (title "Back to top.") (id "top")) @@ -239,14 +277,45 @@ a#top:hover, a#top:focus { // 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