1
0
forked from x/volcanos

add user.agent

This commit is contained in:
shylinux 2020-10-23 11:23:12 +08:00
parent 0a35665fc4
commit 190aa052a7
7 changed files with 89 additions and 15 deletions

View File

@ -343,7 +343,7 @@ Volcanos("onappend", {help: "渲染引擎", list: [], _init: function(can, meta,
typeof cb == "function" && cb(msg) typeof cb == "function" && cb(msg)
if (silent) { return } if (silent) { return }
var display = (msg.Option("_display")||meta.feature.display||"table.js") var display = meta.feature.display || "table.js"
display.indexOf("/") == 0 || (display = "/plugin/"+display) display.indexOf("/") == 0 || (display = "/plugin/"+display)
display.endsWith(".js") || (display += ".js") display.endsWith(".js") || (display += ".js")

View File

@ -1,9 +1,33 @@
var user = Volcanos("user", {help: "用户模块", var user = Volcanos("user", {help: "用户模块", agent: {
getLocation: function(cb) {
typeof cb == "function" && cb({name: "some"})
},
openLocation: function(msg) {
window.open("https://map.baidu.com/search/"+encodeURIComponent(msg.Option("text"))+"/@12958750.085,4825785.55,16z?querytype=s&da_src=shareurl&wd="+encodeURIComponent(msg.Option("text")))
},
scanQRCode: function(cb) {
typeof cb == "function" && cb({name: "some"})
},
chooseImage: function(cb) {
typeof cb == "function" && cb([])
},
},
alert: function(text) {alert(JSON.stringify(text))}, alert: function(text) {alert(JSON.stringify(text))},
confirm: function(text) {return confirm(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}, 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()}, reload: function(force) {(force || confirm("重新加载页面?")) && location.reload()},
title: function(text) {return text && (document.title = text), document.title}, title: function(text) {return text && (document.title = text), document.title},
scan: function(str) {
try { var value = JSON.parse(str) } catch(e) { try {
var value = {"type": "url", "text": str}
var ls = str.split("?"); if (ls.length > 1) { ls = ls[1].split("&")
for (var i = 0; i < ls.length; i++) { var vs = ls[i].split("=")
value[vs[0]] = decodeURIComponent(vs[1])
}
}
} catch(e) { } }
return value
},
copy: function(can, text) { copy: function(can, text) {
var input = can.page.Append(can, document.body, [{type: "input", value: text}]).first var input = can.page.Append(can, document.body, [{type: "input", value: text}]).first

View File

@ -1,4 +1,30 @@
Volcanos("onimport", {help: "导入数据", list: [], _init: function(can, meta, list, cb, target) { Volcanos("onimport", {help: "导入数据", list: [], _init: function(can, meta, list, cb, target) {
can.user.isWeiXin && can.onimport.weixin(can)
},
weixin: function(can) { can.run({}, ["action", "wx"], function(msg) {
can.require(["https://res.wx.qq.com/open/js/jweixin-1.6.0.js"], function(can) {
can.user.agent = { __proto__: can.user.agent,
getLocation: function(cb) { wx.getLocation({type: "gcj02", success: function (res) {
typeof cb == "function" && cb({latitude: parseInt(res.latitude*100000), longitude: parseInt(res.longitude*100000) })
} }) },
openLocation: function(msg) { wx.openLocation({
latitude: parseInt(msg.Option("latitude"))/100000,
longitude: parseInt(msg.Option("longitude"))/100000,
name: msg.Option("name"), address: msg.Option("text"),
scale: msg.Option("scale")||14, infoUrl: msg.Option("link"),
}) },
scanQRCode: function(cb) { wx.scanQRCode({ needResult: cb? 1: 0, scanType: ["qrCode","barCode"], success: function (res) {
typeof cb == "function" && cb(can.user.scan(res.resultStr))
} }) },
chooseImage: function(cb, count) { wx.chooseImage({count: count||9, sizeType: ['original', 'compressed'], sourceType: ['album', 'camera'], success: function (res) {
typeof cb == "function" && cb(res.localIds)
} }) },
}
wx.config({debug: msg.Option("debug") == "true", jsApiList: can.core.Item(can.user.agent),
nonceStr: msg.Option("noncestr"), timestamp: msg.Option("timestamp"),
appId: msg.Option("appid"), signature: msg.Option("signature"),
})
}) })
}, },
}) })
Volcanos("onaction", {help: "交互数据", list: [], _init: function(can, msg, list, cb, target) { Volcanos("onaction", {help: "交互数据", list: [], _init: function(can, msg, list, cb, target) {

View File

@ -20,14 +20,14 @@ Volcanos("onaction", {help: "控件交互", list: [],
if (event.target.tagName == "SELECT") { can.run(event) } if (event.target.tagName == "SELECT") { can.run(event) }
}, },
ondblclick: function(event, can) { ondblclick: function(event, can) {
if (can.Conf("type") == "textarea") { event.target.setSelectionRange(0, -1) }
if (can.Conf("type") == "text") { event.target.setSelectionRange(0, -1) } if (can.Conf("type") == "text") { event.target.setSelectionRange(0, -1) }
if (can.Conf("type") == "textarea") { event.target.setSelectionRange(0, -1) }
}, },
onclick: function(event, can) { var msg = can.sup.request(event) onclick: function(event, can) { var msg = can.sup.request(event)
// 插件回调 // 插件回调
var name = can.Conf("name") var name = can.Conf("name"), action = can.Conf("action")
var sub = can.sup._outputs && can.sup._outputs[can.sup._outputs.length-1] var sub = can.sup._outputs && can.sup._outputs[can.sup._outputs.length-1]
var cb = sub && sub.onaction && sub.onaction[name] var cb = sub && sub.onaction && (sub.onaction[action] || sub.onaction[name])
if (typeof cb == "function") { return cb(event, sub, name) } if (typeof cb == "function") { return cb(event, sub, name) }
// 交互回调 // 交互回调
@ -39,7 +39,7 @@ Volcanos("onaction", {help: "控件交互", list: [],
} }
// 控件回调 // 控件回调
var cb = can.onaction[name] var cb = can.onaction[action] || can.onaction[name]
if (typeof cb == "function") { return cb(event, can, name) } if (typeof cb == "function") { return cb(event, can, name) }
// 通用回调 // 通用回调

View File

@ -84,6 +84,11 @@ Volcanos("onaction", {help: "组件菜单", list: [
["高度", 100, 200, 400, 600, 800], ["高度", 100, 200, 400, 600, 800],
["倍速", 0.1, 0.2, 0.5, 1, 2, 3, 5, 10], ["倍速", 0.1, 0.2, 0.5, 1, 2, 3, 5, 10],
], ],
chooseImage: function(event, can) { var msg = can.request(event)
can.user.agent.chooseImage(function(list) { can.core.List(list, function(item) {
can.page.Append(can, can._output, [{img: item, height: 200}])
}) })
},
upload: function(event, can) { can.user.upload(event, can) }, upload: function(event, can) { can.user.upload(event, can) },
"上一页": function(event, can, key, value) { "上一页": function(event, can, key, value) {
can.begin > 0 && (can.begin -= can.limit, can.onimport._page(can, can.list, can.begin, can.limit)) can.begin > 0 && (can.begin -= can.limit, can.onimport._page(can, can.list, can.begin, can.limit))

View File

@ -1,6 +1,12 @@
Volcanos("onimport", {help: "导入数据", list: [], _init: function(can, msg, list, cb, target) { target.innerHTML = "" Volcanos("onimport", {help: "导入数据", list: [], _init: function(can, msg, list, cb, target) { target.innerHTML = ""
if (msg.Option("_display") == "table") { if (msg.Option("_display") == "table") {
return can.onappend.table(can, msg, target, "table") return can.onappend.table(can, msg, target, "table", function(value, key) {
return {text: [value, "td"], onclick: function(event) {
can.sup.onaction.change(event, can.sup, key, value, function(msg) {
can.run(event)
})
}}
})
} target.innerHTML = msg.Result() } target.innerHTML = msg.Result()
can.page.Select(can, target, ".story", function(item) { var data = item.dataset can.page.Select(can, target, ".story", function(item) { var data = item.dataset

View File

@ -94,14 +94,8 @@ Volcanos("onimport", {help: "导入数据", list: [], _init: function(can, msg,
}, },
_table: function(can, value, key, index, line, array) { _table: function(can, value, key, index, line, array) {
return {type: "td", inner: value, click: function(event) { var target = event.target return {type: "td", inner: value, click: function(event) { var target = event.target
if (target.tagName == "INPUT" && target.type == "button") { var msg = can.sup.request(event) if (target.tagName == "INPUT" && target.type == "button") { var msg = can.sup.request(event); msg.Option(can.Option())
msg.Option(can.Option()); if (key == "value") { key == "value"? can.core.List(array, function(item, index) { msg.Option(item.key, item.value) }): msg.Option(line)
can.core.List(array, function(item, index) {
msg.Option(item.key, item.value)
})
} else {
msg.Option(line)
}
var cb = can.onaction[target.name]; return typeof cb == "function"? cb(event, can, target.name): var cb = can.onaction[target.name]; return typeof cb == "function"? cb(event, can, target.name):
can.sup.onaction.input(event, can.sup, target.name, function(msg) { can.sup.onaction.input(event, can.sup, target.name, function(msg) {
@ -206,6 +200,25 @@ Volcanos("onimport", {help: "导入数据", list: [], _init: function(can, msg,
}, },
}) })
Volcanos("onaction", {help: "控件交互", list: [], Volcanos("onaction", {help: "控件交互", list: [],
getLocation: function(event, can, cmd) { var msg = can.request(can)
can.user.agent.getLocation(function(res) {
var arg = []; can.core.Item(res, function(key, value) { arg.push(key, value) })
can.run(event, ["action", cmd].concat(arg))
})
},
openLocation: function(event, can) { var msg = can.request(can)
can.user.agent.openLocation(msg)
},
scanQRCode0: function(event, can) { var msg = can.request(can)
can.user.agent.scanQRCode()
},
scanQRCode: function(event, can, cmd) { var msg = can.request(can)
can.user.agent.scanQRCode(function(res) {
var arg = []; can.core.Item(res, function(key, value) { arg.push(key, value) })
can.run(event, ["action", cmd].concat(arg))
})
},
"清空": function(event, can, name) { can._output.innerHTML = "" }, "清空": function(event, can, name) { can._output.innerHTML = "" },
"结束": function(event, can, name) { can.user.confirm("确定结束?") && can.run(event, ["action", name], function(msg) { "结束": function(event, can, name) { can.user.confirm("确定结束?") && can.run(event, ["action", name], function(msg) {
can.run({}) can.run({})