You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
117 lines
3.1 KiB
JavaScript
117 lines
3.1 KiB
JavaScript
/*
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
*/
|
|
//
|
|
//function openFileDialog(callback) {
|
|
// $("#open-file-input").off("change");
|
|
// if (typeof callback != "undefined") {
|
|
// $("#open-file-input").on("change", function () {
|
|
// callback($("#open-file-input").val());
|
|
// });
|
|
// }
|
|
// $("#open-file-input").click();
|
|
//}
|
|
|
|
/**
|
|
* Open a save file dialog and passes the file path back.
|
|
* @param {function} callback
|
|
* @param {string} accept HTML5 accept="thisvar" (optional)
|
|
* @returns {undefined}
|
|
*/
|
|
function openFileDialog(callback, accept) {
|
|
var dialog = document.createElement("input");
|
|
dialog.setAttribute("type", "file");
|
|
if (typeof accept != "undefined") {
|
|
dialog.setAttribute("accept", accept);
|
|
}
|
|
dialog.onchange = function () {
|
|
callback(dialog.value, this.files[0]);
|
|
}
|
|
dialog.dispatchEvent(new MouseEvent("click", {
|
|
"view": window,
|
|
"bubbles": false,
|
|
"cancelable": false
|
|
}));
|
|
}
|
|
|
|
/**
|
|
* Open a save file dialog and passes the file path back.
|
|
* @param {function} callback
|
|
* @param {string} defaultfilename something like file.pdf (optional)
|
|
* @param {string} accept HTML5 accept="thisvar" (optional)
|
|
* @returns {undefined}
|
|
*/
|
|
function openSaveFileDialog(callback, defaultfilename, accept) {
|
|
var dialog = document.createElement("input");
|
|
dialog.setAttribute("type", "file");
|
|
if (typeof defaultfilename == "undefined") {
|
|
defaultfilename = "";
|
|
}
|
|
dialog.setAttribute("nwsaveas", defaultfilename);
|
|
if (typeof accept != "undefined") {
|
|
dialog.setAttribute("accept", accept);
|
|
}
|
|
dialog.onchange = function () {
|
|
callback(dialog.value);
|
|
}
|
|
dialog.dispatchEvent(new MouseEvent("click", {
|
|
"view": window,
|
|
"bubbles": false,
|
|
"cancelable": false
|
|
}));
|
|
}
|
|
|
|
function getFileAsString(path) {
|
|
const fs = require("fs");
|
|
return fs.readFileSync(path, "utf8");
|
|
}
|
|
|
|
function getFileAsUint8Array(path) {
|
|
const fs = require("fs");
|
|
return fs.readFileSync(path, null);
|
|
}
|
|
|
|
function writeToFile(path, data) {
|
|
const fs = require("fs");
|
|
fs.writeFileSync(path, data);
|
|
}
|
|
|
|
function appendToFile(path, data) {
|
|
const fs = require("fs");
|
|
fs.appendFileSync(path, data);
|
|
}
|
|
|
|
function copyFile(source, dest) {
|
|
const fs = require("fs");
|
|
fs.copyFileSync(source, dest);
|
|
}
|
|
|
|
function deleteFile(path) {
|
|
const fs = require('fs');
|
|
fs.unlinkSync(path);
|
|
}
|
|
|
|
function getBasename(fullpath) {
|
|
const path = require("path");
|
|
return path.basename(fullpath);
|
|
}
|
|
|
|
/**
|
|
* Get the path to a nonexistent, randomly-named temporary file.
|
|
* @returns {String}
|
|
*/
|
|
function getNewTempFilePath() {
|
|
const fs = require('fs');
|
|
const path = require("path");
|
|
const crypto = require('crypto');
|
|
const os = require('os');
|
|
|
|
const randomname = crypto.randomBytes(6).readUIntLE(0,6).toString(36);
|
|
var temppath = path.join(os.tmpdir(), "nwjs-app-");
|
|
|
|
var folder = fs.mkdtempSync(temppath);
|
|
|
|
return path.join(folder, randomname);
|
|
} |