cloudcmd/client/modules/terminal.js
2018-05-22 16:52:04 +03:00

126 lines
2.4 KiB
JavaScript

'use strict';
/* global CloudCmd, gritty */
require('../../css/terminal.css');
const exec = require('execon');
const load = require('../dom/load');
const DOM = require('../dom');
const Images = require('../dom/images');
const {Dialog} = DOM;
const TITLE = 'Terminal';
CloudCmd.Terminal = TerminalProto;
const {Key} = CloudCmd;
let Element;
let Loaded;
let Terminal;
const {config} = CloudCmd;
function TerminalProto() {
const noop = () => {};
if (!config('terminal'))
return {
show: noop
};
Images.show.load('top');
exec.series([
CloudCmd.View,
loadAll,
create,
show,
]);
Element = load({
name: 'div',
className : 'terminal',
});
return module.exports;
}
module.exports.show = show;
module.exports.hide = hide;
function hide () {
CloudCmd.View.hide();
}
function getPrefix() {
return CloudCmd.PREFIX + '/gritty';
}
function getEnv() {
return {
ACTIVE_DIR: DOM.getCurrentDirPath,
PASSIVE_DIR: DOM.getNotCurrentDirPath,
CURRENT_NAME: DOM.getCurrentName,
CURRENT_PATH: DOM.getCurrentPath,
};
}
function create(callback) {
const options = {
env: getEnv(),
prefix: getPrefix(),
socketPath: CloudCmd.PREFIX,
fontFamily: 'Droid Sans Mono',
};
const {socket, terminal} = gritty(Element, options);
Terminal = terminal;
terminal.on('key', (char, {keyCode, shiftKey}) => {
if (shiftKey && keyCode === Key.ESC) {
hide();
}
});
socket.on('connect', exec.with(authCheck, socket));
exec(callback);
}
function authCheck(spawn) {
spawn.emit('auth', config('username'), config('password'));
spawn.on('reject', () => {
Dialog.alert(TITLE, 'Wrong credentials!');
});
}
function show(callback) {
if (!Loaded)
return;
CloudCmd.View.show(Element, {
afterShow: () => {
if (Terminal)
Terminal.focus();
exec(callback);
}
});
}
function loadAll(callback) {
const prefix = getPrefix();
const url = prefix + '/gritty.js';
DOM.load.js(url, (error) => {
if (error)
return Dialog.alert(TITLE, error.message);
Loaded = true;
exec(callback);
});
}