-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
136 lines (120 loc) · 3.39 KB
/
index.js
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
import React from "react"
import { StaticQuery, graphql, Link } from "gatsby"
import PropTypes from "prop-types";
const createMenuHierarchy = (menuData, menuName) => {
let tree = [],
mappedArr = {},
arrElem,
mappedElem
// First map the nodes of the array to an object -> create a hash table.
for (let i = 0, len = menuData.length; i < len; i++) {
arrElem = menuData[i].node
if (arrElem.menu_name === menuName && arrElem.enabled === true) {
mappedArr[arrElem.drupal_id] = arrElem
if (arrElem.drupal_parent_menu_item != null && arrElem.drupal_parent_menu_item.includes(arrElem.bundle)) {
let stripped_drupal_id = arrElem.drupal_parent_menu_item.replace(arrElem.bundle + ':', '')
mappedArr[arrElem.drupal_id].drupal_parent_menu_item = stripped_drupal_id
}
mappedArr[arrElem.drupal_id]['children'] = []
}
}
for (let id in mappedArr) {
if (mappedArr.hasOwnProperty(id)) {
mappedElem = mappedArr[id]
// If the element is not at the root level, add it to its parent array of children.
if (mappedElem.drupal_parent_menu_item) {
mappedArr[mappedElem.drupal_parent_menu_item]['children'].push(mappedElem)
}
// If the element is at the root level, add it to first level elements array.
else {
tree.push(mappedElem)
}
}
}
return tree
}
const buildLink = link => {
if(!link.external && link.link.uri_alias) {
return ( <Link activeClassName="active" to={link.link.uri_alias}>
{link.title}
</Link>)
} else if(!link.external && link.link.uri.includes('internal:')) {
return ( <Link activeClassName="active" to={link.link.uri.replace('internal:', '')}>
{link.title}
</Link>)
} else {
return ( <a href={link.link.uri} className={'external'}>
{link.title}
</a>)
}
}
const buildMenu = menuArray => {
if(!menuArray) {
return
}
let menu = []
for(let item in menuArray) {
if(menuArray[item].children.length !== 0) {
menu.push(
<li key={menuArray[item].drupal_id}>
{buildLink(menuArray[item])}
<ul className="submenu">
{buildMenu(menuArray[item].children)}
</ul>
</li>)
} else {
menu.push(<li key={menuArray[item].drupal_id}>{buildLink(menuArray[item])}</li>)
}
}
return menu
};
const generateMenu = (menuLinks, menuName) => {
let menu
menu = createMenuHierarchy(menuLinks.allMenuLinkContentMenuLinkContent.edges, menuName)
menu = buildMenu(menu)
return menu
}
const Menu = ({menuName}) => (
<StaticQuery
query={
graphql`
query MenuQuery {
allMenuLinkContentMenuLinkContent(sort: {order: ASC, fields: weight}) {
edges {
node {
enabled
title
expanded
external
langcode
weight
link {
uri
uri_alias
}
drupal_parent_menu_item
bundle
drupal_id
menu_name
}
}
}
}
`
}
render={data => (
<nav className={menuName}>
<ul >
{generateMenu(data, menuName)}
</ul>
</nav>
)}
/>
)
Menu.propTypes = {
menuName: PropTypes.string,
}
Menu.defaultProps = {
menuName: `main`,
}
export default Menu