Advertisement
Guest User

Untitled

a guest
Dec 9th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.03 KB | None | 0 0
  1. import React, {Component} from 'react';
  2. import { connect } from 'react-redux';
  3.  
  4. function mapDispatchToProps(dispatch){
  5. return {
  6. action: (x) => {
  7. dispatch(x)
  8. },
  9. }
  10. }
  11.  
  12. function mapStateToProps(store) {
  13. return {
  14. tree: store.tree
  15. };
  16. }
  17.  
  18. class Structure extends Component {
  19. state = {
  20. update: false,
  21. drag: false,
  22. popup: {display:'none', position: 'fixed', left:0, top:0, zIndex: 10}
  23. }
  24. componentDidMount(){/*
  25. let interval = 1500;
  26. setInterval(()=>{
  27. this.setState({ update: !this.state.update })
  28. }, interval);*/
  29. window.document.addEventListener('click', this.contextMenuHide.bind(this), false);
  30. this.refs.main.setAttribute('onclick', 'tree_toggle(arguments[0])');
  31. }
  32. update(){
  33. this.setState({ update: !this.state.update })
  34. }
  35. contextMenuShow(id, e) {
  36. e.preventDefault();
  37. this.refs.create.setAttribute('data-create', id);
  38. this.refs.edit.setAttribute('data-edit', id);
  39. this.refs.del.setAttribute('data-del', id);
  40.  
  41. let pos = {display:'block', position: 'fixed', left: e.pageX+'px', top: e.pageY-window.scrollY+'px'};
  42. this.setState({popup: pos})
  43. }
  44. contextMenuHide(e) {
  45. let pos = {display:'none', position: 'fixed', left: e.pageX+'px', top: e.pageY+'px'};
  46. this.setState({popup: pos})
  47. }
  48. getPropId(){
  49. let id = this.refs.edit.getAttribute('data-edit');
  50. this.editFree(id)
  51. }
  52. editFree(id){
  53. this.props.action({
  54. type: 'editId',
  55. data: id
  56. })
  57. this.props.action({
  58. type: 'newBranch',
  59. data: null
  60. })
  61. }
  62. addBranch0(){
  63. this.props.action({
  64. type: 'editId',
  65. data: '0'
  66. })
  67. this.props.action({
  68. type: 'newBranch',
  69. data: '0'
  70. })
  71. }
  72. addBranch(){
  73. let id = this.refs.create.getAttribute('data-create');
  74. this.props.action({
  75. type: 'editId',
  76. data: id
  77. })
  78. this.props.action({
  79. type: 'newBranch',
  80. data: id
  81. })
  82. }
  83. delBranch(){
  84. let id = this.refs.del.getAttribute('data-del');
  85. this.props.action({
  86. type: 'delBranch',
  87. data: { id: id }
  88. })
  89. }
  90. DragStart(el, e){
  91. let img = e.target.getElementsByClassName('Content')[0];
  92. e.dataTransfer.effectAllowed = 'move';
  93. e.dataTransfer.setDragImage(img,10,5);
  94. e.dataTransfer.setData('application/text', JSON.stringify(el) );
  95. this.setState({ drag: true });
  96. e.stopPropagation();
  97. }
  98. DragEnter(e){
  99. if (e.target.className != 'Content') return false;
  100. e.target.style.color = '#f00';
  101. }
  102. DragLeave(e){
  103. if (e.target.className != 'Content') return false;
  104. e.target.style.color = '#000'
  105. }
  106. DragEnd(e){
  107.  
  108. }
  109. DragOver(e){
  110. e.preventDefault();
  111. }
  112. onDrop(parent, e){
  113. if (!e) return;
  114. let elMv = JSON.parse(e.dataTransfer.getData('application/text') );
  115. if (parent.id == elMv.id) return;
  116.  
  117. e.target.style.color = '#000';
  118.  
  119. if (parent == 'del') {
  120. this.props.action({
  121. type: 'delBranch',
  122. data: elMv
  123. })
  124. }
  125. else {
  126. this.props.action({
  127. type: 'editParent',
  128. data: {
  129. parent: parent,
  130. children: elMv
  131. }
  132. })
  133. }
  134.  
  135. this.setState({ drag: false });
  136. e.preventDefault();
  137. e.stopPropagation();
  138. }
  139. DragSpace(){
  140. return(
  141. <div>
  142. <div className="drag"
  143. onDragOver={this.DragOver.bind(this)}
  144. onDrop={this.onDrop.bind(this, { id: 0} )} >
  145. <strong>Переместить в корень</strong>
  146. </div>
  147. <div className="drag"
  148. onDragOver={this.DragOver.bind(this)}
  149. onDrop={this.onDrop.bind(this, 'del')} >
  150. <strong>Удалить</strong>
  151. </div>
  152. </div>
  153. )
  154. }
  155. renderTree(id){
  156. const {tree} = this.props;
  157. return tree.map((el, i, arr) => {
  158.  
  159. if (id == el.parent) {
  160.  
  161. let nextPar = arr[i+1] ? arr[i+1].parent : null;
  162.  
  163. function Parent(){
  164. let last = true;
  165.  
  166. tree.map(el_2 => {
  167. if (el.id == el_2.parent) {
  168. last = false
  169. }
  170. })
  171.  
  172. if (last) {
  173. if (el.parent == nextPar) {
  174. return 'Node ExpandLeaf'
  175. }
  176. else {
  177. return 'Node ExpandLeaf IsLast'
  178. }
  179. }
  180. if (el.parent == nextPar) {
  181. return 'Node ExpandClosed'
  182. }
  183. else {
  184. return 'Node ExpandClosed IsLast'
  185. }
  186. }
  187.  
  188. return(
  189. <ul className="Container" key={el.id} >
  190. <li className={Parent()} draggable="true"
  191. onDragStart={this.DragStart.bind(this, el)}
  192. onDragEnter={this.DragEnter.bind(this)}
  193. onDragLeave={this.DragLeave.bind(this)}
  194. onDragEnd={this.DragEnd.bind(this)}
  195. onDragOver={this.DragOver.bind(this)}
  196. onDrop={this.onDrop.bind(this, el)} >
  197. <div className="Expand"></div>
  198. <div className="Content" onContextMenu={this.contextMenuShow.bind(this, el.id)} onClick={this.editFree.bind(this, el.id)}>{el.key}</div>
  199. { this.renderTree(el.id) }
  200. </li>
  201. </ul>
  202. )
  203. }
  204. })
  205. }
  206. renderRoot(){
  207. let tree = this.props.tree.sort((a, b) => {
  208. if( a.parent == b.parent ){
  209. return a.id - b.id
  210. }
  211. else{
  212. return a.parent - b.parent
  213. }
  214. });
  215. return tree.map((el, i, tree) => {
  216. let nextPar = tree[i+1] ? tree[i+1].parent : null;
  217. let check = 'ExpandLeaf';
  218. tree.forEach(el_2 => {
  219. if(el.id == el_2.parent)
  220. check = 'ExpandClosed';
  221. })
  222. if (!el.parent)
  223. return(
  224. <li className={el.parent == nextPar ? 'Node IsRoot '+check : 'Node IsRoot '+check+' IsLast'} key={i}
  225. draggable="true"
  226. onDragStart={this.DragStart.bind(this, el)}
  227. onDragEnter={this.DragEnter.bind(this)}
  228. onDragLeave={this.DragLeave.bind(this)}
  229. onDragEnd={this.DragEnd.bind(this)}
  230. onDragOver={this.DragOver.bind(this)}
  231. onDrop={this.onDrop.bind(this, el)} >
  232. <div className="Expand"></div>
  233. <div className="Content" onContextMenu={this.contextMenuShow.bind(this, el.id)} onClick={this.editFree.bind(this, el.id)}>{el.key}</div>
  234. {this.renderTree( el.id)}
  235. </li>
  236. )
  237. })
  238. }
  239. render(){
  240.  
  241. return(
  242. <div ref="main">
  243. <div style={{cursor: 'pointer'}} onClick={ function(){this.update(); this.addBranch0() }.bind(this) }>Скрипт диалогов</div>
  244. <ul className="Container">
  245. {this.renderRoot()}
  246. </ul>
  247. {this.state.drag ? this.DragSpace() : null}
  248. <ul className="list-ul" style={this.state.popup}>
  249. <li ref="edit" data-edit="" onClick={this.getPropId.bind(this)}>Редактировать</li>
  250. <li ref="create" data-create="" onClick={this.addBranch.bind(this)}>Создать дочерний элемент</li>
  251. <li ref="del" data-del="" onClick={this.delBranch.bind(this)}>Удалить</li>
  252. </ul>
  253. </div>
  254. )
  255. }
  256. }
  257.  
  258. export default connect(mapStateToProps,mapDispatchToProps)(Structure)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement