0200-domain-select.lua 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. return function(form, uci)
  2. local fs = require 'nixio.fs'
  3. local json = require 'jsonc'
  4. local site = require 'gluon.site'
  5. local selected_domain = uci:get('gluon', 'core', 'domain')
  6. local configured = uci:get_first('gluon-setup-mode','setup_mode', 'configured') == '1' or (selected_domain ~= site.default_domain())
  7. local function get_domain_list()
  8. local list = {}
  9. for domain_path in fs.glob('/lib/gluon/domains/*.json') do
  10. local domain_code = domain_path:match('([^/]+)%.json$')
  11. local domain = assert(json.load(domain_path))
  12. if not domain.hide_domain or (configured and domain.domain_code == selected_domain) then
  13. table.insert(list, {
  14. domain_code = domain_code,
  15. domain_name = domain.domain_names[domain_code],
  16. })
  17. end
  18. end
  19. table.sort(list, function(a, b) return a.domain_name < b.domain_name end)
  20. return list
  21. end
  22. local s = form:section(Section, nil, translate('gluon-config-mode:domain-select'))
  23. local o = s:option(ListValue, 'domain', translate('gluon-config-mode:domain'))
  24. if configured then
  25. o.default = selected_domain
  26. end
  27. for _, domain in ipairs(get_domain_list()) do
  28. o:value(domain.domain_code, domain.domain_name)
  29. end
  30. local domain_changed = false
  31. function o:write(data)
  32. if data ~= selected_domain then
  33. domain_changed = true
  34. uci:set('gluon', 'core', 'domain', data)
  35. end
  36. end
  37. local function reconfigure()
  38. if domain_changed then
  39. os.execute('gluon-reconfigure')
  40. end
  41. end
  42. return {'gluon', reconfigure}
  43. end