index.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. const markdown = require('./markdown.json');
  6. const intro = `
  7. Cette appli est un petit guide pour débuter en JavaScript, qui s'adresse à ceux
  8. qui connaissent déjà un autre langage - Java en l'occurence.
  9. On va présenter les bases nécessaires pour interagir avec une page web, sans
  10. entrer dans les détails du langage.
  11. Précisons que le fait qu'il soit disponible sur tous les navigateurs et machines
  12. (ordinateurs, tablettes, smartphones), fait de JavaScript la plate-forme d'exécution
  13. de programmes **la plus répandue**.
  14. `;
  15. class Page extends React.Component {
  16. componentDidMount() {
  17. Prism.highlightAll();
  18. $('.page ul').addClass('browser-default');
  19. }
  20. render() {
  21. const { title, content } = this.props.page;
  22. const { section } = this.props;
  23. return (
  24. <div className="page">
  25. <div>
  26. <Link to='/' className="breadcrumb light-blue-text"><i className="material-icons">home</i></Link>
  27. <Link to={section.path} className="breadcrumb light-blue-text">{section.title}</Link>
  28. <span className="breadcrumb grey-text">{title}</span>
  29. </div>
  30. <h4 className="header orange-text">{title}</h4>
  31. <ReactMarkdown source={content} escapeHtml={false} />
  32. </div>
  33. );
  34. }
  35. }
  36. class Home extends React.Component {
  37. render() {
  38. return (
  39. <div>
  40. <div>
  41. <span className="breadcrumb light-blue-text"><i className="material-icons">home</i></span>
  42. <span className="breadcrumb grey-text">Accueil</span>
  43. </div>
  44. <h4 className="header orange-text">Accueil</h4>
  45. <ReactMarkdown source={intro} />
  46. {getSectionLinks()}
  47. </div>
  48. );
  49. }
  50. }
  51. function getSection(sectionIndex) {
  52. const section = markdown[sectionIndex];
  53. return () => (
  54. <div>
  55. <div>
  56. <Link to='/' className="breadcrumb light-blue-text"><i className="material-icons">home</i></Link>
  57. <span className="breadcrumb grey-text">{section.title}</span>
  58. </div>
  59. <h4 className="header orange-text">{section.title}</h4>
  60. {getSectionPageLinks(section)}
  61. </div>
  62. );
  63. }
  64. function getSectionPagination(section, activeIndex) {
  65. const lastIndex = section.items.length - 1;
  66. const previousHref = activeIndex === 0 ? section.path :
  67. section.path + section.items[activeIndex - 1].path;
  68. const nextHref = activeIndex === lastIndex ? section.path :
  69. section.path + section.items[activeIndex + 1].path;
  70. return (
  71. <div className="pagination-wrapper">
  72. <ul className="pagination">
  73. <li className={ activeIndex === 0 ? "disabled": "waves-effect"}>
  74. <Link to={previousHref}><i className="material-icons">chevron_left</i></Link>
  75. </li>
  76. {
  77. section.items.map((page, pIndex) => (
  78. <li key={pIndex} className={ activeIndex === pIndex ? "active orange": "waves-effect"}>
  79. <Link to={section.path + page.path}>{pIndex + 1}</Link>
  80. </li>
  81. ))
  82. }
  83. <li className={ activeIndex === lastIndex ? "disabled": "waves-effect"}>
  84. <Link to={nextHref}><i className="material-icons">chevron_right</i></Link>
  85. </li>
  86. </ul>
  87. </div>
  88. );
  89. }
  90. function getPage(sectionIndex, pageIndex) {
  91. const section = markdown[sectionIndex];
  92. const page = section.items[pageIndex];
  93. return () => (
  94. <div>
  95. <Page page={page} section={section} />
  96. {getSectionPagination(section, pageIndex)}
  97. </div>
  98. );
  99. }
  100. function getSectionPageLinks(section) {
  101. return (
  102. <ul>
  103. {section.items.map((page, pIndex) => (
  104. <li key={pIndex}><Link to={section.path + page.path}>{page.title}</Link></li>
  105. ))}
  106. </ul>
  107. );
  108. }
  109. function getSectionLinks() {
  110. return (
  111. <ul>
  112. {markdown.map((section, sIndex) => (
  113. <li key={sIndex}><Link to={section.path}>{section.title}</Link></li>
  114. ))}
  115. </ul>
  116. );
  117. }
  118. function getSectionRoutes(section, sIndex) {
  119. const routes = [{
  120. key: sIndex, path: section.path, component: getSection(sIndex)
  121. }].concat(section.items.map((page, pIndex) => ({
  122. key: sIndex + '-' + pIndex,
  123. path: section.path + page.path,
  124. component: getPage(sIndex, pIndex)
  125. })));
  126. // return <Route key={sIndex} exact path={section.path} component={getSection(sIndex)} />;
  127. return routes.map(r => <Route key={r.key} exact path={r.path} component={r.component} />);
  128. }
  129. ReactDOM.render(
  130. <HashRouter>
  131. <div>
  132. {/*}<ul>{
  133. markdown.map((section, sIndex) => (
  134. <li key={sIndex}><Link to={section.path}>{section.title}</Link>
  135. {getSectionPageLinks(section)}
  136. </li>
  137. ))
  138. }
  139. <li><Link to="/">Home</Link></li>
  140. </ul>*/}
  141. <Switch>
  142. <Route exact path="/" component={Home} />
  143. {
  144. markdown.map(getSectionRoutes)
  145. }
  146. </Switch>
  147. </div>
  148. </HashRouter>,
  149. document.getElementById('root')
  150. );