/
FrameTitlebar.jsx
317 lines (293 loc) · 9.09 KB
/
FrameTitlebar.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
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*
* Copyright (c) 2002-2019 "Neo4j,"
* Neo4j Sweden AB [http://neo4j.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { connect } from 'react-redux'
import React, { Component } from 'react'
import { withBus } from 'react-suber'
import { saveAs } from 'file-saver'
import { map } from 'lodash-es'
import * as editor from 'shared/modules/editor/editorDuck'
import * as commands from 'shared/modules/commands/commandsDuck'
import {
cancel as cancelRequest,
getRequest,
REQUEST_STATUS_PENDING
} from 'shared/modules/requests/requestsDuck'
import { remove, pin, unpin } from 'shared/modules/stream/streamDuck'
import { removeComments, sleep } from 'shared/services/utils'
import { FrameButton } from 'browser-components/buttons'
import Render from 'browser-components/Render'
import { CSVSerializer } from 'services/serializer'
import {
ExpandIcon,
ContractIcon,
RefreshIcon,
CloseIcon,
UpIcon,
DownIcon,
PinIcon,
DownloadIcon
} from 'browser-components/icons/Icons'
import {
DottedLineHover,
DropdownList,
DropdownContent,
DropdownButton,
DropdownItem
} from '../Stream/styled'
import {
StyledFrameTitleBar,
StyledFrameTitlebarButtonSection,
StyledFrameCommand
} from './styled'
import {
downloadPNGFromSVG,
downloadSVG
} from 'shared/services/exporting/imageUtils'
import {
stringifyResultArray,
transformResultRecordsToResultArray,
recordToJSONMapper
} from 'browser/modules/Stream/CypherFrame/helpers'
import { csvFormat } from 'services/bolt/cypherTypesFormatting'
import arrayHasItems from 'shared/utils/array-has-items'
const JSON_EXPORT_INDENT = 2
class FrameTitlebar extends Component {
hasData () {
return this.props.numRecords > 0
}
exportCSV (records) {
const exportData = stringifyResultArray(
csvFormat,
transformResultRecordsToResultArray(records)
)
let data = exportData.slice()
const csv = CSVSerializer(data.shift())
csv.appendRows(data)
var blob = new Blob([csv.output()], {
type: 'text/plain;charset=utf-8'
})
saveAs(blob, 'export.csv')
}
exportTXT = () => {
const { frame } = this.props
if (frame.type === 'history') {
const asTxt = frame.result
.map(result => {
const safe = `${result}`.trim()
if (safe.startsWith(':')) {
return safe
}
return safe.endsWith(';') ? safe : `${safe};`
})
.join('\n\n')
const blob = new Blob([asTxt], {
type: 'text/plain;charset=utf-8'
})
saveAs(blob, 'history.txt')
}
}
exportJSON (records) {
const data = JSON.stringify(
map(records, recordToJSONMapper),
null,
JSON_EXPORT_INDENT
)
const blob = new Blob([data], {
type: 'text/plain;charset=utf-8'
})
saveAs(blob, 'records.json')
}
exportPNG () {
const { svgElement, graphElement, type } = this.props.visElement
downloadPNGFromSVG(svgElement, graphElement, type)
}
exportSVG () {
const { svgElement, graphElement, type } = this.props.visElement
downloadSVG(svgElement, graphElement, type)
}
exportGrass (data) {
var blob = new Blob([data], {
type: 'text/plain;charset=utf-8'
})
saveAs(blob, 'style.grass')
}
canExport = () => {
let props = this.props
const { frame = {} } = props
return (
this.canExportTXT() ||
(frame.type === 'cypher' && (this.hasData() || this.props.visElement)) ||
(frame.type === 'style' && this.hasData())
)
}
canExportTXT () {
const { frame = {} } = this.props
return frame.type === 'history' && arrayHasItems(frame.result)
}
render () {
let props = this.props
const { frame = {} } = props
const fullscreenIcon = props.fullscreen ? <ContractIcon /> : <ExpandIcon />
const expandCollapseIcon = props.collapse ? <DownIcon /> : <UpIcon />
const cmd = removeComments(frame.cmd)
return (
<StyledFrameTitleBar>
<StyledFrameCommand selectedDb={frame.useDb}>
<DottedLineHover
data-testid='frameCommand'
onClick={() => props.onTitlebarClick(frame.cmd)}
>
{cmd}
</DottedLineHover>
</StyledFrameCommand>
<StyledFrameTitlebarButtonSection>
<Render if={this.canExport()}>
<DropdownButton data-testid='frame-export-dropdown'>
<DownloadIcon />
<DropdownList>
<DropdownContent>
<Render if={props.visElement}>
<span>
<DropdownItem onClick={() => this.exportPNG()}>
Export PNG
</DropdownItem>
<DropdownItem onClick={() => this.exportSVG()}>
Export SVG
</DropdownItem>
</span>
</Render>
<Render if={this.hasData() && frame.type === 'cypher'}>
<DropdownItem
onClick={() => this.exportCSV(props.getRecords())}
>
Export CSV
</DropdownItem>
<DropdownItem
onClick={() => this.exportJSON(props.getRecords())}
>
Export JSON
</DropdownItem>
</Render>
<Render if={this.canExportTXT()}>
<DropdownItem onClick={this.exportTXT}>
Export TXT
</DropdownItem>
</Render>
<Render if={this.hasData() && frame.type === 'style'}>
<DropdownItem
data-testid='exportGrassButton'
onClick={() => this.exportGrass(props.getRecords())}
>
Export GraSS
</DropdownItem>
</Render>
</DropdownContent>
</DropdownList>
</DropdownButton>
</Render>
<FrameButton
title='Pin at top'
onClick={() => {
props.togglePin()
props.togglePinning(frame.id, frame.isPinned)
}}
pressed={props.pinned}
>
<PinIcon />
</FrameButton>
<Render
if={['cypher', 'play', 'play-remote', 'queries'].includes(
frame.type
)}
>
<FrameButton
title={props.fullscreen ? 'Close fullscreen' : 'Fullscreen'}
onClick={() => props.fullscreenToggle()}
>
{fullscreenIcon}
</FrameButton>
</Render>
<FrameButton
title={props.collapse ? 'Expand' : 'Collapse'}
onClick={() => props.collapseToggle()}
>
{expandCollapseIcon}
</FrameButton>
<Render if={['cypher', 'style', 'schema'].includes(frame.type)}>
<FrameButton
data-testid='rerunFrameButton'
title='Rerun'
onClick={() =>
props.onReRunClick(frame.cmd, frame.id, frame.requestId)
}
>
<RefreshIcon />
</FrameButton>
</Render>
<FrameButton
title='Close'
onClick={() =>
props.onCloseClick(frame.id, frame.requestId, props.request)
}
>
<CloseIcon />
</FrameButton>
</StyledFrameTitlebarButtonSection>
</StyledFrameTitleBar>
)
}
}
const mapStateToProps = (state, ownProps) => {
const request = ownProps.frame.requestId
? getRequest(state, ownProps.frame.requestId)
: null
return {
request
}
}
const mapDispatchToProps = (dispatch, ownProps) => {
return {
onTitlebarClick: cmd => {
ownProps.bus.send(editor.SET_CONTENT, editor.setContent(cmd))
},
onCloseClick: async (id, requestId, request) => {
if (request && request.status === REQUEST_STATUS_PENDING) {
dispatch(cancelRequest(requestId))
await sleep(3000) // sleep for 3000 ms to let user read the cancel info
}
dispatch(remove(id))
},
onReRunClick: (cmd, id, requestId) => {
if (requestId) {
dispatch(cancelRequest(requestId))
}
dispatch(commands.executeCommand(cmd, id))
},
togglePinning: (id, isPinned) => {
isPinned ? dispatch(unpin(id)) : dispatch(pin(id))
}
}
}
export default withBus(
connect(
mapStateToProps,
mapDispatchToProps
)(FrameTitlebar)
)