This repository has been archived by the owner on Feb 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Import config * Grumbles (performance) * Fix list field; update account.password type List field: previous code undefined the removed element in the array, but didn't remove it; showed "undefined" as a result in the Preview, etc.
- Loading branch information
1 parent
242b96e
commit fa51ade
Showing
11 changed files
with
146 additions
and
37 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.import input { display: none; } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import React, { PureComponent } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
|
||
import toml from 'toml'; | ||
|
||
import './Importer.css'; | ||
|
||
import {mix, clone} from '../util'; | ||
|
||
class Importer extends PureComponent { | ||
|
||
static propTypes = { | ||
defaults: PropTypes.object.isRequired, | ||
onChange: PropTypes.func.isRequired, | ||
onError: PropTypes.func.isRequired | ||
}; | ||
|
||
constructor (props) { | ||
super(props); | ||
this.handleFileChange = this.handleFileChange.bind(this); | ||
this.handleClick = this.handleClick.bind(this); | ||
} | ||
|
||
handleFileChange (ev) { | ||
const file = ev.target.files[0]; | ||
const domTarget = ev.target; | ||
domTarget.value = ''; | ||
|
||
if (!window.confirm('Do you want to overwrite current config?')) { | ||
return; | ||
} | ||
|
||
if (file.name.match(/.*\.toml$/)) { | ||
var reader = new window.FileReader(); | ||
|
||
reader.onload = _ => { | ||
let importedData; | ||
|
||
try { | ||
importedData = toml.parse(reader.result); | ||
} catch (err) { | ||
this.props.onError(`Couldn't parse configuration file. ${err.toString()}`); | ||
return; | ||
} | ||
|
||
const data = mix(clone(this.props.defaults), importedData); | ||
domTarget.blur(); | ||
this.props.onChange(data); | ||
}; | ||
|
||
reader.readAsText(file); | ||
} else { | ||
this.props.onError('Please import a .toml configuration file.'); | ||
} | ||
} | ||
|
||
handleClick () { | ||
this.refs.fileInput.click(); | ||
} | ||
|
||
render () { | ||
return ( | ||
<button | ||
className='mdl-button mdl-button--icon mdl-js-button mdl-js-ripple-efect import' | ||
onClick={this.handleClick}> | ||
|
||
<input | ||
type='file' | ||
ref='fileInput' | ||
onChange={this.handleFileChange} /> | ||
|
||
<i className='material-icons' id='upload'>file_upload</i> | ||
<span className='mdl-tooltip' htmlFor='upload'>Load Config File</span> | ||
</button> | ||
); | ||
} | ||
} | ||
|
||
export default Importer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
export function mix (a, b) { | ||
if (typeof a !== 'object' || typeof b !== 'object' || Array.isArray(a) || Array.isArray(b)) { | ||
return typeof b === 'undefined' ? a : b; | ||
} | ||
|
||
Object.keys(a).forEach(key => { | ||
a[key] = mix(a[key], b[key]); | ||
}); | ||
|
||
return a; | ||
} | ||
|
||
export function clone (val) { | ||
return JSON.parse(JSON.stringify(val)); | ||
} |