/
CommandTable.jsx
168 lines (141 loc) · 4.41 KB
/
CommandTable.jsx
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import React from "react";
import { Table, Icon, Button, Divider, Popconfirm } from "antd";
import AbstractDnDTable from "../../AbstractDnDTable";
import { connectDnD } from "../../DragableRow";
import { CommandRowLabel } from "./CommandRowLabel";
import ErrorBoundary from "component/ErrorBoundary";
import { confirmDeleteEntity } from "service/smalltalk";
import { remote } from "electron";
const { Menu, MenuItem } = remote;
@connectDnD
export class CommandTable extends AbstractDnDTable {
state = {
contextMenuAnchor: null
}
constructor( props ) {
super( props );
this.columns = [
{
title: "Comand",
dataIndex: "target",
render: ( text, record ) => ( <CommandRowLabel record={ record } /> )
},
this.getActionColumn()
];
}
model = "Command";
updateSuiteModified() {
this.props.action.updateSuite({
modified: true
});
}
onContextMenu = ( e, record ) => {
e.preventDefault();
const menu = new Menu();
this.setState({ contextMenuAnchor: record.id });
menu.on( "menu-will-close", () => {
this.setState({ contextMenuAnchor: null });
});
menu.append( new MenuItem({
label: "Edit",
click: () => this.onEditCommand( record )
}) );
menu.append( new MenuItem(
record.disabled ? {
label: "Enable",
click: () => this.updateRecord({ id: record.id, disabled: false })
} : {
label: "Disable",
click: () => this.updateRecord({ id: record.id, disabled: true })
}
) );
menu.append( new MenuItem({
label: "Clone",
click: () => this.cloneRecord( record )
}) );
menu.append( new MenuItem({
type: "separator"
}) );
menu.append( new MenuItem({
label: "Delete",
click: async () => {
await confirmDeleteEntity( "command" ) && this.removeRecord( record.id );
}
}) );
menu.popup({
x: e.x,
y: e.y
});
}
updateRecord = ( options ) => {
const update = this.props.action[ `update${this.model}` ];
update( this.extendActionOptions( options ) );
this.updateSuiteModified();
}
removeRecord = ( id ) => {
const update = this.props.action[ `remove${this.model}` ];
update( this.extendActionOptions({ id }) );
this.updateSuiteModified();
}
addRecord = () => {
const update = this.props.action[ `add${this.model}` ];
update( this.extendActionOptions({ target: "", method: "" }) );
}
componentDidUpdate( prevProps ) {
// Only when we add a new command to the list, so we can edit it in the modal
if ( prevProps.commands.length !== this.props.commands.length ) {
const newlyAdded = this.props.commands.find( command => !command.target && !command.method );
newlyAdded && this.onEditCommand( newlyAdded );
}
}
onEditCommand( record ) {
const { updateApp } = this.props.action;
updateApp({
commandModal: {
isVisible: true,
record,
targets: this.props.targets,
commands: this.props.commands
}
});
}
getActionColumn() {
return {
title: "Action",
dataIndex: "action",
width: 160,
className: "table-actions-cell",
render: ( text, record ) => ( <span className="table-actions" role="status" >
<a className="link--action"
tabIndex={-1} role="button"
onClick={ () => this.onEditCommand( record ) }>Edit</a>
<Divider type="vertical" />
<Popconfirm title="Sure to delete?" onConfirm={() => this.removeRecord( record.id )}>
<a className="link--action" tabIndex={-2} role="button">Delete</a>
</Popconfirm>
</span> )
};
}
onRowClassName = ( record ) => {
return `model--command${ record.disabled ? " row-disabled" : "" } ` + this.getRightClickClassName( record );
}
render() {
const commands = this.props.commands.filter( command => command.target && command.method );
return ( <ErrorBoundary>
<Table
className="draggable-table"
id="cCommandTable"
components={ this.components }
rowClassName={ this.onRowClassName }
onRow={ this.onRow }
showHeader={ false }
dataSource={ commands }
columns={ this.columns }
pagination={ false }
footer={() => ( <Button
id="cCommandTableAddBtn"
onClick={ this.addRecord }><Icon type="plus" />Add a command</Button> )}
/>
</ErrorBoundary> );
}
}