Skip to content

Commit

Permalink
SVYX-410 add sync methods for selectDIrectory and showOpenDialog
Browse files Browse the repository at this point in the history
  • Loading branch information
mvid-servoy committed Apr 20, 2022
1 parent 4cc6889 commit f45ac70
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 0 deletions.
57 changes: 57 additions & 0 deletions ngdesktopfile/ngdesktopfile/ngdesktopfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,61 @@ angular.module('ngdesktopfile',['servoy'])
})
})
},

/**
* Return the selected folder.
*/
selectDirectorySync( path ) {
const selDirDefer = $q.defer();
waitForDefered(function() {
var options = {
title: "Select folder",
...(path != null) && ({defaultPath: path}),
buttonLabel : "Select",
properties: ['openDirectory']
}
dialog.showOpenDialog(remote.getCurrentWindow(), options)
.then(function(result) {
if (!result.canceled) {
selDirDefer.resolve(result.filePaths[0]);
} else {
selDirDefer.resolve(null);
}
}).catch(function(err) {
console.log(err);
selDirDefer.resolve(null);
})
});
return selDirDefer.promise;
},

/**
* Return the selected file.
*/
selectFileSync( path ) {
const selFileDefer = $q.defer();
waitForDefered(function() {
var options = {
title: "Select file",
...(path != null) && ({defaultPath: path}),
buttonLabel : "Select",
properties: ['openFile']
}
dialog.showOpenDialog(remote.getCurrentWindow(), options)
.then(function(result) {
if (!result.canceled) {
selFileDefer.resolve(result.filePaths[0]);
} else {
selFileDefer.resolve(null);
}
}).catch(function(err) {
console.log(err);
selFileDefer.resolve(null);
})
});
return selFileDefer.promise;
},

/**
* Shows a file save dialog and calls the callback method with the file path
*
Expand Down Expand Up @@ -854,6 +909,8 @@ angular.module('ngdesktopfile',['servoy'])
writeFileImpl: function(path, bytes){console.log("not in electron");},
readFileImpl: function(path, id, bytes){console.log("not in electron");},
selectDirectory: function(callback){console.log("not in electron");},
selectDirSync: function(callback){console.log("not in electron");},
selectFileSync: function(callback){console.log("not in electron");},
showSaveDialog: function(callback){console.log("not in electron");},
showSaveDialogSync: function(callback){console.log("not in electron");},
showOpenDialog: function(callback){console.log("not in electron");},
Expand Down
12 changes: 12 additions & 0 deletions ngdesktopfile/ngdesktopfile/ngdesktopfile.spec
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,18 @@
],
"async-now":true
},
"selectDirectorySync": {
"parameters" : [
{"name":"path", "type":"string", "optional": true}
],
"returns": "string"
},
"selectFileSync": {
"parameters" : [
{"name":"path", "type":"string", "optional": true}
],
"returns": "string"
},
"showSaveDialogSync": {
"parameters" : [
{"name":"options", "type":"object", "optional": true}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,64 @@ export class NGDesktopFileService {
});
}

/**
* Return the selected folder.
*
* @param path: initial path
*/
selectDirectorySync( path: string ) {
const selDirDefer = new Deferred();
this.waitForDefered(() => {
const options: electron.OpenDialogOptions = {
title: 'Select folder',
...(path != null) && ({defaultPath: path}),
buttonLabel: 'Select',
properties: ['openDirectory']
};
this.dialog.showOpenDialog(this.remote.getCurrentWindow(), options)
.then((result) => {
if (!result.canceled) {
selDirDefer.resolve(result.filePaths[0]);
} else {
selDirDefer.resolve(null);
}
}).catch((err) => {
this.log.info(err);
selDirDefer.resolve(null);
});
});
return selDirDefer.promise;
}

/**
* Return the selected file.
*
* @param path: initial path
*/
selectFileSync( path: string ) {
const selFileDefer = new Deferred();
this.waitForDefered(() => {
const options: electron.OpenDialogOptions = {
title: 'Select file',
...(path != null) && ({defaultPath: path}),
buttonLabel: 'Select',
properties: ['openFile']
};
this.dialog.showOpenDialog(this.remote.getCurrentWindow(), options)
.then((result) => {
if (!result.canceled) {
selFileDefer.resolve(result.filePaths[0].replace(/\\/g, '/'));
} else {
selFileDefer.resolve(null);
}
}).catch((err) => {
this.log.info(err);
selFileDefer.resolve(null);
});
});
return selFileDefer.promise;
}

/**
* Shows a file save dialog and calls the callback method with the file path
*
Expand Down

0 comments on commit f45ac70

Please sign in to comment.