Commit 5afb1e8b by hfpp2012 yinsigan

fix ElementGroup mobx

1 parent 54785816
...@@ -2,35 +2,22 @@ import React from 'react' ...@@ -2,35 +2,22 @@ import React from 'react'
import Element from './Element' import Element from './Element'
import ReactAddonsUpdate from 'react-addons-update' import ReactAddonsUpdate from 'react-addons-update'
import autobind from 'autobind-decorator' import autobind from 'autobind-decorator'
import { observer } from 'mobx-react'
@autobind @autobind
@observer
export default class ElementGroup extends React.Component { export default class ElementGroup extends React.Component {
constructor(props){
super(props);
this.state = {
elements: [
{ id: 1, name: "药库", initialPos: {x: 80, y: 70}, target_type: 'RegionalDesign::Repo' },
{ id: 2, name: "药架", initialPos: {x: 80, y: 70 + 60}, target_type: 'RegionalDesign::DrugRepo' },
{ id: 3, name: "药架格", initialPos: {x: 80, y: 70 + 60 * 2}, target_type: 'RegionalDesign::DrugBrandreth' }
]
};
}
cloneElement(element) { cloneElement(element) {
const elements = ReactAddonsUpdate(this.state.elements, { $push: [element] }) this.props.ElementGroupStore.cloneElement(element)
this.setState({elements: elements})
} }
deleteElement(element) { deleteElement(element) {
const index = this.state.elements.indexOf(element) this.props.ElementGroupStore.deleteElement(element)
const elements = ReactAddonsUpdate(this.state.elements, { $splice: [[index, 1]] })
this.setState({elements: elements})
} }
// 返回element的index为-1, 删除最后一个元素 // 返回element的index为-1, 删除最后一个元素
deleteLastElement() { deleteLastElement() {
const elements = ReactAddonsUpdate(this.state.elements, { $splice: [[-1, 1]] }) this.props.ElementGroupStore.deleteLastElement()
this.setState({elements: elements})
} }
sidebar() { sidebar() {
...@@ -41,7 +28,7 @@ export default class ElementGroup extends React.Component { ...@@ -41,7 +28,7 @@ export default class ElementGroup extends React.Component {
return ( return (
<div> <div>
{ {
this.state.elements.map(function(element) { this.props.ElementGroupStore.elements.map(function(element) {
return ( return (
<Element meta_element={ element } key={ "key_" + element.id } id={ element.id } name={ element.name } initialPos={ element.initialPos } target_type={ element.target_type } handleCloneElement={ this.cloneElement } handleDeleteElement={ this.deleteElement } handleDeleteLastElement={ this.deleteLastElement } /> <Element meta_element={ element } key={ "key_" + element.id } id={ element.id } name={ element.name } initialPos={ element.initialPos } target_type={ element.target_type } handleCloneElement={ this.cloneElement } handleDeleteElement={ this.deleteElement } handleDeleteLastElement={ this.deleteLastElement } />
) )
......
...@@ -7,6 +7,9 @@ import EventEmitter from '../libs/eventEmitter' ...@@ -7,6 +7,9 @@ import EventEmitter from '../libs/eventEmitter'
import headerModel from '../stores/HeaderStore' import headerModel from '../stores/HeaderStore'
const headerStore = new headerModel() const headerStore = new headerModel()
import ElementGroupModel from '../stores/ElementGroupStore'
const ElementGroupStore = new ElementGroupModel()
export default class Spd extends React.Component { export default class Spd extends React.Component {
componentDidMount() { componentDidMount() {
EventEmitter.subscribe('alert', function(info){ EventEmitter.subscribe('alert', function(info){
...@@ -46,7 +49,7 @@ export default class Spd extends React.Component { ...@@ -46,7 +49,7 @@ export default class Spd extends React.Component {
<div className="row"> <div className="row">
<div className="col-md-2 sidebar" id='sidebar'> <div className="col-md-2 sidebar" id='sidebar'>
<h3 className='sidebar-header'>元件</h3> <h3 className='sidebar-header'>元件</h3>
<ElementGroup /> <ElementGroup ElementGroupStore={ ElementGroupStore } />
</div> </div>
<div id="dropzone" className="col-md-10 dropzone"> <div id="dropzone" className="col-md-10 dropzone">
{this.props.children} {this.props.children}
......
import { observable, useStrict, action } from 'mobx'
useStrict(true)
export default class headerStore {
@observable elements = [
{ id: 1, name: "药库", initialPos: {x: 80, y: 70}, target_type: 'RegionalDesign::Repo' },
{ id: 2, name: "药架", initialPos: {x: 80, y: 70 + 60}, target_type: 'RegionalDesign::DrugRepo' },
{ id: 3, name: "药架格", initialPos: {x: 80, y: 70 + 60 * 2}, target_type: 'RegionalDesign::DrugBrandreth' }
]
@action cloneElement(element) {
this.elements.push(element)
}
@action deleteElement(element) {
this.elements.remove(element)
}
@action deleteLastElement() {
this.elements.pop()
}
}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!