From 5e3dcff569a976a55443cf0ea5e87b5f19fa7bdd Mon Sep 17 00:00:00 2001 From: shaoying Date: Tue, 10 Sep 2019 21:07:59 +0800 Subject: [PATCH] add plugin mind --- src/contexts/cli/version.go | 2 +- src/contexts/ssh/ssh.go | 24 ++++++++--- src/plugin/mind/index.go | 85 +++++++++++++++++++++++++++++++++++++ src/plugin/mind/index.js | 75 ++++++++++++++++++++++++++++++++ src/plugin/mind/index.shy | 15 +++++++ src/plugin/mind/local.shy | 1 + usr/librarys/example.css | 4 ++ usr/librarys/example.js | 1 + usr/librarys/toolkit.js | 18 ++++---- 9 files changed, 209 insertions(+), 16 deletions(-) create mode 100644 src/plugin/mind/index.go create mode 100644 src/plugin/mind/index.js create mode 100644 src/plugin/mind/index.shy create mode 100644 src/plugin/mind/local.shy diff --git a/src/contexts/cli/version.go b/src/contexts/cli/version.go index 5bca2f7f..5e7d084c 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-10 15:30:05", "centos", 502, + "2019-09-10 18:45:48", "centos", 508, } diff --git a/src/contexts/ssh/ssh.go b/src/contexts/ssh/ssh.go index 11d58888..4ba83904 100644 --- a/src/contexts/ssh/ssh.go +++ b/src/contexts/ssh/ssh.go @@ -390,7 +390,7 @@ var Index = &ctx.Context{Name: "ssh", Help: "集群中心", } return }}, - "data": {Name: "data show|save|create|insert", Help: "数据", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) { + "data": {Name: "data show|save|create|insert", Help: "数据", Form: map[string]int{"format": 1}, Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) { if len(arg) == 0 { arg = append(arg, "show") } @@ -431,10 +431,18 @@ var Index = &ctx.Context{Name: "ssh", Help: "集群中心", }) default: // 记录值 - m.Confm("flow", []string{m.Option("river"), "data", arg[1], "list", arg[2]}, func(key string, value string) { - m.Push("key", key) - m.Push("value", value) - }) + index := kit.Int(arg[2]) - 1 + switch m.Option("format") { + case "object": + m.Confm("flow", []string{m.Option("river"), "data", arg[1], "list", kit.Format(index)}, func(key string, value string) { + m.Push(key, value) + }) + default: + m.Confm("flow", []string{m.Option("river"), "data", arg[1], "list", kit.Format(index)}, func(key string, value string) { + m.Push("key", key) + m.Push("value", value) + }) + } } m.Table() @@ -509,6 +517,12 @@ var Index = &ctx.Context{Name: "ssh", Help: "集群中心", m.Log("info", "insert %s:%s %s", m.Option("river"), arg[1], kit.Format(data)) m.Confv("flow", []string{m.Option("river"), "data", arg[1], "list", "-2"}, data) m.Cmdy("ssh.data", "save", arg[1]) + + case "update": + index := kit.Int(arg[2]) - 1 + for i := 3; i < len(arg) - 1; i += 2 { + m.Confv("flow", []string{m.Option("river"), "data", arg[1], "list", kit.Format(index), arg[i]}, arg[i+1]) + } } return }}, diff --git a/src/plugin/mind/index.go b/src/plugin/mind/index.go new file mode 100644 index 00000000..a36232a9 --- /dev/null +++ b/src/plugin/mind/index.go @@ -0,0 +1,85 @@ +package main + +import ( + "contexts/cli" + "contexts/ctx" + "toolkit" + + "encoding/json" + "fmt" + "os" +) + +var Index = &ctx.Context{Name: "mind", Help: "思维导图", + Caches: map[string]*ctx.Cache{}, + Configs: map[string]*ctx.Config{}, + Commands: map[string]*ctx.Command{ + "doc": {Name: "doc", Help: "文档", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) { + switch len(arg) { + case 0: + m.Cmdy("ssh.data", "show", "doc") + case 1: + m.Cmdy("ssh.data", "show", "doc", arg[0]) + case 2: + m.Cmdy("ssh.data", "insert", "doc", "title", arg[0], "content", arg[1]) + } + return + }}, + "xls": {Name: "xls", Help: "表格", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) { + switch len(arg) { + case 0: + m.Cmdy("ssh.data", "show", "xls") + m.Meta["append"] = []string{"id", "title"} + + case 1: + var data map[int]map[int]string + what := m.Cmd("ssh.data", "show", "xls", arg[0], "format", "object").Append("content") + json.Unmarshal([]byte(what), &data) + + max, n := 0, 0 + for i, v := range data { + if i > n { + n = i + } + for i := range v { + if i > max { + max = i + } + } + } + m.Log("info", "m: %d n: %d", m, n) + + for k := 0; k < n + 2; k++ { + for i := 0; i < max + 2; i++ { + m.Push(kit.Format(k), kit.Format(data[k][i])) + } + } + + case 2: + m.Cmdy("ssh.data", "insert", "xls", "title", arg[0], "content", arg[1]) + + default: + data := map[int]map[int]string{} + what := m.Cmd("ssh.data", "show", "xls", arg[0], "format", "object").Append("content") + json.Unmarshal([]byte(what), &data) + + for i := 1; i < len(arg) - 2; i += 3 { + if _, ok := data[kit.Int(arg[i])]; !ok { + data[kit.Int(arg[i])] = make(map[int]string) + } + data[kit.Int(arg[i])][kit.Int(arg[i+1])] = arg[i+2] + } + m.Cmdy("ssh.data", "update", "xls", arg[0], "content", kit.Format(data)) + } + return + }}, + "ppt": {Name: "ppt", Help: "文稿", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) { + m.Echo(kit.Select("hello world", arg, 0)) + return + }}, + }, +} + +func main() { + fmt.Print(cli.Index.Plugin(Index, os.Args[1:])) +} diff --git a/src/plugin/mind/index.js b/src/plugin/mind/index.js new file mode 100644 index 00000000..ca29ce8e --- /dev/null +++ b/src/plugin/mind/index.js @@ -0,0 +1,75 @@ + +{init: function(run, field, option, output) { + var stop = false + return { + ondaemon: { + table: function(msg, cb) { + if (stop) {return} + var plugin = field.Plugin + output.innerHTML = "", msg.append && kit.OrderTable(kit.AppendTable(kit.AppendChild(output, "table"), ctx.Table(msg), msg.append), "", function(event, value, name, line, index) { + if (name == "id") { + page.Sync("plugin_"+plugin.exports[0]).set(plugin.onexport[plugin.exports[2]||""](value, name, line)) + } else { + var td = event.target + function submit(event) { + td.innerText = event.target.value + if (event.target.value != value) { + stop = true + plugin.Run(event, [option.title.value, name, index-1, event.target.value], function() { + plugin.Check() + stop = false + }) + } + } + kit.AppendChilds(td, [{type: "input", value: value, data: {onblur: function(event) { + submit(event) + }, onkeyup: function(event) { + switch (event.key) { + case "Enter": + break + case "Tab": + break + default: + return + } + event.stopPropagation() + event.preventDefault() + }, onkeydown: function(event) { + console.log(event.key) + switch (event.key) { + case "Enter": + if (event.shiftKey) { + td.parentNode.previousSibling.querySelector("td").click() + } else { + td.parentNode.nextSibling.querySelector("td").click() + } + break + case "Tab": + if (event.shiftKey) { + if (td.previousSibling) { + td.previousSibling.click() + } else { + td.parentNode.previousSibling.querySelector("td").click() + } + } else { + if (td.nextSibling) { + td.nextSibling.click() + } else { + td.parentNode.nextSibling.querySelector("td").click() + } + } + break + default: + return + } + event.stopPropagation() + event.preventDefault() + }}}]).first.focus() + } + }) + typeof cb == "function" && cb(msg) + }, + }, + + } +}} diff --git a/src/plugin/mind/index.shy b/src/plugin/mind/index.shy new file mode 100644 index 00000000..fac1b6c2 --- /dev/null +++ b/src/plugin/mind/index.shy @@ -0,0 +1,15 @@ + +kit doc "文档" private \ + text "" name title imports plugin_doc_id action auto \ + text "" name content \ + button "执行" \ + button "返回" click Last \ + exports doc_id id + +kit xls "表格" "index.js" private \ + text "" name title imports plugin_xls_id action auto \ + text "" name content \ + button "执行" \ + button "返回" click Last \ + feature style "output full" \ + exports xls_id id diff --git a/src/plugin/mind/local.shy b/src/plugin/mind/local.shy new file mode 100644 index 00000000..0519ecba --- /dev/null +++ b/src/plugin/mind/local.shy @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/usr/librarys/example.css b/usr/librarys/example.css index dec49c2c..e6396b4d 100644 --- a/usr/librarys/example.css +++ b/usr/librarys/example.css @@ -253,6 +253,9 @@ fieldset.item>div.output>div.status>input.cmd { font-size:16px; width:250px; } +fieldset.item div.output.full table td { + height:16px; +} fieldset.item.select { background-color:gold; @@ -284,6 +287,7 @@ fieldset table th.order { cursor:pointer; } fieldset table td { + min-height:16px; max-width:1200px; font-family:monospace; padding-left: 10px; diff --git a/usr/librarys/example.js b/usr/librarys/example.js index da1c5286..6c0a06de 100644 --- a/usr/librarys/example.js +++ b/usr/librarys/example.js @@ -702,6 +702,7 @@ function Plugin(page, pane, field, runs) { var exports = JSON.parse(meta.exports||'["",""]') var deal = (feature && feature.display) || "table" var history = [] + output.className = feature.style || "item" var plugin = Meta(field, (field.Script && field.Script.init || function() { })(run, field, option, output)||{}, { diff --git a/usr/librarys/toolkit.js b/usr/librarys/toolkit.js index 98495e26..ee3e90b4 100644 --- a/usr/librarys/toolkit.js +++ b/usr/librarys/toolkit.js @@ -407,19 +407,17 @@ kit = toolkit = { OrderTable: function(table, field, cb) { if (!table) {return} table.onclick = function(event) { + var index = 0 var target = event.target var dataset = target.dataset var head = target.parentElement.parentElement.querySelector("tr") - kit.Selector(table, "tr.select", function(item) { - item.className = "" - }) - kit.Selector(table, "td.select", function(item) { - item.className = "" - }) + kit.Selector(table, "tr.select", function(item) {item.className = ""}) + kit.Selector(table, "td.select", function(item) {item.className = ""}) + kit.Selector(table, "tr", function(item, i) {item == target.parentElement && (index = i)}) + target.parentElement.childNodes.forEach(function(item, i) { - if (item != target) { - return - } + if (item != target) {return} + if (target.tagName == "TH") { dataset["sort_asc"] = (dataset["sort_asc"] == "1") ? 0: 1 kit.RangeTable(table, i, dataset["sort_asc"] == "1") @@ -429,7 +427,7 @@ kit = toolkit = { if (name.startsWith(field)) { item.className = "select" item.parentElement.className = "select" - typeof cb == "function" && cb(event, item.innerText, name,item.parentNode.Meta) + typeof cb == "function" && cb(event, item.innerText, name, item.parentNode.Meta, index) } kit.CopyText() })