From 6128bc86db4e1eb807953921110df356b460b421 Mon Sep 17 00:00:00 2001 From: Lisa Milne Date: Thu, 16 Nov 2023 07:40:44 +1000 Subject: [PATCH] move clite.state.input and clite.events to clite.term --- clite/core.js | 143 +++++++++++++++++++++++++------------------------- 1 file changed, 71 insertions(+), 72 deletions(-) diff --git a/clite/core.js b/clite/core.js index 7fe95ee..4df068f 100644 --- a/clite/core.js +++ b/clite/core.js @@ -1,54 +1,7 @@ var clite = { state:{ version:'0.1-13', - isinit:false, - terminal:null, - input:{ - type:'text', - form:null, - field:null - } - }, - events:{ - keydown:function(e) { - return clite.events.keypress(e); - }, - keypress:function(e) { - if (e.key == 'Tab') { - clite.shell.tabfill(); - clite.events.refocus(); - return false; - } - return true; - }, - keyup:function(e) { - try { - if (e.key == 'Down' || e.key == 'ArrowDown') { - clite.shell.history.down(); - return false; - } - if (e.key == 'Up' || e.key == 'ArrowUp') { - clite.shell.history.up(); - return false; - } - if (e.key == 'Tab') { - return false; - } - var t = clite.state.input.field.value; - if (e.key != 'Enter') { - clite.shell.history.setCurrent(t); - return true; - } - clite.shell.run(t); - return false; - } catch(err) {return false;} - }, - refocus:function(e) { - try { - clite.term.genForm(); - clite.state.input.field.focus(); - } catch(err) {} - } + isinit:false }, core:{ execSafe:function(f) { @@ -489,7 +442,7 @@ clite.user = { } vfs.mkLink('/usr/home/guest/web','/usr/share/site'); setLogin('guest',1,1,[]); - clite.events.refocus(); + clite.term.events.refocus(); } return false; }, @@ -1055,6 +1008,52 @@ clite.log = { }; clite.term = { + data:{ + type:'text', + form:null, + field:null + }, + events:{ + keydown:function(e) { + return clite.term.events.keypress(e); + }, + keypress:function(e) { + if (e.key == 'Tab') { + clite.shell.tabfill(); + clite.term.events.refocus(); + return false; + } + return true; + }, + keyup:function(e) { + try { + if (e.key == 'Down' || e.key == 'ArrowDown') { + clite.shell.history.down(); + return false; + } + if (e.key == 'Up' || e.key == 'ArrowUp') { + clite.shell.history.up(); + return false; + } + if (e.key == 'Tab') { + return false; + } + var t = clite.term.data.field.value; + if (e.key != 'Enter') { + clite.shell.history.setCurrent(t); + return true; + } + clite.shell.run(t); + return false; + } catch(err) {return false;} + }, + refocus:function(e) { + try { + clite.term.genForm(); + clite.term.data.field.focus(); + } catch(err) {} + } + }, tty:{ data:null, keydown:function(e) { @@ -1063,7 +1062,7 @@ clite.term = { }, keypress:function(e) { if (e.key == 'Tab') { - clite.events.refocus(); + clite.term.events.refocus(); return false; } // urgh, no. handle press in keyup because fuck js @@ -1112,8 +1111,8 @@ clite.term = { i.type = clite.term.getType(); i.value = clite.shell.history.getCurrent(); f.appendChild(i); - clite.state.input.form = f; - clite.state.input.field = i; + clite.term.data.form = f; + clite.term.data.field = i; var t = document.getElementById('terminal'); if (!t) return; @@ -1127,24 +1126,24 @@ clite.term = { i.onkeypress = clite.shell.reading.keypress; i.onkeyup = clite.shell.reading.keyup; }else{ - i.onkeydown = clite.events.keydown; - i.onkeypress = clite.events.keypress; - i.onkeyup = clite.events.keyup; + i.onkeydown = clite.term.events.keydown; + i.onkeypress = clite.term.events.keypress; + i.onkeyup = clite.term.events.keyup; } - i.onfocusout = clite.events.refocus; - i.onblur = clite.events.refocus; + i.onfocusout = clite.term.events.refocus; + i.onblur = clite.term.events.refocus; if (i.type == 'file') { // this is only needed due to dirty hacks to read in a file: path i.onchange = function(e) { var reader = new FileReader(); reader.onload = function() { - var cb = clite.state.input.type.callback; + var cb = clite.term.data.type.callback; clite.term.setPass(false); clite.term.writeLine('loaded'); clite.term.preventInput(); cb(reader.result); }; reader.onerror = function() { - var cb = clite.state.input.type.callback; + var cb = clite.term.data.type.callback; clite.term.setPass(false); clite.term.writeLine('load failed'); clite.term.preventInput(); @@ -1156,16 +1155,16 @@ clite.term = { f.onsubmit = function() {return false;}; }, setPass:function(is) { - clite.state.input.type = (!!is) ? 'password' : 'text'; + clite.term.data.type = (!!is) ? 'password' : 'text'; }, setCustom:function(type) { - clite.state.input.type = type; + clite.term.data.type = type; }, getType:function() { - if (typeof clite.state.input.type == 'string') - return clite.state.input.type; - if (typeof clite.state.input.type == 'object' && typeof clite.state.input.type.type == 'string') - return clite.state.input.type.type; + if (typeof clite.term.data.type == 'string') + return clite.term.data.type; + if (typeof clite.term.data.type == 'object' && typeof clite.term.data.type.type == 'string') + return clite.term.data.type.type; return 'text'; }, api:{ @@ -1228,7 +1227,7 @@ clite.term = { // TODO: shouldn't need the 0.8 thing, but urgh tty.chars = parseInt((tty.data.el.offsetWidth/s.offsetWidth)*0.8); tty.data.el.removeChild(s); - clite.events.refocus(); + clite.term.events.refocus(); return tty; }, closeTTY:function(tty) { @@ -1252,7 +1251,7 @@ clite.shell = { }, keypress:function(e) { if (e.key == 'Tab') { - clite.events.refocus(); + clite.term.events.refocus(); return false; } // urgh, no. handle press in keyup because fuck js @@ -1277,17 +1276,17 @@ clite.shell = { if (clite.term.tty.data != null) clite.term.api.closeTTY(clite.term.tty.data); clite.shell.prompt.pop(); - clite.events.refocus(); + clite.term.events.refocus(); }, readLine:function(cb) { clite.shell.reading.is = true; clite.shell.reading.buff = ''; clite.shell.reading.callback = cb; - clite.events.refocus(); + clite.term.events.refocus(); }, writeLine:function(txt) { clite.term.writeLine(txt); - clite.events.refocus(); + clite.term.events.refocus(); }, resolvePath:function(txt) { var paths = clite.shell.env.PATH.split(':'); @@ -1600,13 +1599,13 @@ clite.shell = { if (clite.shell.history.index == 0) return; clite.shell.history.index--; - clite.events.refocus(); + clite.term.events.refocus(); }, down:function() { if (clite.shell.history.index == clite.shell.history.data.length) return; clite.shell.history.index++; - clite.events.refocus(); + clite.term.events.refocus(); }, setCurrent:function(txt) { clite.shell.history.current = txt;