Use Electron API for file dialogs
This commit is contained in:
@ -1,6 +1,7 @@
|
||||
var React = require('react');
|
||||
var connect = require('react-redux').connect;
|
||||
var actions = require('../../store/actions');
|
||||
var dialog = require('remote').require('dialog');
|
||||
|
||||
var ProfileMenu = React.createClass({
|
||||
|
||||
@ -8,7 +9,6 @@ var ProfileMenu = React.createClass({
|
||||
|
||||
return (
|
||||
<div className="profile-menu">
|
||||
<input ref="fileInput" style={{display: 'none'}} filter=".json" type="file" />
|
||||
<button className="btn btn-default" onClick={this.handleOpenClick}>Ouvrir</button>
|
||||
<button className="btn btn-primary" onClick={this.handleSaveClick}>Enregistrer</button>
|
||||
</div>
|
||||
@ -18,9 +18,9 @@ var ProfileMenu = React.createClass({
|
||||
|
||||
handleOpenClick: function() {
|
||||
var dispatch = this.props.dispatch;
|
||||
this.showFileDialog()
|
||||
this.showOpenProfileDialog()
|
||||
.then(function(profilePath) {
|
||||
dispatch(actions.common.loadProfile(profilePath));
|
||||
if(profilePath) dispatch(actions.common.loadProfile(profilePath));
|
||||
})
|
||||
;
|
||||
},
|
||||
@ -31,39 +31,44 @@ var ProfileMenu = React.createClass({
|
||||
var profile = this.props.profile;
|
||||
var profilePath = this.props.profilePath;
|
||||
|
||||
var promise = profilePath ? Promise.resolve(profilePath) : this.showFileDialog(true);
|
||||
var promise = profilePath ? Promise.resolve(profilePath) : this.showSaveProfileDialog();
|
||||
|
||||
promise.then(function(profilePath) {
|
||||
dispatch(actions.edit.saveProfile(profilePath, profile));
|
||||
if(profilePath) dispatch(actions.edit.saveProfile(profilePath, profile));
|
||||
});
|
||||
|
||||
},
|
||||
|
||||
showFileDialog: function(saveAs) {
|
||||
|
||||
var fileInput = this.refs.fileInput.getDOMNode();
|
||||
|
||||
// Toggle 'save as' feature
|
||||
if(saveAs) {
|
||||
fileInput.nwsaveas = true;
|
||||
} else {
|
||||
fileInput.removeAttribute('nwsaveas');
|
||||
}
|
||||
|
||||
return new Promise(function(resolve, reject) {
|
||||
|
||||
fileInput.addEventListener('change', handleChange, false);
|
||||
fileInput.click();
|
||||
|
||||
function handleChange(evt) {
|
||||
fileInput.removeEventListener('change', handleChange);
|
||||
var value = this.value;
|
||||
this.value = null;
|
||||
resolve(value);
|
||||
}
|
||||
showOpenProfileDialog: function() {
|
||||
|
||||
return new Promise(function(resolve) {
|
||||
dialog.showOpenDialog(
|
||||
{
|
||||
title: 'Éditer un profil',
|
||||
filters: [ {name: 'Profils Pitaya', extensions: ['json'] } ],
|
||||
properties: ['openFile']
|
||||
},
|
||||
function(files) {
|
||||
return resolve(files ? files[0] : null);
|
||||
}
|
||||
)
|
||||
});
|
||||
|
||||
},
|
||||
|
||||
showSaveProfileDialog: function() {
|
||||
|
||||
return new Promise(function(resolve) {
|
||||
dialog.showSaveDialog(
|
||||
{
|
||||
title: 'Éditer un profil',
|
||||
filters: [ {name: 'Profils Pitaya', extensions: ['json'] } ]
|
||||
},
|
||||
function(file) {
|
||||
return resolve(file);
|
||||
}
|
||||
)
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user