UiConfig.coffee 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. window.h = maquette.h
  2. class UiConfig extends ZeroFrame
  3. init: ->
  4. @save_visible = true
  5. @config = null # Setting currently set on the server
  6. @values = null # Entered values on the page
  7. @config_view = new ConfigView()
  8. window.onbeforeunload = =>
  9. if @getValuesChanged().length > 0
  10. return true
  11. else
  12. return null
  13. onOpenWebsocket: =>
  14. @cmd("wrapperSetTitle", "Config - ZeroNet")
  15. @restart_loading = false
  16. @updateConfig()
  17. updateConfig: (cb) =>
  18. @cmd "configList", [], (res) =>
  19. @config = res
  20. @values = {}
  21. @config_storage = new ConfigStorage(@config)
  22. @config_view.values = @values
  23. @config_view.config_storage = @config_storage
  24. for key, item of res
  25. value = item.value
  26. @values[key] = @config_storage.formatValue(value)
  27. @projector.scheduleRender()
  28. cb?()
  29. createProjector: =>
  30. @projector = maquette.createProjector()
  31. @projector.replace($("#content"), @render)
  32. @projector.replace($("#bottom-save"), @renderBottomSave)
  33. @projector.replace($("#bottom-restart"), @renderBottomRestart)
  34. getValuesChanged: =>
  35. values_changed = []
  36. for key, value of @values
  37. if @config_storage.formatValue(value) != @config_storage.formatValue(@config[key]?.value)
  38. values_changed.push({key: key, value: value})
  39. return values_changed
  40. getValuesPending: =>
  41. values_pending = []
  42. for key, item of @config
  43. if item.pending
  44. values_pending.push(key)
  45. return values_pending
  46. saveValues: (cb) =>
  47. changed_values = @getValuesChanged()
  48. for item, i in changed_values
  49. last = i == changed_values.length - 1
  50. value = @config_storage.deformatValue(item.value, typeof(@config[item.key].default))
  51. value_same_as_default = JSON.stringify(@config[item.key].default) == JSON.stringify(value)
  52. if value_same_as_default
  53. value = null
  54. if @config[item.key].item.valid_pattern and value
  55. match = value.match(@config[item.key].item.valid_pattern)
  56. if not match or match[0] != value
  57. message = "Invalid value of #{@config[item.key].item.title}: #{value} (does not matches #{@config[item.key].item.valid_pattern})"
  58. Page.cmd("wrapperNotification", ["error", message])
  59. cb(false)
  60. break
  61. @saveValue(item.key, value, if last then cb else null)
  62. saveValue: (key, value, cb) =>
  63. if key == "open_browser"
  64. if value
  65. value = "default_browser"
  66. else
  67. value = "False"
  68. Page.cmd "configSet", [key, value], (res) =>
  69. if res != "ok"
  70. Page.cmd "wrapperNotification", ["error", res.error]
  71. cb?(true)
  72. render: =>
  73. if not @config
  74. return h("div.content")
  75. h("div.content", [
  76. @config_view.render()
  77. ])
  78. handleSaveClick: =>
  79. @save_loading = true
  80. @logStart "Save"
  81. @saveValues (success) =>
  82. @save_loading = false
  83. @logEnd "Save"
  84. if success
  85. @updateConfig()
  86. Page.projector.scheduleRender()
  87. return false
  88. renderBottomSave: =>
  89. values_changed = @getValuesChanged()
  90. h("div.bottom.bottom-save", {classes: {visible: values_changed.length}}, h("div.bottom-content", [
  91. h("div.title", "#{values_changed.length} configuration item value changed"),
  92. h("a.button.button-submit.button-save", {href: "#Save", classes: {loading: @save_loading}, onclick: @handleSaveClick}, "Save settings")
  93. ]))
  94. handleRestartClick: =>
  95. @restart_loading = true
  96. Page.cmd("serverShutdown", {restart: true})
  97. Page.projector.scheduleRender()
  98. return false
  99. renderBottomRestart: =>
  100. values_pending = @getValuesPending()
  101. values_changed = @getValuesChanged()
  102. h("div.bottom.bottom-restart", {classes: {visible: values_pending.length and not values_changed.length}}, h("div.bottom-content", [
  103. h("div.title", "Some changed settings requires restart"),
  104. h("a.button.button-submit.button-restart", {href: "#Restart", classes: {loading: @restart_loading}, onclick: @handleRestartClick}, "Restart ZeroNet client")
  105. ]))
  106. window.Page = new UiConfig()
  107. window.Page.createProjector()