]> gitweb.factorcode.org Git - factor.git/commitdiff
http.server.static: add sorts on columns 1671/head
authorJon Harper <jon.harper87@gmail.com>
Sun, 26 Jun 2016 18:35:25 +0000 (20:35 +0200)
committerJon Harper <jon.harper87@gmail.com>
Sun, 3 Jul 2016 21:21:19 +0000 (23:21 +0200)
basis/http/server/static/static.factor

index 6a0ce7da62a3e7bf6fa81aafe0a472cd97dfd437..8469e90f32da8828ba1f9bb9205102eea87bb024 100644 (file)
@@ -9,7 +9,7 @@ sorting logging calendar.format accessors splitting io io.files
 io.files.info io.directories io.pathnames io.encodings.binary
 fry xml.entities destructors urls html xml.syntax
 html.templates.fhtml http http.server http.server.responses
-http.server.redirection xml.writer locals ;
+http.server.redirection xml.writer locals combinators ;
 QUALIFIED: sets
 
 TUPLE: file-responder root hook special index-names allow-listings ;
@@ -67,8 +67,8 @@ TUPLE: file-responder root hook special index-names allow-listings ;
         </tr>
     XML] ;
 
-: file>html ( name -- xml )
-    dup link-info [
+: file>html ( name infos -- xml )
+    [
         dup directory?
         [ drop "/" append "-" ]
         [ size>> number>string ] if
@@ -83,13 +83,13 @@ TUPLE: file-responder root hook special index-names allow-listings ;
 : listing-title ( -- title )
     url get [ path>> "Index of " prepend ] [ "" ] if* ;
 
-:: listing-html-template ( title listing ?parent -- xml )
+:: listing-html-template ( title listing ?parent CO-N CO-M CO-S -- xml )
     [XML <h1><-title-></h1>
         <table>
             <tr>
-                <th>Name</th>
-                <th>Last modified</th>
-                <th>Size</th>
+                <th><a href=<-CO-N->>Name</a></th>
+                <th><a href=<-CO-M->>Last modified</a></th>
+                <th><a href=<-CO-S->>Size</a></th>
             </tr>
             <tr><th colspan="5"><hr/></th></tr>
             <-?parent->
@@ -98,11 +98,55 @@ TUPLE: file-responder root hook special index-names allow-listings ;
         </table>
     XML] ;
 
+: sort-column ( -- column ) params get "C" of "N" or ;
+
+: sort-order ( -- order ) params get "O" of "A" or ;
+
+: sort-asc? ( -- ? ) sort-order "A" = ;
+
+: toggle-order ( order -- order' ) "A" = "D" "A" ? ;
+
+: ?toggle-sort-order ( col current-col -- order )
+    = [ sort-order toggle-order ] [ "A" ] if ;
+
+: sort-orders ( -- CO-N CO-M CO-S )
+    "N" "M" "S" sort-column [
+        [ drop "?C=" ";O=" surround ]
+        [ ?toggle-sort-order ] 2bi append
+    ] curry tri@ ;
+
+: listing-sort-with ( seq quot: ( elt -- key ) -- sortedseq )
+    sort-with sort-asc? [ reverse ] unless ; inline
+
+: sort-with-name ( {file,info} -- sorted )
+    [ first ] listing-sort-with ;
+
+: sort-with-modified ( {file,info} -- sorted )
+    [ second modified>> ] listing-sort-with ;
+
+: size-without-directories ( info -- size )
+    dup directory? [ drop -1 ] [ size>> ] if ;
+
+: sort-with-size ( {file,info} -- sorted )
+    [ second size-without-directories ] listing-sort-with ;
+
+: sort-listing ( zipped-files-infos -- sorted )
+    sort-column {
+        { "M" [ sort-with-modified ] }
+        { "S" [ sort-with-size ] }
+        [ drop sort-with-name ]
+    } case ; inline
+
+: zip-files-infos ( files -- zipped )
+    dup [ link-info ] map zip ;
+
 : listing ( path -- seq-xml )
-    [ natural-sort [ file>html ] map ] with-directory-files ;
+    [
+        zip-files-infos sort-listing [ first2 file>html ] map
+    ] with-directory-files ;
 
 : listing-body ( title path -- xml )
-    listing ?parent-dir-link listing-html-template ;
+    listing ?parent-dir-link sort-orders listing-html-template ;
 
 : directory>html ( path -- xml )
     [ listing-title f over ] dip listing-body simple-page ;