ES6ify some things

cpsdqs/unified-input
cpsdqs 7 years ago
parent f6b47ee358
commit 54d314517f
Signed by untrusted user: cpsdqs
GPG Key ID: 3F59586BB7448DD1
  1. 16
      js/soft_keyboard.js
  2. 4
      js/term.js
  3. 4
      js/term_conn.js
  4. 42
      js/term_input.js

@ -4,6 +4,9 @@ window.initSoftKeyboard = function (screen, input) {
let keyboardOpen = false let keyboardOpen = false
// moves the input to where the cursor is on the canvas.
// this is because most browsers will always scroll to wherever the focused
// input is
let updateInputPosition = function () { let updateInputPosition = function () {
if (!keyboardOpen) return if (!keyboardOpen) return
@ -20,16 +23,9 @@ window.initSoftKeyboard = function (screen, input) {
screen.on('cursor-moved', updateInputPosition) screen.on('cursor-moved', updateInputPosition)
let kbOpen = function (open) { qs('#term-kb-open').addEventListener('click', e => {
keyboardOpen = open e.preventDefault()
updateInputPosition() keyInput.focus()
if (open) keyInput.focus()
else keyInput.blur()
}
qs('#term-kb-open').addEventListener('click', function () {
kbOpen(true)
return false
}) })
// Chrome for Android doesn't send proper keydown/keypress events with // Chrome for Android doesn't send proper keydown/keypress events with

@ -1,7 +1,5 @@
/** Init the terminal sub-module - called from HTML */ /** Init the terminal sub-module - called from HTML */
window.termInit = function (opts) { window.termInit = function ({ labels, theme, allFn }) {
let { labels, theme, allFn } = opts
const screen = new TermScreen() const screen = new TermScreen()
const conn = Conn(screen) const conn = Conn(screen)
const input = Input(conn) const input = Input(conn)

@ -137,8 +137,8 @@ window.Conn = function (screen) {
return { return {
ws: null, ws: null,
init: init, init,
send: doSend, send: doSend,
canSend: canSend // check flood control canSend // check flood control
} }
} }

@ -32,7 +32,7 @@ window.Input = function (conn) {
/** Send a button event */ /** Send a button event */
function sendBtnMsg (n) { function sendBtnMsg (n) {
conn.send('b' + Chr(n)) conn.send('b' + String.fromCharCode(n))
} }
/** Fn alt choice for key message */ /** Fn alt choice for key message */
@ -50,7 +50,7 @@ window.Input = function (conn) {
return cfg.np_alt ? alt : normal return cfg.np_alt ? alt : normal
} }
function _bindFnKeys (allFn) { function bindFnKeys (allFn) {
const keymap = { const keymap = {
'tab': '\x09', 'tab': '\x09',
'backspace': '\x08', 'backspace': '\x08',
@ -139,9 +139,7 @@ window.Input = function (conn) {
} }
/** Bind/rebind key messages */ /** Bind/rebind key messages */
function _initKeys (opts) { function initKeys ({ allFn }) {
let { allFn } = opts
// This takes care of text characters typed // This takes care of text characters typed
window.addEventListener('keypress', function (evt) { window.addEventListener('keypress', function (evt) {
if (cfg.no_keys) return if (cfg.no_keys) return
@ -188,11 +186,11 @@ window.Input = function (conn) {
bind('⌥+right', '\x1bf') // ⌥→ to go forward one word (^[f) bind('⌥+right', '\x1bf') // ⌥→ to go forward one word (^[f)
bind('⌘+left', '\x01') // ⌘← to go to the beginning of a line (^A) bind('⌘+left', '\x01') // ⌘← to go to the beginning of a line (^A)
bind('⌘+right', '\x05') // ⌘→ to go to the end of a line (^E) bind('⌘+right', '\x05') // ⌘→ to go to the end of a line (^E)
bind('⌥+backspace', '\x17') // ⌥⌫ to delete a word (^W, I think) bind('⌥+backspace', '\x17') // ⌥⌫ to delete a word (^W)
bind('⌘+backspace', '\x15') // ⌘⌫ to delete to the beginning of a line (possibly ^U) bind('⌘+backspace', '\x15') // ⌘⌫ to delete to the beginning of a line (^U)
/* eslint-enable */ /* eslint-enable */
_bindFnKeys(allFn) bindFnKeys(allFn)
} }
// mouse button states // mouse button states
@ -202,23 +200,23 @@ window.Input = function (conn) {
/** Init the Input module */ /** Init the Input module */
function init (opts) { function init (opts) {
_initKeys(opts) initKeys(opts)
// Button presses // Button presses
$('#action-buttons button').forEach(function (s) { $('#action-buttons button').forEach(s => {
s.addEventListener('click', function () { s.addEventListener('click', () => {
sendBtnMsg(+this.dataset['n']) sendBtnMsg(+this.dataset['n'])
}) })
}) })
// global mouse state tracking - for motion reporting // global mouse state tracking - for motion reporting
window.addEventListener('mousedown', function (evt) { window.addEventListener('mousedown', evt => {
if (evt.button === 0) mb1 = 1 if (evt.button === 0) mb1 = 1
if (evt.button === 1) mb2 = 1 if (evt.button === 1) mb2 = 1
if (evt.button === 2) mb3 = 1 if (evt.button === 2) mb3 = 1
}) })
window.addEventListener('mouseup', function (evt) { window.addEventListener('mouseup', evt => {
if (evt.button === 0) mb1 = 0 if (evt.button === 0) mb1 = 0
if (evt.button === 1) mb2 = 0 if (evt.button === 1) mb2 = 0
if (evt.button === 2) mb3 = 0 if (evt.button === 2) mb3 = 0
@ -235,7 +233,7 @@ window.Input = function (conn) {
return { return {
/** Init the Input module */ /** Init the Input module */
init: init, init,
/** Send a literal string message */ /** Send a literal string message */
sendString: sendStrMsg, sendString: sendStrMsg,
@ -249,24 +247,24 @@ window.Input = function (conn) {
cfg.crlf_mode = crlf cfg.crlf_mode = crlf
// rebind keys - codes have changed // rebind keys - codes have changed
_bindFnKeys() bindFnKeys()
} }
}, },
setMouseMode: function (click, move) { setMouseMode (click, move) {
cfg.mt_click = click cfg.mt_click = click
cfg.mt_move = move cfg.mt_move = move
}, },
// Mouse events // Mouse events
onMouseMove: function (x, y) { onMouseMove (x, y) {
if (!cfg.mt_move) return if (!cfg.mt_move) return
const b = mb1 ? 1 : mb2 ? 2 : mb3 ? 3 : 0 const b = mb1 ? 1 : mb2 ? 2 : mb3 ? 3 : 0
const m = packModifiersForMouse() const m = packModifiersForMouse()
conn.send('m' + encode2B(y) + encode2B(x) + encode2B(b) + encode2B(m)) conn.send('m' + encode2B(y) + encode2B(x) + encode2B(b) + encode2B(m))
}, },
onMouseDown: function (x, y, b) { onMouseDown (x, y, b) {
if (!cfg.mt_click) return if (!cfg.mt_click) return
if (b > 3 || b < 1) return if (b > 3 || b < 1) return
const m = packModifiersForMouse() const m = packModifiersForMouse()
@ -274,7 +272,7 @@ window.Input = function (conn) {
// console.log("B ",b," M ",m); // console.log("B ",b," M ",m);
}, },
onMouseUp: function (x, y, b) { onMouseUp (x, y, b) {
if (!cfg.mt_click) return if (!cfg.mt_click) return
if (b > 3 || b < 1) return if (b > 3 || b < 1) return
const m = packModifiersForMouse() const m = packModifiersForMouse()
@ -282,7 +280,7 @@ window.Input = function (conn) {
// console.log("B ",b," M ",m); // console.log("B ",b," M ",m);
}, },
onMouseWheel: function (x, y, dir) { onMouseWheel (x, y, dir) {
if (!cfg.mt_click) return if (!cfg.mt_click) return
// -1 ... btn 4 (away from user) // -1 ... btn 4 (away from user)
// +1 ... btn 5 (towards user) // +1 ... btn 5 (towards user)
@ -292,11 +290,11 @@ window.Input = function (conn) {
// console.log("B ",b," M ",m); // console.log("B ",b," M ",m);
}, },
mouseTracksClicks: function () { mouseTracksClicks () {
return cfg.mt_click return cfg.mt_click
}, },
blockKeys: function (yes) { blockKeys (yes) {
cfg.no_keys = yes cfg.no_keys = yes
} }
} }

Loading…
Cancel
Save