-
-
Notifications
You must be signed in to change notification settings - Fork 254
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f6dcdb7
commit c416429
Showing
9 changed files
with
231 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
'use strict'; | ||
|
||
/* global DOM */ | ||
/* global CloudCmd */ | ||
|
||
const { | ||
Dialog, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
'use strict'; | ||
|
||
/* global CloudCmd, DOM */ | ||
|
||
require('../../css/user-menu.css'); | ||
|
||
const currify = require('currify/legacy'); | ||
const {promisify} = require('es6-promisify'); | ||
const load = require('load.js'); | ||
const createElement = require('@cloudcmd/create-element'); | ||
const Images = require('../dom/images'); | ||
|
||
const loadCSS = promisify(load.css); | ||
|
||
const Name = 'UserMenu'; | ||
CloudCmd[Name] = module.exports; | ||
|
||
const {Key} = CloudCmd; | ||
const {stringify} = JSON; | ||
|
||
const userMenuUrl = '/api/v1/user-menu'; | ||
|
||
module.exports.init = async () => { | ||
await Promise.all([ | ||
loadCSS(`${CloudCmd.prefix}/dist/user-menu.css`), | ||
CloudCmd.View(), | ||
]); | ||
}; | ||
|
||
module.exports.show = show; | ||
module.exports.hide = hide; | ||
|
||
const getKey = (a) => a.split(' - ')[0]; | ||
const beginWith = (a) => (b) => !b.indexOf(a); | ||
|
||
const defaultUserMenu = { | ||
'F2 - Rename file': async ({DOM}) => { | ||
const {element} = DOM.CurrentInfo; | ||
DOM.renameCurrent(element); | ||
}, | ||
}; | ||
|
||
function getUserMenu(menuFn) { | ||
if (!menuFn) | ||
return defaultUserMenu; | ||
|
||
const module = {}; | ||
const fn = Function('module', menuFn); | ||
|
||
fn(module); | ||
|
||
return module.exports; | ||
} | ||
|
||
const {CurrentInfo} = DOM; | ||
|
||
async function show() { | ||
Images.show.load('top'); | ||
|
||
const {dirPath} = CurrentInfo; | ||
const res = await fetch(`${userMenuUrl}?dir=${dirPath}`); | ||
const userMenu = getUserMenu(await res.text()); | ||
const options = Object.keys(userMenu); | ||
|
||
const el = createElement('select', { | ||
className: 'cloudcmd-user-menu', | ||
innerHTML: fillTemplate(options), | ||
size: 10, | ||
}); | ||
|
||
const keys = options.map(getKey); | ||
el.addEventListener('keydown', onKeyDown(keys, options, userMenu)); | ||
|
||
const afterShow = () => el.focus(); | ||
const autoSize = true; | ||
|
||
Images.hide(); | ||
|
||
CloudCmd.View.show(el, { | ||
autoSize, | ||
afterShow, | ||
}); | ||
} | ||
|
||
function fillTemplate(options) { | ||
const result = []; | ||
|
||
for (const option of options) { | ||
result.push(`<option>${option}</option>`); | ||
} | ||
|
||
return result.join(''); | ||
} | ||
|
||
function hide() { | ||
CloudCmd.View.hide(); | ||
} | ||
|
||
const onKeyDown = currify(async (keys, options, userMenu, e) => { | ||
const {keyCode} = e; | ||
const key = e.key.toUpperCase(); | ||
|
||
let value; | ||
|
||
if (keyCode === Key.ENTER) | ||
({value} = e.target); | ||
else if (keys.includes(key)) | ||
value = options.find(beginWith(key)); | ||
else | ||
return; | ||
|
||
e.preventDefault(); | ||
e.stopPropagation(); | ||
|
||
hide(); | ||
|
||
const cmd = await userMenu[value]({ | ||
DOM, | ||
}); | ||
|
||
if (!cmd) | ||
return; | ||
|
||
const res = await fetch(userMenuUrl, { | ||
method: 'PUT', | ||
body: stringify({ | ||
cmd, | ||
}), | ||
}); | ||
|
||
const data = await res.text(); | ||
|
||
if (data) | ||
DOM.Dialog.alert(data); | ||
|
||
CloudCmd.refresh(); | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
.cloudcmd-user-menu { | ||
font-size: 16px; | ||
font-family: 'Droid Sans Mono', 'Ubuntu Mono', 'Consolas', monospace; | ||
width: 400px; | ||
} | ||
|
||
.cloudcmd-user-menu:focus { | ||
outline: 0; | ||
} | ||
|
||
.cloudcmd-user-menu > option:checked { | ||
box-shadow: 20px -20px 0 2px rgba(49, 123, 249) inset; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,8 @@ | |
"operation", | ||
"konsole", | ||
"terminal", | ||
"cloud" | ||
"cloud", | ||
"user-menu" | ||
], | ||
"remote": [{ | ||
"name": "socket", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
'use strict'; | ||
|
||
const {homedir} = require('os'); | ||
const fs = require('fs'); | ||
const {join} = require('path'); | ||
const {exec} = require('child_process'); | ||
|
||
const tryToCatch = require('try-to-catch'); | ||
const pullout = require('pullout'); | ||
const {promisify} = require('util'); | ||
const {parse} = JSON; | ||
|
||
const readFile = promisify(fs.readFile); | ||
const execute = promisify(exec); | ||
const menuName = '.cloudcmd.menu.js'; | ||
const userMenuPath = join(homedir(), menuName); | ||
|
||
module.exports = async (req, res, next) => { | ||
if (req.url.indexOf('/api/v1/user-menu')) | ||
return next(); | ||
|
||
const {method} = req; | ||
|
||
if (method === 'GET') | ||
return onGET(req.query, res); | ||
|
||
if (method === 'PUT') { | ||
return onPUT(await pullout(req), res); | ||
} | ||
|
||
next(); | ||
}; | ||
|
||
async function onGET({dir}, res) { | ||
const userMenuCurrentPath = join(dir, menuName); | ||
const [dirError, dirData] = await tryToCatch(readFile, userMenuCurrentPath, 'utf8'); | ||
|
||
if (!dirError) | ||
return res.send(dirData); | ||
|
||
if (dirError.code !== 'ENOENT') | ||
return res | ||
.status(404) | ||
.send(dirError.message); | ||
|
||
const [e, data] = await tryToCatch(readFile, userMenuPath, 'utf8'); | ||
|
||
if (!e) | ||
return res.send(data); | ||
|
||
if (e.code === 'ENOENT') | ||
return res.send(''); | ||
|
||
return res | ||
.status(404) | ||
.send(e.message); | ||
} | ||
|
||
async function onPUT(str, res) { | ||
const {cmd} = parse(str); | ||
const [e, data] = await tryToCatch(execute, cmd); | ||
|
||
if (e) | ||
return res.send(e.message); | ||
|
||
return res.send(data); | ||
} | ||
|