1
0
forked from x/ContextOS
This commit is contained in:
shaoying 2019-03-11 09:21:14 +08:00
parent 52ae984215
commit a27fabeaad
5 changed files with 199 additions and 68 deletions

View File

@ -1,19 +1,41 @@
App({
log: function(type, args) {
console[type](args)
switch (type) {
case "info":
console.log(args)
break
default:
console.log(type, args)
}
},
toast: function(text) {
wx.showToast()
wx.showToast(text)
},
sheet: function(list, cb) {
wx.showActionSheet({itemList: list, success(res) {
typeof cb == "function" && cb(list[res.tapIndex])
}})
},
confirm: function(content, confirm, cancel) {
wx.showModal({
title: "context", content: content, success: function(res) {
res.confirm && typeof confirm == "function" && confirm()
res.cancel && typeof cancel == "function" && cancel()
}
})
},
place: function(cb) {
var app = this
wx.authorize({scope: "scope.userLocation"})
wx.chooseLocation({success: function(res) {
app.log("info", res)
app.log(res)
typeof cb == "function" && cb(res)
}})
},
stoprefresh: function(cb) {
wx.stopPullDownRefresh()
},
navigate: function(page, args) {
if (!page) {
wx.navigateBack()
@ -35,12 +57,12 @@ App({
var what = {method: "POST", url: "https://shylinux.com/chat/mp", data: data,
success: function(res) {
what.res = res
app.log("info", what)
app.log(what)
typeof done == "function" && done(res.data)
},
fail: function(res) {
what.res = res
app.log("info", what)
app.log(what)
typeof done == "function" && done(res.data)
},
}
@ -89,6 +111,19 @@ App({
})
})
},
table: function(res, cb) {
if (res.append) {
for (var i = 0; i < res[res.append[0]].length; i++) {
var obj = {}
var line = []
for (var j = 0; j < res.append.length; j++) {
line.push(res[res.append[j]][i])
obj[res.append[j]] = res[res.append[j]][i]
}
typeof cb == "function" && cb(i, obj, line)
}
}
},
model: {},
data: {model: {}, list: []},
@ -110,11 +145,17 @@ App({
var ncol = res.append.length
var nrow = res[res.append[0]].length
for (var i = 0; i < nrow; i++) {
app.data.model[res["name"][i]] = {
name: res["name"][i],
data: JSON.parse(res["data"][i] || "[]"),
view: JSON.parse(res["view"][i] || "{}"),
var view = JSON.parse(res["view"][i] || "{}")
var data = JSON.parse(res["data"][i] || "[]")
data.unshift({"name": "model", "type": "text", "value": res["name"][i]})
data.unshift({"name": "name", "type": "text"})
if (view.edit) {
for (var j = 0; j < data.length; j++) {
data[j].view = view.edit[data[j].name]
}
}
app.data.model[res["name"][i]] = {name: res["name"][i], data: data, view: view}
}
typeof cb == "function" && cb(app.data.model)
})
@ -125,37 +166,53 @@ App({
return
}
var cmd = {"cmd": ["note", "search", "username"]}
var cmd = {"cmd": ["note", "show", "username", "username", "full"]}
app.command(cmd, function(res) {
if (!res || !res.append) {
return
}
var list = []
var ncol = res.append.length
var nrow = res[res.append[0]].length
for (var i = 0; i < nrow; i++) {
var args = {}
var value = JSON.parse(res["value"][i] || "[]")
for (var j = 0; j < value.length; j++) {
args[value[j].name] = value[j].value
value.unshift({"type": "text", "name": "model", "value": res["model"][i]})
value.unshift({"type": "text", "name": "name", "value": res["name"][i]})
value.unshift({"type": "text", "name": "create_date", "value": res["create_time"][i].split(" ")[0].replace("-", "/").replace("-", "/")})
var line = {
create_time: res["create_time"][i],
model: res["model"][i], value: value,
view: JSON.parse(res["view"][i] || "{}"), data: {},
}
app.data.list.push({
create_date: res["create_time"][i].split(" ")[0].replace("-", "/").replace("-", "/"),
create_time: res["create_time"][i],
model: res["model"][i],
name: res["name"][i],
value: value,
args: args,
view: JSON.parse(res["view"][i] || "{}"),
})
for (var v in line.view) {
var view = line.view[v]
var data = []
for (var k in view) {
if (k in line) {
data.push({name: k, view: view[k], value: line[k]})
}
}
for (var j = 0; j < value.length; j++) {
var k = value[j]["name"]
if (((v == "edit") || (k in view)) && !(k in line)) {
data.push({name: k, view: view[k] || "", value: value[j]["value"]})
}
}
line.data[v] = data
}
list.push(line)
}
typeof cb == "function" && cb(app.data.list)
app.data.list = list
typeof cb == "function" && cb(list)
})
break
}
},
onLaunch: function () {},
})

View File

@ -11,7 +11,8 @@
"backgroundTextStyle": "light",
"navigationBarBackgroundColor": "#000",
"navigationBarTitleText": "context",
"navigationBarTextStyle": "white"
"navigationBarTextStyle": "white",
"enablePullDownRefresh": true
},
"permission": {
"scope.userLocation": {

View File

@ -2,53 +2,120 @@ const app = getApp()
Page({
data: {
focus: false,
cmd: "",
table: [],
append: [],
result: "",
nodes: [
["note", "shy"],
["ctx", "cmd"],
["note", "show"],
],
shows: [0, 0, 0],
ctx: "", cmd: "", focus: false,
append: [], table: [], result: "",
},
getPod(cb) {
var page = this
app.command({"cmd": ["context", "ssh", "remote"]}, function(res) {
var pod = [""]
app.table(res, function(i, obj, line) {
pod.push(obj.key)
})
page.data.nodes[0] = pod
page.data.shows[0] = 0
page.setData({nodes: page.data.nodes, shows: page.data.shows})
typeof cb == "function" && cb(pod)
})
},
getCtx(pod, cb) {
var page = this
app.command({"cmd": ["context", "ssh", "sh", pod, "context", "ctx", "context"]}, function(res) {
var ctx = []
app.table(res, function(i, obj, line) {
ctx.push(obj.names)
})
page.data.nodes[1] = ctx
page.data.shows[1] = 0
page.setData({nodes: page.data.nodes, shows: page.data.shows})
typeof cb == "function" && cb(ctx)
})
},
getCmd(pod, ctx, cb) {
var page = this
app.command({"cmd": ["context", "ssh", "sh", pod, "context", ctx, "command", "all"]}, function(res) {
var cmd = [""]
app.table(res, function(i, obj, line) {
cmd.push(obj.key)
})
page.data.nodes[2] = cmd
page.data.shows[2] = 0
page.setData({nodes: page.data.nodes, shows: page.data.shows})
page.setData({ctx: "context ssh sh node '"+pod+"' context "+ctx+" "+cmd[0]})
typeof cb == "function" && cb(cmd)
})
},
onChange: function(e) {
var column = e.detail.column
var value = e.detail.value
var page = this
page.data.shows[column] = value
var pod = page.data.nodes[0][page.data.shows[0]]
var ctx = page.data.nodes[1][page.data.shows[1]]
var cmd = page.data.nodes[2][page.data.shows[2]]
switch (column) {
case 0:
page.getCtx(pod, function(ctx) {
page.getCmd(pod, ctx[0], function(cmd) {
this.onCommand({detail:{value: ""}})
})
})
break
case 1:
page.getCmd(pod, ctx, function(cmd) {
this.onCommand({detail:{value: ""}})
})
break
case 2:
page.setData({ctx: "context ssh sh node '"+pod+"' context "+ctx+" "+cmd})
this.onCommand({detail:{value: ""}})
break
}
},
onCommand: function(e) {
var page = this
var cmd = e.detail.value
app.command({"cmd": ["source", cmd]}, function(res) {
if (res.append) {
var table = []
for (var i = 0; i < res[res.append[0]].length; i++) {
var line = []
for (var j = 0; j < res.append.length; j++) {
line.push(res[res.append[j]][i])
}
table.push(line)
}
page.setData({append: res.append, table: table})
} else {
page.setData({append: [], table: []})
}
page.setData({result: res.result? res.result.join("") :res})
var page = this
app.command({"cmd": ["source", page.data.ctx, cmd]}, function(res) {
var table = []
app.table(res, function(i, obj, line) {
table.push(line)
})
page.setData({append: res.append || [], table: table, result: res.result? res.result.join("") :res})
if (page.data.cmd) {
return
}
app.command({"cmd": ["note", cmd, "flow", cmd]}, function(res) {})
app.command({"cmd": ["note", cmd, "proxy", page.data.ctx, cmd]})
})
},
onLoad: function (options) {
app.log("info", {page: "pages/index/index", options: options})
app.log({page: this.route, options: options})
var data = app.data.list[options.index]
var cmd = ""
var page = this
app.load("model", function(model) {
app.log("info", app.data.list[options.index])
var cmd = app.data.list[options.index]? app.data.list[options.index].args["cmd"]: ""
page.setData({
model: model[options.model],
value: app.data.list[options.index],
view: model[options.model].view,
cmd: cmd,
focus: cmd? false: true,
page.getPod(function(pod) {
page.getCtx(pod[0], function(ctx) {
page.getCmd(pod[0], ctx[0], function(cmd) {
})
})
if (cmd) {
page.onCommand({detail:{value:cmd}})
}
})
if (data) {
for (var i = 0; i < data.value.length; i++) {
if (data.value[i].name == "cmd") {
cmd = data.value[i].value
this.onCommand({detail:{value: cmd}})
}
}
}
this.setData({cmd: cmd, focus: cmd? false: true})
},
})

View File

@ -1,10 +1,16 @@
<view class="container">
<view class="page">
<picker
mode="multiSelector" range="{{nodes}}" value="{{shows}}"
bindchange="onChange" bindcolumnchange="onChange">
<view class="input">{{ctx}}</view>
</picker>
<view class="input">
<input class="command" type="text" value="{{cmd}}" bindconfirm="onCommand" confirm-hold="true" focus="{{focus}}"/>
</view>
<scroll-view scroll-y class="table">
<view class="table-th" wx:for="{{append}}">{{item}}</view>
<view class="table-row" wx:for="{{table}}"><view class="table-td" wx:for="{{item}}">{{item}}</view></view>
<scroll-view class="table" scroll-y>
<view wx:for="{{append}}" class="table-th">{{item}}</view>
<view wx:for="{{table}}" class="table-row"><view wx:for="{{item}}" class="table-td">{{item}}</view></view>
</scroll-view>
<view class="result">{{result}}</view>
</view>

View File

@ -1,9 +1,10 @@
.container {
.page {
color:cyan;
font-size:16px;
font-family:Courier;
background-color:black;
min-height:800px;
overflow:auto;
}
.command {
border:solid 2px green;
@ -13,7 +14,6 @@
border:solid 1px green;
display:table;
overflow:auto;
max-width:100%;
}
.table-row {
display:table-row;