From 6f17d35c7c05302c091adbcddd2ca19fba2ca146 Mon Sep 17 00:00:00 2001 From: shaoying Date: Wed, 16 Dec 2020 10:18:13 +0800 Subject: [PATCH] opt base.js --- index.js | 2 ++ lib/base.js | 39 +++++++++++++++++++-------------------- lib/core.js | 39 +++++++++++++++++++-------------------- lib/misc.js | 41 ++++++++++++++++++++--------------------- plugin/input.js | 1 + proto.js | 4 ++-- 6 files changed, 63 insertions(+), 63 deletions(-) diff --git a/index.js b/index.js index 065105d7..3c4e805e 100644 --- a/index.js +++ b/index.js @@ -17,5 +17,7 @@ Volcanos({name: "chat", iceberg: "/chat/", volcano: "/frame.js", "/plugin/local/wiki/word.js", "/plugin/local/code/vimer.js", "/plugin/local/code/inner.js", + "/plugin/story/trend.js", + "/plugin/story/spide.js", ], }) diff --git a/lib/base.js b/lib/base.js index 4e624305..9f084842 100644 --- a/lib/base.js +++ b/lib/base.js @@ -1,5 +1,5 @@ Volcanos("base", {help: "基础模块", - Int: function(value) {return parseInt(value)||0}, + Int: function(value) { return parseInt(value)||0 }, Obj: function(value, def) { try { return (typeof value == "string" && value != ""? JSON.parse(value): value) || def || {} @@ -15,13 +15,13 @@ Volcanos("base", {help: "基础模块", } return res }, - Args: function(obj) {var res = []; for (var k in obj) { res.push(encodeURIComponent(k)+"="+encodeURIComponent(obj[k])) } return res.join("&") }, + Size: function(size) {size = parseInt(size) if (size > 1000000000) { return parseInt(size / 1000000000) + "." + parseInt(size / 10000000 % 100) + "G" @@ -62,20 +62,19 @@ Volcanos("base", {help: "基础模块", fmt = fmt.replace("%S", this.Number(now.getSeconds(), 2)) return fmt }), - Duration: function(n) {var res = "", h = 0; - h = parseInt(n/3600000/24), h > 0 && (res += h+"d"), n = n % (3600000*24); - h = parseInt(n/3600000), h > 0 && (res += h+"h"), n = n % 3600000; - h = parseInt(n/60000), h > 0 && (res += h+"m"), n = n % 60000; - h = parseInt(n/1000), h > 0 && (res += h), n = n % 1000; - return res + (n > 0? "."+parseInt(n/10): "") + "s"; + Duration: function(n) { var res = "", h = 0 + h = parseInt(n/3600000/24), h > 0 && (res += h+"d"), n = n % (3600000*24) + h = parseInt(n/3600000), h > 0 && (res += h+"h"), n = n % 3600000 + h = parseInt(n/60000), h > 0 && (res += h+"m"), n = n % 60000 + h = parseInt(n/1000), h > 0 && (res += h), n = n % 1000 + return res + (n > 0? "."+parseInt(n/10): "") + "s" }, - 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(""); + 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-- } + return result.reverse(), result.join("") }), - Format: shy("数据格式化", function(obj) {return JSON.stringify(obj)}), + Format: shy("数据格式化", function(obj) { return JSON.stringify(obj) }), TimeAdd: shy("时间格式化", function(t, d) { return new Date(t - t%(24*3600*1000) - 8*3600*1000 + d*24*3600*1000) @@ -83,23 +82,23 @@ Volcanos("base", {help: "基础模块", isNight: function() { var now = new Date() return now.getHours() < 7 || now.getHours() > 17 }, - parseSize: function(size) { - if (size.endsWith("TB") || size.endsWith("tb") || size.endsWith("T") || size.endsWith("t")) { + parseSize: function(size) { size = size.toLowerCase() + if (size.endsWith("tb") || size.endsWith("t")) { return parseInt(size) * 1024 * 1024 * 1024 * 1024 } - if (size.endsWith("GB") || size.endsWith("gb") || size.endsWith("G") || size.endsWith("g")) { + if (size.endsWith("gb") || size.endsWith("g")) { return parseInt(size) * 1024 * 1024 * 1024 } - if (size.endsWith("MB") || size.endsWith("mb") || size.endsWith("M") || size.endsWith("m")) { + if (size.endsWith("mb") || size.endsWith("m")) { return parseInt(size) * 1024 * 1024 } - if (size.endsWith("KB") || size.endsWith("kb") || size.endsWith("K") || size.endsWith("k")) { + if (size.endsWith("kb") || size.endsWith("k")) { return parseInt(size) * 1024 } return parseInt(size) }, - _fileLine: function() { var obj = {}; Error.captureStackTrace(obj, arguments.callee); return obj.stack; }, + _fileLine: function() { var obj = {}; Error.captureStackTrace(obj, arguments.callee); return obj.stack }, FileLine: function(depth) { return this._fileLine().split("\n")[1+depth].trim() }, Log: function() { var args = [this.Time(), this.FileLine(2, 3).split("/").slice(3).slice(-length).join("/") ] diff --git a/lib/core.js b/lib/core.js index 076e5604..d8dd78ef 100644 --- a/lib/core.js +++ b/lib/core.js @@ -1,5 +1,5 @@ Volcanos("core", {help: "核心模块", - Items: shy("迭代器", function(obj, cb) {var list = []; + Items: shy("迭代器", function(obj, cb) { var list = [] for (var key in obj) { list = list.concat(this.List(obj[key], function(value, index, array) { return typeof cb == "function" && cb(value, index, key, obj) @@ -7,7 +7,7 @@ Volcanos("core", {help: "核心模块", } return list }), - Item: shy("迭代器", function(obj, cb) {var list = []; + Item: shy("迭代器", function(obj, cb) { var list = [] for (var k in obj) { var res = typeof cb == "function"? cb(k, obj[k]): k res && list.push(res) @@ -16,12 +16,12 @@ Volcanos("core", {help: "核心模块", }), List: shy("迭代器", function(obj, cb, interval, cbs) { if (typeof obj == "number") { - var begin = 0, end = obj, step = 1; + var begin = 0, end = obj, step = 1 if (typeof interval == "number") { step = interval } if (typeof cb == "number") { - begin = obj, end = cb; + begin = obj, end = cb } var list = [] @@ -33,20 +33,20 @@ Volcanos("core", {help: "核心模块", 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); + 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; + obj.length > 0 && setTimeout(function() { loop(0) }, interval/4) + return obj } - var list = [], res; + 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(obj[i]); + typeof cb == "function"? (res = cb(obj[i], i, obj)) != undefined && list.push(res): list.push(obj[i]) } - return list; + return list }), - Next: shy("迭代器", function(obj, cb, cbs) {obj = typeof obj == "string"? [obj]: (obj || []) + Next: shy("迭代器", function(obj, cb, cbs) { obj = typeof obj == "string"? [obj]: (obj || []) function next(list, cb, index) { list && list.length > 0? typeof cb == "function" && cb(list[0], function() { list.length > 0 && next(list.slice(1), cb, index+1) @@ -55,22 +55,22 @@ Volcanos("core", {help: "核心模块", next(obj, cb, 0) }), - Split: shy("分词器", function(str) { if (!str || !str.length) {return []} + Split: shy("分词器", function(str) { if (!str || !str.length) { return [] } var opt = {simple: false}, arg = []; for (var i = 1; i < arguments.length; i++) { - typeof arguments[i] == "object"? opt=arguments[i]: arg.push(arguments[i]) + typeof arguments[i] == "object"? opt = arguments[i]: arg.push(arguments[i]) } function _list(str) { var res = {}; for (var i = 0; i < str.length; i++) { res[str[i]] = true }; return res } // 空白符 - var seps = _list(arg[0]||"\t ,\n"); + var seps = _list(arg[0]||"\t ,\n") // 分隔符 - var sups = _list(arg[1]||"{[(.:)]}"); + var sups = _list(arg[1]||"{[(.:)]}") // 引用符 - var subs = _list(arg[2]||"'\"`"); + var subs = _list(arg[2]||"'\"`") // 开始分词 - var res = [], list = str; - var left = "", space = true, begin = 0; + var res = [], list = str + var left = "", space = true, begin = 0 for (var i = 0; i < list.length; i++) { if (seps[list[i]]) { // 空白符 @@ -164,6 +164,5 @@ Volcanos("core", {help: "核心模块", } return obj === other }, - }) diff --git a/lib/misc.js b/lib/misc.js index 24221004..18ee02f6 100644 --- a/lib/misc.js +++ b/lib/misc.js @@ -1,23 +1,23 @@ Volcanos("misc", {help: "工具模块", Message: function(event, can) { var msg = {} - msg.__proto__ = {_event: event, _can: can, _create_time: new Date(), + msg.__proto__ = {_event: event, _can: can, Option: function(key, val) { if (key == undefined) { return msg && msg.option || [] } if (typeof key == "object") { can.core.Item(key, msg.Option) } - if (val == undefined) { return msg && msg[key] && msg[key][0] || ""} - msg.option = msg.option || [], can.core.List(msg.option, function(k) { if (k == key) {return k} }).length > 0 || msg.option.push(key) + if (val == undefined) { return msg && msg[key] && msg[key][0] || "" } + msg.option = msg.option || [], can.core.List(msg.option, function(k) { if (k == key) { return k } }).length > 0 || msg.option.push(key) msg[key] = can.core.List(arguments).slice(1) return val }, Append: function(key, val) { if (key == undefined) { return msg && msg.append || [] } if (typeof key == "object") { can.core.Item(key, msg.Append) } - if (val == undefined) { return msg && msg[key] && msg[key][0] || ""} - msg.append = msg.append || [], can.core.List(msg.append, function(k) { if (k == key) {return k} }).length > 0 || msg.append.push(key) + if (val == undefined) { return msg && msg[key] && msg[key][0] || "" } + msg.append = msg.append || [], can.core.List(msg.append, function(k) { if (k == key) { return k } }).length > 0 || msg.append.push(key) msg[key] = can.core.List(arguments).slice(1) return val }, - Result: function(cb) { + Result: function() { return msg.result && msg.result.join("") || "" }, Table: function(cb) { if (!msg.append || !msg.append.length || !msg[msg.append[0]]) { return } @@ -66,15 +66,14 @@ Volcanos("misc", {help: "工具模块", break } } - if (i >= msg.append.length) {msg.append.push(key)} + if (i >= msg.append.length) { msg.append.push(key) } msg[key] = msg[key] || [] msg[key].push(""+(typeof value == "object"? JSON.stringify(value): value)+"") return msg }, - Echo: function(res) {msg.result = msg.result || [] - msg._hand = true - for (var i = 0; i < arguments.length; i++) {msg.result.push(arguments[i])} - return msg + Echo: function(res) { msg.result = msg.result || [] + for (var i = 0; i < arguments.length; i++) { msg.result.push(arguments[i]) } + return msg._hand = true, msg }, } return msg @@ -96,7 +95,7 @@ Volcanos("misc", {help: "工具模块", try { // 解析响应 var res = JSON.parse(xhr.responseText) } catch (e) { - var res = {"result": []} + var res = {"result": [xhr.responseText]} } } xhr.status == 200 && typeof cb == "function" && cb(msg.Copy(res)) @@ -105,12 +104,12 @@ Volcanos("misc", {help: "工具模块", if (msg.upload) { // 文件参数 var data = new FormData() - can.core.Item(form, function(key, value) { - can.core.List(value, function(item) {data.append(key, item)}) - }) - data.append("upload", msg.upload) + can.core.Items(form, function(value, index, key) { + data.append(key, item) + }), data.append("upload", msg.upload) + xhr.upload.onprogress = function(event) { - typeof msg._progress == "function" && msg._progress(event, parseInt(event.loaded/event.total*100), event.total, event.loaded) + typeof msg._progress == "function" && msg._progress(event, parseInt(event.loaded*100/event.total), event.total, event.loaded) } } else { // 表单参数 @@ -132,11 +131,11 @@ Volcanos("misc", {help: "工具模块", } msg._xhr = xhr }), - Run: shy("请求后端", {order: 0}, function(event, can, dataset, cmd, cb) { event = event || {} - var msg = (can.request||can.Event)(event) + Run: shy("请求后端", {order: 0}, function(event, can, dataset, cmd, cb) { + var msg = can.request(event = event || {}) // 解析参数 - var option = {"cmds": cmd||msg.cmd} + var option = {cmds: cmd||msg.cmd} msg.option && msg.option.forEach(function(item) { msg[item] && (option[item] = msg[item]) }) @@ -144,7 +143,7 @@ Volcanos("misc", {help: "工具模块", return msg[key] = value, key }) - msg._hand = true, can.misc.POST(can, msg, can.Conf("iceberg")+(msg.names||dataset.names||event.names||"").toLowerCase()+"?="+event._pane, option, function(msg) { + msg._hand = true, can.misc.POST(can, msg, can.Conf("iceberg")+(msg.names||dataset.names||event.names||"").toLowerCase()+"?="+(msg._can.sup||msg._can)._name, option, function(msg) { typeof cb == "function" && cb(msg) }), delete(event.msg) }), diff --git a/plugin/input.js b/plugin/input.js index 741d2bba..53ecc55f 100644 --- a/plugin/input.js +++ b/plugin/input.js @@ -31,6 +31,7 @@ Volcanos("onaction", {help: "控件交互", list: [], _init: function(can, meta, } }, + "upload": function(event, can) { can.user.upload(event, can) }, "关闭": function(event, can) { can.page.Remove(can, can.sup._target) }, "上传": function(event, can) { can.user.upload(event, can) }, "执行": function(event, can) { can.run(event) }, diff --git a/proto.js b/proto.js index 34d3b44c..4453f080 100644 --- a/proto.js +++ b/proto.js @@ -20,8 +20,8 @@ var Volcanos = shy("火山架", {libs: [], cache: {}}, [], function(name, can, l }); Preload = Preload.concat(Config.plugin) // 根模块 - meta.libs = Config.libs, meta.volcano = Config.volcano - name = Config.name, can = { _follow: Config.name, + meta.volcano = Config.volcano, meta.libs = Config.libs + name = Config.name, can = {_follow: Config.name, _target: document.body, _width: window.innerWidth, _height: window.innerHeight, }, libs = Preload.concat(Config.volcano), cb = function(can) { can.onengine._init(can, can.Conf(Config), Config.panes, function(msg) {