xrforge-v2/src/page/admin/index.lua

89 lines
2.3 KiB
Lua
Raw Normal View History

2026-07-22 18:36:02 +02:00
local util = require("util")
local data = util.merge({notification=''},{ opts = app.opts })
data.plugins = ""
data.tabs = {}
2026-07-22 18:36:02 +02:00
function pluginInput(opt)
local html = ""
html = html .. [[
<tr>
<td>
]]
html = html .. opt.title
html = html .. [[
</td>
<td>
]]
local value = app.opts[ opt.key:gsub("^app.opts.","") ]
if type(opt.values) == 'table' then
html = html .. util.template('<select name="${key}" value="${value}">\n', {
key = opt.key,
value = value
})
print(opt.key:gsub("^app.opts.","") )
foreach( opt.values, function(k,v)
html = html .. util.template('<option value="${k}">${v}</option>\n',{
k=k,
v=v
})
end)
html = html .. "</select>\n"
2026-07-22 18:36:02 +02:00
else
html = html .. util.template('<input type="text" name="${key}" value="${value}"></input>',{
key = opt.key,
value = value
})
end
html = html .. util.template([[
</td>
<td class="col3">${info}</td>
</td>
</tr>
]],opt)
return html
end
function pluginUI(seen)
return function(name,plugin)
if seen[ plugin.name ] then return end -- already processed
table.insert( data.tabs, plugin.name ) -- insert tabname
seen[ plugin.name ] = true
local plugindata = {plugin=plugin}
plugindata.opts = ""
foreach(plugin.opts, function(k,opt)
plugindata.opts = plugindata.opts .. pluginInput(opt)
end)
data.plugins = data.plugins .. app.tpl( LoadAsset('/page/admin/plugin.html'), plugindata)
end
end
function saveData()
if GetMethod() == 'POST' then
local params = GetParams()
local saveconfig = false
foreach(params, function(k,v)
if v[1]:match("^opts.") then
app.opts[ v[1]:gsub("^opts.","") ] = v[2] -- update
saveconfig = true
end
end)
if saveconfig then
app.set('app.opts', app.opts )
data.notification = '<div class="notification">updated</div>'
else
data.notification = '<div class="notification">no changes detected</div>'
end
2026-07-22 18:36:02 +02:00
end
end
local seen = {}
foreach( app.plugin, pluginUI(seen) )
saveData()
2026-07-22 18:36:02 +02:00
local html = util.template( LoadAsset("page/admin/index.html"),data)
SetStatus(200)
SetHeader('Content-Type', 'text/html; charset=utf-8')
Write( html )