forked from fossasia/visdom
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPane.js
More file actions
264 lines (240 loc) · 7.47 KB
/
Pane.js
File metadata and controls
264 lines (240 loc) · 7.47 KB
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/**
* Copyright 2017-present, The Visdom Authors
* All rights reserved.
*
* This source code is licensed under the license found in the
* LICENSE file in the root directory of this source tree.
*
*/
//Pane.js
import React, { forwardRef, useRef, useState } from 'react';
import PropertyItem from './PropertyItem';
var classNames = require('classnames');
var Pane = forwardRef((props, ref) => {
const { id, title, content, children, widgets, enablePropertyList } = props;
var { barwidgets = [] } = props;
// state varibles
// --------------
const [propertyListShown, setPropertyListShown] = useState(false);
const [exportMenuOpen, setExportMenuOpen] = useState(false);
const barRef = useRef();
// public events
// -------------
const handleOnFocus = props.handleOnFocus || (() => props.onFocus(id));
const handleDownload = props.handleDownload || (() => {});
const handleReset = props.handleReset || (() => {});
const handleZoom = props.handleZoom || (() => {});
const handleMouseMove = props.handleMouseMove || (() => {});
const handleClose = props.handleClose || (() => props.onClose(id));
const handleShowExportHistory =
props.onShowExportHistory || (() => {});
// rendering
// ---------
let windowClassNames = classNames({ window: true, focus: props.isFocused });
let barClassNames = classNames({ bar: true, focus: props.isFocused });
// add property list button to barwidgets
if (
enablePropertyList &&
content &&
typeof content == 'object' &&
content.data
) {
barwidgets = [
...barwidgets,
<button
key="properties-widget-button"
title="properties"
onClick={() => {
setPropertyListShown(!propertyListShown);
}}
className={propertyListShown ? 'pull-right active' : 'pull-right'}
>
<span className="glyphicon glyphicon-tags" />
</button>,
];
}
// render content.data & content.layout as property list
let propertyListOverlay = '';
if (propertyListShown && typeof content == 'object') {
let propertylists = [];
// properties for content.data
if (typeof content.data == 'object') {
propertylists = propertylists.concat(
content.data.map((data, dataId) => [
<span key={dataId}>
<b>Data[{dataId}] Properties</b>
<PropertyList
keylist={'data[' + dataId + ']'}
content={data}
title={'Data[' + dataId + ']'}
/>
<hr />
</span>,
])
);
}
// properties for content.data
if (typeof content.layout == 'object') {
propertylists.push(
<span key="layout">
<b>Layout Properties</b>
<PropertyList
keylist="layout"
content={content.layout}
title="Layout"
/>
</span>
);
}
propertyListOverlay = <div className="attachedWindow">{propertylists}</div>;
}
return (
<div
role="presentation"
className={windowClassNames}
onClick={handleOnFocus}
onDoubleClick={handleReset}
onWheel={handleZoom}
onMouseMove={handleMouseMove}
ref={ref}
>
<div className={barClassNames} ref={barRef}>
<button title="close" onClick={handleClose}>
{' '}
X{' '}
</button>
<button
title="save"
onClick={(e) => {
e.stopPropagation();
setExportMenuOpen((v) => !v);
}}
>
{' '}
⇩{' '}
</button>
{exportMenuOpen && (
<div
className="export-menu"
onClick={(e) => e.stopPropagation()}
style={{
position: 'absolute',
zIndex: 20,
marginTop: '4px',
padding: '6px',
background: '#fff',
border: '1px solid #ccc',
borderRadius: '4px',
boxShadow: '0 2px 8px rgba(0,0,0,0.15)',
}}
>
<button
onClick={() => {
handleDownload('svg');
setExportMenuOpen(false);
}}
>
Export SVG
</button>
<button
onClick={() => {
handleDownload('png');
setExportMenuOpen(false);
}}
>
Export PNG
</button>
<button
onClick={() => {
handleShowExportHistory();
setExportMenuOpen(false);
}}
>
Export history
</button>
</div>
)}
<button title="reset" onClick={handleReset} hidden={!props.handleReset}>
{' '}
⟲{' '}
</button>
{barwidgets}
<div className="pull-right">{title}</div>
</div>
<div className="content">{children}</div>
<div className="widgets">{widgets}</div>
{propertyListOverlay}
</div>
);
});
Pane.displayName = 'Pane';
// prevent rerender unless we know we need one
// (previously known as shouldComponentUpdate)
Pane = React.memo(Pane, (props, nextProps) => {
if (props.contentID !== nextProps.contentID) return false;
else if (props.h !== nextProps.h || props.w !== nextProps.w) return false;
else if (props.children !== nextProps.children) return false;
else if (props.isFocused !== nextProps.isFocused) return false;
return true;
});
// this component is an overlay containing a property list
// (specialized for Pane)
function PropertyList(props) {
var { content } = props;
// private events
// --------------
// updates the property of the window dynamically
// note: props refers in this content to the Components directly responsible
// to the key, e.g. EditablePropertyText object from PropertyItem
const updateValue = (key, value) => {
content[key] = value;
};
// rendering
// ---------
// create for each element of content a representation in the PropertyList
let propitems = Object.entries(content).map(([key_local, value]) => {
// append key for multi-level objects
var keylist = props.keylist
? Array.isArray(props.keylist)
? props.keylist.concat([key_local])
: [props.keylist, key_local]
: [key_local];
var key_string =
keylist.length > 1 ? keylist.slice(1).join('.') : keylist[0];
// map value type to property type
var type;
if (typeof value == 'number') type = 'number';
else if (typeof value == 'boolean') type = 'checkbox';
else if (typeof value == 'string') type = 'text';
else if (Array.isArray(value)) return [];
else if (value && typeof value === 'object')
return (
<PropertyList key={key_string} content={value} keylist={keylist} />
);
else return [];
// list new property as part of a table
return (
<tr key={key_string}>
<td className="table-properties-name">{key_string}</td>
<td className="table-properties-value">
<PropertyItem
name={key_string}
type={type}
value={value}
propId={key_local}
updateValue={updateValue}
/>
</td>
</tr>
);
});
// only first PropertyList in recursion should create a table-tag
if (!Array.isArray(props.keylist))
return (
<table className="table table-bordered table-condensed table-properties">
<tbody>{propitems}</tbody>
</table>
);
else return propitems;
}
export default Pane;