mirror of
https://shylinux.com/x/volcanos
synced 2025-04-25 08:48:06 +08:00
231 lines
11 KiB
JavaScript
231 lines
11 KiB
JavaScript
var user = Volcanos("user", {help: "用户模块",
|
|
alert: function(text) {alert(JSON.stringify(text))},
|
|
confirm: function(text) {return confirm(JSON.stringify(text))},
|
|
prompt: function(text, cb, def, silent) {(text = silent? def: prompt(text, def||"")) != undefined && typeof cb == "function" && cb(text); return text},
|
|
reload: function(force) {(force || confirm("重新加载页面?")) && location.reload()},
|
|
title: function(text) {return text && (document.title = text), document.title},
|
|
|
|
login: function(can, cb) {
|
|
var ui = can.user.input({clientX: 200, clientY: 100}, can, [
|
|
{username: "username", name: "用户"},
|
|
{password: "password", name: "密码"},
|
|
{button: [["登录", function(event) {
|
|
can.user.Cookie(can, "sessid", "")
|
|
can.run({}, ["login", ui["用户"].value, ui["密码"].value], function(msg) {
|
|
if (can.user.Cookie(can, "sessid")||msg.Option("user.name")||msg.Result()) {
|
|
can.page.Remove(can, ui.first); return typeof cb == "function" && cb()
|
|
}
|
|
can.user.alert("用户或密码错误")
|
|
})
|
|
}], ["扫码", function(event) {
|
|
// TODO
|
|
}]]},
|
|
], function(event, button, data, list) {
|
|
// TODO
|
|
})
|
|
},
|
|
toast: function(can, text, title, duration) {
|
|
var meta = typeof text == "object"? text: {text: text, title: title, duration: duration}
|
|
var width = meta.width||200, height = meta.height||100
|
|
|
|
var ui = can.page.Append(can, document.body, [{view: "toast", style: {
|
|
width: width, bottom: 100, left: document.body.clientWidth/2-width/2,
|
|
}, list: [
|
|
{text: [meta.title||"", "div", "title"]},
|
|
typeof meta.text == "object"? meta.text: {text: [meta.text||"执行成功", "div", "content"]},
|
|
{view: ["button"], list: meta.button},
|
|
{text: ["", "div", "duration"]},
|
|
]}])
|
|
|
|
can.Timer({value: 1000, length: (meta.duration||3000)/1000}, function(event, interval, index) {
|
|
if (index > 2) { ui.duration.innerHTML = index+"s..." }
|
|
}, function() { can.page.Remove(can, ui.first) })
|
|
|
|
ui.Close = function() { can.page.Remove(can, ui.first) }
|
|
return ui
|
|
},
|
|
share: function(can, msg, cmd) {
|
|
can.run(msg._event, cmd||["action", "share"], function(msg) {
|
|
var src = can.user.Share(can, {_path: "/share/"+msg.Result()}, true)
|
|
var ui = can.user.toast(can, {title: can.page.Format("a", src, msg.Result()), text: can.page.Format("img", src+"/share"),
|
|
width: 300, height: 300, duration: 100000, button: [{button: ["确定", function(event) {
|
|
ui.Close()
|
|
}]}] })
|
|
})
|
|
},
|
|
carte: function(can, meta, list, cb) { meta = meta || can.ondetail, list = list && list.length > 0? list: meta.list; if (list.length == 0) { return }
|
|
meta = meta||can.ondetail||{}, cb = cb||function(ev, item, meta) {
|
|
var cb = can.ondetail[item] || can.onaction[item] || can.onkeymap&&can.onkeymap._remote
|
|
cb && cb(event, can, item)
|
|
}
|
|
|
|
var x = event.clientX, y = event.clientY; y += 0; if (x > 600) { x -= 20 }
|
|
var ui = can.page.Append(can, document.body, [{view: "carte", style: {left: x+"px", top: y+"px"}, onmouseleave: function(event) {
|
|
can.page.Remove(can, ui.first)
|
|
}, list: can.core.List(list, function(item) {
|
|
return {view: "item", list: [typeof item == "string"? /* button */ {text: item, click: function(event) {
|
|
typeof cb == "function" && cb(event, item, meta)
|
|
}}: item.args? /* input */ {text: item.name, click: function(event) {
|
|
can.user.input(event, can, item.args, cb)
|
|
}}: /* select */ {select: [item, function(event) {
|
|
typeof cb == "function" && cb(event, event.target.value, meta)
|
|
}]} ]}
|
|
}) }])
|
|
|
|
event.stopPropagation()
|
|
event.preventDefault()
|
|
return ui
|
|
},
|
|
input: function(event, can, form, cb) {
|
|
function cbs(event, button) {
|
|
var data = {}; var list = can.page.Select(can, ui.table, "select,input,textarea", function(item) {
|
|
return data[item.name] = item.value
|
|
})
|
|
if (typeof cb == "function" && cb(event, button, data, list)) {
|
|
can.page.Remove(can, ui.first)
|
|
}
|
|
}
|
|
|
|
var x = event.clientX, y = event.clientY; y += 10; if (x > 600) { x -= 160 }
|
|
var button; var ui = can.page.Append(can, document.body, [{view: ["input", "fieldset"], style: {left: x+"px", top: y+"px"}, list: [
|
|
{view: ["option", "table"], list: can.core.List(form, function(item) {
|
|
if (item.button) { button = can.core.List(item.button, function(item) {
|
|
return {button: typeof item == "object"? item: [item, function(event) { cbs(event, item) }]}
|
|
}); return }
|
|
return {type: ["tr"], list: [
|
|
{type: "td", list: [{text: typeof item == "string"? item: item.length > 0? item[0]: item.name || ""}]},
|
|
{type: "td", list: [typeof item == "string"? /* input */ {input: item, data: {autofocus: true}}:
|
|
item.length > 0? /* select */ {select: [item]}:
|
|
item._input == "select"? /* select */ {select: [[item.name].concat(item.values)], data: item}:
|
|
item._input? /* input */ {type: "input", data: (item.type = item._input, item)}:
|
|
/* other */ item,
|
|
]},
|
|
]}
|
|
})},
|
|
{view: "action", list: button||[{button: ["提交", function(event) {
|
|
cbs(event, "提交")
|
|
}]}, {button: ["关闭", function(event, button) {
|
|
can.page.Remove(can, ui.first)
|
|
}]}]},
|
|
]}])
|
|
can.page.Select(can, ui.first, "input", function(item, index) {
|
|
index == 0 && (item.setSelectionRange(0, -1), item.focus())
|
|
})
|
|
return ui
|
|
},
|
|
upload: function(event, can) { var begin = new Date()
|
|
var x = event.clientX, y = event.clientY; y += 10; if (x > 400) { x -= 200 }
|
|
var ui = can.page.Append(can, document.body, [{view: "upload", style: {left: x+"px", top: y+"px"}, list: [
|
|
{view: "action"}, {view: "output"},
|
|
]}])
|
|
|
|
function show(event, value, total, loaded) {
|
|
var now = new Date(); can.page.Appends(can, ui.output, [
|
|
{view: ["progress"], style: {height: "10px", border: "solid 2px red"}, list: [{
|
|
view: ["progress"], style: {height: "10px", width: value + "%", background: "red"},
|
|
}]},
|
|
{text: [value+"%", "div"], style: {"float": "right"}},
|
|
{text: [can.base.Duration(now - begin), "div"], style: {"float": "left"}},
|
|
{text: [can.base.Size(loaded)+"/"+can.base.Size(total), "div"], style: {"text-align": "center"}},
|
|
])
|
|
}
|
|
|
|
var action = can.page.AppendAction(can, ui.action, [
|
|
{type: "input", data: {name: "upload", type: "file", onchange: function(event) {
|
|
var file = action.upload.files[0]
|
|
show(event, 0, file.size, 0)
|
|
}}, style: {width: "200px"}}, "上传", "关闭"], function(event, value, cmd) {
|
|
if (value == "关闭") { return can.page.Remove(can, ui.first) }
|
|
if (action.upload.files.length == 0) {return action.upload.focus()}
|
|
|
|
var msg = can.request(event)
|
|
can.core.Item(can.Option(), msg.Option)
|
|
|
|
// 上传文件
|
|
begin = new Date()
|
|
msg._progress = show
|
|
msg.upload = action.upload.files[0]
|
|
can.run(event, ["action", "upload"], function(msg) {
|
|
can.user.toast(can, "上传成功")
|
|
}, true)
|
|
})
|
|
action.upload.click()
|
|
return ui
|
|
},
|
|
|
|
Share: shy("共享链接", function(can, objs, clear) {var obj = objs || {}; var path = location.pathname;
|
|
obj._path && (path = obj._path, delete(obj._path))
|
|
!clear && can.core.Item(can.user.Search(), function(key, value) {obj[key] || (obj[key] = value)});
|
|
return can.core.List([location.origin+path, can.core.Item(obj, function(key, value) {
|
|
return can.core.List(value, function(value) {return key+"="+encodeURIComponent(value)}).join("&");
|
|
}).join("&")], function(item) { return item? item: undefined }).join("?")
|
|
}),
|
|
Search: shy("请求参数", function(can, key, value) {var args = {}
|
|
location.search && location.search.slice(1).split("&").forEach(function(item) {var x = item.split("=")
|
|
x[1] != "" && (args[x[0]] = decodeURIComponent(x[1]))
|
|
})
|
|
|
|
if (typeof key == "object") {
|
|
can.core.Item(key, function(key, value) {
|
|
if (value != undefined) {args[key] = value}
|
|
args[key] == "" && delete(args[key])
|
|
})
|
|
} else if (key == undefined) {
|
|
return args
|
|
} else if (value == undefined) {
|
|
// return args[key] || can.user.Cookie(can, key)
|
|
return args[key]||can.user.locals(can, key)
|
|
} else {
|
|
args[key] = value
|
|
args[key] == "" && delete(args[key])
|
|
}
|
|
|
|
return location.search = can.core.Item(args, function(key, value) {
|
|
return key+"="+encodeURIComponent(value)
|
|
}).join("&")
|
|
}),
|
|
Searchs: shy("请求参数", function(can, key, def) {
|
|
return can.user.Search(can, "active") == can._name? can.user.Search(can, key): def
|
|
}),
|
|
Cookie: shy("会话变量", function(can, key, value, path) {
|
|
function set(k, v) {document.cookie = k+"="+v+";path="+(path||"/")}
|
|
|
|
if (typeof key == "object") {
|
|
for (var k in key) {set(k, key[k])}
|
|
key = null
|
|
}
|
|
if (key == undefined) {var cs = {}
|
|
document.cookie.split("; ").forEach(function(item) {
|
|
var cookie = item.split("=")
|
|
cs[cookie[0]] = cookie[1]
|
|
})
|
|
return cs
|
|
}
|
|
|
|
value != undefined && set(key, value)
|
|
var result = (new RegExp(key+"=([^;]*);?")).exec(document.cookie)
|
|
return result && result.length > 0? result[1]: ""
|
|
}),
|
|
locals: function(can, key, value) {
|
|
if (typeof key == "object") {
|
|
can.core.Item(key, function(key, value) {
|
|
localStorage.setItem(key, value)
|
|
})
|
|
return key
|
|
}
|
|
if (value != undefined) {
|
|
localStorage.setItem(key, value)
|
|
}
|
|
if (key != undefined) {
|
|
return localStorage.getItem(key)
|
|
}
|
|
},
|
|
|
|
isWeiXin: navigator.userAgent.indexOf("MicroMessenger") > -1,
|
|
isMobile: navigator.userAgent.indexOf("Mobile") > -1,
|
|
isIPhone: navigator.userAgent.indexOf("iPhone") > -1,
|
|
isMacOSX: navigator.userAgent.indexOf("Mac OS X") > -1,
|
|
isWindows: navigator.userAgent.indexOf("Windows") > -1,
|
|
})
|
|
|