Home.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import React from 'react';
  2. import ReactMarkdown from 'react-markdown';
  3. import { Link } from 'react-router-dom';
  4. import { intro } from '../resources/const';
  5. const markdown = require('../resources/markdown.json');
  6. const getHandleId = (() => {
  7. let id = 0;
  8. return () => ('handle-' + (++id));
  9. })();
  10. console.log(getHandleId(), getHandleId());
  11. function getSectionLinks() {
  12. return (
  13. <ul>
  14. {markdown.map((section, sIndex) => (
  15. <li key={sIndex}><Link to={section.path}>{section.title}</Link></li>
  16. ))}
  17. </ul>
  18. );
  19. }
  20. const tutos = [
  21. { name: 'JavaScript', items: [
  22. { name: 'pouet', items: [
  23. { name: 'pouet child', items: [] }
  24. ] },
  25. { name: 'popop', items: [] },
  26. { name: 'douap', items: [] }
  27. ] },
  28. { name: 'jQuery', items: [] },
  29. { name: 'AngularJS', items: [] },
  30. { name: 'React', items: [] }
  31. ];
  32. class Tree extends React.Component {
  33. render() {
  34. const { items } = this.props;
  35. return (
  36. <div>
  37. <h2>X</h2>
  38. {items.map(item =>
  39. <TreeNode item={item} key={item.name} level={0} />
  40. )}
  41. </div>
  42. );
  43. }
  44. }
  45. /*
  46. * get DOM ref => https://reactjs.org/docs/refs-and-the-dom.html
  47. * dnd w/ handle => https://github.com/jdiamond/html5-examples/blob/master/drag-and-drop/drag-handle.html
  48. */
  49. class TreeNode extends React.Component {
  50. constructor(props) {
  51. super(props);
  52. this.onMouseDown = this.onMouseDown.bind(this);
  53. this.onDragStart = this.onDragStart.bind(this);
  54. }
  55. onMouseDown(e) {
  56. this.dragTarget = e.target;
  57. console.log('onMouseDown', this.dragTarget);
  58. }
  59. onDragStart(e) {
  60. // e.preventDefault();
  61. e.stopPropagation();
  62. console.log('onDragStart', this.props.item.name, this.draggable);
  63. const { draggable } = this;
  64. var handle = draggable.getElementsByClassName('drag-handle')[0];
  65. console.log(this.draggable, handle.id)
  66. // draggable.onmousedown = function(e) {
  67. // target = e.target;
  68. // };
  69. // draggable.ondragstart = function(e) {
  70. if (handle.contains(this.dragTarget)) {
  71. console.log('transfer to handle')
  72. e.dataTransfer.setData('text/plain', handle.id);
  73. } else {
  74. console.log('abord onDragStart')
  75. e.preventDefault();
  76. }
  77. // };
  78. }
  79. render() {
  80. const { name, items } = this.props.item;
  81. const { level } = this.props;
  82. const paddingLeft = (level * 15) + 'px';
  83. return (
  84. <div className="tree-node" style={{ paddingLeft }} onMouseDown={this.onMouseDown} onDragStart={this.onDragStart} ref={(div) => { this.draggable = div; }} draggable>
  85. <h5><i id={ getHandleId() } className="material-icons drag-handle">drag_handle</i><span>{ name }</span></h5>
  86. {items.map(item =>
  87. <TreeNode item={item} key={item.name} level={level + 1} />
  88. )}
  89. </div>
  90. );
  91. }
  92. }
  93. class Home extends React.Component {
  94. render() {
  95. return (
  96. <div className="page">
  97. <div className="breadcrumb-wrapper">
  98. <span className="breadcrumb light-blue-text"><i className="material-icons">home</i></span>
  99. <span className="breadcrumb grey-text">Accueil</span>
  100. </div>
  101. <Tree items={tutos} />
  102. </div>
  103. );
  104. }
  105. }
  106. export default Home;