filebrowser: wip remote file browser

This commit is contained in:
jean-emmanuel 2019-03-29 23:36:10 +01:00
parent 08d1b8f40c
commit 4ab4abc44d
4 changed files with 251 additions and 4 deletions

View file

@ -2,7 +2,7 @@ var ipc = require('../ipc/'),
parser = require('../parser'),
editor = require('../editor/'),
lobby = require('../ui/lobby'),
{loading, icon, Popup, upload} = require('../ui/utils'),
{loading, icon, Popup, upload, remoteBrowse} = require('../ui/utils'),
{saveAs} = require('file-saver'),
widgetManager = require('./widgets'),
html = require('nanohtml'),
@ -183,6 +183,10 @@ var SessionManager = class SessionManager {
browse() {
remoteBrowse('json', (path)=>{
ipc.send('sessionOpen',{path:path})
})
return
upload('.json', (path, result)=>{
ipc.send('sessionOpen',{file:result, path:path})
}, ()=>{

View file

@ -2,14 +2,19 @@ var popupSingleton = null,
uploadSingleton = null,
locales = require('../locales'),
html = require('nanohtml'),
raw = require('nanohtml/raw')
raw = require('nanohtml/raw'),
ipc = require('../ipc'),
EventEmitter = require('../events/event-emitter'),
doubleTap = require('../widgets/mixins/double_tap')
module.exports = {
Popup: class Popup {
Popup: class Popup extends EventEmitter {
constructor(options) {
super()
if (options.closable) {
if (popupSingleton) {
@ -61,7 +66,7 @@ module.exports = {
}).bind(this)
}
this.open()
if (!options.hide) this.open()
}
@ -69,21 +74,28 @@ module.exports = {
close() {
if (!this.state) return
this.state = 0
if (this.escKey) document.removeEventListener('keydown', this.escKeyHandler)
if (this.enterKey) document.removeEventListener('keydown', this.enterKeyHandler)
document.body.removeChild(this.html)
this.trigger('close')
}
open() {
if (this.state) return
this.state = 1
if (this.escKey) document.addEventListener('keydown', this.escKeyHandler)
if (this.enterKey) document.addEventListener('keydown', this.enterKeyHandler)
document.body.appendChild(this.html)
this.trigger('open')
}
},
@ -130,6 +142,129 @@ module.exports = {
uploadSingleton.click()
},
remoteBrowse: function(ext='json', callback) {
var popup = new module.exports.Popup({
closable: true,
title: `Open file`,
hide: true
})
var container = DOM.get(popup.html, '.popup-content')[0],
browser = html`<div class="file-browser"></div>`,
ariane = html`<div class="ariane"></div>`,
list = html`<form class="file-list"></form>`,
actions = html`
<div class="file-actions">
<div class="btn cancel">${raw(module.exports.icon('times'))} Cancel</div>
<div class="btn open">${raw(module.exports.icon('folder-open'))} Open</div>
</div>
`
var files = [],
path = ''
doubleTap(list, submit)
function submit(){
var choice = DOM.get(list, ':checked')[0]
if (!choice) return
if (choice.classList.contains('folder')) {
ipc.send('listDir', {path: [path, choice.value]})
} else {
callback([path, choice.value])
popup.close()
}
}
actions.addEventListener('click', (e)=>{
if (e.target.classList.contains('open')) {
submit()
} else {
popup.close()
}
})
function keyHandler(e){
if (e.keyCode === 13) {
submit()
} else if (e.keyCode === 8) {
list.childNodes[0].firstElementChild.click()
submit()
} else if (e.key.length === 1) {
var letter = e.key.toLowerCase()
for (let i in files) {
if (files[i].name[0].toLowerCase() === letter && !list.childNodes[i].firstElementChild.checked) {
list.childNodes[i].firstElementChild.click()
list.childNodes[i].firstElementChild.focus()
break
}
}
}
}
document.addEventListener('keydown', keyHandler)
ipc.on('listDir', (data)=>{
ariane.innerHTML = path = data.path
if (ext) {
if (!Array.isArray(ext)) ext = [ext]
var filter = new RegExp('.*\\.(' + ext.join('|') + ')')
files = data.files.filter(x=>x.folder || x.name.match(filter))
}
files.sort(function(a, b) {
// convert to strings and force lowercase
a = typeof a.name === 'string' ? a.name.toLowerCase() : a.name.toString();
b = typeof b.name === 'string' ? b.name.toLowerCase() : b.name.toString();
return a.localeCompare(b);
})
files.unshift({
name: '..',
folder: true
})
list.innerHTML = ''
for (let i in files) {
var file = files[i]
list.appendChild(html`
<div class="file">
<input type="radio" value="${file.name}" name="file" class="${file.folder ? 'folder' : ''}"/>
<div class="label"><span>${raw(module.exports.icon(file.folder ? 'folder' : 'file-alt'))} ${file.name}</span></div>
</div>
`)
}
container.innerHTML = ''
browser.appendChild(ariane)
browser.appendChild(list)
browser.appendChild(actions)
container.appendChild(browser)
popup.open()
list.childNodes[0].firstElementChild.focus()
}, {context: popup})
popup.on('close', ()=>{
ipc.off('listDir', null, popup)
document.removeEventListener('keydown', keyHandler)
})
ipc.send('listDir', {})
}
}

View file

@ -597,3 +597,82 @@ body {
}
@import 'themes/default';
.file-browser {
width: 540px;
height: 400px;
max-width: 100%;
display: flex;
flex-direction: column;
flex: 1;
.ariane {
height: 40rem;
flex-shrink: 0;
line-height: 40rem;
padding-left: 10rem;
}
.file-list {
overflow-y: auto;
flex:1;
background: var(--color-bg);
padding: 0 10rem;
}
.file {
position: relative;
height: 40rem;
line-height: 40rem;
input {
position: absolute;
width:100%;
height: 100%;
opacity: 0;
cursor: pointer;
}
.label {
position: absolute;
width:100%;
height: 100%;
pointer-events: none;
white-space: nowrap;
span {
display: block;
width: 100%;
text-overflow: ellipsis;
overflow: hidden;
}
}
input:checked + .label {
color: var(--color-custom);
&:before {
content:"";
position: absolute;
left:-10rem;
right:-10rem;
height: 100%;
background: var(--color-custom);
opacity: 0.1;
}
}
}
.file-actions {
display: flex;
flex-direction: row;
align-items: flex-end;
justify-content: flex-end;
height: 40rem;
flex-shrink: 0;
margin-top: 10rem;
div {
margin-left: 10rem;
text-transform: uppercase;
}
i {
pointer-events: none;
}
}
}

View file

@ -70,6 +70,8 @@ module.exports = {
sessionOpen: function(data,clientId) {
if (Array.isArray(data.path)) data.path = path.resolve(...data.path)
var file = data.file || (function(){try {return fs.readFileSync(data.path,'utf8')} catch(err) {return false}})(),
error = file === false && data.path ? 'Session file "' + data.path + '" not found.' : false,
session
@ -327,6 +329,33 @@ module.exports = {
errorPopup: function(data) {
ipc.send('error', data)
},
listDir: function(data, clientId) {
var p = process.env[(process.platform == 'win32') ? 'USERPROFILE' : 'HOME'];
if (data.path) p = path.resolve(...data.path)
fs.readdir(p, (err, files)=>{
if (err) {
ipc.send('notify', {class: 'error', message: err.message}, clientId)
throw err
} else {
var list = files.filter(x=>x[0] !== '.').map((x)=>{
return {
name: x,
folder: fs.statSync(path.resolve(p, x)).isDirectory()
}
})
ipc.send('listDir', {
path: p,
files: list
}, clientId)
}
})
}
}