]> gitweb.factorcode.org Git - factor-unmaintained.git/blob - recipes/recipes.factor
images.processing.rotation: moving to extra/
[factor-unmaintained.git] / recipes / recipes.factor
1 USING: accessors arrays colors.constants combinators
2 db.sqlite db.tuples db.types kernel locals math
3 monads persistency sequences sequences.extras ui ui.gadgets.controls
4 ui.gadgets.layout models.combinators ui.gadgets.labels
5 ui.gadgets.scrollers ui.pens.solid io.files.temp ;
6 FROM: sets => prune ;
7 IN: recipes
8
9 STORED-TUPLE: recipe { title { VARCHAR 100 } } { votes INTEGER } { txt TEXT } { genre { VARCHAR 100 } } ;
10 : <recipe> ( title genre text -- recipe ) recipe new swap >>txt swap >>genre swap >>title 0 >>votes ;
11 "recipes.db" temp-file <sqlite-db> recipe define-db
12 : top-recipes ( offset search -- recipes ) <query> T{ recipe } rot >>title >>tuple
13     "votes" >>order 30 >>limit swap >>offset get-tuples ;
14 : top-genres ( -- genres ) f f top-recipes [ genre>> ] map prune 4 short head-slice ;
15
16 : interface ( -- book ) [ 
17      [
18         [ $ TOOLBAR $ ] <hbox> COLOR: AliceBlue <solid> >>interior ,
19         [ "Genres:" <label> , <spacer> $ ALL $ $ GENRES $ ] <hbox>
20             { 5 0 } >>gap COLOR: gray <solid> >>interior ,
21         $ RECIPES $
22      ] <vbox> ,
23      [
24         [ "Title:" <label> , $ TITLE $ "Genre:" <label> , $ GENRE $ ] <hbox> ,
25         $ BODY $
26         $ BUTTON $
27      ] <vbox> ,
28   ] <book*> { 350 245 } >>pref-dim ;
29   
30 :: recipe-browser ( -- ) [ [
31     interface
32       <table*> :> tbl
33       "okay" <model-border-btn> BUTTON -> :> ok
34       IMG-MODEL-BTN: submit [ store-tuple ] >>value TOOLBAR -> :> submit
35       IMG-MODEL-BTN: love 1 >>value TOOLBAR ->
36       IMG-MODEL-BTN: hate -1 >>value -> 2array merge :> votes
37       IMG-MODEL-BTN: back -> [ -30 ] <$
38       IMG-MODEL-BTN: more -> [ 30 ] <$ 2array merge :> viewed
39       <spacer> <model-field*> ->% 1 :> search
40       submit ok [ [ drop ] ] <$ 2array merge [ drop ] >>value :> quot
41       viewed 0 [ + ] fold search ok t <basic> "all" <model-btn> ALL ->
42       tbl selection>> votes [ [ + ] curry change-votes modify-tuple ] 2$>
43         4array merge
44         [ drop [ f ] [ "%" dup surround <pattern> ] if-empty top-recipes ] 3fmap :> ups
45       ups [ top-genres [ <model-btn> GENRES -> ] map merge ] bind*
46         [ text>> T{ recipe } swap >>genre get-tuples ] fmap
47       tbl swap ups 2merge >>model
48         [ [ title>> ] [ genre>> ] bi 2array ] >>quot
49         { "Title" "Genre" } >>column-titles dup <scroller> RECIPES ,% 1 actions>>
50       submit [ "" dup dup <recipe> ] <$ 2array merge
51         { [ [ title>> ] fmap <model-field> TITLE ->% .5 ]
52           [ [ genre>> ] fmap <model-field> GENRE ->% .5 ]
53           [ [ txt>> ] fmap <model-editor> BODY ->% 1 ]
54         } cleave
55         [ <recipe> ] 3fmap
56       [ [ 1 ] <$ ]
57       [ quot ok updates #1 [ call( recipe -- ) 0 ] 2fmap ] bi
58       2merge 0 <basic> switch-models >>model
59    ] with-interface "recipes" open-window ] with-ui ;
60
61 MAIN: recipe-browser