1
0
mirror of https://shylinux.com/x/volcanos synced 2025-04-26 01:04:06 +08:00

opt base.js

This commit is contained in:
shaoying 2020-12-16 10:18:13 +08:00
parent 3bad92a73b
commit 6f17d35c7c
6 changed files with 63 additions and 63 deletions

View File

@ -17,5 +17,7 @@ Volcanos({name: "chat", iceberg: "/chat/", volcano: "/frame.js",
"/plugin/local/wiki/word.js", "/plugin/local/wiki/word.js",
"/plugin/local/code/vimer.js", "/plugin/local/code/vimer.js",
"/plugin/local/code/inner.js", "/plugin/local/code/inner.js",
"/plugin/story/trend.js",
"/plugin/story/spide.js",
], ],
}) })

View File

@ -1,5 +1,5 @@
Volcanos("base", {help: "基础模块", Volcanos("base", {help: "基础模块",
Int: function(value) {return parseInt(value)||0}, Int: function(value) { return parseInt(value)||0 },
Obj: function(value, def) { Obj: function(value, def) {
try { try {
return (typeof value == "string" && value != ""? JSON.parse(value): value) || def || {} return (typeof value == "string" && value != ""? JSON.parse(value): value) || def || {}
@ -15,13 +15,13 @@ Volcanos("base", {help: "基础模块",
} }
return res return res
}, },
Args: function(obj) {var res = []; Args: function(obj) {var res = [];
for (var k in obj) { for (var k in obj) {
res.push(encodeURIComponent(k)+"="+encodeURIComponent(obj[k])) res.push(encodeURIComponent(k)+"="+encodeURIComponent(obj[k]))
} }
return res.join("&") return res.join("&")
}, },
Size: function(size) {size = parseInt(size) Size: function(size) {size = parseInt(size)
if (size > 1000000000) { if (size > 1000000000) {
return parseInt(size / 1000000000) + "." + parseInt(size / 10000000 % 100) + "G" 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)) fmt = fmt.replace("%S", this.Number(now.getSeconds(), 2))
return fmt return fmt
}), }),
Duration: function(n) {var res = "", h = 0; 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/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/3600000), h > 0 && (res += h+"h"), n = n % 3600000
h = parseInt(n/60000), h > 0 && (res += h+"m"), n = n % 60000; h = parseInt(n/60000), h > 0 && (res += h+"m"), n = n % 60000
h = parseInt(n/1000), h > 0 && (res += h), n = n % 1000; h = parseInt(n/1000), h > 0 && (res += h), n = n % 1000
return res + (n > 0? "."+parseInt(n/10): "") + "s"; return res + (n > 0? "."+parseInt(n/10): "") + "s"
}, },
Number: shy("数字格式化", function(d, n) {var result = []; Number: shy("数字格式化", function(d, n) { var result = []
while (d>0) {result.push(d % 10); d = parseInt(d / 10); n--} while (d > 0) { result.push(d % 10); d = parseInt(d / 10); n-- }
while (n > 0) {result.push("0"); n--} while (n > 0) { result.push("0"); n-- }
result.reverse(); return result.reverse(), result.join("")
return result.join("");
}), }),
Format: shy("数据格式化", function(obj) {return JSON.stringify(obj)}), Format: shy("数据格式化", function(obj) { return JSON.stringify(obj) }),
TimeAdd: shy("时间格式化", function(t, d) { TimeAdd: shy("时间格式化", function(t, d) {
return new Date(t - t%(24*3600*1000) - 8*3600*1000 + d*24*3600*1000) 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() isNight: function() { var now = new Date()
return now.getHours() < 7 || now.getHours() > 17 return now.getHours() < 7 || now.getHours() > 17
}, },
parseSize: function(size) { parseSize: function(size) { size = size.toLowerCase()
if (size.endsWith("TB") || size.endsWith("tb") || size.endsWith("T") || size.endsWith("t")) { if (size.endsWith("tb") || size.endsWith("t")) {
return parseInt(size) * 1024 * 1024 * 1024 * 1024 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 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 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) * 1024
} }
return parseInt(size) 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() }, FileLine: function(depth) { return this._fileLine().split("\n")[1+depth].trim() },
Log: function() { Log: function() {
var args = [this.Time(), this.FileLine(2, 3).split("/").slice(3).slice(-length).join("/") ] var args = [this.Time(), this.FileLine(2, 3).split("/").slice(3).slice(-length).join("/") ]

View File

@ -1,5 +1,5 @@
Volcanos("core", {help: "核心模块", Volcanos("core", {help: "核心模块",
Items: shy("迭代器", function(obj, cb) {var list = []; Items: shy("迭代器", function(obj, cb) { var list = []
for (var key in obj) { for (var key in obj) {
list = list.concat(this.List(obj[key], function(value, index, array) { list = list.concat(this.List(obj[key], function(value, index, array) {
return typeof cb == "function" && cb(value, index, key, obj) return typeof cb == "function" && cb(value, index, key, obj)
@ -7,7 +7,7 @@ Volcanos("core", {help: "核心模块",
} }
return list return list
}), }),
Item: shy("迭代器", function(obj, cb) {var list = []; Item: shy("迭代器", function(obj, cb) { var list = []
for (var k in obj) { for (var k in obj) {
var res = typeof cb == "function"? cb(k, obj[k]): k var res = typeof cb == "function"? cb(k, obj[k]): k
res && list.push(res) res && list.push(res)
@ -16,12 +16,12 @@ Volcanos("core", {help: "核心模块",
}), }),
List: shy("迭代器", function(obj, cb, interval, cbs) { List: shy("迭代器", function(obj, cb, interval, cbs) {
if (typeof obj == "number") { if (typeof obj == "number") {
var begin = 0, end = obj, step = 1; var begin = 0, end = obj, step = 1
if (typeof interval == "number") { if (typeof interval == "number") {
step = interval step = interval
} }
if (typeof cb == "number") { if (typeof cb == "number") {
begin = obj, end = cb; begin = obj, end = cb
} }
var list = [] var list = []
@ -33,20 +33,20 @@ Volcanos("core", {help: "核心模块",
obj = typeof obj == "string"? [obj]: (obj || []) obj = typeof obj == "string"? [obj]: (obj || [])
if (interval > 0) { if (interval > 0) {
function loop(i) {if (i >= obj.length) {return typeof cbs == "function" && cbs(obj)} 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); typeof cb == "function" && cb(obj[i], i, obj), setTimeout(function() { loop(i+1) }, interval)
} }
obj.length > 0 && setTimeout(function() {loop(0)}, interval/4); obj.length > 0 && setTimeout(function() { loop(0) }, interval/4)
return obj; return obj
} }
var list = [], res; var list = [], res
for (var i = 0; i < obj.length; i++) { 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) { function next(list, cb, index) {
list && list.length > 0? typeof cb == "function" && cb(list[0], function() { list && list.length > 0? typeof cb == "function" && cb(list[0], function() {
list.length > 0 && next(list.slice(1), cb, index+1) list.length > 0 && next(list.slice(1), cb, index+1)
@ -55,22 +55,22 @@ Volcanos("core", {help: "核心模块",
next(obj, cb, 0) 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++) { 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 } 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 res = [], list = str
var left = "", space = true, begin = 0; var left = "", space = true, begin = 0
for (var i = 0; i < list.length; i++) { for (var i = 0; i < list.length; i++) {
if (seps[list[i]]) { if (seps[list[i]]) {
// 空白符 // 空白符
@ -164,6 +164,5 @@ Volcanos("core", {help: "核心模块",
} }
return obj === other return obj === other
}, },
}) })

View File

@ -1,23 +1,23 @@
Volcanos("misc", {help: "工具模块", Volcanos("misc", {help: "工具模块",
Message: function(event, can) { var msg = {} 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) { Option: function(key, val) {
if (key == undefined) { return msg && msg.option || [] } if (key == undefined) { return msg && msg.option || [] }
if (typeof key == "object") { can.core.Item(key, msg.Option) } if (typeof key == "object") { can.core.Item(key, msg.Option) }
if (val == undefined) { return msg && msg[key] && msg[key][0] || ""} 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.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) msg[key] = can.core.List(arguments).slice(1)
return val return val
}, },
Append: function(key, val) { Append: function(key, val) {
if (key == undefined) { return msg && msg.append || [] } if (key == undefined) { return msg && msg.append || [] }
if (typeof key == "object") { can.core.Item(key, msg.Append) } if (typeof key == "object") { can.core.Item(key, msg.Append) }
if (val == undefined) { return msg && msg[key] && msg[key][0] || ""} 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.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) msg[key] = can.core.List(arguments).slice(1)
return val return val
}, },
Result: function(cb) { Result: function() {
return msg.result && msg.result.join("") || "" return msg.result && msg.result.join("") || ""
}, },
Table: function(cb) { if (!msg.append || !msg.append.length || !msg[msg.append[0]]) { return } Table: function(cb) { if (!msg.append || !msg.append.length || !msg[msg.append[0]]) { return }
@ -66,15 +66,14 @@ Volcanos("misc", {help: "工具模块",
break 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] = msg[key] || []
msg[key].push(""+(typeof value == "object"? JSON.stringify(value): value)+"") msg[key].push(""+(typeof value == "object"? JSON.stringify(value): value)+"")
return msg return msg
}, },
Echo: function(res) {msg.result = msg.result || [] Echo: function(res) { msg.result = msg.result || []
msg._hand = true for (var i = 0; i < arguments.length; i++) { msg.result.push(arguments[i]) }
for (var i = 0; i < arguments.length; i++) {msg.result.push(arguments[i])} return msg._hand = true, msg
return msg
}, },
} }
return msg return msg
@ -96,7 +95,7 @@ Volcanos("misc", {help: "工具模块",
try { // 解析响应 try { // 解析响应
var res = JSON.parse(xhr.responseText) var res = JSON.parse(xhr.responseText)
} catch (e) { } catch (e) {
var res = {"result": []} var res = {"result": [xhr.responseText]}
} }
} }
xhr.status == 200 && typeof cb == "function" && cb(msg.Copy(res)) xhr.status == 200 && typeof cb == "function" && cb(msg.Copy(res))
@ -105,12 +104,12 @@ Volcanos("misc", {help: "工具模块",
if (msg.upload) { if (msg.upload) {
// 文件参数 // 文件参数
var data = new FormData() var data = new FormData()
can.core.Item(form, function(key, value) { can.core.Items(form, function(value, index, key) {
can.core.List(value, function(item) {data.append(key, item)}) data.append(key, item)
}) }), data.append("upload", msg.upload)
data.append("upload", msg.upload)
xhr.upload.onprogress = function(event) { 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 { } else {
// 表单参数 // 表单参数
@ -132,11 +131,11 @@ Volcanos("misc", {help: "工具模块",
} }
msg._xhr = xhr msg._xhr = xhr
}), }),
Run: shy("请求后端", {order: 0}, function(event, can, dataset, cmd, cb) { event = event || {} Run: shy("请求后端", {order: 0}, function(event, can, dataset, cmd, cb) {
var msg = (can.request||can.Event)(event) 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.option && msg.option.forEach(function(item) {
msg[item] && (option[item] = msg[item]) msg[item] && (option[item] = msg[item])
}) })
@ -144,7 +143,7 @@ Volcanos("misc", {help: "工具模块",
return msg[key] = value, key 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) typeof cb == "function" && cb(msg)
}), delete(event.msg) }), delete(event.msg)
}), }),

View File

@ -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.page.Remove(can, can.sup._target) },
"上传": function(event, can) { can.user.upload(event, can) }, "上传": function(event, can) { can.user.upload(event, can) },
"执行": function(event, can) { can.run(event) }, "执行": function(event, can) { can.run(event) },

View File

@ -20,8 +20,8 @@ var Volcanos = shy("火山架", {libs: [], cache: {}}, [], function(name, can, l
}); Preload = Preload.concat(Config.plugin) }); Preload = Preload.concat(Config.plugin)
// 根模块 // 根模块
meta.libs = Config.libs, meta.volcano = Config.volcano meta.volcano = Config.volcano, meta.libs = Config.libs
name = Config.name, can = { _follow: Config.name, name = Config.name, can = {_follow: Config.name,
_target: document.body, _width: window.innerWidth, _height: window.innerHeight, _target: document.body, _width: window.innerWidth, _height: window.innerHeight,
}, libs = Preload.concat(Config.volcano), cb = function(can) { }, libs = Preload.concat(Config.volcano), cb = function(can) {
can.onengine._init(can, can.Conf(Config), Config.panes, function(msg) { can.onengine._init(can, can.Conf(Config), Config.panes, function(msg) {