Skip to content

Commit

Permalink
+ add drag and drop
Browse files Browse the repository at this point in the history
+ add support for recursive directories reading
  • Loading branch information
Kolerts committed Oct 3, 2020
1 parent c0e15fd commit 3f2cc95
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 5 deletions.
22 changes: 22 additions & 0 deletions html/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,28 @@ function upload(input, callback) {
fr.onload = r => {callback(r.target.result)};
}

function initDragDrop(dropArea, func) {
['dragenter', 'dragover'].forEach(event => {
dropArea.addEventListener(event, (e) => {
preventDef(e);
dropArea.classList.add('highlight');
}, false)
});

['dragleave', 'drop'].forEach(event => {
dropArea.addEventListener(event, (e) => {
preventDef(e);
dropArea.classList.remove('highlight');
if (event === 'drop') func(e.dataTransfer);
}, false)
});
}

function preventDef(e) {
e.preventDefault();
e.stopPropagation();
}

const cfgSet = (key, val) => {
if (typeof val != 'string') {
val = JSON.stringify(val);
Expand Down
38 changes: 35 additions & 3 deletions html/main.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
const DBI = require('switch-dbi');
const Path = require('path');
const fs = require('fs');

let dbi;
let list = {};
nspDrawList();
initDragDrop(getEl('main'), ({files}) => {
addNsp({files});
})

async function addNsp(input) {
await nspStop();
console.log(input.files);
for (let i = 0; i < input.files.length; i++) {
const {name, path, size} = input.files[i];
const files = [...input.files];

for (let i = 0; i < files.length; i++) {
const {name, path, size} = files[i];
if (fs.statSync(path).isDirectory()) {
readDirRecursive(path, files);
continue;
}

list[name] = {
name,
path,
Expand All @@ -22,6 +33,27 @@ async function addNsp(input) {
nspDrawList();
}

function readDirRecursive(path, files) {
for (const f of fs.readdirSync(path)) {
const ext = f.split('.').pop();
if (!['nsp', 'nsz','xci'].includes(ext)) continue;
const file = {
name: f,
path: Path.join(path, f),
size: 0,
};
const stat = fs.statSync(file.path);
if (stat.isDirectory()) {
readDirRecursive(file.path, files)
continue;
}

file.size = stat.size;
files.push(file);
console.log({file, files});
};
}

async function nspSend() {
await nspStop();
if (!Object.keys(list).length) return status('Add NSP files before');
Expand Down
6 changes: 5 additions & 1 deletion html/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ hr {
}

div {
/*border: 1px solid #636D81;*/
border: 2px solid #2E3038;
margin: 10px;
padding: 15px;
padding-left: 30px;
Expand Down Expand Up @@ -140,6 +140,10 @@ tr:nth-child(2n) {
text-align: center;
}

.highlight {
background: #2E3038BB;
border: 2px dotted #319249;
}

.close {
cursor: pointer;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "NSW-DBI",
"version": "1.0.0-b",
"version": "1.0.1-b",
"description": "DBI backend GUI",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit 3f2cc95

Please sign in to comment.