From a1afe94027c9b471952523ebf3d5359fc2466dba Mon Sep 17 00:00:00 2001 From: shaoying Date: Thu, 26 Sep 2019 21:08:08 +0800 Subject: [PATCH] opt ctx.Event --- etc/init.shy | 10 ++--- src/contexts/cli/cli.go | 1 + src/contexts/cli/version.go | 2 +- src/plugin/context/index.shy | 6 ++- usr/librarys/chat.js | 2 + usr/librarys/context.js | 16 ++++--- usr/librarys/example.js | 81 +++++++++++++++++++----------------- 7 files changed, 67 insertions(+), 51 deletions(-) diff --git a/etc/init.shy b/etc/init.shy index cfebef44..eb43e308 100644 --- a/etc/init.shy +++ b/etc/init.shy @@ -1,11 +1,11 @@ ~cli - config load var/tmp/runtime.json runtime + config load tmp/runtime.json runtime ~aaa - config load var/tmp/short.json short - config load var/tmp/auth.json auth + config load tmp/short.json short + config load tmp/auth.json auth ~ssh - config load var/data/work.json work - config load var/data/flow.json flow + config load data/work.json work + config load data/flow.json flow ~nfs source etc/common.shy diff --git a/src/contexts/cli/cli.go b/src/contexts/cli/cli.go index 5c043f73..d24dbc04 100644 --- a/src/contexts/cli/cli.go +++ b/src/contexts/cli/cli.go @@ -356,6 +356,7 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心", // 工作目录 cmd.Dir = kit.Select(kit.Chains(conf, "dir"), m.Option("cmd_dir")) if cmd.Dir != "" { + os.MkdirAll(cmd.Dir, 0777) m.Log("info", "dir %v", cmd.Dir) } diff --git a/src/contexts/cli/version.go b/src/contexts/cli/version.go index 3b5235b8..5dea56b2 100644 --- a/src/contexts/cli/version.go +++ b/src/contexts/cli/version.go @@ -4,5 +4,5 @@ var version = struct { host string self int }{ - "2019-09-25 12:54:59", "centos", 603, + "2019-09-26 17:44:10", "centos", 606, } diff --git a/src/plugin/context/index.shy b/src/plugin/context/index.shy index e0a4c1f2..70ee679d 100644 --- a/src/plugin/context/index.shy +++ b/src/plugin/context/index.shy @@ -21,15 +21,17 @@ kit upgrade "升级" private "ssh._route" _ "cli.upgrade" \ text "" name see imports plugin_see \ button "升级" -kit missyou "任务" private \ +kit missyou "任务" private "ssh._route" _ "cli.missyou" \ + text "" name pod imports plugin_pod action auto \ text "" name see imports plugin_see \ text "" name you view long \ button "行动" action auto \ exports you "" you kit pod "设备" private "ssh._route" _ "ssh.remote" __ \ - text "" name pod \ + text "" name pod imports plugin_pod action auto \ button "查看" action auto \ + button "返回" cb Last \ exports pod pod pod kit ctx "模块" private "ssh._route" _ context _ list __ \ diff --git a/usr/librarys/chat.js b/usr/librarys/chat.js index 89044780..28c1be79 100644 --- a/usr/librarys/chat.js +++ b/usr/librarys/chat.js @@ -174,6 +174,7 @@ var page = Page({check: true, initRiver: function(page, field, option, output) { return { Show: function(which) {var pane = field.Pane + ctx.Event(event, {}, {name: "river.show"}) output.innerHTML = "", pane.Update([], "text", ["nick", "count"], "key", which||ctx.Search("river")||true) }, Action: { @@ -556,6 +557,7 @@ var page = Page({check: true, prev? prev.click(): output.lastChild.click() }, Show: function(which) {var pane = field.Pane + ctx.Event(event, {}, {name: "storm.show"}) pane.which.get("") == which && page.action.Pane.Show() output.innerHTML = "", pane.Update([river], "text", ["key", "count"], "key", which||ctx.Search("storm")||true) }, diff --git a/usr/librarys/context.js b/usr/librarys/context.js index a9db58e5..568d38da 100644 --- a/usr/librarys/context.js +++ b/usr/librarys/context.js @@ -1,6 +1,7 @@ ctx = context = {__proto__: kit, Event: function(event, msg, proto) { - if (event.msg) {return event.msg} + if (event.msg && !msg) {return event.msg} + event.msg = msg = msg || {}, proto = proto || {}, msg.__proto__ = proto, proto.__proto__ = { Push: function(key, value) { msg.append || (msg.append = []) @@ -26,7 +27,7 @@ ctx = context = {__proto__: kit, return msg }, Run: function(dataset, cmd, cb) { - var msg = ctx.Event(event) + var msg = ctx.Event(event||document.createEvent("Event"), null, {name: "ctx.run"}) var option = {"cmds": cmd} msg.option && msg.option.forEach(function(item) { @@ -265,7 +266,8 @@ ctx = context = {__proto__: kit, xhr.setRequestHeader("Accept", "application/json") xhr.send(args.join("&")) }, - WSS: function(cb, onerror, onclose) { + WSS: Wrap(function(cb, onerror, onclose) { + var meta = arguments.callee var s = new WebSocket(location.protocol.replace("http", "ws")+"//"+location.host+"/wss?wssid="+(page.wssid||"")) s.onopen = function(event) { kit.Log("wss", "open") @@ -278,8 +280,12 @@ ctx = context = {__proto__: kit, var msg = {"result": [event.data]} } + meta.order++ + // Event入口 -1.0 msg = ctx.Event(event, msg, { - reply: function(msg) { + name: meta.order, + Order: meta.order, + Reply: function(msg) { kit.Log(["wss", "result"].concat(msg.result)) delete(msg.event), s.send(JSON.stringify(msg)) }, @@ -302,5 +308,5 @@ ctx = context = {__proto__: kit, typeof onclose == "function" && onclose(event) } return s - }, + }, {order: 0}), } diff --git a/usr/librarys/example.js b/usr/librarys/example.js index 001f983c..70d7316e 100644 --- a/usr/librarys/example.js +++ b/usr/librarys/example.js @@ -130,6 +130,8 @@ function Page(page) { var script = {}, record = "" page = Meta(document.body, page, {__proto__: ctx, onload: function() { + // Event入口 0 + ctx.Event(event, {}, {name: document.title}) if (page.check && !ctx.Cookie("sessid")) { // 用户登录 document.querySelectorAll("body>fieldset.Login").forEach(function(field) { @@ -406,7 +408,7 @@ function Page(page) { WSS: function(cb, onerror, onclose) { return page.socket || (page.socket = ctx.WSS(cb || (function(m) { if (m.detail) { - page.action.Pane.Core(event, m, ["_cmd", m.detail], m.reply) + page.action.Pane.Core(event, m, ["_cmd", m.detail], m.Reply) } else { page.ontoast(m.result.join("")) } @@ -547,7 +549,6 @@ function Pane(page, field) { var index = list.length, ui = pane.View(output, type, line, key, function(event, cmds, cbs) { (type != "plugin" && type != "field") && pane.Select(index, line[which]) page.script("record", [name, line[key[0]]]) - ctx.Event(event, {}, {name: name}) typeof cb == "function" && cb(line, index, event, cmds, cbs) }) @@ -574,6 +575,8 @@ function Pane(page, field) { Select: function(index, key) { -1 < last && last < list.length && (kit.classList.del(list[last], "select")) last = index, list[index] && (kit.classList.add(list[index], "select")) + // Event入口 1.0 + ctx.Event(event, {}, {name: name+"."+key}) key && pane.which.set(key) }, clear: function() { @@ -686,14 +689,14 @@ function Pane(page, field) { page.Sync(k).change(pane.Listen[k]) } pane.Button && pane.Button.length > 0 && (kit.InsertChild(field, output, "div", pane.Button.map(function(value) { - return typeof value == "object"? {className: value[0], select: [value.slice(1), function(value, event) { - value = event.target.value + function call(value, event) { + // Event入口 1.1 + ctx.Event(event, {}, {name: name+"."+value}) page.script("record", [name, value]) typeof pane.Action == "function"? pane.Action(value, event): pane.Action[value](event, value) - }]}: value == ""? {view: ["space"]} :value == "br"? {type: "br"}: {button: [value, function(value, event) { - page.script("record", [name, value]) - typeof pane.Action == "function"? pane.Action(value, event): pane.Action[value](event, value) - }]} + } + return typeof value == "object"? {className: value[0], select: [value.slice(1), call]}: + value == ""? {view: ["space"]} :value == "br"? {type: "br"}: {button: [value, call]} })).className="action") option.onsubmit = function(event) { event.preventDefault() @@ -718,7 +721,8 @@ function Plugin(page, pane, field, runs) { var history = [] var run = function(event, cmds, cbs) { - event.Plugin = plugin, runs(event, cmds, cbs) + ctx.Event(event, null, {name: name, Plugin: plugin}) + runs(event, cmds, cbs) } var plugin = Meta(field, (field.Script && field.Script.init || function() { })(run, field, option, output)||{}, { @@ -781,13 +785,11 @@ function Plugin(page, pane, field, runs) { (typeof item.imports == "object"? item.imports: typeof item.imports == "string"? [item.imports]: []).forEach(function(imports) { page.Sync(imports).change(function(value) { history.push({target: action.target, value: action.target.value}); - (action.target.value = value) && item.action == "auto" && plugin.Runs(window.event) + (action.target.value = value) && item.action == "auto" && plugin.Runs(document.createEvent("Event")) }) }) - item.type == "button" && item.action == "auto" && plugin.Runs(window.event, function() { - var td = output.querySelector("td") - td && td.click() - }) + item.type == "button" && item.action == "auto" && action.target.click() + item.which = plugin.Sync(input.name) plugin.Inputs[input.name] = ui[input.name] return action.target @@ -979,9 +981,10 @@ function Plugin(page, pane, field, runs) { you: function(value, name, line) { var event = window.event event.Plugin = plugin + line.you && name == "status" && (line.status == "start"? function() { - 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+" stop...") && field.Run(event, [option.pod.value, line.you, "stop"]) + }(): field.Run(event, [option.pod.value, line.you], function(msg) { plugin.Delay(3000, event, line.you+" start...") })) return name == "status" || line.status == "stop" ? undefined: line.you @@ -999,8 +1002,8 @@ function Plugin(page, pane, field, runs) { }, onaction: { onfocus: function(event, action, type, name, item) { - page.input = event.target, plugin.Select(true) - plugin.which.set(name) + plugin.Select(true) + page.input = event.target, plugin.which.set(name) }, onblur: function(event, action, type, name, item) { item.which.set(action.target.value) @@ -1009,7 +1012,8 @@ function Plugin(page, pane, field, runs) { onclick: function(event, action, type, name, item) { switch (type) { case "button": - ctx.Event(event, {}, {name: meta.name+"."+name, pane: pane, plugin: plugin, input: item}) + // Event入口 2.0 + ctx.Event(event, {}, {name: meta.name+"."+name}) action[item.cb]? action[item.cb](event, item, option, field): plugin[item.cb]? plugin[item.cb](event, item, option, field): plugin.Check() @@ -1025,25 +1029,7 @@ function Plugin(page, pane, field, runs) { type == "text" && (action.target.value = kit.History.get("txt", -1).data.trim()) }, onchange: function(event, action, type, name, item) { - type == "select" && plugin.Check(item.action == "auto"? undefined: action) - }, - onkeyup: function(event, action, type, name, item) { - switch (event.key) { - case " ": - event.stopPropagation() - return true - } - - page.oninput(event, function(event) { - switch (event.key) { - case " ": - case "w": - break - default: - return false - } - return true - }) + type == "select" && ctx.Event(event, {}, {name: meta.name+"."+name}) && plugin.Check(item.action == "auto"? undefined: action) }, onkeydown: function(event, action, type, name, item) { switch (event.key) { @@ -1091,12 +1077,31 @@ function Plugin(page, pane, field, runs) { return true }) if (item.type != "textarea" && event.key == "Enter") { + // Event入口 2.1 ctx.Event(event, {}, {name: meta.name+"."+name, pane: pane, plugin: plugin, input: item}) item.which.set(action.target.value) history.push({target: action.target, value: action.target.value}); plugin.Check(action.target) } }, + onkeyup: function(event, action, type, name, item) { + switch (event.key) { + case " ": + event.stopPropagation() + return true + } + + page.oninput(event, function(event) { + switch (event.key) { + case " ": + case "w": + break + default: + return false + } + return true + }) + }, }, exports: JSON.parse(meta.exports||'["",""]'), })