195 lines
6.3 KiB
HTML
195 lines
6.3 KiB
HTML
|
|
<!DOCTYPE html>
|
||
|
|
<html lang="zh-cmn-Hans">
|
||
|
|
|
||
|
|
<head>
|
||
|
|
<meta charset="UTF-8">
|
||
|
|
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
||
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||
|
|
<title>Settings</title>
|
||
|
|
<script src="../statics/modules/jquery.min.js"></script>
|
||
|
|
<link rel="stylesheet" href="../statics/github.css">
|
||
|
|
<link rel="stylesheet" href="../statics/theme.css">
|
||
|
|
<style>
|
||
|
|
* {
|
||
|
|
padding: 0;
|
||
|
|
margin: 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
textarea {
|
||
|
|
width: 85vw;
|
||
|
|
height: 70vh;
|
||
|
|
padding: 2vw;
|
||
|
|
margin: 3vw;
|
||
|
|
font-size: medium;
|
||
|
|
line-height: 1.2;
|
||
|
|
border: 1px solid #cbcbcb;
|
||
|
|
border-radius: 5px;
|
||
|
|
}
|
||
|
|
|
||
|
|
</style>
|
||
|
|
<script type="module">
|
||
|
|
import { IO } from "../statics/modules/tools.js"
|
||
|
|
const io = new IO("settings")
|
||
|
|
|
||
|
|
$(() => {
|
||
|
|
const textarea = $("#textarea")
|
||
|
|
|
||
|
|
// 写入默认设置
|
||
|
|
if (readSettings().length == 0) {
|
||
|
|
writeDefaultSettings(() => {
|
||
|
|
$("#refresh").click()
|
||
|
|
})
|
||
|
|
}
|
||
|
|
|
||
|
|
$("#reset").click(() => {
|
||
|
|
if (confirm("确定要恢复默认设置吗?\n该选项将会覆盖当前已保存的设置")) {
|
||
|
|
writeDefaultSettings(() => {
|
||
|
|
$("#refresh").click()
|
||
|
|
})
|
||
|
|
}
|
||
|
|
})
|
||
|
|
|
||
|
|
$("#go_index").click(() => {
|
||
|
|
window.location.href = "../index.html"
|
||
|
|
})
|
||
|
|
|
||
|
|
$("#refresh").click(() => {
|
||
|
|
let settings = readSettingsWithComments()
|
||
|
|
let string = ""
|
||
|
|
settings.forEach(item => {
|
||
|
|
string = string.concat(item).concat("\n")
|
||
|
|
})
|
||
|
|
textarea.val(string)
|
||
|
|
})
|
||
|
|
$("#refresh").click()
|
||
|
|
|
||
|
|
$("#save").click(() => {
|
||
|
|
let text = $("#textarea").val()
|
||
|
|
let settings = text.split("\n")
|
||
|
|
writeSettings(settings)
|
||
|
|
})
|
||
|
|
|
||
|
|
})
|
||
|
|
|
||
|
|
function readSettingsWithComments() {
|
||
|
|
let texts = []
|
||
|
|
texts.push("# 以井号开头的行是注释,注释将被忽略。")
|
||
|
|
texts.push("# 一行仅可填写一个设置,以键值对 (key=value) 的形式出现。")
|
||
|
|
texts.push("# 等号左边为设置的唯一标识,右边为该设置的值")
|
||
|
|
texts.push("# 若值有多个,使用英文逗号分隔 (e.g. key=value1,value2)")
|
||
|
|
texts.push("# 等号和用于分隔多个值的英文逗号左右两边不得有空格")
|
||
|
|
texts.push("")
|
||
|
|
for (const setting of readSettings()) {
|
||
|
|
let comment = io.read(`${setting.key}.desc`)
|
||
|
|
texts.push(`# ${comment}`)
|
||
|
|
texts.push(`${setting.key}=${setting.value}`)
|
||
|
|
texts.push("")
|
||
|
|
}
|
||
|
|
return texts
|
||
|
|
}
|
||
|
|
|
||
|
|
function readSettings() {
|
||
|
|
let settings = []
|
||
|
|
for (const iterator of io.listKeys().sort()) {
|
||
|
|
if (!iterator.endsWith(".desc")) {
|
||
|
|
settings.push({
|
||
|
|
key: iterator,
|
||
|
|
value: io.read(iterator)
|
||
|
|
})
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return settings
|
||
|
|
}
|
||
|
|
|
||
|
|
function writeSettings(settings) {
|
||
|
|
if (settings.length == 0) {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
io.listKeys().forEach(key => {
|
||
|
|
if (!key.endsWith(".desc")) {
|
||
|
|
io.remove(key)
|
||
|
|
}
|
||
|
|
})
|
||
|
|
|
||
|
|
settings.forEach(item => {
|
||
|
|
if (item.startsWith("#")) {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
let key = item.split('=')[0]
|
||
|
|
let value = item.split('=')[1]
|
||
|
|
if (key != item && key != '') {
|
||
|
|
io.write(key, value)
|
||
|
|
}
|
||
|
|
})
|
||
|
|
}
|
||
|
|
|
||
|
|
function writeDefaultSettings(action) {
|
||
|
|
io.listKeys().forEach(key => {
|
||
|
|
io.remove(key)
|
||
|
|
})
|
||
|
|
|
||
|
|
$.getJSON("../statics/settings.json", (settings) => {
|
||
|
|
for (const iterator of settings) {
|
||
|
|
io.write(iterator.id, iterator.default)
|
||
|
|
io.write(`${iterator.id}.desc`, iterator.description)
|
||
|
|
}
|
||
|
|
|
||
|
|
action()
|
||
|
|
})
|
||
|
|
}
|
||
|
|
|
||
|
|
function createItem(data) {
|
||
|
|
let div = document.createElement("div")
|
||
|
|
div.setAttribute("class", "setting_item")
|
||
|
|
|
||
|
|
let span = document.createElement("span")
|
||
|
|
let span_title = document.createElement("span")
|
||
|
|
let span_description = document.createElement("span")
|
||
|
|
span_title.setAttribute("class", "title")
|
||
|
|
span_description.setAttribute("class", "description")
|
||
|
|
span_description.innerHTML = data.description
|
||
|
|
span.appendChild(span_title)
|
||
|
|
span.appendChild(document.createElement("br"))
|
||
|
|
span.appendChild(span_description)
|
||
|
|
div.appendChild(span)
|
||
|
|
switch (data.type) {
|
||
|
|
case 'boolean':
|
||
|
|
span_title.innerHTML = `${data.title} (${data.default ? "ON" : "OFF"})`
|
||
|
|
let button = document.createElement("button")
|
||
|
|
button.innerText = data.default ? "OFF" : "ON"
|
||
|
|
button.setAttribute("id", data.id)
|
||
|
|
div.appendChild(button)
|
||
|
|
break
|
||
|
|
case 'number':
|
||
|
|
span_title.innerHTML = `${data.title} (${data.default})`
|
||
|
|
let input = document.createElement("input")
|
||
|
|
input.setAttribute("type", "number")
|
||
|
|
input.setAttribute("value", data.default)
|
||
|
|
input.setAttribute("id", data.id)
|
||
|
|
div.appendChild(input)
|
||
|
|
break
|
||
|
|
default:
|
||
|
|
break
|
||
|
|
}
|
||
|
|
return div
|
||
|
|
}
|
||
|
|
|
||
|
|
</script>
|
||
|
|
</head>
|
||
|
|
|
||
|
|
<body>
|
||
|
|
<textarea id="textarea" wrap="off" autocomplete="off"></textarea>
|
||
|
|
|
||
|
|
<br>
|
||
|
|
<div id="buttons" style="text-align: center;">
|
||
|
|
<button id="reset">恢复默认设置</button>
|
||
|
|
<button id="go_index">回到首页</button>
|
||
|
|
<button id="refresh">刷新</button>
|
||
|
|
<button id="save">保存</button>
|
||
|
|
</div>
|
||
|
|
<div id="msg"></div>
|
||
|
|
</body>
|
||
|
|
|
||
|
|
</html>
|