-
Notifications
You must be signed in to change notification settings - Fork 7
/
ImportExportModal.jsx
158 lines (142 loc) · 5.6 KB
/
ImportExportModal.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
import React, { Component } from 'react'
import PropTypes from 'prop-types'
import { Modal, Button } from 'react-bootstrap'
import './ColormapWidget.css'
class ImportExportModal extends Component {
constructor(props) {
super(props);
this.state = {
name: "",
importName: "",
importColormap: [],
colormapData: ""
}
}
static get propTypes() {
return {
show: PropTypes.bool.isRequired, // show the modal
close: PropTypes.func.isRequired, // close the modal
current_colormap: PropTypes.array,
current_colormap_name: PropTypes.string,
saveColormap: PropTypes.func,
createNewColormap: PropTypes.func
};
}
handleChange(e) {
let colormap = {
name: e.target.value,
colormap: this.props.current_colormap,
}
this.setState({
name: e.target.value,
colormapData: this.props.current_colormap
})
}
convertToOldJson(obj){
let old_json_format = {}
let name = Object.keys(obj["Cp"])[0]
old_json_format["name"] = name
old_json_format["colormap"] = []
let myColorValues = Object(obj["Cp"][name]["index"]["data"]);
for (let i=0 ; i < Object.keys(myColorValues).length; i++){
old_json_format["colormap"].push(myColorValues[String(i)])
}
return old_json_format
}
handleFileChange(e){
let reader = new window.FileReader()
function handleLoad(event){
let obj = {}
try{
obj = JSON.parse(event.target.result);
}
catch(err){
console.error(err)
console.error("Unable to import colormap. Please check that the file contains valid json.")
return
}
if (Object.keys(obj)[0] == "Cp"){
obj = this.convertToOldJson(obj);
}
let keys = Object.keys(obj)
if(keys.indexOf("name") === -1 || keys.indexOf("colormap") === -1){
console.error(
"Unable to import colormap. Please check that the file contains both 'name' and 'colormap' keys."
)
return
}
this.setState({importName: obj.name, importColormap: obj.colormap})
}
reader.onload = handleLoad.bind(this)
reader.readAsText(e.target.files[0])
}
exportColormap(){
const name = this.state.name.substring(0, this.state.name.indexOf( ".json" ));
let myObject = {"Cp":{}};
myObject["Cp"][name] = {"index": {"data": {}}}
for (var i = 0; i < this.state.colormapData.length; i++){
myObject["Cp"][name]["index"]["data"][i] = this.state.colormapData[i]
}
// Create hidden anchor tag for downloading the dynamically create JSON object
var element = document.createElement('a');
var dataStr = "data:text/json;charset=utf-8," + encodeURIComponent(JSON.stringify(myObject));
element.setAttribute('href', dataStr);
element.setAttribute('download', this.state.name);
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
importColormap(){
this.props.createNewColormap(this.state.importName, this.props.current_colormap_name)
this.props.saveColormap(this.state.importName, this.state.importColormap)
}
render(){
return(
<div>
<Modal id="export-modal" show={this.props.show} onHide={this.props.close}>
<Modal.Header closeButton>
<Modal.Title>Import/Export Colormaps</Modal.Title>
</Modal.Header>
<Modal.Body>
<h4>Export Current Colormap</h4>
<div className="form-group form-inline">
<input
placeholder="filename.json"
className="form-control"
type="text"
value={this.state.name}
onChange={(e)=>{this.handleChange(e)}}>
</input>
<Button
id="downloadAnchorElem"
className="btn btn-primary form-control"
onClick={()=>{this.exportColormap()}}>
Download
</Button>
</div>
<hr/>
<h4>Import a Colormap</h4>
<div className="form-group form-inline">
<input
style={{display: "inline-block"}}
className="form-control"
type="file"
onChange={(e)=>{this.handleFileChange(e)}}>
</input>
<Button
className="btn btn-primary form-control"
onClick={()=>{this.importColormap()}}>
Upload
</Button>
</div>
</Modal.Body>
<Modal.Footer>
<Button onClick={this.props.close}>Close</Button>
</Modal.Footer>
</Modal>
</div>
)
}
}
export default ImportExportModal;