diff --git a/frame.js b/frame.js index 1e74aa87..9e41696e 100644 --- a/frame.js +++ b/frame.js @@ -36,10 +36,14 @@ var can = Volcanos("chat", { Export: function(event, value, key) {can.Report(event, value, key)}, Import: function(event, value, key) {var cb = pane.onimport[key]; + // 导入数据 typeof cb == "function" && cb(event, pane, value, key, pane.output); + // 分发数据 can.core.List(pane._plugins, function(item) {item.Import(event, value, key)}) + // 导入数据 pane.page.Select(pane, pane.action, "input."+key, function(item) {item.value = value}) }, + Action: function(key, value) { return can.page.Select(can, pane.action, "input[name="+key+"],select."+key+",select[name="+key+"]", function(item) { value != undefined && (item.value = value), value = item.value @@ -120,9 +124,26 @@ var can = Volcanos("chat", { Inputs: can.Inputs, Output: can.Output, Import: function(event, value, key) {var cb = plugin.onimport[key]; + // 导入数据 typeof cb == "function" && cb(event, plugin, value, key, plugin.output); + // 下发数据 key && plugin[key] && plugin[key].target && plugin[key].Import(event, value, key) }, + Report: function(event, value, key, index) { + // 导入数据 + plugin.Import(event, value, key) + + for (var i = 0; i < exports.length; i += 3) { + if (exports[i+1] == key) {key = exports[i] + if (exports[i+2]) {var cb = plugin.onexport[exports[i+2]], res; + // 数据转换 + value = typeof cb == "function" && ((res = cb(event, plugin, plugin.msg, value, key, index)) != undefined) && res || value; + } + // 上报数据 + key && can.Import(event, value, key) + } + } + }, Share: function(event) { can.user.input(event, can, ["name", "text"], function(event, cmd, meta, list) { @@ -161,17 +182,6 @@ var can = Volcanos("chat", { return key != undefined? option[key] && option[key].value || "": plugin.page.Select(can, option, ".args", function(item) {return item.value}) }, - Report: function(event, value, key, index) { - key && plugin[key] && plugin[key].target && plugin[key].Import(event, value, key, index) - for (var i = 0; i < exports.length; i += 3) { - if (exports[i+1] == key) {key = exports[i] - if (exports[i+2]) {var cb = plugin.onexport[exports[i+2]], res; - value = typeof cb == "function" && ((res = cb(event, plugin, plugin.msg, value, key, index)) != undefined) && res || value; - } - key && can.Import(event, value, key) - } - } - }, Check: function(event, target, cb) { plugin.page.Select(can, option, ".args", function(item, index, list) { if (item == target && index < list.length-1) {can._plugin && can._plugin.target == field && list[index+1].focus(); return item} diff --git a/lib/misc.js b/lib/misc.js index e724eb29..df38286e 100644 --- a/lib/misc.js +++ b/lib/misc.js @@ -1,101 +1,76 @@ Volcanos("misc", {help: "工具模块", POST: shy("请求后端", {order: 0}, function(can, msg, url, form, cb) { - var xhr = new XMLHttpRequest() - xhr.onreadystatechange = function() { + var xhr = new XMLHttpRequest(); + xhr.open("POST", url), xhr.onreadystatechange = function() { if (xhr.readyState != 4) {return} - if (xhr.status != 200) {return} - - try { - var res = JSON.parse(xhr.responseText||'[{"result":[]}]') - res.length > 0 && res[0] && (res = res[0]) - - if (res.download_file) { - window.open(res.download_file.join("")) - } else if (res.page_redirect) { - location.href = res.page_redirect.join("") - } else if (res.page_refresh) { - location.reload() - } + try { // 解析响应 + var res = JSON.parse(xhr.responseText); } catch (e) { var res = {"result": [xhr.responseText]} } - - typeof cb == "function" && cb(msg.Copy(res)) + xhr.status == 200 && typeof cb == "function" && cb(msg.Copy(res)); } - xhr.open("POST", url) if (msg.upload) { - var data = new FormData() + // 文件参数 + var data = new FormData(); can.core.Item(form, function(key, value) { - can.core.List(value, function(item) { - data.append(key, item) - }) + can.core.List(value, function(item) {data.append(key, item)}); }) - data.append("upload", msg.upload) + data.append("upload", msg.upload); } else { - var args = can.core.Items(form, function(value, index, key) { + // 表单参数 + var data = can.core.Items(form, function(value, index, key) { return key+"="+encodeURIComponent(value) - }) - var data = args.join("&") - xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded") + }).join("&"); + xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); } + // 发送请求 + ++arguments.callee.meta.order xhr.setRequestHeader("Accept", "application/json") xhr.send(data) - ++arguments.callee.meta.order }), Run: shy("请求后端", {order: 0}, function(event, can, dataset, cmd, cb) { - var msg = can.Event(event) + var msg = can.Event(event); + // 解析参数 var option = {"cmds": cmd||msg.cmd} msg.option && msg.option.forEach(function(item) { msg[item] && (option[item] = msg[item]) }) - // for (var k in dataset) { - // option[k] = dataset[k].toLowerCase().split(",") - // } + msg.option = can.core.Item(option, function(key, value) { + return msg[key] = value, key + }) - var what = ++arguments.callee.meta.order - - msg._hand = true - msg.option = [] - for (var k in option) { - msg.option.push(k) - msg[k] = option[k] - } - // msg.detail = ["run", what].concat(option.group).concat(option.names).concat(option.cmds) - // kit.Log(msg.detail.concat([msg])) - // - // - - // kit.History("run", -1, option) - this.POST(can, msg, can.Conf("iceberg")+(dataset.names||msg.names).toLowerCase(), option, function(msg) { - // kit.Log("run", what, "result", msg.result? msg.result[0]: "", msg) + msg._hand = true, can.misc.POST(can, msg, can.Conf("iceberg")+(dataset.names||msg.names).toLowerCase(), option, function(msg) { typeof cb == "function" && cb(msg) }), delete(event.msg) }), WSS: shy("请求后端", {order: 0}, function(can, url, args, cb, onopen, onerror, onclose) {var meta = arguments.callee.meta can._socket = new WebSocket(url+"?"+can.base.Args(args)) - var timer = can.Timer(30000, function() { - can._socket.send("{}") - }) - can._socket.onerror = onerror || function() { - can._socket.close() - }, can._socket.onclose = onclose || function() { + var timer = can.Timer(30000, function() {can._socket.send("{}")}) + can._socket.onclose = onclose || function() { timer.stop = true, can.user.toast("wss redial") can.Log("wss", "close"), delete(can._socket), setTimeout(function() { + // 断线重连 can.misc.WSS(can, url, args, cb, onerror, onclose, onopen) }, 1000) + }, can._socket.onerror = onerror || function() { + can._socket.close() }, can._socket.onopen = onopen + can._socket.onmessage = function(event) {var order = ++meta.order try { - var msg = JSON.parse(event.data||'{}') + // 解析命令 + var msg = JSON.parse(event.data) } catch (e) { var msg = {"result": [event.data]} } msg = can.Event(event, msg), msg.Reply = function() { + // 回复命令 msg.Option("_handle", true) msg.Option("_target", msg.Option("_source")) can.Log(["wss", order, "result"].concat(msg.result).concat([msg])) @@ -103,10 +78,11 @@ Volcanos("misc", {help: "工具模块", } try { + // 执行命令 can.Log(["wss", order].concat(msg.detail).concat([msg])) typeof cb == "function" && cb(event, msg) } catch (e) { - msg.Option("_handle", true) + // 执行失败 msg.Reply(can.Log("err", e)) } } diff --git a/lib/page.js b/lib/page.js index 9774383e..82aebed0 100644 --- a/lib/page.js +++ b/lib/page.js @@ -199,6 +199,8 @@ Volcanos("page", {help: "网页模块", return field.Meta = item, field; }), AppendTable: shy("添加表格", function(can, target, msg, list, cb, cbs) { + if (!msg.append || msg.append.length == 0) {return {}} + var table = can.page.Append(can, target, "table"); var tr = can.page.Append(can, table, "tr"); can.core.List(list, function(key, index) {can.page.Append(can, tr, "th", key).onclick = function(event) { diff --git a/plugin/table.js b/plugin/table.js index 1d18adaa..b4c7f13c 100644 --- a/plugin/table.js +++ b/plugin/table.js @@ -37,8 +37,8 @@ Volcanos("onimport", {help: "导入数据", list: [], } } - var code = can.page.Append(can, output, [{view: ["code", "div", can.page.Display(msg.Result())]}]).code; - return typeof cb == "function" && cb(msg), code; + msg.result && can.page.Append(can, output, [{view: ["code", "div", can.page.Display(msg.Result())]}]).code; + return typeof cb == "function" && cb(msg); }, which: function(event, table, list, cb) {if (event.target == table) {return cb(-1, "")} can.page.Select(can, table, "tr", function(tr, index) {if (event.target == tr) {return cb(tr.dataset.index, "")}