From f6f22b1bc264ecf10b4fc4f590f43f82205fd14a Mon Sep 17 00:00:00 2001 From: shaoying Date: Sun, 18 Aug 2019 19:24:37 +0800 Subject: [PATCH] add Jshy --- usr/librarys/chat.js | 82 ++++++++++++++++---- usr/librarys/example.css | 22 ++++++ usr/librarys/example.js | 159 ++++++++++++++++++++++++++++++++++----- usr/librarys/toolkit.js | 46 +++++++---- 4 files changed, 265 insertions(+), 44 deletions(-) diff --git a/usr/librarys/chat.js b/usr/librarys/chat.js index 7311c964..5ca31192 100644 --- a/usr/librarys/chat.js +++ b/usr/librarys/chat.js @@ -72,8 +72,12 @@ Page({ break } else { switch (event.key) { + case " ": + page.footer.Pane.Select() + break case "Escape": page.dialog && page.dialog.Pane.Show() + break } } break @@ -157,8 +161,8 @@ Page({ field.Pane.Show() }) }, - Show: function() { - this.Dialog() && (this.Clear("good"), this.Run([], this.Append)) + Show: function() {var pane = field.Pane + pane.Dialog() && (pane.Clear("good"), pane.Run([], pane.Append)) }, Action: { "取消": function(event) { @@ -180,8 +184,8 @@ Page({ }, initRiver: function(page, field, option, output) { return { - Show: function() { - this.Update([], "text", ["nick", "count"], "key", ctx.Search("river")||true) + Show: function() {var pane = field.Pane + output.innerHTML = "", pane.Update([], "text", ["nick", "count"], "key", ctx.Search("river")||true) }, Action: { "创建": function(event) { @@ -196,7 +200,7 @@ Page({ output.DisplayUser = true output.DisplayTime = true return { - Send: function(type, text, cb) {var pane = this + Send: function(type, text, cb) {var pane = field.Pane pane.Run([river, "flow", type, text], function(msg) { pane.Show(), typeof cb == "function" && cb(msg) }) @@ -204,7 +208,7 @@ Page({ Stop: function() { return field.style.display == "none" }, - Show: function(i) {var pane = this + Show: function(i) {var pane = field.Pane field.Pane.Back(river, output) var foot = page.footer.Pane, cmds = [river, "brow", i||which[river]||0] @@ -250,6 +254,20 @@ Page({ var river = "", storm = 0, input = "", share = "" output.DisplayRaw = true return { + Tutor: function() {var pane = field.Pane + var event = window.event + function loop(list, index) { + if (index >= list.length) {return} + kit.Log(index, list[index]) + pane.Core(event, {}, ["_cmd", list[index]]) + setTimeout(function() {loop(list, index+1)}, 1000) + } + loop([ + "聊天", "help", "最高", "最大", "聊天", + "工作", "串行", "清空", "并行", "help storm", "help storm list", "help action", "help action list", + "聊天", "help target", "help target list", + ], 0) + }, Core: function(event, line, args, cbs) { var plugin = event.Plugin || {}, engine = { share: function(args) { @@ -258,6 +276,42 @@ Page({ ]})) return true }, + echo: function(one, two) { + kit.Log(one, two) + }, + _split: function(str) {return str.trim().split(" ")}, + help: function() { + var args = kit.List(arguments) + if (args.length > 1 && page[args[0]] && page[args[0]].Pane[args[1]]) { + return kit._call(page[args[0]].Pane[args[1]].Plugin.Help, args.slice(2)) + } + if (args.length > 0 && page[args[0]]) { + return kit._call(page[args[0]].Pane.Help, args.slice(1)) + } + return kit._call(page.Help, args) + }, + _cmd: function(arg) { + var args = engine._split(arg[1]); + if (typeof engine[args[0]] == "function") { + return kit._call(engine[args[0]], args.slice(1)) + } + + if (page.plugin && page.plugin.Plugin.Jshy(event, args)) {return true} + if (page.dialog && page.dialog.Pane.Jshy(event, args)) {return true} + if (page.pane && page.pane.Pane.Jshy(event, args)) {return true} + if (page && page.Jshy(event, args)) {return true} + + if (page.plugin) { + if (args.length > 1) { + return kit._call(page.plugin.Plugin.Run, [window.event].concat(args.slice(1))) + } else { + return kit._call(page.plugin.Plugin.Runs, [window.event]) + } + } + + kit.Log("not find", arg[1]) + return true + }, _msg: function(msg) { if (msg) { var text = plugin? plugin.Reveal(msg): "" @@ -276,16 +330,16 @@ Page({ if (args.length > 0 && engine[args[0]] && engine[args[0]](args)) {return} event.shiftKey? engine._msg(): engine._run() }, - Show: function() {var pane = this + Show: function() {var pane = field.Pane if (field.Pane.Back(river+storm, output)) {return} - this.Clear(), this.Update([river, storm], "plugin", ["node", "name"], "index", false, function(line, index, event, args, cbs) { + pane.Clear(), pane.Update([river, storm], "plugin", ["node", "name"], "index", false, function(line, index, event, args, cbs) { pane.Core(event, line, args, cbs) }) }, - Layout: function(name) { + Layout: function(name) {var pane = field.Pane var layout = field.querySelector("select.layout") - name && this.Action[layout.value = name](window.event, layout.value) + name && pane.Action[layout.value = name](window.event, layout.value) return layout.value }, Listen: { @@ -385,9 +439,9 @@ Page({ var prev = output.querySelector("div.item.select").previousSibling prev? prev.click(): output.lastChild.click() }, - Show: function(which) { - this.which.get("") == which && page.action.Pane.Show() - output.innerHTML = "", this.Update([river], "text", ["key", "count"], "key", which||ctx.Search("storm")||true) + Show: function(which) {var pane = field.Pane + pane.which.get("") == which && page.action.Pane.Show() + output.innerHTML = "", pane.Update([river], "text", ["key", "count"], "key", which||ctx.Search("storm")||true) }, Listen: { river: function(value, old) { @@ -518,5 +572,7 @@ Page({ page.onaction[item] && page.onaction[item](event, item, value) }) page.river.Pane.Show() + page.pane = page.action + page.plugin = kit.Selector(page.action, "fieldset")[0] }, }) diff --git a/usr/librarys/example.css b/usr/librarys/example.css index 37aadfd5..d5966605 100644 --- a/usr/librarys/example.css +++ b/usr/librarys/example.css @@ -6,6 +6,7 @@ html, body { margin:0px; } fieldset { + position:relative; background-color:#d8d8d8; padding:0px; min-width:10px; @@ -16,6 +17,20 @@ legend { margin-left:10px; } +fieldset>div.Help { + position:absolute; + left:0px; + top:0px; + width:calc(100% - 4px); + height:calc(100% - 4px); +} +fieldset>div.Help>div{ + border:solid 2px red; + background-color:cyan; + color:blue; + opacity:0.7; + overflow:auto; +} fieldset>form.option div { margin-right:6px; float:left; @@ -164,6 +179,13 @@ fieldset.Footer { fieldset.Footer>div.output div.title { float:left; } +fieldset.Footer>div.output div.magic { + float:left; +} +fieldset.Footer>div.output div.magic>input { + background-color:black; + color:lightgreen;; +} fieldset.Footer>div.output div.state { float:right; } diff --git a/usr/librarys/example.js b/usr/librarys/example.js index 872cd506..ae3ab6b5 100644 --- a/usr/librarys/example.js +++ b/usr/librarys/example.js @@ -317,6 +317,9 @@ function Page(page) { }, onscroll: function(event, target, action) { switch (event.key) { + case " ": + page.footer.Pane.Select() + break case "h": if (event.ctrlKey) { target.scrollBy(-conf.scroll_x*10, 0) @@ -360,6 +363,26 @@ function Page(page) { } }, + Help: function(pane, type, action) { + if (pane == undefined) { + kit.Selector(document.body, "body>fieldset", function(field) { + field.Pane.Help() + }) + return true + } + page[pane].Pane.Help(type, action) + }, + Jshy: function(event, args) { + if (page[args[0]] && page[args[0]].type == "fieldset") { + if (args.length > 1) { + return page[args[0]].Pane.Jshy(event, args.slice(1)) + } else { + return page[args[0]].Pane.Show() + } + } + return typeof page[args[0]] == "function" && kit._call(page[args[0]], args.slice(1)) + }, + initLogin: function(page, field, option, output) { var ui = kit.AppendChilds(option, [ {label: "username"}, {input: ["username"]}, {type: "br"}, @@ -376,7 +399,7 @@ function Page(page) { ]) return { Login: function(username, password, cb) { - this.Run([username, password], function(msg) {cb(msg.result && msg.result[0] || "")}) + field.Pane.Run([username, password], function(msg) {cb(msg.result && msg.result[0] || "")}) }, Exit: function() {ctx.Cookie("sessid", ""), kit.reload()}, } @@ -386,10 +409,10 @@ function Page(page) { field.onclick = function(event) {page.pane && page.pane.scrollTo(0,0)} return { Order: function(value, order, cbs) { - state = value, list = order, cb = cbs || cb, this.Show() + state = value, list = order, cb = cbs || cb, field.Pane.Show() }, State: function(name, value) { - value != undefined && (state[name] = value, this.Show()) + value != undefined && (state[name] = value, field.Pane.Show()) return name == undefined? state: state[name] }, Show: function() { @@ -406,22 +429,55 @@ function Page(page) { }, initFooter: function(page, field, option, output) { var state = {}, list = [], cb = function(event, item, value) {} - field.onclick = function(event) {page.pane.scrollTo(0,page.pane.scrollHeight)} + field.onclick = function(event) {page.pane && page.pane.scrollTo(0,page.pane.scrollHeight)} + var ui, w = 0 return { + Select: function() { + ui.magic.focus() + }, Order: function(value, order, cbs) { - state = value, list = order, cb = cbs || cb, this.Show() + state = value, list = order, cb = cbs || cb, field.Pane.Show() }, State: function(name, value) { - value != undefined && (state[name] = value, this.Show()) + value != undefined && (state[name] = value, field.Pane.Show()) return name == undefined? state: state[name] }, + Size: function(width, height) { + kit.size(field, width, height) + ui && (w = width - ui.first.clientWidth - ui.last.clientWidth - 20) && kit.size(ui.magic, w, height-6) + }, Show: function() { - output.innerHTML = "", kit.AppendChild(output, [ + output.innerHTML = "", ui = kit.AppendChild(output, [ {"view": ["title", "div", "shylinux@163.com"]}, + {"view": ["magic"], style: {"margin-top": "-4px"}, list: [{input: ["magic", function(event) { + if (event.type != "keydown") {return} + + switch (event.key) { + case " ": + return + case "Enter": + page.action.Pane.Core(event, {}, ["_cmd", event.target.value]), event.target.value = "" + return + } + + page.oninput(event, function(event) { + switch (event.key) { + case "j": + page.action.Pane.Core(event, {}, ["_cmd", event.target.value]), event.target.value = "" + break + case "Enter": + kit.Log(event.target.value) + break + } + }) + return true + + }], style: {width: w, "margin-top": "-2px", "font-size": "16px"}}]}, {"view": ["state"], list: list.map(function(item) {return {text: [item+":"+state[item], "div"], click: function(item) { cb(event, item, state[item]) }}})}, ]) + field.Pane.Size(field.clientWidth, field.clientHeight) }, } }, @@ -468,6 +524,46 @@ function Pane(page, field) { output.innerHTML = "", list = [], last = -1 }, + Help: function(type, action) { + if (kit.Selector(field, "div.Help", function(help) { + field.removeChild(help) + return help + }).length>0 || action == "hide") {return} + + var text = [], delay = 30000 + switch (type) { + case "name": + case undefined: + text = [name] + break + case "list": + var list = [] + for (var k in pane) {list.push(k)} + list.sort(), text = text.concat(list.map(function(item) {return "func: "+item+"\n"})) + + var list = [] + for (var k in pane.Action) {list.push(k)} + list.sort(), text = text.concat(list.map(function(item) {return "action: "+item+"\n"})) + break + } + + kit.AppendChild(field, [{view: ["Help"], list: [{text: [text.join(""), "div"]}]}]) + setTimeout(function() {pane.Help("", "hide")}, delay) + }, + Jshy: function(event, args) { + if (pane[args[0]] && pane[args[0]].type == "fieldset") { + if (args.length > 1) { + return kit._call(pane[args[0]].Plugin.Run, [event].concat(args.slice(1))) + } else { + return kit._call(pane[args[0]].Plugin.Runs, [event]) + } + } + if (typeof pane.Action[args[0]] == "function") { + return kit._call(pane.Action[args[0]], [event, args[0]]) + } + return typeof pane[args[0]] == "function" && kit._call(pane[args[0]], args.slice(1)) + }, + Tickers: function(time, cmds, cb) { pane.Ticker(time, cmds, function(msg) { ctx.Table(msg, function(line, index) { @@ -487,12 +583,12 @@ function Pane(page, field) { Runs: function(cmds, cb) { pane.Run(cmds, function(msg) { ctx.Table(msg, function(line, index) { - (cb||this.ondaemon)(line, index, msg) + (cb||pane.ondaemon)(line, index, msg) }) }) }, Run: function(cmds, cb) { - ctx.Run(page, option.dataset, cmds, cb||this.ondaemon) + ctx.Run(page, option.dataset, cmds, cb||pane.ondaemon) }, Size: function(width, height) { @@ -599,7 +695,33 @@ function Plugin(page, pane, field, run) { return JSON.stringify(field.Meta) }, Clone: function() { - return pane.Append("field", {text: plugin.Format()}, [], run).field.Plugin + return pane.Append("field", {text: plugin.Format()}, [], "", function(line, index, event, cmds, cbs) { + run(event, cmds, cbs) + }).field.Plugin + }, + + Help: function(type, action) { + if (kit.Selector(field, "div.Help", function(help) { + field.removeChild(help) + return help + }).length>0 || action == "hide") {return} + + var text = [], delay = 30000 + switch (type) { + case "name": + case undefined: + text = [meta.name] + break + case "help": + text = [meta.help] + break + } + + kit.AppendChild(field, [{view: ["Help"], list: [{text: [text.join(""), "div"]}]}]) + setTimeout(function() {plugin.Help("", "hide")}, delay) + }, + Jshy: function(event, args) { + return typeof plugin[args[0]] == "function" && kit._call(plugin[args[0]], args.slice(1)) }, Delay: function(time, event, text) { @@ -610,11 +732,12 @@ function Plugin(page, pane, field, run) { }, Check: function(target, cb) { option.querySelectorAll(".args").forEach(function(item, index, list) { - item == target && (index == list.length-1? plugin.Runs(event, cb): page.plugin == field && list[index+1].focus()) + target == undefined && index == list.length-1 && plugin.Runs(window.event, cb) + item == target && (index == list.length-1? plugin.Runs(window.event, cb): page.plugin == field && list[index+1].focus()) }) }, Runs: function(event, cb) { - this.Run(event, kit.Selector(option, ".args", function(item, index) {return item.value}), cb) + plugin.Run(event, kit.Selector(option, ".args", function(item, index) {return item.value}), cb) }, Run: function(event, args, cb) { var show = true @@ -655,11 +778,12 @@ function Plugin(page, pane, field, run) { return value.split("/")[0] }, you: function(value, name, line) { - window.event.Plugin = plugin + var event = window.event + event.Plugin = plugin line.you && name == "status" && (line.status == "start"? function() { - plugin.Delay(3000, window.event, line.you+" stop...") && field.Run(window.event, [line.you, "stop"]) - }(): field.Run(window.event, [line.you], function(msg) { - plugin.Delay(3000, window.event, line.you+" start...") + plugin.Delay(3000, event, line.you+" stop...") && field.Run(event, [line.you, "stop"]) + }(): field.Run(event, [line.you], function(msg) { + plugin.Delay(3000, event, line.you+" start...") })) return name == "status" || line.status == "stop" ? undefined: line.you }, @@ -756,11 +880,12 @@ function Plugin(page, pane, field, run) { }) var meta = field.Meta + var name = meta.name var args = meta.args || [] var display = JSON.parse(meta.display||'{}') var exports = JSON.parse(meta.exports||'["",""]') JSON.parse(meta.inputs || "[]").map(plugin.Append) - return page[field.id] = pane[field.id] = field, field.Plugin = plugin + return page[field.id] = pane[field.id] = pane[name] = field, field.Plugin = plugin } function Editor(plugin, option, output, width, height, space, msg) { exports = ["dir", "path", "dir"] diff --git a/usr/librarys/toolkit.js b/usr/librarys/toolkit.js index 4a2051d5..17789ccb 100644 --- a/usr/librarys/toolkit.js +++ b/usr/librarys/toolkit.js @@ -571,6 +571,9 @@ kit = toolkit = { location.reload() }, + distance: function(x0, y0, x1, y1) { + return Math.sqrt(Math.pow(x1-x0, 2)+Math.pow(y1-y0, 2)) + }, number: function(d, n) { var result = [] while (d>0) { @@ -585,17 +588,6 @@ kit = toolkit = { result.reverse() return result.join("") }, - time: function(t, fmt) { - var now = t? new Date(t): new Date() - fmt = fmt || "%y-%m-%d %H:%M:%S" - fmt = fmt.replace("%y", now.getFullYear()) - fmt = fmt.replace("%m", kit.number(now.getMonth()+1, 2)) - fmt = fmt.replace("%d", kit.number(now.getDate(), 2)) - fmt = fmt.replace("%H", kit.number(now.getHours(), 2)) - fmt = fmt.replace("%M", kit.number(now.getMinutes(), 2)) - fmt = fmt.replace("%S", kit.number(now.getSeconds(), 2)) - return fmt - }, right: function(arg) { if (arg == "true") { return true @@ -608,6 +600,17 @@ kit = toolkit = { } return false }, + time: function(t, fmt) { + var now = t? new Date(t): new Date() + fmt = fmt || "%y-%m-%d %H:%M:%S" + fmt = fmt.replace("%y", now.getFullYear()) + fmt = fmt.replace("%m", kit.number(now.getMonth()+1, 2)) + fmt = fmt.replace("%d", kit.number(now.getDate(), 2)) + fmt = fmt.replace("%H", kit.number(now.getHours(), 2)) + fmt = fmt.replace("%M", kit.number(now.getMinutes(), 2)) + fmt = fmt.replace("%S", kit.number(now.getSeconds(), 2)) + return fmt + }, format_date: function(arg) { var date = arg.getDate() if (date < 10) { @@ -631,6 +634,24 @@ kit = toolkit = { } return arg.getFullYear()+"-"+month+"-"+date+" "+hour+":"+minute+":"+second }, + size: function(obj, width, height) { + obj.style.width = width+"px" + obj.style.height = height+"px" + }, + _call: function(cb, arg) { + switch (arg.length) { + case 0: cb(); break + case 1: cb(arg[0]); break + case 2: cb(arg[0], arg[1]); break + case 3: cb(arg[0], arg[1], arg[2]); break + case 4: cb(arg[0], arg[1], arg[2], arg[3]); break + case 5: cb(arg[0], arg[1], arg[2], arg[3], arg[4]); break + case 6: cb(arg[0], arg[1], arg[2], arg[3], arg[4], arg[5]); break + case 7: cb(arg[0], arg[1], arg[2], arg[3], arg[4], arg[5], arg[6]); break + } + return true + }, + OrderForm: function(page, field, option, append, result) { if (!option) {return} option.ondaemon = option.ondaemon || function(msg) { @@ -697,8 +718,5 @@ kit = toolkit = { } }) }, - distance: function(x0, y0, x1, y1) { - return Math.sqrt(Math.pow(x1-x0, 2)+Math.pow(y1-y0, 2)) - }, }