-
Notifications
You must be signed in to change notification settings - Fork 948
/
PickListTransferControls.js
117 lines (95 loc) · 4.14 KB
/
PickListTransferControls.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import React, { Component } from 'react';
import { ObjectUtils, classNames } from '../utils/Utils';
import { Button } from '../button/Button';
export class PickListTransferControls extends Component {
constructor(props) {
super(props);
this.moveRight = this.moveRight.bind(this);
this.moveAllRight = this.moveAllRight.bind(this);
this.moveLeft = this.moveLeft.bind(this);
this.moveAllLeft = this.moveAllLeft.bind(this);
}
moveRight(event) {
let selection = this.props.sourceSelection;
if (ObjectUtils.isNotEmpty(selection)) {
let targetList = [...this.props.target];
let sourceList = [...this.props.source];
for (let i = 0; i < selection.length; i++) {
let selectedItem = selection[i];
if (ObjectUtils.findIndexInList(selectedItem, targetList, this.props.dataKey) === -1) {
targetList.push(sourceList.splice(ObjectUtils.findIndexInList(selectedItem, sourceList, this.props.dataKey), 1)[0]);
}
}
if (this.props.onTransfer) {
this.props.onTransfer({
originalEvent: event,
source: sourceList,
target: targetList,
direction: 'toTarget'
})
}
}
}
moveAllRight(event) {
if (this.props.source) {
let targetList = [...this.props.target, ...this.props.source];
let sourceList = [];
if (this.props.onTransfer) {
this.props.onTransfer({
originalEvent: event,
source: sourceList,
target: targetList,
direction: 'allToTarget'
})
}
}
}
moveLeft(event) {
let selection = this.props.targetSelection;
if (ObjectUtils.isNotEmpty(selection)) {
let targetList = [...this.props.target];
let sourceList = [...this.props.source];
for (let i = 0; i < selection.length; i++) {
let selectedItem = selection[i];
if (ObjectUtils.findIndexInList(selectedItem, sourceList, this.props.dataKey) === -1) {
sourceList.push(targetList.splice(ObjectUtils.findIndexInList(selectedItem, targetList, this.props.dataKey), 1)[0]);
}
}
if (this.props.onTransfer) {
this.props.onTransfer({
originalEvent: event,
source: sourceList,
target: targetList,
direction: 'toSource'
})
}
}
}
moveAllLeft(event) {
if (this.props.source) {
let sourceList = [...this.props.source, ...this.props.target];
let targetList = [];
if (this.props.onTransfer) {
this.props.onTransfer({
originalEvent: event,
source: sourceList,
target: targetList,
direction: 'allToSource'
})
}
}
}
render() {
let moveRightDisabled = ObjectUtils.isEmpty(this.props.sourceSelection);
let moveLeftDisabled = ObjectUtils.isEmpty(this.props.targetSelection);
let moveAllRightDisabled = ObjectUtils.isEmpty(this.props.source);
let moveAllLeftDisabled = ObjectUtils.isEmpty(this.props.target);
let className = classNames('p-picklist-buttons p-picklist-transfer-buttons', this.props.className);
return <div className={className}>
<Button disabled={moveRightDisabled} type="button" icon="pi pi-angle-right" onClick={this.moveRight}></Button>
<Button disabled={moveAllRightDisabled} type="button" icon="pi pi-angle-double-right" onClick={this.moveAllRight}></Button>
<Button disabled={moveLeftDisabled} type="button" icon="pi pi-angle-left" onClick={this.moveLeft}></Button>
<Button disabled={moveAllLeftDisabled} type="button" icon="pi pi-angle-double-left" onClick={this.moveAllLeft}></Button>
</div>;
}
}