-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Nav.hh
104 lines (97 loc) · 2.71 KB
/
Nav.hh
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
<?hh // strict
/**
* Copyright (c) 2014, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/
require_once $_SERVER['DOCUMENT_ROOT'].'/vendor/hhvm/xhp/src/init.php';
type NavItem = shape(
'name' => string,
'location' => string,
);
type NavSection = shape(
'name' => string,
'location' => ?string,
'items' => Vector<NavItem>,
);
final class :hack:nav extends :x:element {
private function getNavSections(): Vector<NavSection> {
return Vector{
shape(
'name' => 'Home',
'location' => '/',
'items' => Vector {},
),
shape(
'name' => 'GitHub',
'location' => 'http://github.com/facebook/hack-example-site',
'items' => Vector {},
),
shape(
'name' => 'Recipes',
'location' => null,
'items' => Vector {
shape(
'name' => '$_GET and $_POST',
'location' => '/recipes/get-and-post/',
),
shape(
'name' => 'Assert',
'location' => '/recipes/assert/',
),
shape(
'name' => 'DB Result',
'location' => '/recipes/db-result/',
),
shape(
'name' => 'Unescaped String',
'location' => '/recipes/unescaped-string/',
),
shape(
'name' => 'User ID',
'location' => '/recipes/user-id/',
),
},
),
};
}
private function renderNavItems(Vector<NavItem> $items): :xhp {
$render_item = $item ==>
<li>
<a class="navItem" href={$item['location']}>
{$item['name']}
</a>
</li>;
return
<x:frag>
{$items->map($render_item)->toArray()}
</x:frag>;
}
private function renderNavSection(NavSection $section): :xhp {
$section_item = <h3 class="navItem">{$section['name']}</h3>;
if ($section['location'] !== null) {
$section_item = <a href={$section['location']}>{$section_item}</a>;
}
return
<li class="navSectionItem">
{$section_item}
<ul class="navItems">
{$this->renderNavItems($section['items'])}
</ul>
</li>;
}
public function render(): :xhp {
$sections = $this->getNavSections()
->map($section ==> $this->renderNavSection($section));
return
<div class="nav">
<ul class="navSections">
{$sections->toArray()}
</ul>
</div>;
}
}