1
0
mirror of https://shylinux.com/x/ContextOS synced 2025-04-25 16:58:06 +08:00

add ctx.Event

This commit is contained in:
shaoying 2019-09-25 20:50:25 +08:00
parent 53afdd1f28
commit 953aad1d53
7 changed files with 140 additions and 93 deletions

View File

@ -4,5 +4,5 @@ var version = struct {
host string host string
self int self int
}{ }{
"2019-09-24 11:15:57", "centos", 602, "2019-09-25 12:54:59", "centos", 603,
} }

View File

@ -1114,7 +1114,7 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心",
} }
m.Conf("wss", []string{m.Option("username"), h}, meta) m.Conf("wss", []string{m.Option("username"), h}, meta)
m.Conf("wss", h, meta) m.Conf("wss", h, meta)
p <- m.Spawn().Add("detail", "wss", h) p <- m.Spawn().Add("detail", "wssid", h)
what := m what := m
m.Log("wss", "conn %v %s", h, agent) m.Log("wss", "conn %v %s", h, agent)

View File

@ -1,8 +1,8 @@
{init: function(run, field, option, output) { {init: function(run, field, option, output) {
return { return {
Run: function(event, args, cb) { Run: function(event, args, cb) {
run(event, ["share", args[0]], function(url) { run(event, ["share", args[0]], function(msg) {
kit.AppendChilds(output, [{img: [url]}]) kit.AppendChilds(output, [{img: [msg.result.join("")]}])
typeof cb == "function" && cb({}) typeof cb == "function" && cb({})
}) })
}, },

View File

@ -288,20 +288,14 @@ var page = Page({check: true,
], 0) ], 0)
}, },
Core: function(event, line, args, cbs) { Core: function(event, line, args, cbs) {
var msg = event.msg || {}, res var msg = ctx.Event(event)
function result(res) {
res != null && res != undefined && !msg.result && (msg.result = kit.Trans(res))
return true
}
var plugin = event.Plugin || page.plugin && page.plugin.Plugin || {}, engine = { var plugin = event.Plugin || page.plugin && page.plugin.Plugin || {}, engine = {
share: function(args) { 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]||"", river, line.storm, line.action, args[1]||"",
]})) ]})
return true
}, },
wss: function(id) { wssid: function(id) {
return id && (page.wssid = id) return id && (page.wssid = id)
}, },
pwd: function(name, value) { pwd: function(name, value) {
@ -317,8 +311,8 @@ var page = Page({check: true,
msg.append = ["name", "value"] msg.append = ["name", "value"]
msg.name = [], msg.value = [] msg.name = [], msg.value = []
return kit.Selector(page.plugin, ".args", function(item) { return kit.Selector(page.plugin, ".args", function(item) {
msg.name.push(item.name) msg.Push("name", item.name)
msg.value.push(item.value) msg.Push("value", item.value)
return item.name+":"+item.value return item.name+":"+item.value
}) })
@ -347,17 +341,15 @@ var page = Page({check: true,
} }
if (!pid) { if (!pid) {
return kit.Selector(page.action, "fieldset.item>legend", function(item) { return kit.Selector(page.action, "fieldset.item>legend", function(item) {
msg.push("name", item.parentNode.Meta.name) msg.Push("name", item.parentNode.Meta.name)
msg.push("help", item.parentNode.Meta.help) msg.Push("help", item.parentNode.Meta.help)
return item.innerText return item.innerText
}) })
} }
if (!uid) { if (!uid) {
msg.append = ["name", "value"]
msg.name = [], msg.value = []
return kit.Selector(page.plugin, "input", function(item) { return kit.Selector(page.plugin, "input", function(item) {
msg.name.push(item.name) msg.Push("name", item.name)
msg.value.push(item.value) msg.Push("value", item.value)
return item.name+":"+item.value return item.name+":"+item.value
}) })
} }
@ -371,7 +363,6 @@ var page = Page({check: true,
engine.help("action") engine.help("action")
engine.help("storm") engine.help("storm")
}, },
reload: function() {kit.reload()},
help: function() { help: function() {
var args = kit.List(arguments), cb, target var args = kit.List(arguments), cb, target
if (args.length > 0 && page.pane && page.pane.Pane[args[0]] && page.pane.Pane[args[0]].Plugin) { 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)) kit.Log(["cmd"].concat(args))
if (typeof engine[args[0]] == "function") { 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") { 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.dialog && (res = page.dialog.Pane.Jshy(event, args))) {return res}
if (page.pane && (res = page.pane.Pane.Jshy(event, args))) {return result(res)} if (page.pane && (res = page.pane.Pane.Jshy(event, args))) {return res}
if (page.storm && (res = page.storm.Pane.Jshy(event, args))) {return result(res)} if (page.storm && (res = page.storm.Pane.Jshy(event, args))) {return res}
if (page.river && (res = page.river.Pane.Jshy(event, args))) {return result(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 && (res = page.Jshy(event, args))) {return res}
if (page.plugin && (res = page.plugin.Plugin.Jshy(event, args))) {return result(res)} if (page.plugin && (res = page.plugin.Plugin.Jshy(event, args))) {return res}
kit.Log("not find", arg[1]) return kit.Log(["warn", "not", "find"].concat(args))
return true
}, },
_msg: function(msg) { _msg: function(msg) {
if (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() event.shiftKey? engine._msg(): engine._run()
}, },
Show: function() {var pane = field.Pane Show: function() {var pane = field.Pane
@ -706,12 +696,12 @@ var page = Page({check: true,
} }
}, },
init: function(page) { 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.header.Pane.Order({"logout": "logout", "user": ""}, ["logout", "user"], function(event, item, value) {
page.onaction[item] && page.onaction[item](event, item, value, page) 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.river.Pane.Show()
page.action.Pane.Layout(ctx.Search("layout")? ctx.Search("layout"): kit.device.isMobile? page.conf.first: page.conf.mobile)
page.WSS() page.WSS()
}, },
}) })

View File

@ -1,26 +1,53 @@
ctx = context = {__proto__: kit, ctx = context = {__proto__: kit,
Run: function(dataset, cmd, cb) { Event: function(event, msg, proto) {
var option = {"cmds": cmd} if (event.msg) {return event.msg}
for (var k in dataset) { event.msg = msg = msg || {}, proto = proto || {}, msg.__proto__ = proto, proto.__proto__ = {
option[k] = dataset[k].split(",") Push: function(key, value) {
} msg.append || (msg.append = [])
msg[key]? msg[key].push(value): (msg[key] = [value], msg.append.push(key))
kit.Log(["run"].concat(option.group).concat(option.name).concat(option.cmds)) return msg
},
var event = window.event Echo: function(res) {
this.POST("", option, function(msg) { res != null && res != undefined && (msg.result = (msg.result || []).concat(kit.Trans(res)))
msg[0] && (msg = msg[0]) return msg
msg.Result = msg.result? msg.result.join(""): "" },
msg.Results = function() { Result: function() {
var s = msg.Result return msg.result? msg.result.join(""): ""
},
Results: function() {
var s = msg.Result()
s = s.replace(/</g, "&lt;") s = s.replace(/</g, "&lt;")
s = s.replace(/>/g, "&gt;") s = s.replace(/>/g, "&gt;")
s = kit.Color(s) s = kit.Color(s)
return s return s
} },
msg.event = event }, msg.event = event
typeof cb == "function" && cb(msg || {}) 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) { Runs: function(form, cb) {
var data = {} var data = {}
@ -190,7 +217,7 @@ ctx = context = {__proto__: kit,
document.cookie = key+"="+value+";path=/" document.cookie = key+"="+value+";path=/"
return arguments.callee(key) return arguments.callee(key)
}, },
POST: function(url, form, cb) { POST: function(url, form, cb, msg) {
var args = [] var args = []
for (var k in form) { for (var k in form) {
if (form[k] instanceof Array) { if (form[k] instanceof Array) {
@ -212,19 +239,25 @@ ctx = context = {__proto__: kit,
} }
try { try {
var msg = JSON.parse(xhr.responseText||'{"result":[]}') var res = JSON.parse(xhr.responseText||'[{"result":[]}]')
res = res[0] && res[0]
} catch (e) { } catch (e) {
var msg = {"result": [xhr.responseText]} var res = {"result": [xhr.responseText]}
} }
if (msg.download_file) { res.result && (msg.result = res.result)
window.open(msg.download_file.join("")) res.append && (msg.append = res.append) && res.append.forEach(function(item) {
} else if (msg.page_redirect) { res[item] && (msg[item] = res[item])
location.href = msg.page_redirect.join("") })
} else if (msg.page_refresh) {
location.reload() // if (msg.download_file) {
} // window.open(msg.download_file.join(""))
typeof cb == "function" && cb(msg) // } 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) xhr.open("POST", url)
@ -245,20 +278,15 @@ ctx = context = {__proto__: kit,
var msg = {"result": [event.data]} var msg = {"result": [event.data]}
} }
event.msg = msg, msg.event = event, msg.reply = function(res) { msg = ctx.Event(event, msg, {
msg.result = (msg.result||[]).concat(kit.Trans(res)) reply: function(msg) {
kit.Log(["wss", "detail"].concat(msg.detail)) kit.Log(["wss", "result"].concat(msg.result))
kit.Log(["wss", "result"].concat(msg.result)) delete(msg.event), s.send(JSON.stringify(msg))
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
}
try { try {
kit.Log("wss", "msg", msg) kit.Log(["wss"].concat(msg.detail).concat([msg]))
typeof cb == "function" && cb(msg) typeof cb == "function" && cb(msg)
} catch (e) { } catch (e) {
msg.reply(kit.Log("err", e)) msg.reply(kit.Log("err", e))

View File

@ -139,10 +139,10 @@ function Page(page) {
// 登录成功 // 登录成功
document.querySelectorAll("body>fieldset").forEach(function(field) { document.querySelectorAll("body>fieldset").forEach(function(field) {
page.Pane(page, field) page.Pane(page, field)
}), page.init(page), page.check && page.login.Pane.Run([], function(msg) { }), page.check? page.login.Pane.Run([], function(msg) {
msg.result && msg.result[0]? page.header.Pane.State("user", msg.nickname[0]) msg.result && msg.result[0]? (page.init(page), page.header.Pane.State("user", msg.nickname[0]))
:page.login.Pane.Dialog(1, 1) :page.login.Pane.Dialog(1, 1)
}) }): page.init(page)
} }
// 微信接口 // 微信接口
@ -529,6 +529,7 @@ function Page(page) {
Pane: Pane, Pane: Pane,
}) })
page.which = page.Sync("layout") page.which = page.Sync("layout")
kit.Log("init", "page", page)
return window.onload = page.onload, page return window.onload = page.onload, page
} }
function Pane(page, field) { 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) { var index = list.length, ui = pane.View(output, type, line, key, function(event, cmds, cbs) {
(type != "plugin" && type != "field") && pane.Select(index, line[which]) (type != "plugin" && type != "field") && pane.Select(index, line[which])
page.script("record", [name, line[key[0]]]) page.script("record", [name, line[key[0]]])
ctx.Event(event, {}, {name: name})
typeof cb == "function" && cb(line, index, event, cmds, cbs) typeof cb == "function" && cb(line, index, event, cmds, cbs)
}) })
@ -696,6 +698,7 @@ function Pane(page, field) {
option.onsubmit = function(event) { option.onsubmit = function(event) {
event.preventDefault() event.preventDefault()
}; };
kit.Log("init", "pane", name, pane)
return page[name] = field, pane.Field = field, field.Pane = pane return page[name] = field, pane.Field = field, field.Pane = pane
} }
function Plugin(page, pane, field, runs) { function Plugin(page, pane, field, runs) {
@ -727,7 +730,12 @@ function Plugin(page, pane, field, runs) {
Append: function(item, name, value) { Append: function(item, name, value) {
kit.Item(plugin.onaction, function(k, cb) { kit.Item(plugin.onaction, function(k, cb) {
item[k] == undefined && (item[k] = typeof cb == "function"? function(event) { 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) }: cb)
}); });
switch (item.value) { switch (item.value) {
@ -739,9 +747,11 @@ function Plugin(page, pane, field, runs) {
!item.title && item.name && (item.title = item.name) !item.title && item.name && (item.title = item.name)
!item.placeholder && item.title && (item.placeholder = item.title) !item.placeholder && item.title && (item.placeholder = item.title)
name = item.name || "input" var input = {type: "input", name: name || item.name || "input", data: item}
var input = {type: "input", name: name, data: item}
switch (item.type) { switch (item.type) {
case "button":
input.name = item.name || item.value || name
break
case "upfile": case "upfile":
item.type = "file" item.type = "file"
break 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 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) { (typeof item.imports == "object"? item.imports: typeof item.imports == "string"? [item.imports]: []).forEach(function(imports) {
page.Sync(imports).change(function(value) { page.Sync(imports).change(function(value) {
@ -778,8 +788,8 @@ function Plugin(page, pane, field, runs) {
var td = output.querySelector("td") var td = output.querySelector("td")
td && td.click() td && td.click()
}) })
item.which = plugin.Sync(item.name) item.which = plugin.Sync(input.name)
plugin.Inputs[item.name] = ui[name] plugin.Inputs[input.name] = ui[input.name]
return action.target return action.target
}, },
Remove: function() { Remove: function() {
@ -999,6 +1009,8 @@ function Plugin(page, pane, field, runs) {
onclick: function(event, action, type, name, item) { onclick: function(event, action, type, name, item) {
switch (type) { switch (type) {
case "button": 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): action[item.cb]? action[item.cb](event, item, option, field):
plugin[item.cb]? plugin[item.cb](event, item, option, field): plugin.Check() plugin[item.cb]? plugin[item.cb](event, item, option, field): plugin.Check()
break break
@ -1079,6 +1091,7 @@ function Plugin(page, pane, field, runs) {
return true return true
}) })
if (item.type != "textarea" && event.key == "Enter") { 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) item.which.set(action.target.value)
history.push({target: action.target, value: action.target.value}); history.push({target: action.target, value: action.target.value});
plugin.Check(action.target) plugin.Check(action.target)
@ -1090,5 +1103,6 @@ function Plugin(page, pane, field, runs) {
plugin.which = plugin.Sync("input") plugin.which = plugin.Sync("input")
inputs.map(function(item) {plugin.Append(item)}) 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 return page[field.id] = pane[field.id] = pane[name] = field, field.Plugin = plugin
} }

View File

@ -1,4 +1,16 @@
Wrap = function(cb, obj) {
for (var k in obj) {
cb[k] = obj[k]
}
return cb
}
kit = toolkit = {__proto__: document, kit = toolkit = {__proto__: document,
meta: function(cb, obj) {
for (var k in obj) {
cb[k] = obj[k]
}
return cb
},
// 用户终端 // 用户终端
device: { device: {
isWeiXin: navigator.userAgent.indexOf("MicroMessenger") > -1, isWeiXin: navigator.userAgent.indexOf("MicroMessenger") > -1,
@ -33,11 +45,12 @@ kit = toolkit = {__proto__: document,
return index == undefined? this[type]: this[type][(index+len)%len] return index == undefined? this[type]: this[type][(index+len)%len]
}, },
}, },
Debug: function(key) { Debug: Wrap(function(key) {
if (kit.debug[key]) {debugger} var list = arguments.callee.list
}, debug: {"why": true}, if (list[key]) {debugger}
Log: function(type, arg) { }, {list: {why: true, msg: true}}),
var args = [kit.time()] Log: Wrap(function(type, arg) {
var args = [kit.time().split(" ")[1]]
if (arg == undefined) { if (arg == undefined) {
args = args.concat(kit.Trans(type)) args = args.concat(kit.Trans(type))
} else { } 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]) kit.Debug(args[1])
return args.slice(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) { Delay: function(time, cb) {
return setTimeout(cb, time) return setTimeout(cb, time)
}, },