Commits

Jesper Axelsson  committed 2cada16

We now handle the gally preview better

  • Participants
  • Parent commits b610b2f

Comments (0)

Files changed (3)

File src/HtmlGenerators.hs

                                     xs
 
 
---generateTable :: (ToValue a2, ToValue a, ToValue a1, ToHtml b) => a2 -> a1 -> a -> Int -> [b] -> Html
---generateTable :: Html b => String -> String -> String -> Int -> [b] -> Html
+
 generateTable ::  String -> String -> String -> Int -> [Html] -> Html                
 generateTable tableId rowId columnId widht xs = 
     table ! A.id (toValue tableId)
 byteToString :: B.ByteString -> String
 byteToString bs = DT.unpack $ T.decodeUtf8 bs
 
+
+
 ------------------------------------------------------------------------------
 -- | Renders the front page of the sample site.
 --
 galleryImageIndex ::  Handler App App ()
 galleryImageIndex = do
     dir <- decodedString "dir"
-    imageFilePaths <- liftIO $ getGalleryImages dir
+    offset <- getOffsetParam 
+    
+    allImages <- liftIO $ getGalleryImages dir
 
-    renderBlazePage $ galleryImageIndexHtml $ 
-        case imageFilePaths of
-             [] -> H.p "Sorry, nothing here"
-             _  -> generateImageListHtml (makeFileTuple dir imageFilePaths) 
-  
+    let imageFilePaths = takePart offset picsPerPage allImages
+        imageCount = length allImages
+        nextOffset = if (offset + picsPerPage) >= imageCount then Nothing 
+                      else Just (offset + picsPerPage)
+        prevOffset = if (offset - picsPerPage) < 0 then Nothing 
+                      else Just (offset - picsPerPage)
+        content    = (case imageFilePaths of
+                             [] -> H.p "Sorry, nothing here"
+                             _  -> generateImageListHtml $ makeFileTuple dir imageFilePaths) 
+    
+--    writeText $ DT.pack $ show offset
+    renderBlazePage $ galleryImageIndexHtml nextOffset prevOffset dir content
+             
+    where
+        getOffsetParam :: (MonadSnap m) => m Int
+        getOffsetParam = fmap read (byteToString <$> fromMaybe "0" <$> getParam "offset") 
+        
+        picsPerPage = 20
+
+
 
 getGalleryImages :: String -> IO [FilePath]
 getGalleryImages dir = liftA sort (getFileNames (galleryPath ++ dir))
         fixImgPaths :: String -> [String] -> [String]
         fixImgPaths baseDir = P.map (\s -> "/" ++ baseDir ++ "/" ++ s) 
 
+
+takePart :: Int -> Int -> [a] -> [a]
+takePart off cnt lst = take cnt $ drop off lst
+
+
 ------------------------------------------------------------------------------
 -- | Handle Routes
 
           , ("/dologin"            , method POST tryLogin )
           , ("/echo/:stuff"        , echo )
           , ("/gallery"            , galleryIndex)
-          , ("/gallery/:dir"       , galleryImageIndex )
+          , ("/gallery/:dir"       , galleryImageIndex )          
+          , ("/gallery/:dir:offset", galleryImageIndex )
           , ("/gallery/:dir/:file" , galleryPage)
           , ("", serveDirectory "resources/static")
           , ("", requireUser auth showAuthError $ serveDirectory "resources/gallery") -- home to the galleries

File src/Template/GalleryImageIndexPage.hs

 --import           Prelude ($)
 --import qualified Prelude as P
 
---import qualified Data.Text as DT
+import qualified Data.Text as DT
 --import qualified Data.Text.Encoding as T
 --import qualified Data.ByteString as B
 
 --import           Control.Monad (forM_)
-
+import           Text.Blaze.Html
 import           Text.Blaze.Html5 ((!), Html)
 import qualified Text.Blaze.Html5 as H
 import           Text.Blaze.Html5.Attributes hiding (dir, content)
 import Template.MainHtml
 -- import HtmlGenerators
 
-galleryImageIndexHtml :: Html -> Html
-galleryImageIndexHtml images = 
+
+
+galleryImageIndexHtml :: Maybe Int -> Maybe Int -> String -> Html -> Html
+galleryImageIndexHtml forwUrl backUrl dir images = 
     H.docTypeHtml $ do
         pageHeader "The Fubar Net Gallery"
         H.body $ do
                 H.h1 "Gallery!"
                 H.p "Images!"
                 images
-            H.p $ H.a ! href "/gallery"
+                
+            H.p $ do
+                case backUrl of
+                     Just bak -> H.a ! href (path bak) $ "Previous"
+                     _ -> return ()
+                H.br
+                case forwUrl of
+                     Just fow -> H.a ! href (path fow) $ "Next" 
+                     _ -> return ()
+                H.br          
+                H.a ! href "/gallery"
                       $ "Return"
+    where path i = toValue (DT.concat ["/gallery/", DT.pack dir, "?offset=", DT.pack $ show i])
+    -- ?offset=2