ConfigView.coffee 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. class ConfigView extends Class
  2. constructor: () ->
  3. @
  4. render: ->
  5. @config_storage.items.map @renderSection
  6. renderSection: (section) =>
  7. h("div.section", {key: section.title}, [
  8. h("h2", section.title),
  9. h("div.config-items", section.items.map @renderSectionItem)
  10. ])
  11. handleResetClick: (e) =>
  12. node = e.currentTarget
  13. config_key = node.attributes.config_key.value
  14. default_value = node.attributes.default_value?.value
  15. Page.cmd "wrapperConfirm", ["Reset #{config_key} value?", "Reset to default"], (res) =>
  16. if (res)
  17. @values[config_key] = default_value
  18. Page.projector.scheduleRender()
  19. renderSectionItem: (item) =>
  20. value_pos = item.value_pos
  21. if item.type == "textarea"
  22. value_pos ?= "fullwidth"
  23. else
  24. value_pos ?= "right"
  25. value_changed = @config_storage.formatValue(@values[item.key]) != item.value
  26. value_default = @config_storage.formatValue(@values[item.key]) == item.default
  27. if item.key in ["open_browser", "fileserver_port"] # Value default for some settings makes no sense
  28. value_default = true
  29. marker_title = "Changed from default value: #{item.default} -> #{@values[item.key]}"
  30. if item.pending
  31. marker_title += " (change pending until client restart)"
  32. if item.isHidden?()
  33. return null
  34. h("div.config-item", {key: item.title, enterAnimation: Animation.slideDown, exitAnimation: Animation.slideUpInout}, [
  35. h("div.title", [
  36. h("h3", item.title),
  37. h("div.description", item.description)
  38. ])
  39. h("div.value.value-#{value_pos}",
  40. if item.type == "select"
  41. @renderValueSelect(item)
  42. else if item.type == "checkbox"
  43. @renderValueCheckbox(item)
  44. else if item.type == "textarea"
  45. @renderValueTextarea(item)
  46. else
  47. @renderValueText(item)
  48. h("a.marker", {
  49. href: "#Reset", title: marker_title,
  50. onclick: @handleResetClick, config_key: item.key, default_value: item.default,
  51. classes: {default: value_default, changed: value_changed, visible: not value_default or value_changed or item.pending, pending: item.pending}
  52. }, "\u2022")
  53. )
  54. ])
  55. # Values
  56. handleInputChange: (e) =>
  57. node = e.target
  58. config_key = node.attributes.config_key.value
  59. @values[config_key] = node.value
  60. Page.projector.scheduleRender()
  61. handleCheckboxChange: (e) =>
  62. node = e.currentTarget
  63. config_key = node.attributes.config_key.value
  64. value = not node.classList.contains("checked")
  65. @values[config_key] = value
  66. Page.projector.scheduleRender()
  67. renderValueText: (item) =>
  68. value = @values[item.key]
  69. if not value
  70. value = ""
  71. h("input.input-#{item.type}", {type: item.type, config_key: item.key, value: value, placeholder: item.placeholder, oninput: @handleInputChange})
  72. autosizeTextarea: (e) =>
  73. if e.currentTarget
  74. # @handleInputChange(e)
  75. node = e.currentTarget
  76. else
  77. node = e
  78. height_before = node.style.height
  79. if height_before
  80. node.style.height = "0px"
  81. h = node.offsetHeight
  82. scrollh = node.scrollHeight + 20
  83. if scrollh > h
  84. node.style.height = scrollh + "px"
  85. else
  86. node.style.height = height_before
  87. renderValueTextarea: (item) =>
  88. value = @values[item.key]
  89. if not value
  90. value = ""
  91. h("textarea.input-#{item.type}.input-text",{
  92. type: item.type, config_key: item.key, oninput: @handleInputChange, afterCreate: @autosizeTextarea, updateAnimation: @autosizeTextarea, value: value
  93. })
  94. renderValueCheckbox: (item) =>
  95. if @values[item.key] and @values[item.key] != "False"
  96. checked = true
  97. else
  98. checked = false
  99. h("div.checkbox", {onclick: @handleCheckboxChange, config_key: item.key, classes: {checked: checked}}, h("div.checkbox-skin"))
  100. renderValueSelect: (item) =>
  101. h("select.input-select", {config_key: item.key, oninput: @handleInputChange},
  102. item.options.map (option) =>
  103. h("option", {selected: option.value == @values[item.key], value: option.value}, option.title)
  104. )
  105. window.ConfigView = ConfigView