From 092e4176b029d27ec668552cb3953db706e7d911 Mon Sep 17 00:00:00 2001 From: Mathew Date: Mon, 24 Jun 2019 15:12:52 +0200 Subject: [PATCH] module cleanup --- public/packages/Socket.js | 79 ++++++++++++++++++++------------------- public/packages/Tree.js | 62 ++++++++++++++++++++++-------- 2 files changed, 88 insertions(+), 53 deletions(-) diff --git a/public/packages/Socket.js b/public/packages/Socket.js index e7e3b93..11d738d 100644 --- a/public/packages/Socket.js +++ b/public/packages/Socket.js @@ -44,60 +44,51 @@ export default class Socket { constructor(con) { this.con = con - // throw new Error('test') - // socket event register function modification (pass additional parameter) - // this.socket.originalOn = this.socket.on; - // this.socket.on = function (event, data, callback) { - // return this.originalOn.call(this, event, (e) => callback(e, data)); - // }; - // newScript() - // .then(() => { - this.socket = io('/') + this.hostname = '/' + this.socket = io(`${this.hostname}`) - // socket connecting start event - this.socket.on('connecting', (data) => con.log("Conecting...", "info", 2)); - this.socket.on("connect_error", (data) => con.log(data, "error")) + this.waiting = {} - this.socket.on('auth', d => this.auth(d)) - this.socket.on('com', d => this._receive(d)) - this.socket.on('silent', d => this._silent(d)) + this.socket.on('auth', d => this.auth(d)) + this.socket.on('com', d => this._receive(d)) + this.socket.on('silent', d => this._silent(d)) - // socket disconnected event - this.socket.on("disconnect", (data) => { - con.log(`Disconnected from server: ${data}`, "warning") - this.socket.connect() - }) + this.socket.on('connecting', data => con.log('Conecting...', 'info', 2)); + this.socket.on('connect_error', data => con.log(data, 'error')) - // socket succesfully connected event - this.socket.on('connect', (data) => { - con.log("Conected to the server", "ok") - if (!this.tryAuth()) this.motd() - else con.log("Credentials detected, waiting for auth before motd", "info", 2) + this.socket.on('disconnect', data => { + con.log(`Disconnected from server: ${data}`, 'warning') + }) - }); - // }) - // .catch(err=>{ - // this.con.log(`socket.io.js download failed: ${err}`, 'error') - // }) + this.socket.on('connect', data => { + + con.log("Conected to the server", 'ok') + if (!this.tryAuth()) this.motd() + else con.log("Credentials detected, waiting for auth before motd", "info", 2) + + }); } motd() { if (this.con.first) return this.con.first = true - this.con.executeCom("motd"); + this.con.commandlineParse('motd'); if (location.hash != "") { this.con.log(`Executing command from URI (${location.hash.slice(1)})`, "info", 3) this.con.executeCom(decodeURIComponent(location.hash.slice(1))); } } - _silent(res){ - this.con.unpack(res) - } - - _receive(res) { - this.con.getCom(res.res.id).update(res); + getSilent(data){ + return new Promise((resolve, reject) => { + var out = { + query: data, + id: makeID(5) + } + this.waiting[out.id] = {resolve: resolve, reject: reject} + this.socket.emit('silent', out) + }) } send(com) { @@ -125,7 +116,7 @@ export default class Socket { return this.con.udataExp } - auth(udata) { + _auth(udata) { con.log("Auth data received", "ok", 2); this.con.udata = udata; this.con.uAuth(true) @@ -134,6 +125,18 @@ export default class Socket { this.con.motd() } + _receive(res) { + this.con.getCom(res.res.id).update(res); + } + + _silent(res) { + // this.con.unpack(res) + this.waiting[res.id].resolve(res) + delete this.waiting[res.id] + } + + _broadcast(data) {} + tryAuth() { if (con.credentials) { con.socket.emit("auth".credentials); diff --git a/public/packages/Tree.js b/public/packages/Tree.js index bc515a9..9495e3e 100644 --- a/public/packages/Tree.js +++ b/public/packages/Tree.js @@ -3,14 +3,31 @@ export var requires = { styles: [ 'tree.css' ], + modules: [ + // 'Fontello' + ] +} + +var colors = { + 'number': 'txred', + 'string': 'txgrn', + 'boolean': 'txyel', + 'function': 'txcya', + 'array': 'txyel', + 'object': 'txblu', + 'regex': 'txprp', + 'null': 'txxblk', + 'undefined': 'txxblk', + 'empty': 'txxprp', + 'last': 'txxprp', + 'fname': 'txxprp', + 'default': 'txxblk' } export default class Tree { constructor(data) { this.data = data - // requiresStyle('tree.css') - if (this._typeOf(data) != "array" && this._typeOf(data) != "object") this.$ = $("").append(this._contentType(data)).append(": ").append(`${data}`) else this.$ = $("") @@ -24,6 +41,21 @@ export default class Tree { ) ) + // tree collapse subtree click handling + this.$.on("click", ".collapse", function (e) { + var c = $(this); + + c.siblings("ul").toggle(200) + if (c.children("i").hasClass("icon-minus-squared")) { + c.children("i").removeClass(); + c.children("i").addClass("icon-plus-squared"); + } else { + c.children("i").removeClass(); + c.children("i").addClass("icon-minus-squared"); + } + + }) + } get() { @@ -46,16 +78,16 @@ export default class Tree { switch (type) { - case 'number': return $("").append(name ? name : "number") - case 'string': return $("").append(name ? name : "string") - case 'boolean': return $("").append(name ? name : "boolean") - case 'function': return $("").append(name ? name : "function") - case 'array': return $("").append(name ? name : "array") - case 'object': return $("").append(name ? name : "object") - case 'regex': return $("").append(name ? name : "regex") - case 'null': return $("").append(name ? name : "null") - case 'undefined': return $("").append(name ? name : "undefined") - default: return $("").append(name ? name : "?") + case 'number': return $(`<${colors.number}>`).append(name ? name : 'number') + case 'string': return $(`<${colors.string}>`).append(name ? name : 'string') + case 'boolean': return $(`<${colors.boolean}>`).append(name ? name : 'boolean') + case 'function': return $(`<${colors.function}>`).append(name ? name : 'function') + case 'array': return $(`<${colors.array}>`).append(name ? name : 'array') + case 'object': return $(`<${colors.object}>`).append(name ? name : 'object') + case 'regex': return $(`<${colors.regex}>`).append(name ? name : 'regex') + case 'null': return $(`<${colors.null}>`).append(name ? name : 'null') + case 'undefined': return $(`<${colors.undefined}>`).append(name ? name : 'undefined') + default: return $(`<${colors.default}>`).append(name ? name: '?') } } @@ -107,7 +139,7 @@ export default class Tree { $("
  • ") .append(this._contentType(content, key)) .append(": ") - .append(`[empty]`) + .append(`<${colors.empty}>[empty]`) ) } else { // content not empty if (depth > maxd) { // is max depth reached @@ -115,7 +147,7 @@ export default class Tree { $("
  • ") .append(this._contentType(content, key)) .append(": ") - .append(`${isWhat(content)}`) + .append(`<${colors.last}>${isWhat(content)}`) ) } else { // max depth not reached $list.append( // continue recursion @@ -137,7 +169,7 @@ export default class Tree { .append(this._contentType(content, key)) .append(": ") .append( - `${isWhat(content)}` + `<${colors.fname}>${isWhat(content)}` ) ) break;