From 4341dc49dd1f26af8747ba9eddb729e7f0947792 Mon Sep 17 00:00:00 2001 From: shaoying Date: Sat, 7 Dec 2019 20:22:40 +0800 Subject: [PATCH] add table.js --- chart.js | 21 ++++++ table.js | 64 ++++++++++++++++++ volcanos.js | 187 ++++++++++++++++++++++++++++++++++++++++++++-------- 3 files changed, 244 insertions(+), 28 deletions(-) create mode 100644 chart.js create mode 100644 table.js diff --git a/chart.js b/chart.js new file mode 100644 index 00000000..b7834147 --- /dev/null +++ b/chart.js @@ -0,0 +1,21 @@ +Volcanos("onimport", { + init: function(can, msg, cb, target, option) { + target.innerHTML = msg.result.join("") + }, +}) +Volcanos("onaction", { + copy: function(event, can, msg, cb, target, option) { + can.user.alert("hello world") + }, +}) +Volcanos("onchoice", { + list: ["copy", "复制", "下载"], + copy: function(event, can, msg, cb, target, option) { + can.user.alert("hello world") + }, +}) +Volcanos("ondetail", { + list: ["复制", "下载"], + copy: function(event, can, msg, cb, target, option) { + }, +}) diff --git a/table.js b/table.js new file mode 100644 index 00000000..ca91bdb3 --- /dev/null +++ b/table.js @@ -0,0 +1,64 @@ +Volcanos("onimport", { + init: function(can, msg, cb, target, option) {target.innerHTML = "" + var table = can.node.AppendTable(can, target, msg, msg.append) + table.onclick = function(event) { + switch (event.target.tagName) { + case "TD": + can.onimport.which(event, table, msg.append, function(index, key) { + can.onexport.init(event, can, msg, index, key) + }) + break + case "TH": + case "TR": + case "TABLE": + } + } + table.oncontextmenu = function(event) { + switch (event.target.tagName) { + case "TD": + can.onimport.which(event, table, msg.append, function(index, key) { + can.oncarte(event, shy("", can.ondetail, can.ondetail.list, function(event, value, meta) { + meta[value](event, can, msg, index, key, value) + })) + }) + case "TH": + case "TR": + case "TABLE": + can.oncarte(event, shy("", can.onchoice, can.onchoice.list, function(event, value, meta) { + meta[value](event, can, msg, value) + })) + } + event.stopPropagation() + event.preventDefault() + } + typeof cb == "function" && cb(msg); + }, + which: function(event, table, list, cb) {if (event.target == table) {return cb(-1, "")} + can.node.Select(table, "tr", can.core.List, function(tr, index) {if (event.target == tr) {return cb(index, "")} + can.node.Select(tr, "th,td", can.core.List, function(td, order) { + if (event.target == td) {return cb(index, list[order])} + }) + }) + }, +}) +Volcanos("onaction", {list: [], + onmouseover: function(event, can, msg, cb, target, option) { + msg.Log(event, can, msg, index, key) + }, +}) +Volcanos("onchoice", {list: ["copy", "复制", "下载"], + copy: function(event, can, msg, value) { + can.Log(event, can, msg, value) + }, +}) +Volcanos("ondetail", {list: ["copy", "复制", "下载"], + copy: function(event, can, msg, index, key, value) { + can.Log(event, can, msg, index, key, value) + }, +}) +Volcanos("onexport", {list: ["复制", "下载"], + init: function(event, can, msg, index, key) { + can.Log(event, can, msg, index, key) + }, +}) + diff --git a/volcanos.js b/volcanos.js index 52e65ee7..ec9aa794 100644 --- a/volcanos.js +++ b/volcanos.js @@ -1,16 +1,102 @@ +function shy(help, meta, list, cb) { + var index = -1, value = "", type = "string", args = arguments; + function next(check) { + if (++index >= args.length) {return false} + if (check && check != typeof args[index]) {index--; return false} + return value = args[index], type = typeof value, value; + } + + var cb = arguments[arguments.length-1] || function() {}; + cb.help = next("string") || "还没有写"; + cb.meta = next("object") || {}; + cb.list = next("object") || {}; + cb.runs = function() {}; + return cb; +} function Volcanos(name, can, libs, cb) {var list = arguments.callee.list || [], meta = arguments.callee.meta || { // 全局属性 path: "/static/volcanos/", index: 1, }; + + var id = 1, conf = {}, conf_cb = {}, sync = {}; can[name] || list.push({name: name, can: can, create_time: new Date()}) && (can.__proto__ = { // 通用属性 - create_time: new Date(), - name: name, help: "静态模块", load: function() { + create_time: new Date(), name: name, help: "静态模块", load: shy("加载器", function() { for (var i = meta.index; i < list.length; i++) {var item = list[i]; can[item.name] = item.can, item.can.name != can.name && (item.can.name = can.name + "." + item.name); } - meta.index = i - }, + meta.index = i; + return can + }), + ID: shy("生成器", function() {return id++}), + Log: shy("日志器", function() {console.log(arguments)}), + Conf: shy("配置器", function(key, value, cb) {if (key == undefined) {return conf} + if (typeof key == "object") {conf = key; return conf} + typeof cb == "function" && (conf_cb[key] = cb); + if (value != undefined) {var old = conf[key], res; can.Log("conf", key, old, value); + conf[key] = conf_cb[key] && (res = conf_cb[key](value, old, key)) != undefined && res || value + } + return conf[key] + }), + Sync: shy("同步器", function(name) {var data = "", list = []; name = name||"sync"+can.ID() + return sync[name] || (sync[name] = { + watch: shy("监听变量", function(cb) {typeof cb == "function" && list.push(cb); return list.length-1}), + get: shy("读取变量", function() {return data}), + set: shy("设置变量", function(value, force) {if (value == undefined) {return data} + if (value == data && !force) {return data} + can.Log("sync", name, data, value); + for (var i = 0; i < list; i++) {list[i](value, data, name)} + return data = value + }), + }) + }), + Timer: shy("定时器", function(interval, cb, cbs) {interval = typeof interval == "object"? interval || []: [interval]; + function loop(i) {if (i >= interval.length) {return typeof cbs == "function" && cbs(interval)} + return typeof cb == "function" && cb(interval[i], i, interval), setTimeout(function() {loop(i+1)}, interval[i]); + } + return loop(0) + }), + Event: shy("触发器", function(event, msg, proto) { + msg = event.msg = msg || event.msg || {}, msg.__proto__ = proto || msg.__proto__ || { + Log: shy("输出日志", function() {console.log(arguments)}), + Echo: shy("输出响应", function(res) {msg.result = msg.result || [] + for (var i = 0; i < arguments.length; i++) {msg.result.push(arguments[i])} + return msg; + }), + Table: shy("遍历数据", function(cb) {if (!msg.append || !msg.append.length || !msg[msg.append[0]]) {return} + var max = "", len = 0; + can.core.List(msg.append, function(key, index) { + if (msg[key].length > len) {max = key, len = msg[key].length} + }); + + return can.core.List(msg[max], function(value, index, array) {var one = {} + can.core.List(msg.append, function(key) {one[key] = msg[key][index]||""}) + return typeof cb == "function" && cb(one, index, array) + }) + }), + }; + msg.event = event + return msg + }), + Dream: shy("构造器", function(target, type, msg, list) { + switch (type) { + case "table": + var table = can.node.Append(can, target, type) + var tr = can.node.Append(can, table, "tr"); + can.core.List(list, function(key) {can.node.Append(can, tr, "th", key)}); + msg.Table(function(line) {var tr = can.node.Append(can, table, "tr"); + can.core.List(list, function(key) {can.node.Append(can, tr, "td", can.node.Display(line[key]))}); + }) + return table + } + }), + Cache: shy("缓存器", function(name, data) { + if (data == undefined) { + return + } + }), + Story: shy("存储器", function(type, meta, list) { + }), }), arguments.callee.meta = meta, arguments.callee.list = list; if (libs && libs.length > 0) { @@ -34,37 +120,82 @@ function Volcanos(name, can, libs, cb) {var list = arguments.callee.list || [], } Volcanos("type", {help: "类型模块", - Format: function(obj) {return JSON.stringify(obj)}, + isNone: function(c) {return c === undefined || c === null}, + isSpace: function(c) {return c == " " || c == "Enter"}, - List: function(obj, cb, interval, cbs) {obj = typeof obj == "string"? [obj]: (obj || []) - if (interval > 0) { - function loop(i) {if (i >= obj.length) {return kit._call(cbs)} - kit._call(cb, [obj[i], i, obj]), setTimeout(function() {loop(i+1)}, interval) - } - obj.length > 0 && setTimeout(function() {loop(0)}, interval/4) - return obj - } - - var list = [] - for (var i = 0; i < obj.length; i++) { - kit.Push(list, kit._call(cb, [obj[i], i, obj])) - } - return list - }, + Format: shy("数据格式化", function(obj) {return JSON.stringify(obj)}), + Number: shy("数字格式化", function(d, n) {var result = []; + while (d>0) {result.push(d % 10); d = parseInt(d / 10); n--} + while (n > 0) {result.push("0"); n--} + result.reverse(); + return result.join(""); + }), + Time: shy("时间格式化", function(t, fmt) {var now = t? new Date(t): new Date(); + fmt = fmt || "%y-%m-%d %H:%M:%S"; + fmt = fmt.replace("%y", now.getFullYear()) + fmt = fmt.replace("%m", kit.number(now.getMonth()+1, 2)) + fmt = fmt.replace("%d", kit.number(now.getDate(), 2)) + fmt = fmt.replace("%H", kit.number(now.getHours(), 2)) + fmt = fmt.replace("%M", kit.number(now.getMinutes(), 2)) + fmt = fmt.replace("%S", kit.number(now.getSeconds(), 2)) + return fmt + }), }) Volcanos("core", {help: "核心模块", - Log: function() { - }, -}) -Volcanos("node", {help: "节点模块", - Log: function() { - }, - Split: function() {}, + List: shy("迭代器", function(obj, cb, interval, cbs) {obj = typeof obj == "string"? [obj]: (obj || []) + if (interval > 0) { + function loop(i) {if (i >= obj.length) {return typeof cbs == "function" && cbs(obj)} + typeof cb == "function" && cb(obj[i], i, obj), setTimeout(function() {loop(i+1)}, interval); + } + obj.length > 0 && setTimeout(function() {loop(0)}, interval/4); + return obj; + } + + var list = [], res; + for (var i = 0; i < obj.length; i++) { + typeof cb == "function"? (res = cb(obj[i], i, obj)) != undefined && list.push(res): list.push(res); + } + return list; + }), }) Volcanos("misc", {help: "其它模块", }) -Volcanos("device", {help: "设备模块", +Volcanos("node", {help: "节点模块", + Display: function(text) { + if (text.startsWith("http")) {return ""+text+""} + return text + }, + Select: shy("选择器", function(obj, key, list, cb, interval, cbs) { + var item = obj && obj.querySelectorAll(key); + return list? list(item, cb, interval, cbs): item; + }), + Modify: shy("修改节点", function(can, target, value) { + if (typeof value == "string") {target.innerHTML = value} + return target + }), + Append: shy("添加节点", function(can, target, key, value) { + if (typeof key == "string") {var res = document.createElement(key); + return target.appendChild(res), can.node.Modify(can, res, value); + } + }), + AppendTable: shy("添加表格", function(can, target, msg, list) { + var table = can.node.Append(can, target, "table") + var tr = can.node.Append(can, table, "tr"); + can.core.List(list, function(key) {can.node.Append(can, tr, "th", key)}); + msg.Table(function(line) {var tr = can.node.Append(can, table, "tr"); + can.core.List(list, function(key) {can.node.Append(can, tr, "td", can.node.Display(line[key]))}); + }) + return table + }), +}) +Volcanos("user", {help: "用户模块", + toast: function(text) {}, + alert: function(text) {alert(JSON.stringify(text))}, + confirm: function(text) {return confirm(JSON.stringify(text))}, + prompt: function(text, cb) {(text = prompt(text)) != undefined && typeof cb == "function" && cb(text); return text}, + reload: function() {confirm("重新加载页面?") && location.reload()}, + isWeiXin: navigator.userAgent.indexOf("MicroMessenger") > -1, isMobile: navigator.userAgent.indexOf("Mobile") > -1, isIPhone: navigator.userAgent.indexOf("iPhone") > -1,