2015-03-17 14:24:03 +03:00
|
|
|
(function () {
|
|
|
|
var ns = $.namespace('pskl.utils');
|
|
|
|
|
|
|
|
var stopPropagation = function (e) {
|
|
|
|
e.stopPropagation();
|
|
|
|
};
|
|
|
|
|
|
|
|
ns.FileUtilsDesktop = {
|
|
|
|
|
|
|
|
chooseFileDialog: function (callback) {
|
|
|
|
var tagString = '<input type="file" nwworkingdir=""/>';
|
|
|
|
var $chooser = $(tagString);
|
2015-04-14 19:02:33 +03:00
|
|
|
$chooser.change(function (e) {
|
2015-03-17 14:24:03 +03:00
|
|
|
var filename = $(this).val();
|
|
|
|
callback(filename);
|
|
|
|
});
|
|
|
|
$chooser.trigger('click');
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param content
|
2015-03-19 13:46:53 +03:00
|
|
|
* @param defaultFileName - file name to pre-populate the dialog
|
|
|
|
* @param extension - if supplied, the selected extension will guaranteed to be on the filename -
|
|
|
|
* NOTE: there is a possible danger here... If the extension is added to a fileName, but there
|
|
|
|
* is already another file of the same name *with* the extension, it will get overwritten.
|
2015-03-17 14:24:03 +03:00
|
|
|
* @param callback
|
|
|
|
*/
|
2015-03-19 13:46:53 +03:00
|
|
|
saveAs: function (content, defaultFileName, extension, callback) {
|
2015-03-17 14:24:03 +03:00
|
|
|
// NodeWebkit has no js api for opening the save dialog.
|
|
|
|
// Instead, it adds two new attributes to the anchor tag: nwdirectory and nwsaveas
|
|
|
|
// (see: https://github.com/nwjs/nw.js/wiki/File-dialogs )
|
2015-04-14 19:02:33 +03:00
|
|
|
defaultFileName = defaultFileName || '';
|
|
|
|
var tagString = '<input type="file" nwsaveas="' + defaultFileName + '" nwworkingdir=""/>';
|
2015-03-17 14:24:03 +03:00
|
|
|
var $chooser = $(tagString);
|
2015-04-14 19:02:33 +03:00
|
|
|
$chooser.change(function (e) {
|
2015-03-19 13:46:53 +03:00
|
|
|
var filename = $(this).val();
|
|
|
|
if (typeof extension == 'string') {
|
|
|
|
if (extension[0] !== '.') {
|
2015-04-14 19:02:33 +03:00
|
|
|
extension = '.' + extension;
|
2015-03-19 13:46:53 +03:00
|
|
|
}
|
|
|
|
var hasExtension = (filename.substring(filename.length - extension.length) === extension);
|
|
|
|
if (!hasExtension) {
|
|
|
|
filename += extension;
|
|
|
|
}
|
|
|
|
}
|
2015-04-14 19:02:33 +03:00
|
|
|
pskl.utils.FileUtilsDesktop.saveToFile(content, filename, function () {
|
2015-03-17 14:24:03 +03:00
|
|
|
callback(filename);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
$chooser.trigger('click');
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Save data directly to disk, without showing a save dialog
|
|
|
|
* Requires Node-Webkit environment for file system access
|
|
|
|
* @param content - data to be saved
|
|
|
|
* @param {string} filename - fill path to the file
|
|
|
|
* @callback callback
|
|
|
|
*/
|
|
|
|
saveToFile : function(content, filename, callback) {
|
2015-03-25 08:36:55 +03:00
|
|
|
var fs = window.require('fs');
|
2015-04-14 19:02:33 +03:00
|
|
|
fs.writeFile(filename, content, function (err) {
|
2015-03-17 14:24:03 +03:00
|
|
|
if (err) {
|
|
|
|
//throw err;
|
|
|
|
console.log('FileUtilsDesktop::savetoFile() - error saving file:', filename, 'Error:', err);
|
|
|
|
}
|
|
|
|
callback();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
readFile : function(filename, callback) {
|
2015-03-25 08:36:55 +03:00
|
|
|
var fs = window.require('fs');
|
2015-03-17 14:24:03 +03:00
|
|
|
// NOTE: currently loading everything as utf8, which may not be desirable in future
|
2015-04-14 19:02:33 +03:00
|
|
|
fs.readFile(filename, 'utf8', function (err, data) {
|
2015-03-17 14:24:03 +03:00
|
|
|
if (err) {
|
|
|
|
console.log('FileUtilsDesktop::readFile() - error reading file:', filename, 'Error:', err);
|
|
|
|
}
|
|
|
|
callback(data);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
})();
|