Navigation.tsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. /*
  2. Copyright (C) 2017 Cloudbase Solutions SRL
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. import React, { CSSProperties } from 'react'
  15. import { Link } from 'react-router-dom'
  16. import { observer } from 'mobx-react'
  17. import styled from 'styled-components'
  18. import autobind from 'autobind-decorator'
  19. import Logo from '@src/components/ui/Logo'
  20. import userStore from '@src/stores/UserStore'
  21. import configLoader from '@src/utils/Config'
  22. import { navigationMenu } from '@src/constants'
  23. import { ThemeProps } from '@src/components/Theme'
  24. import backgroundImage from './images/star-bg.jpg'
  25. import cbsImage from './images/cbsl-logo.svg'
  26. import cbsImageSmall from './images/cbsl-logo-small.svg'
  27. import tinyLogo from './images/logo-small.svg'
  28. import replicaImage from './images/replica-menu.svg'
  29. import endpointImage from './images/endpoint-menu.svg'
  30. import planningImage from './images/planning-menu.svg'
  31. import projectImage from './images/project-menu.svg'
  32. import userImage from './images/user-menu.svg'
  33. import logsImage from './images/logs-menu.svg'
  34. import dashboardImage from './images/dashboard-menu.svg'
  35. import minionPoolsImage from './images/minion-pool-menu.svg'
  36. import bareMetalServersImage from './images/bare-metal-servers.svg'
  37. const isCollapsed = (props: any) => props.collapsed
  38. || (window.outerWidth <= ThemeProps.mobileMaxWidth)
  39. const ANIMATION = '200ms'
  40. const Wrapper = styled.div<any>`
  41. background-image: url('${backgroundImage}');
  42. display: flex;
  43. flex-direction: column;
  44. align-items: center;
  45. height: 100%;
  46. width: ${props => (isCollapsed(props) ? '80px' : '320px')};
  47. transition: width ${ANIMATION};
  48. `
  49. const LogoWrapper = styled.div<any>`
  50. position: relative;
  51. height: 48px;
  52. margin-top: 48px;
  53. width: 100%;
  54. display: flex;
  55. justify-content: center;
  56. `
  57. const LogoStyled = styled(Logo)<any>`
  58. position: absolute;
  59. top: 0;
  60. left: ${props => (isCollapsed(props) ? '-9999px' : 'auto')};
  61. cursor: pointer;
  62. display: flex;
  63. `
  64. const WrappedLink = (props: any) => (
  65. <div
  66. style={{ transition: `all ${ThemeProps.animations.swift}` }}
  67. className={props.className}
  68. ref={r => { if (props.customRef) props.customRef(r) }}
  69. >
  70. <Link to={props.to} style={{ display: 'flex', width: '100%' }} />
  71. </div>
  72. )
  73. const TinyLogo = styled(WrappedLink)`
  74. position: absolute;
  75. top: 0;
  76. opacity: ${props => (isCollapsed(props) ? 1 : 0)};
  77. background: url('${tinyLogo}') center no-repeat;
  78. display: flex;
  79. width: 48px;
  80. height: 48px;
  81. transition: opacity ${ANIMATION};
  82. `
  83. const MenuWrapper = styled.div<any>`
  84. position: relative;
  85. display: flex;
  86. flex-direction: column;
  87. align-items: center;
  88. flex-grow: 1;
  89. margin-top: 32px;
  90. width: 100%;
  91. `
  92. const Menu = styled.div<any>`
  93. display: flex;
  94. flex-direction: column;
  95. position: absolute;
  96. top: 0;
  97. left: ${props => (isCollapsed(props) ? '-9999px' : 'auto')};
  98. opacity: ${props => (isCollapsed(props) ? 0 : 1)};
  99. transition: opacity ${ANIMATION};
  100. `
  101. const MenuItem = styled(Link)<{ selected?: boolean | null }>`
  102. font-size: 18px;
  103. color: ${props => (props.selected ? '#007AFF' : 'white')};
  104. cursor: pointer;
  105. margin-top: 26px;
  106. text-decoration: none;
  107. width: 160px;
  108. margin-left: 32px;
  109. `
  110. const SmallMenu = styled.div<any>`
  111. display: flex;
  112. flex-direction: column;
  113. position: absolute;
  114. opacity: ${props => (isCollapsed(props) ? 1 : 0)};
  115. left: ${props => (isCollapsed(props) ? 'auto' : '-9999px')};
  116. top: 0;
  117. transition: opacity ${ANIMATION};
  118. `
  119. const SmallMenuBackground = styled.div<any>`
  120. border-radius: 50%;
  121. opacity: 0.15;
  122. position: absolute;
  123. top: 0;
  124. left: 0;
  125. right: 0;
  126. bottom: 0;
  127. transition: background-color ${ANIMATION};
  128. `
  129. const MenuTooltip = styled.div<any>`
  130. position: absolute;
  131. font-size: 12px;
  132. color: #202234;
  133. background: #D8DBE2;
  134. border-radius: 8px;
  135. padding: 1px 6px;
  136. white-space: nowrap;
  137. transition: opacity ${ANIMATION};
  138. opacity: 0;
  139. left: -9999px;
  140. `
  141. const SmallMenuItem = styled(Link)<any>`
  142. position: relative;
  143. cursor: pointer;
  144. width: 38px;
  145. height: 38px;
  146. margin-top: 16px;
  147. display: flex;
  148. justify-content: center;
  149. align-items: center;
  150. ${SmallMenuBackground} {
  151. background: ${props => (props.selected ? 'white' : 'inherit')}
  152. }
  153. &:hover {
  154. ${SmallMenuBackground} {
  155. background: white;
  156. }
  157. ${MenuTooltip} {
  158. opacity: 1;
  159. left: 42px;
  160. }
  161. }
  162. `
  163. const SmallMenuItemBullet = styled.div<any>`
  164. width: 7px;
  165. height: 7px;
  166. border-radius: 50%;
  167. position: absolute;
  168. left: -12px;
  169. background: ${props => (props.bullet === 'replica' ? '#E62565' : '#0044CA')};
  170. `
  171. const MenuImage = styled.div<any>`
  172. width: 24px;
  173. height: 24px;
  174. background: url('${props => props.image}') center no-repeat;
  175. background-size: contain;
  176. `
  177. const Footer = styled.div<any>`
  178. flex-shrink: 1;
  179. display: flex;
  180. flex-direction: column;
  181. justify-content: center;
  182. align-items: center;
  183. margin-bottom: 32px;
  184. `
  185. const CbsLogoWrapper = styled.div<any>`
  186. position: relative;
  187. display: flex;
  188. width: 100%;
  189. height: 34px;
  190. justify-content: center;
  191. `
  192. const CbsLogo = styled.a<any>`
  193. position: absolute;
  194. top: 0;
  195. left: ${props => (isCollapsed(props) ? '-9999px' : 'auto')};
  196. opacity: ${props => (isCollapsed(props) ? 0 : 1)};
  197. width: 128px;
  198. height: 34px;
  199. background: url('${cbsImage}') center no-repeat;
  200. cursor: pointer;
  201. display: flex;
  202. transition: opacity ${ANIMATION};
  203. `
  204. const CbsLogoSmall = styled.a<any>`
  205. position: absolute;
  206. top: 0;
  207. left: ${props => (isCollapsed(props) ? 'auto' : '-9999px')};
  208. opacity: ${props => (isCollapsed(props) ? 1 : 0)};
  209. width: 48px;
  210. height: 34px;
  211. background: url('${cbsImageSmall}') center no-repeat;
  212. cursor: pointer;
  213. display: flex;
  214. transition: opacity ${ANIMATION};
  215. `
  216. export const TEST_ID = 'navigation'
  217. type Props = {
  218. currentPage?: string,
  219. className?: string,
  220. collapsed?: boolean,
  221. hideLogos?: boolean,
  222. }
  223. @observer
  224. class Navigation extends React.Component<Props> {
  225. wrapper: HTMLElement | null | undefined
  226. coriolisLogo: HTMLElement | null | undefined
  227. coriolisLogoSmall: HTMLElement | null | undefined
  228. cbsLogo: HTMLElement | null | undefined
  229. cbsLogoSmall: HTMLElement | null | undefined
  230. menu: HTMLElement | null | undefined
  231. smallMenu: HTMLElement | null | undefined
  232. resizeTimeout: number | null = null
  233. isCollapsed: boolean = false
  234. componentDidMount() {
  235. if (this.props.collapsed) {
  236. return
  237. }
  238. window.addEventListener('resize', this.handleWindowResize)
  239. }
  240. componentWillUnmount() {
  241. if (this.props.collapsed) {
  242. return
  243. }
  244. window.removeEventListener('resize', this.handleWindowResize)
  245. }
  246. get filteredMenu() {
  247. const isAdmin = userStore.loggedUser ? userStore.loggedUser.isAdmin : false
  248. const isDisabled = (page: string) => (configLoader.config
  249. ? configLoader.config.disabledPages.find(p => p === page) : false)
  250. return navigationMenu.filter(i => !isDisabled(i.value) && (!i.requiresAdmin || isAdmin))
  251. }
  252. @autobind
  253. handleCollapsedTransitionEnd() {
  254. if (!this.coriolisLogo || !this.cbsLogo || !this.menu || !this.isCollapsed) {
  255. return
  256. }
  257. this.coriolisLogo.style.left = '-9999px'
  258. this.cbsLogo.style.left = '-9999px'
  259. this.menu.style.left = '-9999px'
  260. this.cbsLogo.removeEventListener('transitionend', this.handleCollapsedTransitionEnd)
  261. }
  262. @autobind
  263. handleExpandedTransitionEnd() {
  264. if (!this.smallMenu || this.isCollapsed || !this.cbsLogoSmall) {
  265. return
  266. }
  267. this.smallMenu.style.left = '-9999px'
  268. this.cbsLogoSmall.style.left = '-9999px'
  269. this.smallMenu.removeEventListener('transitionend', this.handleExpandedTransitionEnd)
  270. }
  271. @autobind
  272. handleWindowResize() {
  273. if (this.resizeTimeout) {
  274. return
  275. }
  276. this.resizeTimeout = window.setTimeout(() => {
  277. this.resizeTimeout = null
  278. this.toggleMenu(window.outerWidth <= ThemeProps.mobileMaxWidth)
  279. }, 100)
  280. }
  281. toggleMenu(toCollapsed: boolean) {
  282. if (
  283. !this.wrapper
  284. || !this.coriolisLogo
  285. || !this.coriolisLogoSmall
  286. || !this.cbsLogo
  287. || !this.cbsLogoSmall
  288. || !this.menu
  289. || !this.smallMenu
  290. ) {
  291. return
  292. }
  293. if (toCollapsed) {
  294. this.smallMenu.style.left = 'auto'
  295. this.cbsLogoSmall.style.left = 'auto'
  296. this.cbsLogo.addEventListener('transitionend', this.handleCollapsedTransitionEnd)
  297. } else {
  298. this.coriolisLogo.style.left = 'auto'
  299. this.cbsLogo.style.left = 'auto'
  300. this.menu.style.left = 'auto'
  301. this.smallMenu.addEventListener('transitionend', this.handleExpandedTransitionEnd)
  302. }
  303. this.isCollapsed = toCollapsed
  304. this.wrapper.style.width = toCollapsed ? '80px' : '320px'
  305. this.coriolisLogoSmall.style.opacity = toCollapsed ? '1' : '0'
  306. this.coriolisLogo.style.opacity = toCollapsed ? '0' : '1'
  307. this.cbsLogo.style.opacity = toCollapsed ? '0' : '1'
  308. this.cbsLogoSmall.style.opacity = toCollapsed ? '1' : '0'
  309. this.menu.style.opacity = toCollapsed ? '0' : '1'
  310. this.smallMenu.style.opacity = toCollapsed ? '1' : '0'
  311. }
  312. renderMenu() {
  313. return (
  314. <Menu
  315. ref={(ref: HTMLElement | null | undefined) => { this.menu = ref }}
  316. collapsed={this.props.collapsed}
  317. >
  318. {
  319. this.filteredMenu.map(item => (
  320. <MenuItem
  321. key={item.value}
  322. selected={this.props.currentPage === item.value}
  323. to={`/${item.value}`}
  324. >{item.label}
  325. </MenuItem>
  326. ))
  327. }
  328. </Menu>
  329. )
  330. }
  331. renderSmallMenu() {
  332. return (
  333. <SmallMenu
  334. ref={(ref: HTMLElement | null | undefined) => { this.smallMenu = ref }}
  335. collapsed={this.props.collapsed}
  336. >
  337. {
  338. this.filteredMenu.map(item => {
  339. let menuImage
  340. let bullet
  341. let style: CSSProperties | null = null
  342. switch (item.value) {
  343. case 'dashboard':
  344. menuImage = dashboardImage
  345. style = { width: '19px', height: '19px' }
  346. break
  347. case 'replicas':
  348. bullet = 'replica'
  349. menuImage = replicaImage
  350. break
  351. case 'migrations':
  352. bullet = 'migration'
  353. menuImage = replicaImage
  354. break
  355. case 'endpoints':
  356. menuImage = endpointImage
  357. break
  358. case 'minion-pools':
  359. menuImage = minionPoolsImage
  360. break
  361. case 'bare-metal-servers':
  362. menuImage = bareMetalServersImage
  363. break
  364. case 'planning':
  365. menuImage = planningImage
  366. break
  367. case 'projects':
  368. menuImage = projectImage
  369. break
  370. case 'users':
  371. menuImage = userImage
  372. break
  373. case 'logging':
  374. menuImage = logsImage
  375. style = { width: '22px', height: '22px' }
  376. break
  377. default:
  378. }
  379. return (
  380. <SmallMenuItem
  381. key={item.value}
  382. selected={this.props.currentPage === item.value}
  383. to={`/${item.value}`}
  384. >
  385. <SmallMenuBackground />
  386. {bullet ? <SmallMenuItemBullet bullet={bullet} /> : null}
  387. <MenuImage image={menuImage} style={style} />
  388. <MenuTooltip>{item.label}</MenuTooltip>
  389. </SmallMenuItem>
  390. )
  391. })
  392. }
  393. </SmallMenu>
  394. )
  395. }
  396. render() {
  397. return (
  398. <Wrapper
  399. ref={(ref: HTMLElement | null | undefined) => { this.wrapper = ref }}
  400. className={this.props.className}
  401. collapsed={this.props.collapsed}
  402. >
  403. {this.props.hideLogos ? null : (
  404. <LogoWrapper>
  405. <LogoStyled
  406. small
  407. collapsed={this.props.collapsed}
  408. to={navigationMenu[0].value}
  409. customRef={(ref: HTMLElement | null | undefined) => { this.coriolisLogo = ref }}
  410. />
  411. <TinyLogo
  412. collapsed={this.props.collapsed}
  413. customRef={(ref: HTMLElement | null | undefined) => { this.coriolisLogoSmall = ref }}
  414. to={navigationMenu[0].value}
  415. />
  416. </LogoWrapper>
  417. )}
  418. <MenuWrapper>
  419. {this.renderMenu()}
  420. {this.renderSmallMenu()}
  421. </MenuWrapper>
  422. <Footer>
  423. {this.props.hideLogos ? null : (
  424. <CbsLogoWrapper>
  425. <CbsLogo
  426. ref={(ref: HTMLElement | null | undefined) => { this.cbsLogo = ref }}
  427. href="https://cloudbase.it"
  428. target="_blank"
  429. collapsed={this.props.collapsed}
  430. />
  431. <CbsLogoSmall
  432. ref={(ref: HTMLElement | null | undefined) => { this.cbsLogoSmall = ref }}
  433. href="https://cloudbase.it"
  434. target="_blank"
  435. collapsed={this.props.collapsed}
  436. />
  437. </CbsLogoWrapper>
  438. )}
  439. </Footer>
  440. </Wrapper>
  441. )
  442. }
  443. }
  444. export default Navigation