diff --git a/client/package.json b/client/package.json index e5b1621b..ee60f1ba 100644 --- a/client/package.json +++ b/client/package.json @@ -45,6 +45,8 @@ "css-loader": "^0.28.4", "i18next-conv": "^3.0.3", "node-sass": "^4.5.3", + "react-dnd-html5-backend": "^2.4.1", + "react-dnd-touch-backend": "^0.3.13", "sass-loader": "^6.0.6", "style-loader": "^0.18.2", "url-loader": "^0.5.9", diff --git a/client/src/lists/segments/CUD.js b/client/src/lists/segments/CUD.js index 5a0cab1d..cba63c35 100644 --- a/client/src/lists/segments/CUD.js +++ b/client/src/lists/segments/CUD.js @@ -12,11 +12,16 @@ import {DeleteModalDialog} from "../../lib/delete"; import interoperableErrors from '../../../../shared/interoperable-errors'; import styles from './CUD.scss'; +import { DragDropContext } from 'react-dnd'; +import HTML5Backend from 'react-dnd-html5-backend'; +import TouchBackend from 'react-dnd-touch-backend'; import SortableTree from 'react-sortable-tree'; import {ActionLink, Icon} from "../../lib/bootstrap-components"; -console.log(styles); +// https://stackoverflow.com/a/4819886/1601953 +const isTouchDevice = !!('ontouchstart' in window || navigator.maxTouchPoints); +@DragDropContext(isTouchDevice ? TouchBackend : HTML5Backend) @translate() @withForm @withPageHelpers @@ -28,6 +33,7 @@ export default class CUD extends Component { this.compoundRuleTypes = [ 'all', 'some', 'one', 'none' ]; +/* const allRule = { type: 'all' }; @@ -69,11 +75,11 @@ export default class CUD extends Component { ] } ]; - +*/ this.state = { - rules: sampleRules, - rulesTree: this.getTreeFromRules(sampleRules) + rules: [], + rulesTree: this.getTreeFromRules([]) }; this.initForm(); @@ -89,7 +95,7 @@ export default class CUD extends Component { getRulesFromTree(tree) { const rules = []; for (const node of tree) { - const rule = Object.assign({}, node.rule); + const rule = node.rule; rule.rules = this.getRulesFromTree(node.children); rules.push(rule); } @@ -185,16 +191,49 @@ export default class CUD extends Component { } async onRuleDelete(data) { - console.log(data); + let finishedSearching = false; + + function childrenWithoutRule(rules) { + console.log(rules); + const newRules = []; + + for (const rule of rules) { + if (finishedSearching) { + newRules.push(rule); + + } else if (rule !== data.node.rule) { + const newRule = Object.assign({}, rule); + + if (rule.rules) { + newRule.rules = childrenWithoutRule(rule.rules); + } + + newRules.push(newRule); + + } else { + finishedSearching = true; + } + } + + return newRules; + } + + const rules = childrenWithoutRule(this.state.rules); + console.log(rules); + + this.setState({ + rules, + rulesTree: this.getTreeFromRules(rules) + }); } - async onRuleOptions(data) { + async showRuleOptions(data) { this.setState({ ruleOptionsVisible: true }); } - async onRuleTree() { + async hideRuleOptions() { this.setState({ ruleOptionsVisible: false }); @@ -207,6 +246,28 @@ export default class CUD extends Component { }) } + _addRule(type) { + const rules = this.state.rules; + + rules.push({ + type, + rules: [] + }); + + this.setState({ + rules, + rulesTree: this.getTreeFromRules(rules) + }); + } + + async addCompositeRule() { + this._addRule('all'); + } + + async addRule() { + this._addRule('eq'); + } + render() { const t = this.props.t; const isEdit = !!this.props.entity; @@ -251,18 +312,31 @@ export default class CUD extends Component {