Advertisement
Guest User

form

a guest
Apr 25th, 2015
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. {-# LANGUAGE MultiParamTypeClasses #-}
  2. {-# LANGUAGE OverloadedStrings     #-}
  3. {-# LANGUAGE QuasiQuotes           #-}
  4. {-# LANGUAGE TemplateHaskell       #-}
  5. {-# LANGUAGE TypeFamilies          #-}
  6. import           Control.Applicative ((<$>), (<*>))
  7. import           Data.Text           (Text)
  8. import           Data.Time           (Day)
  9. import           Yesod
  10. import           Yesod.Form.Jquery
  11. import Data.List (elemIndex)
  12. import Control.Monad (forM)
  13. import Data.String (fromString)
  14. data App = App
  15.  
  16. mkYesod "App" [parseRoutes|
  17. / HomeR GET
  18. /person PersonR POST
  19. |]
  20.  
  21. instance Yesod App
  22.  
  23. -- Tells our application to use the standard English messages.
  24. -- If you want i18n, then you can supply a translating function instead.
  25. instance RenderMessage App FormMessage where
  26.     renderMessage _ _ = defaultFormMessage
  27.  
  28. -- And tell us where to find the jQuery libraries. We'll just use the defaults,
  29. -- which point to the Google CDN.
  30. instance YesodJquery App
  31.  
  32. -- The datatype we wish to receive from the form
  33.  
  34.  
  35. tempdat = [4,8,7,9,5,2,3,1,6,6,1,9,4,3,8,2,7,5,3,5,2,7,1,6,9,8,4,9,4,5,1,2,7,6,3,8,1,7,8,3,6,4,5,9,2,2,6,3,8,9,5,7,4,1,7,2,4,5,8,3,1,6,9,5,3,1,6,4,9,8,2,7,8,9,6,2,7,1,4,5,3]
  36.  
  37.  
  38. -----------------The Labelling Function---------------
  39. labelling :: [Integer] -> [(String,Integer)]
  40. labelling xs = labelling' xs 0 (length xs)
  41.  
  42. labelling' (x:xs) z y = [helper x z] ++ (labelling' xs (z+1) (y-1))
  43. labelling' [] z 0 = []
  44.  
  45.  
  46. --Gives the (label,value)
  47. helper x y =  ( "r" ++ (rownum y) ++ "c" ++ (colnum y) , x )
  48.  
  49. --Gives row number and column number
  50. rownum y =  (show ((quot y 9)+1) )
  51. colnum y =  (show ((rem y 9)+1) )
  52.  
  53. --Gives the Integer
  54. help::(Num x) => (Maybe x) -> x
  55. help (Just x) = x
  56.  
  57. lis = labelling tempdat
  58. --------------------------------------------------------
  59.  
  60. --------Splits the 81 element list to list of list with each element having 9 elements----------------
  61. fun [] = []
  62. fun xs =
  63.     let (a,b) = (splitAt 9 xs)
  64.     in a:(fun b)
  65. ------------------------------------------------------------------------------------------------------
  66.  
  67.  
  68. --------The Sudoku table------------------
  69. ------This is the part I wanted to modify. I wanted to to remove the 'widget' and generate it in the hamlet file----
  70. listEditMForm extra = do
  71.     ifields <- forM lis (\(s,i) -> mreq intField  (fromString s) (Just i))
  72.     let (iresults,iviews) = unzip ifields
  73.     let y = fun iviews  
  74.     let widget = [whamlet|
  75.         #{extra}
  76.             <table class="table table-bordered" #outer cellspacing="0" cellpadding="0">
  77.                 $forall ivlist <- y
  78.                     <tr>
  79.                       $forall iv <- ivlist
  80.                         <td>^{fvInput iv}
  81.       |]
  82.     return ((FormSuccess iresults), widget)
  83.  
  84.  
  85.  
  86. -- The GET handler displays the form
  87. getHomeR :: Handler Html
  88. getHomeR = do
  89.     -- Generate the form to be displayed
  90.     (widget, enctype) <- generateFormPost listEditMForm
  91.     defaultLayout $ do
  92.         toWidget [lucius| h1 { text-align : center;}|]
  93.         [whamlet|
  94.         <div class="jumbotron">
  95.             <h1><span class="glyphicon glyphicon-th"></span>Sudoku
  96.                 <p>You can solve sudoku puzzels or ask for solutions
  97.         <div >
  98.             <div #contentlayout align="left">
  99.                 <p><h3>Instructions:
  100.                 <ul>
  101.                     <li>Use <strong>Refresh</strong> to get new puzzle
  102.                     <li>Use <strong>Solve</strong> to get the solution
  103.             <div #contentlayout align="center">            
  104.             ^{widget}
  105.         <div #contentlayout >
  106.                 <div>
  107.                     <button .btn .btn-primary type="submit">New Puzzle <span class="glyphicon glyphicon-refresh"></span>
  108.                 <div><br>    
  109.                 <div>    
  110.                     <button .btn .btn-primary type="submit">Solve <span class="glyphicon glyphicon-circle-arrow-right"></span>
  111.                 <div><br>    
  112.                 <div>    
  113.                     <button .btn .btn-primary type="submit">Validate<span class="glyphicon glyphicon-circle-arrow-right"></span>            
  114.         |]
  115.            
  116.  
  117. -- The POST handler processes the form. If it is successful, it displays the
  118. -- parsed person. Otherwise, it displays the form again with error messages.
  119. postPersonR :: Handler Html
  120. postPersonR = error "not implemented"
  121.  
  122. main :: IO ()
  123. main = warp 3000 App
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement