Skip to content

Commit 9249231

Browse files
authored
Merge pull request #986 from oskarhane/fix-json-export
Bring back JSON data export
2 parents bf34d62 + 79b3fa7 commit 9249231

File tree

2 files changed

+32
-318
lines changed

2 files changed

+32
-318
lines changed

src/browser/modules/Frame/FrameTitlebar.jsx

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import { connect } from 'react-redux'
2222
import React, { Component } from 'react'
2323
import { withBus } from 'react-suber'
2424
import { saveAs } from 'file-saver'
25+
import { map } from 'lodash-es'
26+
2527
import * as editor from 'shared/modules/editor/editorDuck'
2628
import * as commands from 'shared/modules/commands/commandsDuck'
2729
import {
@@ -55,22 +57,26 @@ import {
5557
StyledFrameTitleBar,
5658
StyledFrameTitlebarButtonSection,
5759
StyledFrameCommand
58-
} from 'browser/modules/Frame/styled'
60+
} from './styled'
5961
import {
6062
downloadPNGFromSVG,
6163
downloadSVG
6264
} from 'shared/services/exporting/imageUtils'
6365
import {
6466
stringifyResultArray,
65-
transformResultRecordsToResultArray
67+
transformResultRecordsToResultArray,
68+
recordToJSONMapper
6669
} from 'browser/modules/Stream/CypherFrame/helpers'
6770
import { csvFormat } from 'services/bolt/cypherTypesFormatting'
6871
import arrayHasItems from 'shared/utils/array-has-items'
6972

73+
const JSON_EXPORT_INDENT = 2
74+
7075
class FrameTitlebar extends Component {
7176
hasData () {
7277
return this.props.numRecords > 0
7378
}
79+
7480
exportCSV (records) {
7581
const exportData = stringifyResultArray(
7682
csvFormat,
@@ -108,25 +114,43 @@ class FrameTitlebar extends Component {
108114
}
109115
}
110116

117+
exportJSON (records) {
118+
const data = JSON.stringify(
119+
map(records, recordToJSONMapper),
120+
null,
121+
JSON_EXPORT_INDENT
122+
)
123+
const blob = new Blob([data], {
124+
type: 'text/plain;charset=utf-8'
125+
})
126+
127+
saveAs(blob, 'records.json')
128+
}
129+
111130
exportPNG () {
112131
const { svgElement, graphElement, type } = this.props.visElement
113132
downloadPNGFromSVG(svgElement, graphElement, type)
114133
}
134+
115135
exportSVG () {
116136
const { svgElement, graphElement, type } = this.props.visElement
117137
downloadSVG(svgElement, graphElement, type)
118138
}
139+
119140
exportGrass (data) {
120141
var blob = new Blob([data], {
121142
type: 'text/plain;charset=utf-8'
122143
})
123144
saveAs(blob, 'style.grass')
124145
}
146+
125147
canExport = () => {
126148
let props = this.props
127149
const { frame = {} } = props
150+
128151
return (
129-
(frame.type === 'cypher' && (this.hasData() || props.visElement)) ||
152+
this.canExportTXT() ||
153+
(frame.type === 'cypher' && (this.hasData() || this.props.visElement)) ||
130154
(frame.type === 'style' && this.hasData())
131155
)
132156
}
@@ -175,6 +199,11 @@ class FrameTitlebar extends Component {
175199
>
176200
Export CSV
177201
</DropdownItem>
202+
<DropdownItem
203+
onClick={() => this.exportJSON(props.getRecords())}
204+
>
205+
Export JSON
206+
</DropdownItem>
178207
</Render>
179208
<Render if={this.canExportTXT()}>
180209
<DropdownItem onClick={this.exportTXT}>

0 commit comments

Comments
 (0)