factor/extra/recipes/recipes.factor

60 lines
2.8 KiB
Factor
Raw Normal View History

2009-06-16 15:36:01 -04:00
USING: accessors arrays db.tuples db.sqlite persistency db.queries
2009-06-14 14:08:49 -04:00
io.files.temp kernel monads sequences ui ui.frp.gadgets
ui.frp.layout ui.frp.signals ui.gadgets.scrollers ui.gadgets.labels
colors.constants ui.pens.solid combinators math locals strings
2009-06-24 21:06:12 -04:00
ui.images db.types sequences.extras ui.tools.inspector ;
2009-06-14 14:08:49 -04:00
FROM: sets => prune ;
IN: recipes
2009-06-16 15:36:01 -04:00
STORED-TUPLE: recipe { title { VARCHAR 100 } } { votes INTEGER } { txt TEXT } { genre { VARCHAR 100 } } ;
: <recipe> ( title genre text -- recipe ) recipe new swap >>txt swap >>genre swap >>title 0 >>votes ;
2009-06-14 14:08:49 -04:00
"recipes.db" temp-file <sqlite-db> recipe define-db
2009-06-16 15:36:01 -04:00
: top-recipes ( offset search -- recipes ) <query> T{ recipe } rot >>title >>tuple
"votes" >>order 30 >>limit swap >>offset get-tuples ;
: top-genres ( -- genres ) f f top-recipes [ genre>> ] map prune 4 (head-slice) ;
2009-06-14 14:08:49 -04:00
: interface ( -- book ) [
[
2009-06-24 21:06:12 -04:00
[ $ TOOLBAR $ ] <hbox> COLOR: AliceBlue <solid> >>interior ,
[ "Genres:" <label> , <spacer> $ ALL $ $ GENRES $ ] <hbox>
2009-06-16 15:36:01 -04:00
{ 5 0 } >>gap COLOR: gray <solid> >>interior ,
2009-06-14 14:08:49 -04:00
$ RECIPES $
] <vbox> ,
[
[ "Title:" <label> , $ TITLE $ "Genre:" <label> , $ GENRE $ ] <hbox> ,
$ BODY $
$ BUTTON $
] <vbox> ,
] <frp-book*> { 350 245 } >>pref-dim ;
2009-06-16 15:36:01 -04:00
2009-06-18 16:32:11 -04:00
:: recipe-browser ( -- ) [ [
2009-06-14 14:08:49 -04:00
interface
<frp-table*> :> tbl
"okay" <frp-border-button> BUTTON -> :> ok
2009-06-16 15:36:01 -04:00
"submit" <image-button> [ store-tuple ] >>value TOOLBAR -> :> submit
2009-06-16 16:50:48 -04:00
"love" <image-button> 1 >>value TOOLBAR ->
"hate" <image-button> -1 >>value -> 2array <merge> :> votes
2009-06-16 15:36:01 -04:00
"back" <image-button> -> [ -30 ] <$
"more" <image-button> -> [ 30 ] <$ 2array <merge> :> viewed
2009-06-24 21:06:12 -04:00
<spacer> <frp-field*> ->% 1 :> search
2009-06-14 14:08:49 -04:00
submit ok [ [ drop ] ] <$ 2array <merge> [ drop ] >>value :> quot
2009-06-24 21:06:12 -04:00
viewed 0 [ + ] <fold> search ok t <basic> "all" <frp-button> ALL ->
2009-06-16 16:50:48 -04:00
tbl selected-value>> votes [ [ + ] curry change-votes modify-tuple ] 2$>-|
4array <merge>
2009-06-16 15:36:01 -04:00
[ drop [ f ] [ "%" dup surround <pattern> ] if-empty top-recipes ] 3fmap-| :> updates
2009-06-24 21:06:12 -04:00
updates [ top-genres [ <frp-button> GENRES -> ] map <merge> ] bind*
2009-06-14 14:08:49 -04:00
[ text>> T{ recipe } swap >>genre get-tuples ] fmap
tbl swap updates 2array <merge> >>model
[ [ title>> ] [ genre>> ] bi 2array ] >>quot
2009-06-16 16:50:48 -04:00
{ "Title" "Genre" } >>column-titles dup <scroller> RECIPES ,% 1 actions>>
submit [ "" dup dup <recipe> ] <$ 2array <merge>
2009-06-14 14:08:49 -04:00
{ [ [ title>> ] fmap <frp-field> TITLE ->% .5 ]
[ [ genre>> ] fmap <frp-field> GENRE ->% .5 ]
[ [ txt>> ] fmap <frp-editor> BODY ->% 1 ]
} cleave
[ <recipe> ] 3fmap-|
[ [ 1 ] <$ ]
[ quot ok <updates> #1 [ call( recipe -- ) 0 ] 2fmap-& ] bi
2array <merge> 0 <basic> <switch> >>model
2009-06-18 16:32:11 -04:00
] with-interface "recipes" open-window ] with-ui ;
2009-06-14 14:08:49 -04:00
2009-06-16 15:36:01 -04:00
MAIN: recipe-browser