[OCaml] Mobile-friendly clone of cgit.
Implement Topnav module with proper routes.
Changed files
lib/routes.ml
@@ -3,27 +3,30 @@
3
3
type t =
4
4
| Root
5
5
| Repo of string
6
Removed:
| Tag of string * string
6
Added:
| Log of string
7
Added:
| Files of string
8
Added:
| Refs of string
7
9
| Commit of string * string
8
Removed:
| Tree of string * string
9
Removed:
| Blob of string * string
10
Added:
| File of string * string
10
11
11
12
let%path root_path = "/"
12
Removed:
let%path repo_path = "/%s/"
13
Removed:
let%path tag_path = "/%s/refs/%s"
13
Added:
let%path repo_path = "/%s/summary/"
14
Added:
let%path log_path = "/%s/log/"
15
Added:
let%path files_path = "/%s/files/"
16
Added:
let%path refs_path = "/%s/refs/"
14
17
let%path commit_path = "/%s/commit/%s"
15
Removed:
let%path tree_path = "/%s/tree/%s"
16
Removed:
let%path blob_path = "/%s/blob/%s"
18
Added:
let%path file_path = "/%s/file/%s"
17
19
18
Removed:
let link_to route contents =
20
Added:
let link_to route ?(other_attrs = []) contents =
19
21
let open Dream_html in
20
22
let open HTML in
21
23
let path = function
22
24
| Root -> path_attr href root_path
23
25
| Repo repo -> path_attr href repo_path repo
24
Removed:
| Tag (repo, branch) -> path_attr href tag_path repo branch
26
Added:
| Log repo -> path_attr href log_path repo
27
Added:
| Files repo -> path_attr href files_path repo
28
Added:
| Refs repo -> path_attr href refs_path repo
25
29
| Commit (repo, commit) -> path_attr href commit_path repo commit
26
Removed:
| Tree (repo, hash) -> path_attr href tree_path repo hash
27
Removed:
| Blob (repo, hash) -> path_attr href blob_path repo hash
30
Added:
| File (repo, hash) -> path_attr href file_path repo hash
28
31
in
29
Removed:
a [ path route ] [ contents ]
32
Added:
a (path route :: other_attrs) [ contents ]
lib/views.ml
@@ -13,27 +13,26 @@
13
13
module Components = struct
14
14
open Dream_html
15
15
16
Removed:
let topnav repo =
17
Removed:
let open HTML in
18
Removed:
let li_of_a (path, text) =
19
Removed:
let is_active = String.ends_with ~suffix:path repo in
20
Removed:
let attrs = if is_active then [ id "active" ] else [] in
21
Removed:
let url = Printf.sprintf "/%s/%s" repo path in
22
Removed:
li attrs [ a [ href "%s" url ] [ txt text ] ]
23
Removed:
in
24
Removed:
nav
25
Removed:
[ id "top" ]
26
Removed:
[
27
Removed:
ul []
28
Removed:
@@ List.map li_of_a
29
Removed:
[
30
Removed:
("", "summary");
31
Removed:
("refs/", "refs");
32
Removed:
("log/", "log");
33
Removed:
("tree/", "tree");
34
Removed:
("commit/", "commit");
35
Removed:
];
36
Removed:
]
16
Added:
module Topnav = struct
17
Added:
type t = None | Summary | Log | Files | Refs
18
Added:
19
Added:
let v ?(active_path = None) repo =
20
Added:
let open HTML in
21
Added:
let nav_items =
22
Added:
[
23
Added:
(Routes.Repo repo, "Summary", Summary);
24
Added:
(Routes.Log repo, "Log", Log);
25
Added:
(Routes.Files repo, "Files", Files);
26
Added:
(Routes.Refs repo, "Refs", Refs);
27
Added:
]
28
Added:
in
29
Added:
let li_of_item (route, text, path) =
30
Added:
let is_active = path = active_path in
31
Added:
let attrs = if is_active then [ id "active" ] else [] in
32
Added:
HTML.li attrs [ Routes.link_to route (txt "%s" text) ]
33
Added:
in
34
Added:
nav [ id "top" ] [ ul [] @@ List.map li_of_item nav_items ]
35
Added:
end
37
36
end
38
37
39
38
module Page = struct
@@ -146,7 +145,7 @@
146
145
{
147
146
title = repo;
148
147
subtitle = repo_description repo;
149
Removed:
topnav = Components.topnav repo;
148
Added:
topnav = Components.Topnav.(v ~active_path:Summary repo);
150
149
content;
151
150
}
152
151
@@ -162,7 +161,7 @@
162
161
{
163
162
title = repo;
164
163
subtitle = repo_description repo;
165
Removed:
topnav = Components.topnav repo;
164
Added:
topnav = Components.Topnav.(v ~active_path:Refs repo);
166
165
content;
167
166
}
168
167
@@ -178,7 +177,7 @@
178
177
{
179
178
title = repo;
180
179
subtitle = repo_description repo;
181
Removed:
topnav = Components.topnav repo;
180
Added:
topnav = Components.Topnav.(v ~active_path:Log repo);
182
181
content;
183
182
}
184
183
@@ -195,7 +194,7 @@
195
194
{
196
195
title;
197
196
subtitle = repo_description repo;
198
Removed:
topnav = Components.topnav repo;
197
Added:
topnav = Components.Topnav.(v ~active_path:Files repo);
199
198
content;
200
199
}
201
200
@@ -232,7 +231,7 @@
232
231
{
233
232
title;
234
233
subtitle = repo_description repo;
235
Removed:
topnav = Components.topnav repo;
234
Added:
topnav = Components.Topnav.v repo;
236
235
content;
237
236
}
238
237
end