From 953aad1d537748d2684863cfc3b5741c38b495de Mon Sep 17 00:00:00 2001 From: shaoying Date: Wed, 25 Sep 2019 20:50:25 +0800 Subject: [PATCH] add ctx.Event --- src/contexts/cli/version.go | 2 +- src/contexts/web/web.go | 2 +- src/plugin/favor/index.js | 4 +- usr/librarys/chat.js | 56 ++++++++----------- usr/librarys/context.js | 108 +++++++++++++++++++++++------------- usr/librarys/example.js | 32 ++++++++--- usr/librarys/toolkit.js | 29 +++++++--- 7 files changed, 140 insertions(+), 93 deletions(-) diff --git a/src/contexts/cli/version.go b/src/contexts/cli/version.go index b7d141a4..3b5235b8 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-24 11:15:57", "centos", 602, + "2019-09-25 12:54:59", "centos", 603, } diff --git a/src/contexts/web/web.go b/src/contexts/web/web.go index 4c4449ed..cf38f438 100644 --- a/src/contexts/web/web.go +++ b/src/contexts/web/web.go @@ -1114,7 +1114,7 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心", } m.Conf("wss", []string{m.Option("username"), h}, meta) m.Conf("wss", h, meta) - p <- m.Spawn().Add("detail", "wss", h) + p <- m.Spawn().Add("detail", "wssid", h) what := m m.Log("wss", "conn %v %s", h, agent) diff --git a/src/plugin/favor/index.js b/src/plugin/favor/index.js index 32193f4a..e99bb6c1 100644 --- a/src/plugin/favor/index.js +++ b/src/plugin/favor/index.js @@ -1,8 +1,8 @@ {init: function(run, field, option, output) { return { Run: function(event, args, cb) { - run(event, ["share", args[0]], function(url) { - kit.AppendChilds(output, [{img: [url]}]) + run(event, ["share", args[0]], function(msg) { + kit.AppendChilds(output, [{img: [msg.result.join("")]}]) typeof cb == "function" && cb({}) }) }, diff --git a/usr/librarys/chat.js b/usr/librarys/chat.js index 952c7c74..89044780 100644 --- a/usr/librarys/chat.js +++ b/usr/librarys/chat.js @@ -288,20 +288,14 @@ var page = Page({check: true, ], 0) }, Core: function(event, line, args, cbs) { - var msg = event.msg || {}, res - function result(res) { - res != null && res != undefined && !msg.result && (msg.result = kit.Trans(res)) - return true - } - + var msg = ctx.Event(event) var plugin = event.Plugin || page.plugin && page.plugin.Plugin || {}, engine = { share: function(args) { - typeof cbs == "function" && cbs(ctx.Share({"group": option.dataset.group, "name": option.dataset.name, "cmds": [ + return ctx.Share({"group": option.dataset.group, "name": option.dataset.name, "cmds": [ river, line.storm, line.action, args[1]||"", - ]})) - return true + ]}) }, - wss: function(id) { + wssid: function(id) { return id && (page.wssid = id) }, pwd: function(name, value) { @@ -317,8 +311,8 @@ var page = Page({check: true, msg.append = ["name", "value"] msg.name = [], msg.value = [] return kit.Selector(page.plugin, ".args", function(item) { - msg.name.push(item.name) - msg.value.push(item.value) + msg.Push("name", item.name) + msg.Push("value", item.value) return item.name+":"+item.value }) @@ -347,17 +341,15 @@ var page = Page({check: true, } if (!pid) { return kit.Selector(page.action, "fieldset.item>legend", function(item) { - msg.push("name", item.parentNode.Meta.name) - msg.push("help", item.parentNode.Meta.help) + msg.Push("name", item.parentNode.Meta.name) + msg.Push("help", item.parentNode.Meta.help) return item.innerText }) } if (!uid) { - msg.append = ["name", "value"] - msg.name = [], msg.value = [] return kit.Selector(page.plugin, "input", function(item) { - msg.name.push(item.name) - msg.value.push(item.value) + msg.Push("name", item.name) + msg.Push("value", item.value) return item.name+":"+item.value }) } @@ -371,7 +363,6 @@ var page = Page({check: true, engine.help("action") engine.help("storm") }, - reload: function() {kit.reload()}, help: function() { var args = kit.List(arguments), cb, target if (args.length > 0 && page.pane && page.pane.Pane[args[0]] && page.pane.Pane[args[0]].Plugin) { @@ -400,22 +391,21 @@ var page = Page({check: true, kit.Log(["cmd"].concat(args)) if (typeof engine[args[0]] == "function") { - return result(kit._call(engine[args[0]], args.slice(1))) + return kit._call(engine[args[0]], args.slice(1)) } if (page.plugin && typeof page.plugin.Plugin[args[0]] == "function") { - return result(kit._call(page.plugin.Plugin[args[0]], args.slice(1))) + return kit._call(page.plugin.Plugin[args[0]], args.slice(1)) } - if (page.dialog && (res = page.dialog.Pane.Jshy(event, args))) {return result(res)} - if (page.pane && (res = page.pane.Pane.Jshy(event, args))) {return result(res)} - if (page.storm && (res = page.storm.Pane.Jshy(event, args))) {return result(res)} - if (page.river && (res = page.river.Pane.Jshy(event, args))) {return result(res)} + if (page.dialog && (res = page.dialog.Pane.Jshy(event, args))) {return res} + if (page.pane && (res = page.pane.Pane.Jshy(event, args))) {return res} + if (page.storm && (res = page.storm.Pane.Jshy(event, args))) {return res} + if (page.river && (res = page.river.Pane.Jshy(event, args))) {return res} - if (page && (res = page.Jshy(event, args))) {return result(res)} - if (page.plugin && (res = page.plugin.Plugin.Jshy(event, args))) {return result(res)} - kit.Log("not find", arg[1]) - return true + if (page && (res = page.Jshy(event, args))) {return res} + if (page.plugin && (res = page.plugin.Plugin.Jshy(event, args))) {return res} + return kit.Log(["warn", "not", "find"].concat(args)) }, _msg: function(msg) { if (msg) { @@ -432,7 +422,7 @@ var page = Page({check: true, }) }, } - if (args.length > 0 && engine[args[0]] && engine[args[0]](args)) {typeof cbs == "function" && cbs(); return} + if (args.length > 0 && engine[args[0]] && msg.Echo(engine[args[0]](args))) {typeof cbs == "function" && cbs(msg); return} event.shiftKey? engine._msg(): engine._run() }, Show: function() {var pane = field.Pane @@ -706,12 +696,12 @@ var page = Page({check: true, } }, init: function(page) { - page.footer.Pane.Order({"ncmd": "", "ntxt": ""}, ["ncmd", "ntxt"], function(event, item, value) {}) + page.action.Pane.Layout(ctx.Search("layout")? ctx.Search("layout"): kit.device.isMobile? page.conf.first: page.conf.mobile) + page.footer.Pane.Order({"ncmd": "0", "ntxt": "0"}, ["ncmd", "ntxt"], function(event, item, value) {}) page.header.Pane.Order({"logout": "logout", "user": ""}, ["logout", "user"], function(event, item, value) { page.onaction[item] && page.onaction[item](event, item, value, page) }) - page.river.Pane.Show(), page.pane = page.action, page.plugin = kit.Selector(page.action, "fieldset")[0] - page.action.Pane.Layout(ctx.Search("layout")? ctx.Search("layout"): kit.device.isMobile? page.conf.first: page.conf.mobile) + page.river.Pane.Show() page.WSS() }, }) diff --git a/usr/librarys/context.js b/usr/librarys/context.js index f0ef5be2..a9db58e5 100644 --- a/usr/librarys/context.js +++ b/usr/librarys/context.js @@ -1,26 +1,53 @@ ctx = context = {__proto__: kit, - Run: function(dataset, cmd, cb) { - var option = {"cmds": cmd} - for (var k in dataset) { - option[k] = dataset[k].split(",") - } - - kit.Log(["run"].concat(option.group).concat(option.name).concat(option.cmds)) - - var event = window.event - this.POST("", option, function(msg) { - msg[0] && (msg = msg[0]) - msg.Result = msg.result? msg.result.join(""): "" - msg.Results = function() { - var s = msg.Result + Event: function(event, msg, proto) { + if (event.msg) {return event.msg} + event.msg = msg = msg || {}, proto = proto || {}, msg.__proto__ = proto, proto.__proto__ = { + Push: function(key, value) { + msg.append || (msg.append = []) + msg[key]? msg[key].push(value): (msg[key] = [value], msg.append.push(key)) + return msg + }, + Echo: function(res) { + res != null && res != undefined && (msg.result = (msg.result || []).concat(kit.Trans(res))) + return msg + }, + Result: function() { + return msg.result? msg.result.join(""): "" + }, + Results: function() { + var s = msg.Result() s = s.replace(//g, ">") s = kit.Color(s) return s - } - msg.event = event - typeof cb == "function" && cb(msg || {}) + }, + }, msg.event = event + kit.Log("event", event.type, proto.name, msg) + return msg + }, + Run: function(dataset, cmd, cb) { + var msg = ctx.Event(event) + + var option = {"cmds": cmd} + msg.option && msg.option.forEach(function(item) { + msg.option[item] && (option[item] = msg.option[item]) }) + for (var k in dataset) { + option[k] = dataset[k].split(",") + } + + msg.option = [] + for (var k in option) { + msg.option.push(k) + msg[k] = option[k] + } + msg.detail = ["run"].concat(option.group).concat(option.name).concat(option.cmds) + + kit.Log(msg.detail.concat([msg])) + this.POST("", option, function(msg) { + kit.Log("run", "result", msg.result? msg.result[0]: "", msg) + typeof cb == "function" && cb(msg || {}) + }, msg) }, Runs: function(form, cb) { var data = {} @@ -190,7 +217,7 @@ ctx = context = {__proto__: kit, document.cookie = key+"="+value+";path=/" return arguments.callee(key) }, - POST: function(url, form, cb) { + POST: function(url, form, cb, msg) { var args = [] for (var k in form) { if (form[k] instanceof Array) { @@ -212,19 +239,25 @@ ctx = context = {__proto__: kit, } try { - var msg = JSON.parse(xhr.responseText||'{"result":[]}') + var res = JSON.parse(xhr.responseText||'[{"result":[]}]') + res = res[0] && res[0] } catch (e) { - var msg = {"result": [xhr.responseText]} + var res = {"result": [xhr.responseText]} } - if (msg.download_file) { - window.open(msg.download_file.join("")) - } else if (msg.page_redirect) { - location.href = msg.page_redirect.join("") - } else if (msg.page_refresh) { - location.reload() - } - typeof cb == "function" && cb(msg) + res.result && (msg.result = res.result) + res.append && (msg.append = res.append) && res.append.forEach(function(item) { + res[item] && (msg[item] = res[item]) + }) + + // if (msg.download_file) { + // window.open(msg.download_file.join("")) + // } else if (msg.page_redirect) { + // location.href = msg.page_redirect.join("") + // } else if (msg.page_refresh) { + // location.reload() + // } + typeof cb == "function" && cb(msg || {}) } xhr.open("POST", url) @@ -245,20 +278,15 @@ ctx = context = {__proto__: kit, var msg = {"result": [event.data]} } - event.msg = msg, msg.event = event, msg.reply = function(res) { - msg.result = (msg.result||[]).concat(kit.Trans(res)) - kit.Log(["wss", "detail"].concat(msg.detail)) - kit.Log(["wss", "result"].concat(msg.result)) - delete(msg.event), s.send(JSON.stringify(msg)) - } - msg.push = function(key, value) { - msg.append || (msg.append = []) - msg[key]? msg[key].push(value): (msg[key] = [value], msg.append.push(key)) - return msg - } + msg = ctx.Event(event, msg, { + reply: function(msg) { + kit.Log(["wss", "result"].concat(msg.result)) + delete(msg.event), s.send(JSON.stringify(msg)) + }, + }) try { - kit.Log("wss", "msg", msg) + kit.Log(["wss"].concat(msg.detail).concat([msg])) typeof cb == "function" && cb(msg) } catch (e) { msg.reply(kit.Log("err", e)) diff --git a/usr/librarys/example.js b/usr/librarys/example.js index c660420d..001f983c 100644 --- a/usr/librarys/example.js +++ b/usr/librarys/example.js @@ -139,10 +139,10 @@ function Page(page) { // 登录成功 document.querySelectorAll("body>fieldset").forEach(function(field) { page.Pane(page, field) - }), page.init(page), page.check && page.login.Pane.Run([], function(msg) { - msg.result && msg.result[0]? page.header.Pane.State("user", msg.nickname[0]) + }), page.check? page.login.Pane.Run([], function(msg) { + msg.result && msg.result[0]? (page.init(page), page.header.Pane.State("user", msg.nickname[0])) :page.login.Pane.Dialog(1, 1) - }) + }): page.init(page) } // 微信接口 @@ -529,6 +529,7 @@ function Page(page) { Pane: Pane, }) page.which = page.Sync("layout") + kit.Log("init", "page", page) return window.onload = page.onload, page } function Pane(page, field) { @@ -546,6 +547,7 @@ 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) }) @@ -696,6 +698,7 @@ function Pane(page, field) { option.onsubmit = function(event) { event.preventDefault() }; + kit.Log("init", "pane", name, pane) return page[name] = field, pane.Field = field, field.Pane = pane } function Plugin(page, pane, field, runs) { @@ -727,7 +730,12 @@ function Plugin(page, pane, field, runs) { Append: function(item, name, value) { kit.Item(plugin.onaction, function(k, cb) { item[k] == undefined && (item[k] = typeof cb == "function"? function(event) { - cb(event, action, item.type, name, item) + try { + cb(event, action, item.type, input.name, item) + } catch (e) { + console.trace() + kit.Log("err", e) + } }: cb) }); switch (item.value) { @@ -739,9 +747,11 @@ function Plugin(page, pane, field, runs) { !item.title && item.name && (item.title = item.name) !item.placeholder && item.title && (item.placeholder = item.title) - name = item.name || "input" - var input = {type: "input", name: name, data: item} + var input = {type: "input", name: name || item.name || "input", data: item} switch (item.type) { + case "button": + input.name = item.name || item.value || name + break case "upfile": item.type = "file" break @@ -766,7 +776,7 @@ function Plugin(page, pane, field, runs) { } var ui = kit.AppendChild(option, [{view: [item.view||""], list: [{type: "label", inner: item.label||""}, input]}]) - var action = Meta(ui[name] || {}, item, plugin.onaction, plugin); + var action = Meta(ui[input.name] || {}, item, plugin.onaction, plugin); (typeof item.imports == "object"? item.imports: typeof item.imports == "string"? [item.imports]: []).forEach(function(imports) { page.Sync(imports).change(function(value) { @@ -778,8 +788,8 @@ function Plugin(page, pane, field, runs) { var td = output.querySelector("td") td && td.click() }) - item.which = plugin.Sync(item.name) - plugin.Inputs[item.name] = ui[name] + item.which = plugin.Sync(input.name) + plugin.Inputs[input.name] = ui[input.name] return action.target }, Remove: function() { @@ -999,6 +1009,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}) + action[item.cb]? action[item.cb](event, item, option, field): plugin[item.cb]? plugin[item.cb](event, item, option, field): plugin.Check() break @@ -1079,6 +1091,7 @@ function Plugin(page, pane, field, runs) { return true }) if (item.type != "textarea" && event.key == "Enter") { + 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) @@ -1090,5 +1103,6 @@ function Plugin(page, pane, field, runs) { plugin.which = plugin.Sync("input") inputs.map(function(item) {plugin.Append(item)}) + kit.Log("init", "plugin", name, plugin) return page[field.id] = pane[field.id] = pane[name] = field, field.Plugin = plugin } diff --git a/usr/librarys/toolkit.js b/usr/librarys/toolkit.js index b289392a..a9b66181 100644 --- a/usr/librarys/toolkit.js +++ b/usr/librarys/toolkit.js @@ -1,4 +1,16 @@ +Wrap = function(cb, obj) { + for (var k in obj) { + cb[k] = obj[k] + } + return cb +} kit = toolkit = {__proto__: document, + meta: function(cb, obj) { + for (var k in obj) { + cb[k] = obj[k] + } + return cb + }, // 用户终端 device: { isWeiXin: navigator.userAgent.indexOf("MicroMessenger") > -1, @@ -33,11 +45,12 @@ kit = toolkit = {__proto__: document, return index == undefined? this[type]: this[type][(index+len)%len] }, }, - Debug: function(key) { - if (kit.debug[key]) {debugger} - }, debug: {"why": true}, - Log: function(type, arg) { - var args = [kit.time()] + Debug: Wrap(function(key) { + var list = arguments.callee.list + if (list[key]) {debugger} + }, {list: {why: true, msg: true}}), + Log: Wrap(function(type, arg) { + var args = [kit.time().split(" ")[1]] if (arg == undefined) { args = args.concat(kit.Trans(type)) } else { @@ -46,10 +59,12 @@ kit = toolkit = {__proto__: document, } } - !kit.hide[args[1]] && console.log(args) + var mine = arguments.callee + !mine.hide[args[1]] && console[mine.func[args[1]]||"log"](args) + kit.Debug(args[1]) return args.slice(1) - }, hide: {"wss": false}, + }, {hide: {"init": true, "wss": false}, func: {debug: "debug", info: "info", warn: "warn", err: "error"}}), Delay: function(time, cb) { return setTimeout(cb, time) },