index.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import ReactMarkdown from 'react-markdown';
  4. import { Switch, Route, HashRouter, Link } from 'react-router-dom';
  5. import About from './components/About';
  6. import Home from './components/Home';
  7. import Navbar from './components/Navbar';
  8. import Page from './components/Page';
  9. const markdown = require('./resources/markdown.json');
  10. const $leftPanel = $('#left-panel');
  11. const $window = $(window);
  12. function onResize() {
  13. $leftPanel.css('max-height', $window.height());
  14. }
  15. $window.on('resize', onResize);
  16. function getSection(sectionIndex) {
  17. const section = markdown[sectionIndex];
  18. return () => (
  19. <div>
  20. <div className="breadcrumb-wrapper">
  21. <Link to='/' className="breadcrumb teal-text"><i className="material-icons">home</i></Link>
  22. <span className="breadcrumb grey-text">{section.title}</span>
  23. </div>
  24. <h4 className="header brown-text">{section.title}</h4>
  25. {getSectionPageLinks(section)}
  26. </div>
  27. );
  28. }
  29. function getSectionPagination(section, activeIndex) {
  30. const lastIndex = section.items.length - 1;
  31. const previousHref = activeIndex === 0 ? section.path :
  32. section.path + section.items[activeIndex - 1].path;
  33. const nextHref = activeIndex === lastIndex ? section.path :
  34. section.path + section.items[activeIndex + 1].path;
  35. return (
  36. <div className="pagination-wrapper">
  37. <ul className="pagination">
  38. <li className={ activeIndex === 0 ? "disabled": "waves-effect"}>
  39. <Link to={previousHref}><i className="material-icons">chevron_left</i></Link>
  40. </li>
  41. {
  42. section.items.map((page, pIndex) => (
  43. <li key={pIndex} className={ activeIndex === pIndex ? "active brown": "waves-effect"}>
  44. <Link to={section.path + page.path}>{pIndex + 1}</Link>
  45. </li>
  46. ))
  47. }
  48. <li className={ activeIndex === lastIndex ? "disabled": "waves-effect"}>
  49. <Link to={nextHref}><i className="material-icons">chevron_right</i></Link>
  50. </li>
  51. </ul>
  52. </div>
  53. );
  54. }
  55. function getPage(sectionIndex, pageIndex) {
  56. const section = markdown[sectionIndex];
  57. const page = section.items[pageIndex];
  58. return () => (
  59. <div>
  60. <Page page={page} section={section} />
  61. {getSectionPagination(section, pageIndex)}
  62. </div>
  63. );
  64. }
  65. function getSectionPageLinks(section) {
  66. return (
  67. <ul>
  68. {section.items.map((page, pIndex) => (
  69. <li key={pIndex}><Link to={section.path + page.path}>{page.title}</Link></li>
  70. ))}
  71. </ul>
  72. );
  73. }
  74. function getSectionRoutes(section, sIndex) {
  75. const routes = [{
  76. key: sIndex, path: section.path, component: getSection(sIndex)
  77. }].concat(section.items.map((page, pIndex) => ({
  78. key: sIndex + '-' + pIndex,
  79. path: section.path + page.path,
  80. component: getPage(sIndex, pIndex)
  81. })));
  82. return routes.map(r => <Route key={r.key} exact path={r.path} component={r.component} />);
  83. }
  84. ReactDOM.render(
  85. <HashRouter>
  86. <div id="react-wrapper">
  87. <Navbar />
  88. <div className="main">
  89. <Switch>
  90. <Route exact path="/" component={Home} />
  91. <Route exact path="/a-propos" component={About} />
  92. {
  93. markdown.map(getSectionRoutes)
  94. }
  95. </Switch>
  96. </div>
  97. </div>
  98. </HashRouter>,
  99. document.getElementById('left-panel')
  100. );