agent-edit-page.js.coffee 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. class @AgentEditPage
  2. constructor: ->
  3. $("#agent_source_ids").on "change", @showEventDescriptions
  4. @showCorrectRegionsOnStartup()
  5. # The type selector is only available on the new agent form.
  6. if $("#agent_type").length
  7. $("#agent_type").on "change", => @handleTypeChange(false)
  8. @handleTypeChange(true)
  9. handleTypeChange: (firstTime) ->
  10. $(".event-descriptions").html("").hide()
  11. type = $('#agent_type').val()
  12. if type == 'Agent'
  13. $(".agent-settings").hide()
  14. $(".description").hide()
  15. else
  16. $(".agent-settings").show()
  17. $("#agent-spinner").fadeIn()
  18. $("#agent_source_ids").select2("val", {})
  19. $(".model-errors").hide() unless firstTime
  20. $.getJSON "/agents/type_details", { type: type }, (json) =>
  21. if json.can_be_scheduled
  22. if firstTime
  23. @showSchedule()
  24. else
  25. @showSchedule(json.default_schedule)
  26. else
  27. @hideSchedule()
  28. if json.can_receive_events
  29. @showLinks()
  30. else
  31. @hideLinks()
  32. if json.can_control_other_agents
  33. @showControlLinks()
  34. else
  35. @hideControlLinks()
  36. if json.can_create_events
  37. @showEventCreation()
  38. else
  39. @hideEventCreation()
  40. $(".description").show().html(json.description_html) if json.description_html?
  41. $('.oauthable-form').html(json.form) if json.form?
  42. unless firstTime
  43. window.jsonEditor.json = json.options
  44. window.jsonEditor.rebuild()
  45. $("#agent-spinner").stop(true, true).fadeOut();
  46. hideSchedule: ->
  47. $(".schedule-region .can-be-scheduled").hide()
  48. $(".schedule-region .cannot-be-scheduled").show()
  49. showSchedule: (defaultSchedule = null) ->
  50. if defaultSchedule?
  51. $(".schedule-region select").val(defaultSchedule).change()
  52. $(".schedule-region .can-be-scheduled").show()
  53. $(".schedule-region .cannot-be-scheduled").hide()
  54. hideLinks: ->
  55. $(".link-region .select2-container").hide()
  56. $(".link-region .propagate-immediately").hide()
  57. $(".link-region .cannot-receive-events").show()
  58. showLinks: ->
  59. $(".link-region .select2-container").show()
  60. $(".link-region .propagate-immediately").show()
  61. $(".link-region .cannot-receive-events").hide()
  62. @showEventDescriptions()
  63. hideControlLinks: ->
  64. $(".control-link-region").hide()
  65. showControlLinks: ->
  66. $(".control-link-region").show()
  67. hideEventCreation: ->
  68. $(".event-related-region").hide()
  69. showEventCreation: ->
  70. $(".event-related-region").show()
  71. showEventDescriptions: ->
  72. if $("#agent_source_ids").val()
  73. $.getJSON "/agents/event_descriptions", { ids: $("#agent_source_ids").val().join(",") }, (json) =>
  74. if json.description_html?
  75. $(".event-descriptions").show().html(json.description_html)
  76. else
  77. $(".event-descriptions").hide()
  78. else
  79. $(".event-descriptions").html("").hide()
  80. showCorrectRegionsOnStartup: ->
  81. if $(".schedule-region")
  82. if $(".schedule-region").data("can-be-scheduled") == true
  83. @showSchedule()
  84. else
  85. @hideSchedule()
  86. if $(".link-region")
  87. if $(".link-region").data("can-receive-events") == true
  88. @showLinks()
  89. else
  90. @hideLinks()
  91. if $(".control-link-region")
  92. if $(".control-link-region").data("can-control-other-agents") == true
  93. @showControlLinks()
  94. else
  95. @hideControlLinks()
  96. if $(".event-related-region")
  97. if $(".event-related-region").data("can-create-events") == true
  98. @showEventCreation()
  99. else
  100. @hideEventCreation()
  101. $ ->
  102. Utils.registerPage(AgentEditPage, forPathsMatching: /^agents/)