1
0
mirror of https://shylinux.com/x/volcanos synced 2025-04-25 16:58:06 +08:00
volcanos/lib/misc.js
2023-01-29 23:18:46 +08:00

308 lines
19 KiB
JavaScript

Volcanos("misc", {Message: function(event, can) { var msg = {}
var proto = {_event: event, _can: can, _target: can._target,
RunAction: function(event, sub, cmds, meta) { var msg = can.request(event); meta = meta || sub&&sub.onaction || {}
if (msg.Option(ice.MSG_HANDLE) == ice.TRUE) { return }
if (cmds && cmds[0] == ctx.ACTION && can.base.isFunc(meta[cmds[1]])) { msg.Option(ice.MSG_HANDLE, ice.TRUE)
return can.core.CallFunc(meta[cmds[1]], {event: event._event||event, can: sub, msg: msg, button: cmds[1], cmd: cmds[1]}), true
} return false
},
Display: function(file) { msg.Option(ice.MSG_DISPLAY, file) },
DisplayStory: function(file) { msg.Option(ice.MSG_DISPLAY, chat.PLUGIN_STORY+file) },
OptionStatus: function() { return msg.Option(ice.MSG_STATUS) },
OptionProcess: function() { return msg.Option(ice.MSG_PROCESS) },
SearchOrOption: function(key) { return can.misc.Search(can, key)||msg.Option(key) },
StatusTimeCount: function(obj) { msg.append && msg.Status(can.base.Copy(kit.Dict(mdb.TIME, can.base.Time(), mdb.COUNT, msg.Length()+"x"+msg.append.length), obj)) },
Status: function(obj) { msg.Option(ice.MSG_STATUS, JSON.stringify(can.core.Item(obj, function(key, value) { return {name: key, value: value} }))) },
OptionDefault(key, val) { var arg = arguments
for (var i = 0; i < arg.length; i += 2) { msg.Option(arg[i]) || msg.Option(arg[i], arg[i+1]) }
return msg.Option(key) || msg.Option(key, val)
},
Option: function(key, val) {
if (key == undefined) { return msg && msg.option || [] }
if (can.base.isObject(key)) { can.core.Item(key, msg.Option) }
if (val == undefined) { return msg && msg[key] && msg.option && msg.option.indexOf(key) > -1 && msg[key][0] || "" }
return msg.option = can.base.AddUniq(msg.option, key), msg[key] = can.core.List(arguments).slice(1), val
},
Append: function(key, val) {
if (key == undefined) { return msg && msg.append || [] }
if (can.base.isObject(key)) { can.core.Item(key, msg.Append) }
if (val == undefined) { return msg && msg[key] && msg[key][0] || "" }
return msg.append = can.base.AddUniq(msg.append, key), msg[key] = can.core.List(arguments).slice(1), val
},
Result: function() {
if (msg.result && msg.result[0] == ice.ErrWarn) { return msg.result.join(ice.SP) }
return msg.result && msg.result.join("") || ""
},
Results: function() { return msg.result && msg.result[0] == ice.ErrWarn? "": msg.Result() },
TableDetail: function() { var item = can.Option()
return msg.Table(function(value) { can.core.Value(item, value.key, value.value) }), item
},
Table: function(cb) {
return can.core.List(msg.Length(), function(index) { var value = {}
can.core.List(msg.append, function(k) { value[k] = msg[k]&&msg[k][index]||"" })
return can.base.isFunc(cb)? cb(value, index): value
})
},
Length: function() {
var max = 0; can.core.List(msg.append, function(k) { if (msg[k] && msg[k].length > max) { max = msg[k].length } }); return max
},
Clear: function(key) { switch (key||ice.MSG_APPEND) {
case ice.MSG_APPEND:
case ice.MSG_OPTION:
can.core.List(msg[key], function(key) { delete(msg[key]) })
default: delete(msg[key])
} },
Copy: function(res) { if (!res) { return msg }
res.append && (msg.append = res.append) && res.append.forEach(function(item) {
var i = msg.option && msg.option.indexOf(item); if (i > -1) { msg.option[i] = "", delete(msg[item]) }
res[item] && (msg[item] = (msg[item]||[]).concat(res[item]))
}), res.result && (msg.result = (msg.result||[]).concat(res.result))
res.option && (msg.option = res.option) && res.option.forEach(function(item) { res[item] && (msg[item] = res[item]) })
res._option && (msg._option = res._option) && res._option.forEach(function(item) { res[item] && (msg[item] = res[item]) })
return msg
},
Push: function(key, value, detail) {
if (can.base.isObject(key)) {
can.core.List(value||can.base.Obj(msg.Option(ice.MSG_FIELDS))||can.core.Item(key), function(item) {
detail? msg.Push(mdb.KEY, item).Push(mdb.VALUE, key[item]||""): msg.Push(item, key[item]||"")
}); return msg
} msg.append = can.base.AddUniq(msg.append, key), msg[key] = msg[key]||[]
var i = msg.option && msg.option.indexOf(key); if (i > -1) { msg.option[i] = "", msg[key] = [] }
msg[key].push(can.base.isString(value)||can.base.isFunc(value)? value: JSON.stringify(value))
return msg
},
PushAction: function(button) { can.core.List(msg.Length(), function() {
msg.Push(ctx.ACTION, can.page.Format(html.INPUT, "", mdb.TYPE, html.BUTTON, mdb.NAME, button, mdb.VALUE, can.user.trans(can, button)))
}); 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
},
Dump: function(can) {
can.onmotion.clear(can)
can.onappend.table(can, msg)
can.onappend.board(can, msg)
can.onmotion.story.auto(can)
},
_caller: function(skip) {
msg.Option("log.caller") || msg.Option("log.caller", can.misc.fileLine((skip||2)+1).link)
return msg
},
_callers: function(skip) {
msg.Option("log.caller") || msg.Option("log.caller", can.misc._fileLine((skip||2)+1).link)
return msg
},
Defer: function(cb) { msg._defer = msg._defer||[]
if (arguments.length == 0) { msg._defer = can.core.List(msg._defer.reverse(), function(cb) { can.base.isFunc(cb) && cb() }) } else { msg._defer.push(cb) }
}, _defer: [],
}; return kit.proto(msg, proto)
},
concat: function(can, to, from) { to = to||[], from = from||[]
if (arguments.length > 3) { for (var i = 2; i < arguments.length; i++) { to = can.misc.concat(can, to, arguments[i]) } return to }
if (from[0] == ctx.ACTION && from[1] == ice.RUN && can.onengine.plugin(can, from[2])) { return from }
if (can.onengine.plugin(can, from[0])) { return from }
if (from[0] == "_search") { return from }
return to.concat(from)
},
runAction: function(can, msg, cmds, cb, meta) {
if (cmds[0] == ctx.ACTION && meta[cmds[1]]) { return meta[cmds[1]](cmds.slice(2)), true }
if (meta[cmds[0]]) { return meta[cmds[0]](cmds.slice(1)), true }
},
Event: function(event, can, cb) {
for (var i = 3; i < arguments.length; i++) { can.request(event, arguments[i]) }
cb(can.request(event))
},
Run: function(event, can, dataset, cmds, cb) { var msg = can.request(event)
var form = {}; msg.option && msg.option.forEach(function(key) {
if ([ice.MSG_HANDLE, ice.MSG_DAEMON].indexOf(key) > -1) { return }
if (can.base.isObject(msg.Option(key))) { return }
if (can.base.isFunc(msg.Option(key))) { return }
msg[key] && (form[key] = msg[key])
}), form.cmds = cmds||form.cmds; var _can = msg._can._outputs? msg._can: msg._can.sup||msg._can
can.misc.POST(can, msg, can.base.MergeURL(dataset.names.toLowerCase(),
"_name", _can._name, "_index", _can._index, ice.MSG_DAEMON, dataset.daemon), form, cb)
},
POST: function(can, msg, url, form, cb) { var xhr = new XMLHttpRequest(), begin = new Date(); msg._xhr = xhr
xhr.open(msg._method||web.POST, url), xhr.onreadystatechange = function() { if (xhr.readyState != 4) { return }
try { var res = JSON.parse(xhr.responseText) } catch (e) { var res = {result: [xhr.responseText]} } msg.Option("_cost", new Date() - begin)
if (xhr.status == 200) { return msg.detail || (msg.detail = res.detail), can.base.isFunc(cb) && cb(msg.Copy(res)) }
can.user.toastFailure(can, xhr.response), can.misc.Warn(xhr.status, res, url, form)
}, xhr.setRequestHeader(web.Accept, msg._accept||web.ContentJSON)
if (msg._upload) {
var data = new FormData(); can.core.ItemForm(form, function(v, i, k) { data.append(k, v) })
data.append(ice.MSG_UPLOAD, web.UPLOAD), data.append(web.UPLOAD, msg._upload)
xhr.upload.onprogress = function(event) { can.base.isFunc(msg._progress) && msg._progress(event, parseInt(event.loaded*100/event.total), event.total, event.loaded) }
} else if (can.user.isMailMaster) {
var data = can.core.ItemForm(form, function(value, index, key) { return key+ice.EQ+encodeURIComponent(value) }).join("&")
if (data) { xhr.open(msg._method||web.POST, url += (url.indexOf(url, "?") == -1? "?": "&")+data) }
} else {
var data = can.core.ItemForm(form, function(v, i, k) { return k+ice.EQ+encodeURIComponent(v) }).join("&")
xhr.setRequestHeader(web.ContentType, web.ContentFORM)
} try { xhr.send(data) } catch(e) { can.misc.Warn(e) }
},
GET: function(can, url, cb) { var xhr = new XMLHttpRequest()
xhr.open(msg._method||web.GET, url), xhr.onreadystatechange = function() { if (xhr.readyState != 4) { return }
if (xhr.status == 200) { return can.base.isFunc(cb) && cb(xhr.responseText) } can.misc.Warn(xhr.status, res, url, form)
}; try { xhr.send() } catch(e) { can.misc.Warn(e) }
},
WSS: function(can, args, cb, onopen, onclose, onerror, _msg) { if (can.user.isIE) { return }
var url = location.protocol.replace(ice.HTTP, "ws")+"//"+location.host+"/space/"
if (url.indexOf(html.CHROME) == 0) { url = "ws://localhost:9020/space/" }
var socket = new WebSocket(can.base.MergeURL(url, args))
_msg = _msg || can.request()._caller()
socket.onclose = function() { can.misc.Log(html.WSS, cli.CLOSE, args)
can.base.isFunc(onclose)? onclose(socket): can.core.Timer(can.base.random(3000, 100), function() {
args.name = args.name||can._wss_name, can.misc.WSS(can, args, cb, onopen, onerror, onclose, _msg)
})
}, socket.onerror = function() { can.misc.Log(html.WSS, log.ERROR, args)
can.base.isFunc(onerror)? onerror(socket): socket.close()
}, socket.onopen = function() { can.misc.Log(html.WSS, cli.OPEN, args, _msg)
can.base.isFunc(onopen) && onopen(socket)
}, socket.onmessage = function(event) { can.misc.Event(event, can, function(msg) {
try { var data = JSON.parse(event.data) } catch (e) { var data = {detail: [event.data]} }
msg.Reply = function() { var res = can.request({}, {_handle: ice.TRUE})
res._target = (msg[ice.MSG_SOURCE]||[]).reverse(), res._source = (msg[ice.MSG_TARGET]||[]).reverse().slice(1)||[]
res.append = msg.append, can.core.List(msg.append, function(key) { res[key] = msg[key] }), res.result = (msg.result||[]).concat(can.core.List(arguments))
res.Option(ice.LOG_DISABLE, msg.Option(ice.LOG_DISABLE)) != ice.TRUE && can.misc.Log(html.WSS, ice.MSG_RESULT, msg.result&&msg.result.length>0? msg.result: undefined, msg, _msg)
socket.send(JSON.stringify(res))
}, msg.detail = data.detail, msg.Copy(data)
try {
msg.Option(ice.LOG_DISABLE) != ice.TRUE && can.misc.Log(html.WSS, ice.MSG_DETAIL, msg.detail, msg, _msg)
can.core.CallFunc(cb, {event: event, msg: msg, cmd: msg.detail[0], arg: msg.detail.slice(1), cb: function() { msg.Reply() }})
} catch (e) { can.misc.Warn(e), msg.Reply() }
}) }
},
MergePath: function(can, file, path) { return file.indexOf(ice.PS) == 0 || file.indexOf(ice.HTTP) == 0? file: can.base.Path(path, file) },
MergeCache: function(can, hash) { return can.misc.MergeURL(can, {_path: can.base.Path(web.SHARE_CACHE, hash)}, true) },
MergePodCmd: function(can, objs) {
objs.pod = can.core.Keys(can.misc.Search(can, ice.POD), objs.pod)
objs.theme = objs.theme||can.misc.Search(can, chat.THEME)
return can.misc.MergeURL(can, objs, true)
},
MergeURL: function(can, objs, clear) { var pod = ""
var path = location.pathname; objs._path && (path = objs._path), delete(objs._path)
objs.pod && (path = can.base.Path("/chat/pod/", objs.pod)), delete(objs.pod)
var ls = path.split(ice.PS); ls[1] == "chat" && ls[2] == ice.POD && (pod = ls[3])
objs.cmd && (path = can.base.Path("/chat", pod? "pod/"+pod: "", ice.CMD, objs.cmd)), delete(objs.cmd)
objs.website && (path = can.base.Path("/chat", pod? "pod/"+pod: "", web.WEBSITE, objs.website)), delete(objs.website)
var _location = location; if (can.user.isExtension) { var _location = new URL(Volcanos.meta.iceberg) }
return can.base.MergeURL(_location.origin+path+(clear?"":_location.search), objs)
},
ParseURL: function(can, url) { var _url = can.base.ParseURL(url)
var _ls = (_url._origin.split("/chat/")[1]||"").split(ice.PS)
for (var i = 0; i < _ls.length; i += 2) { _url[_ls[i]] = _ls[i+1] }
return _url
},
SearchOrConf: function(can, key, def) {
return can.misc.Search(can, key)||Volcanos.meta.args[key]||can.misc.localStorage(can, "can."+key)||can.Conf(key)||def
},
SearchHash: function(can) { if (!can.isCmdMode()) { return [] }
if (arguments.length > 1) { location.hash = encodeURIComponent(can.core.List(arguments).slice(1).join(ice.FS)) }
return can.core.Split(decodeURIComponent(location.hash.slice(1)), ":,")||[]
},
Search: function(can, key, value) { var args = {}
if (value == undefined && can.base.isString(key)) { var ls = can.core.Split(location.pathname, ice.PS); if (ls[0] == chat.SHARE) { args[chat.SHARE] = ls[1] }
for (var i = 1; i < ls.length; i += 2) { if (kit.Dict(ice.POD, true, ice.CMD, true, web.WEBSITE, true)[ls[i]]) { args[ls[i]] = ls[i+1] } }
} location.search && location.search.slice(1).split("&").forEach(function(item) { var ls = item.split(ice.EQ); ls[1] != "" && (args[decodeURIComponent(ls[0])] = decodeURIComponent(ls[1])) })
if (can.base.isUndefined(key)) { return args } else if (can.base.isObject(key)) {
can.core.Item(key, function(k, v) { args[k] === ""? delete(args[k]): (args[k] = v) })
} else if (can.base.isUndefined(value)) { return args[key] } else {
value === ""? delete(args[key]): (args[key] = value)
}
var search = can.base.Args(args); return search? location.search = search: location.href = location.pathname
},
CookieSessid: function(can, value, path) {
return can.misc.Cookie(can, ice.MSG_SESSID+"_"+(location.port||(location.protocol == "https:"? "443": "80")), value, path)
},
Cookie: function(can, key, value, path) { function set(k, v) { document.cookie = k+ice.EQ+v+";path="+(path||ice.PS) }
if (can.base.isObject(key)) { for (var k in key) { set(k, key[k]) } key = undefined }
if (can.base.isUndefined(key)) { var cs = {}; if (!document.cookie) { return } return document.cookie.split("; ").forEach(function(item) { var ls = item.split(ice.EQ); cs[ls[0]] = ls[1] }), cs }
if (value === "") { var expires = new Date(); expires.setTime(expires.getTime() - 10);
return document.cookie = key+ice.EQ+value+";path="+(path||ice.PS)+";expires=" + expires.toGMTString();
} can.base.isUndefined(value) || set(key, value)
var val = (new RegExp(key+"=([^;]*);?")).exec(document.cookie)
return val && val.length > 1? val[1]: ""
},
localStorage: function(can, key, value) {
if (can.base.isUndefined(key)) { var res = {}; can.core.Item(localStorage, function(name, value) { can.base.isFunc(value) || name == "length" || (res[name] = value) }); return res }
if (!can.base.isUndefined(value)) { if (value === "") { return localStorage.removeItem(key) } localStorage.setItem(key, value) } return localStorage.getItem(key)
},
sessionStorage: function(can, key, value) {
if (can.base.isUndefined(key)) { var res = {}; can.core.Item(sessionStorage, function(name, value) { can.base.isFunc(value) || name == "length" || (res[name] = value) }); return res }
if (!can.base.isUndefined(value)) { if (value === "") { return sessionStorage.removeItem(key) } sessionStorage.setItem(key, value) } return sessionStorage.getItem(key)
},
_signal: function(args) {
this._list.push(args)
}, _list: [],
Log: function() { var pos = arguments[0] && arguments[0].indexOf && arguments[0].indexOf("on") == 0? arguments[0] == "onremote"? 5: 3: 2
var args = [this._time(), this.fileLine(pos).link]
for (var i in arguments) { var arg = arguments[i]; if (!arg) { continue } args.push(arg)
if (arg.Option && arg.Option("log.caller")) { args[1] = arg.Option("log.caller") }
}
console.log.apply(console, args), this._signal(args)
},
Info: function() { var args = [this._time(), this.FileLine(2, 3), log.INFO]
for (var i in arguments) { arguments[i] != undefined && args.push(arguments[i]) }
console.info.apply(console, args), this._signal(args)
},
Warn: function() { var args = [this._time(), this.fileLine(2, 3).link, log.WARN]
for (var i in arguments) { arguments[i] != undefined && args.push(arguments[i]) }
console.warn.apply(console, args), this._signal(args)
debugger
},
Error: function() { var args = [this._time(), this.fileLine(2, 3).link, log.ERROR]
for (var i in arguments) { arguments[i] != undefined && args.push(arguments[i]) }
args.push(ice.NL, this._stacks().slice(1).join(ice.NL))
console.error.apply(console, args), this._signal(args)
debugger
},
Debug: function() { var args = [this._time(), this.fileLine(2, 3).link, log.DEBUG]
for (var i in arguments) { arguments[i] != undefined && args.push(arguments[i]) }
args.push(ice.NL, this._stacks().slice(1, 4).join(ice.NL))
console.debug.apply(console, args), this._signal(args)
},
Trace: function() { var filter = "", output = false
var args = [this._time(), this.fileLine(2, 3).link, log.TRACE]
for (var i in arguments) { var arg = arguments[i]; if (!arg) { continue } args.push(arg)
if (arg.Option && arg.Option("log.trace") == ice.TRUE) { output = true }
if (arg.Conf && arg.Conf("log.trace") == ice.TRUE) { output = true }
} if (!output) { return }
args.push(ice.NL, this._stacks().slice(1, 4).join(ice.NL))
console.debug.apply(console, args), this._signal(args)
debugger
},
FileLine: function(depth, length) { var file = this.fileLine(depth+1, length||9); return file.link },
_fileLine: function(depth, length) {
var file = this.fileLine(depth+1, length||9)
for (var i = depth+1; i < 10; i++) {
var _file = this.fileLine(i+1, length||9)
if (_file && _file.path && _file.link != file.link && !_file.path.indexOf("/lib/") == 0) { return _file }
}
return file
},
fileLine: function(depth, length) { var list = this._stacks()
function split(i) { if (!list[i]) { return {} }
var ls = /(https*:\/\/[^/]+)*([^:]+):([0-9]+):([0-9]+)/.exec(list[i])
var name = ""; list[i].lastIndexOf(ice.TB) > 0 && (name = list[i].split(ice.TB).pop())
return {name: name, link: ls[0], path: ls[2], file: ls[2].split(ice.PS).slice(-length).join(ice.PS), line: ls[3], cols: ls[4]}
}
if (depth < 0) { var current = split(-depth)
for (var i = -depth+1; i < list.length; i++) { var pos = split(i); if (pos.path != current.path) { return pos } }
} return split(depth)||{}
},
_stacks: function() { var list = ((new Error()).stack||"").split(ice.NL).slice(2)
for (var i = 0; i < list.length; i++) { var ls = list[i].trim().split(ice.SP)
list[i] = ls.pop().trim(); if (list[i][0] == "(") { list[i] = list[i].slice(1, -1) }
list[i] = ice.TB+list[i]; if (ls.length > 1) { list[i] += ice.TB+ls.pop() }
} return list
}, _stack: function() { return ((new Error()).stack||"").split(ice.NL).slice(2) },
_time: function() { var now = new Date()
var hour = now.getHours(); hour < 10 && (hour = "0"+hour)
var minute = now.getMinutes(); minute < 10 && (minute = "0"+minute)
var second = now.getSeconds(); second < 10 && (second = "0"+second)
var mill = now.getMilliseconds(); mill < 10 && (mill = "00"+mill) || mill < 100 && (mill = "0"+mill)
return [hour, minute, second].join(ice.DF)+ice.PT+mill
},
})