forked from avh4/elm-format
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLeftSidebar.elm
194 lines (177 loc) · 5.36 KB
/
LeftSidebar.elm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
module Component.LeftSidebar exposing (..)
import Component.LeftSidebar.CurrentDocView as CurrentDoc
import Component.LeftSidebar.OpenMenuView as OpenMenu
import Dreamwriter exposing (..)
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (..)
import Html.Lazy exposing (..)
import Maybe
import Regex
import Signal exposing (Address)
type ViewMode
= CurrentDocMode
| OpenMenuMode
| SettingsMode
type alias Addresses a =
{ a
| print : Address ()
, newDoc : Address ()
, newChapter : Address ()
, openFromFile : Address ()
, navigateToTitle : Address ()
, navigateToChapterId : Address Identifier
, download : Address DownloadOptions
, update : Address Update
}
type alias Model =
{ viewMode : ViewMode
, docs : List Doc
, currentDocId : Maybe Identifier
, currentDoc : Doc
}
initialModel : Model
initialModel =
{ viewMode = CurrentDocMode
, docs = []
, currentDocId = Nothing
, currentDoc = emptyDoc
}
type Update
= NoOp
| SetViewMode ViewMode
| OpenDocId Identifier
transition : Update -> Model -> Model
transition update model =
case update of
NoOp
-> model
SetViewMode mode
-> { model | viewMode = mode }
OpenDocId id
-> { model
| currentDocId = Just id
, viewMode = CurrentDocMode
}
{-| Replace illegal filename characters with underscores
-}
illegalFilenameCharMatcher = Regex.regex "[/\\<>?|\":*]"
legalizeFilename : String -> String
legalizeFilename = Regex.replace Regex.All illegalFilenameCharMatcher (always "_")
downloadContentType = "text/plain;charset=UTF-8"
view : Addresses a -> Model -> Html
view addresses model =
let openDoc = Signal.forwardTo addresses.update OpenDocId
{ sidebarHeader, sidebarBody, sidebarFooter } =
case model.viewMode of
OpenMenuMode
-> { sidebarHeader =
lazy viewOpenMenuHeader
addresses.update
, sidebarBody =
lazy2 (OpenMenu.view addresses.openFromFile openDoc)
model.docs
model.currentDoc
, sidebarFooter =
viewOpenMenuFooter
}
CurrentDocMode
-> { sidebarHeader =
lazy2 viewCurrentDocHeader
model.currentDoc
addresses
, sidebarBody =
lazy3 CurrentDoc.view
addresses.navigateToTitle
addresses.navigateToChapterId
model.currentDoc
, sidebarFooter =
lazy viewCurrentDocFooter
addresses
}
SettingsMode
-> -- TODO make this different than CurrentDocMode
{ sidebarHeader =
lazy2 viewCurrentDocHeader
model.currentDoc
addresses
, sidebarBody =
lazy3 CurrentDoc.view
addresses.navigateToTitle
addresses.navigateToChapterId
model.currentDoc
, sidebarFooter =
lazy viewCurrentDocFooter
addresses
}
in div [ id "left-sidebar-container", class "sidebar" ]
[ sidebarHeader
, div [ id "left-sidebar-body", class "sidebar-body" ]
[ sidebarBody ]
, sidebarFooter
]
sidebarHeaderId = "left-sidebar-header"
sidebarHeaderClass = "sidebar-header"
viewOpenMenuFooter : Html
viewOpenMenuFooter = span [] []
viewCurrentDocFooter : Addresses a -> Html
viewCurrentDocFooter addresses =
div [ id "left-sidebar-footer", class "sidebar-footer" ]
[ span
[ id "add-chapter"
, title "Add Chapter"
, onClick addresses.newChapter ()
, class "flaticon-plus81"
]
[]
]
viewOpenMenuHeader updateChannel =
div
[ key "open-menu-header"
, id sidebarHeaderId
, class sidebarHeaderClass
]
[ span
[ class "sidebar-header-control"
, onClick updateChannel (SetViewMode CurrentDocMode)
]
[ text "cancel" ]
]
viewCurrentDocHeader : Doc -> Addresses a -> Html
viewCurrentDocHeader currentDoc addresses =
let downloadOptions =
{ filename = legalizeFilename currentDoc.title ++ ".html"
, contentType = downloadContentType
}
in menu [ id sidebarHeaderId, class sidebarHeaderClass ]
[ menuitem
[ title "New"
, class "sidebar-header-control flaticon-add26"
, onClick addresses.newDoc ()
]
[]
, menuitem
[ title "Open"
, class "sidebar-header-control flaticon-folder63"
, onClick addresses.update (SetViewMode OpenMenuMode)
]
[]
, menuitem
[ title "Download"
, class "sidebar-header-control flaticon-cloud134"
, onClick addresses.download downloadOptions
]
[]
, menuitem
[ title "Print"
, class "sidebar-header-control flaticon-printer70"
, onClick addresses.print ()
]
[]
, menuitem
[ title "Settings"
, class "sidebar-header-control flaticon-gear33"
, onClick addresses.update (SetViewMode SettingsMode)
]
[]
]