Refactor views.

Commit
85872d1f2c6c28b492edc2861a22b49faf266dcd
Author
Marius Peter <marius.peter@tutanota.com>
Author date
Committer
Marius Peter <marius.peter@tutanota.com>
Committer date
lib/views.ml
index 392075f1..d2f4501d 100644..100644
@@ -3,34 +3,36 @@
3 3 open Dream_html
4 4 open Config
5 5
6 Added: type page = Summary | Commits | Files | Branches | Tags | Readme
7 Added:
6 8 type body_data = {
7 9 title : string;
10 Added: repo : string option;
8 11 subtitle : string;
9 Removed: topnav : Dream_html.node;
12 Added: active : page;
10 13 content : Dream_html.node list;
11 14 }
12 15
13 Removed: module Components = struct
14 Removed: module Topnav = struct
15 Removed: type t = None | Summary | Log | Files | Refs
16 Added: let page_to_nav_item repo = function
17 Added: | Summary -> (Routes.Repo repo, "Summary", Summary)
18 Added: | Commits -> (Routes.Commits repo, "Commits", Commits)
19 Added: | Files -> (Routes.Files repo, "Files", Files)
20 Added: | Branches -> (Routes.Branches repo, "Branches", Branches)
21 Added: | Tags -> (Routes.Tags repo, "Tags", Tags)
22 Added: | Readme -> (Routes.Readme repo, "README", Readme)
16 23
17 Removed: let v ?(active_path = None) repo =
18 Removed: let open HTML in
19 Removed: let nav_items =
20 Removed: [
21 Removed: (Routes.Repo repo, "Summary", Summary);
22 Removed: (Routes.Log repo, "Log", Log);
23 Removed: (Routes.Files repo, "Files", Files);
24 Removed: (Routes.Refs repo, "Refs", Refs);
25 Removed: ]
26 Removed: in
27 Removed: let li_of_item (route, text, path) =
28 Removed: let is_active = path = active_path in
29 Removed: let attrs = if is_active then [ id "active" ] else [] in
30 Removed: HTML.li attrs [ Routes.link_to route (txt "%s" text) ]
31 Removed: in
32 Removed: nav [ id "top" ] [ ul [] @@ List.map li_of_item nav_items ]
33 Removed: end
24 Added: module Components = struct
25 Added: let topnav ?(active = Summary) repo =
26 Added: let nav_items =
27 Added: List.map (page_to_nav_item repo)
28 Added: [ Summary; Commits; Files; Branches; Tags; Readme ]
29 Added: in
30 Added: let li_of_item (route, text, path) =
31 Added: let is_active = path = active in
32 Added: let attrs = if is_active then [ HTML.id "active" ] else [] in
33 Added: HTML.li attrs [ Routes.link_to route (txt "%s" text) ]
34 Added: in
35 Added: HTML.(nav [ id "top" ] [ ul [] @@ List.map li_of_item nav_items ])
34 36 end
35 37
36 38 module Page = struct
@@ -42,13 +44,14 @@
42 44 HTML.(null [ h1 [] [ txt "%s" header1 ]; h2 [] [ txt "%s" header2 ] ])
43 45
44 46 let page_footer () =
45 Removed: let today = Unix.localtime (Unix.time ()) in
46 Removed: let year = string_of_int (today.Unix.tm_year + 1900) in
47 Added: let now = Unix.(time () |> localtime) in
48 Added: let year = string_of_int (now.tm_year + 1900) in
47 49 let footer_text = Printf.sprintf "Copyright %s %s" year config.user in
48 50 HTML.footer [] [ txt "%s" footer_text ]
49 51
50 52 let render ?(page_title = "Ogit") body_data =
51 53 let open HTML in
54 Added: let bd = body_data in
52 55 html []
53 56 [
54 57 head []
@@ -60,9 +63,11 @@
60 63 ];
61 64 body []
62 65 [
63 Removed: page_header body_data.title body_data.subtitle;
64 Removed: body_data.topnav;
65 Removed: div [ id "main" ] body_data.content;
66 Added: page_header bd.title bd.subtitle;
67 Added: (match bd.repo with
68 Added: | None -> HTML.null []
69 Added: | Some repo -> Components.topnav ~active:bd.active repo);
70 Added: div [ id "main" ] bd.content;
66 71 page_footer ();
67 72 ];
68 73 ]
@@ -83,8 +88,9 @@
83 88 @@ Page.render
84 89 {
85 90 title = "Ogit";
91 Added: repo = None;
86 92 subtitle = "Repositories for " ^ config.user;
87 Removed: topnav = HTML.null [];
93 Added: active = Summary;
88 94 content = [ all_repositories ];
89 95 }
90 96
@@ -95,9 +101,12 @@
95 101 let li_of_author (author : Resolvers.Commit.user) =
96 102 HTML.(li [] [ txt "%s" author.name ])
97 103
98 Removed: let li_of_branch repo (branch : Resolvers.Branch.t) =
99 Removed: HTML.(li [] [ Routes.link_to (Refs repo) (txt "%s" branch.name) ])
104 Added: let li_of_branch repo (branch : Resolvers.Reference.t) =
105 Added: HTML.(li [] [ Routes.link_to (Branches repo) (txt "%s" branch.name) ])
100 106
107 Added: let li_of_tag repo (tag : Resolvers.Reference.t) =
108 Added: HTML.(li [] [ Routes.link_to (Tags repo) (txt "%s" tag.name) ])
109 Added:
101 110 let li_of_commit repo (commit : Resolvers.Commit.t) =
102 111 let timestamp (date, _) =
103 112 let tm = date |> Int64.to_float |> Unix.localtime in
@@ -129,9 +138,10 @@
129 138 respond
130 139 @@ Page.render ~page_title:(page_title repo)
131 140 {
141 Added: repo = Some repo;
132 142 title = repo;
133 143 subtitle = Resolvers.repo_description repo;
134 Removed: topnav = Components.Topnav.(v ~active_path:Summary repo);
144 Added: active = Summary;
135 145 content =
136 146 HTML.
137 147 [
@@ -144,49 +154,48 @@
144 154 ];
145 155 }
146 156
147 Removed: let refs repo branches =
157 Added: let branches repo branches =
148 158 respond
149 159 @@ Page.render ~page_title:(page_title repo)
150 160 {
161 Added: repo = Some repo;
151 162 title = repo;
152 163 subtitle = Resolvers.repo_description repo;
153 Removed: topnav = Components.Topnav.(v ~active_path:Refs repo);
154 Removed: content =
155 Removed: HTML.
156 Removed: [
157 Removed: h3 [] [ txt "Branches" ];
158 Removed: ul [] (List.map (li_of_branch repo) branches);
159 Removed: ];
164 Added: active = Branches;
165 Added: content = HTML.[ ul [] @@ List.map (li_of_branch repo) branches ];
160 166 }
161 167
162 Removed: let log repo commits =
168 Added: let tags repo tags =
163 169 respond
164 170 @@ Page.render ~page_title:(page_title repo)
165 171 {
172 Added: repo = Some repo;
166 173 title = repo;
167 174 subtitle = Resolvers.repo_description repo;
168 Removed: topnav = Components.Topnav.(v ~active_path:Log repo);
169 Removed: content =
170 Removed: HTML.
171 Removed: [
172 Removed: h3 [] [ txt "All commits" ];
173 Removed: ul [] (List.map (li_of_commit repo) commits);
174 Removed: ];
175 Added: active = Tags;
176 Added: content = HTML.[ ul [] @@ List.map (li_of_tag repo) tags ];
175 177 }
176 178
179 Added: let commits repo commits =
180 Added: respond
181 Added: @@ Page.render ~page_title:(page_title repo)
182 Added: {
183 Added: repo = Some repo;
184 Added: title = repo;
185 Added: subtitle = Resolvers.repo_description repo;
186 Added: active = Commits;
187 Added: content = HTML.[ ul [] @@ List.map (li_of_commit repo) commits ];
188 Added: }
189 Added:
177 190 let files repo (tree : Resolvers.Tree.t) =
178 191 respond
179 192 @@ Page.render ~page_title:(page_title repo)
180 193 {
181 Removed: title = Printf.sprintf "%s" repo;
194 Added: repo = Some repo;
195 Added: title = repo;
182 196 subtitle = Resolvers.repo_description repo;
183 Removed: topnav = Components.Topnav.v ~active_path:Files repo;
184 Removed: content =
185 Removed: HTML.
186 Removed: [
187 Removed: h3 [] [ txt "Files at %s" tree.path ];
188 Removed: ul [] (List.map (li_of_entry repo) tree.entries);
189 Removed: ];
197 Added: active = Files;
198 Added: content = HTML.[ ul [] @@ List.map (li_of_entry repo) tree.entries ];
190 199 }
191 200
192 201 let file repo (blob : Resolvers.Blob.t) =
@@ -205,11 +214,11 @@
205 214 respond
206 215 @@ Page.render ~page_title:(page_title repo)
207 216 {
208 Removed: title = Printf.sprintf "%s" repo;
217 Added: repo = Some repo;
218 Added: title = repo;
209 219 subtitle = Resolvers.repo_description repo;
210 Removed: topnav = Components.Topnav.v ~active_path:Files repo;
211 Removed: content =
212 Removed: HTML.[ h3 [] [ txt "File" ]; div [ id "blob" ] formatted_blob ];
220 Added: active = Files;
221 Added: content = HTML.[ div [ id "blob" ] formatted_blob ];
213 222 }
214 223
215 224 let commit repo (commit : Resolvers.Commit.t) =
@@ -217,10 +226,11 @@
217 226 respond
218 227 @@ Page.render ~page_title:(page_title repo)
219 228 {
229 Added: repo = Some repo;
220 230 title =
221 231 Printf.sprintf "%s : %s" repo @@ Resolvers.short_hash commit.hash;
222 232 subtitle = Resolvers.repo_description repo;
223 Removed: topnav = Components.Topnav.v repo;
233 Added: active = Summary;
224 234 content = HTML.[ h3 [] [ txt "%s" message ] ];
225 235 }
226 236 end