1
0

agent-edit-page.js.coffee 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. class @AgentEditPage
  2. constructor: ->
  3. $("#agent_source_ids").on "change", @showEventDescriptions
  4. @showCorrectRegionsOnStartup()
  5. $("form.agent-form").on "submit", => @updateFromEditors()
  6. $("#agent_name").each ->
  7. # Select the number suffix if this is a cloned agent.
  8. if matches = this.value.match(/ \(\d+\)$/)
  9. this.focus()
  10. if this.selectionStart?
  11. this.selectionStart = matches.index
  12. this.selectionEnd = this.value.length
  13. # The type selector is only available on the new agent form.
  14. if $("#agent_type").length
  15. $("#agent_type").on "change", => @handleTypeChange(false)
  16. @handleTypeChange(true)
  17. else
  18. @enableDryRunButton()
  19. @buildAce()
  20. handleTypeChange: (firstTime) ->
  21. $(".event-descriptions").html("").hide()
  22. type = $('#agent_type').val()
  23. if type == 'Agent'
  24. $(".agent-settings").hide()
  25. $(".description").hide()
  26. else
  27. $(".agent-settings").show()
  28. $("#agent-spinner").fadeIn()
  29. $(".model-errors").hide() unless firstTime
  30. $.getJSON "/agents/type_details", { type: type }, (json) =>
  31. if json.can_be_scheduled
  32. if firstTime
  33. @showSchedule()
  34. else
  35. @showSchedule(json.default_schedule)
  36. else
  37. @hideSchedule()
  38. if json.can_receive_events
  39. @showLinks()
  40. else
  41. @hideLinks()
  42. if json.can_control_other_agents
  43. @showControlLinks()
  44. else
  45. @hideControlLinks()
  46. if json.can_create_events
  47. @showEventCreation()
  48. else
  49. @hideEventCreation()
  50. $(".description").show().html(json.description_html) if json.description_html?
  51. unless firstTime
  52. $('.oauthable-form').html(json.oauthable) if json.oauthable?
  53. $('.agent-options').html(json.form_options) if json.form_options?
  54. window.jsonEditor = setupJsonEditor()[0]
  55. @enableDryRunButton()
  56. @buildAce()
  57. window.initializeFormCompletable()
  58. $("#agent-spinner").stop(true, true).fadeOut();
  59. hideSchedule: ->
  60. $(".schedule-region .can-be-scheduled").hide()
  61. $(".schedule-region .cannot-be-scheduled").show()
  62. showSchedule: (defaultSchedule = null) ->
  63. if defaultSchedule?
  64. $(".schedule-region select").val(defaultSchedule).change()
  65. $(".schedule-region .can-be-scheduled").show()
  66. $(".schedule-region .cannot-be-scheduled").hide()
  67. hideLinks: ->
  68. $(".link-region .select2-container").hide()
  69. $(".link-region .propagate-immediately").hide()
  70. $(".link-region .cannot-receive-events").show()
  71. showLinks: ->
  72. $(".link-region .select2-container").show()
  73. $(".link-region .propagate-immediately").show()
  74. $(".link-region .cannot-receive-events").hide()
  75. @showEventDescriptions()
  76. hideControlLinks: ->
  77. $(".control-link-region").hide()
  78. showControlLinks: ->
  79. $(".control-link-region").show()
  80. hideEventCreation: ->
  81. $(".event-related-region").hide()
  82. showEventCreation: ->
  83. $(".event-related-region").show()
  84. showEventDescriptions: ->
  85. if $("#agent_source_ids").val()
  86. $.getJSON "/agents/event_descriptions", { ids: $("#agent_source_ids").val().join(",") }, (json) =>
  87. if json.description_html?
  88. $(".event-descriptions").show().html(json.description_html)
  89. else
  90. $(".event-descriptions").hide()
  91. else
  92. $(".event-descriptions").html("").hide()
  93. showCorrectRegionsOnStartup: ->
  94. if $(".schedule-region")
  95. if $(".schedule-region").data("can-be-scheduled") == true
  96. @showSchedule()
  97. else
  98. @hideSchedule()
  99. if $(".link-region")
  100. if $(".link-region").data("can-receive-events") == true
  101. @showLinks()
  102. else
  103. @hideLinks()
  104. if $(".control-link-region")
  105. if $(".control-link-region").data("can-control-other-agents") == true
  106. @showControlLinks()
  107. else
  108. @hideControlLinks()
  109. if $(".event-related-region")
  110. if $(".event-related-region").data("can-create-events") == true
  111. @showEventCreation()
  112. else
  113. @hideEventCreation()
  114. buildAce: ->
  115. $(".ace-editor").each ->
  116. unless $(this).data('initialized')
  117. $(this).data('initialized', true)
  118. $source = $($(this).data('source')).hide()
  119. editor = ace.edit(this)
  120. $(this).data('ace-editor', editor)
  121. session = editor.getSession()
  122. session.setTabSize(2)
  123. session.setUseSoftTabs(true)
  124. session.setUseWrapMode(false)
  125. editor.setTheme("ace/theme/chrome")
  126. setSyntax = ->
  127. switch $("[name='agent[options][language]']").val()
  128. when 'JavaScript' then session.setMode("ace/mode/javascript")
  129. when 'CoffeeScript' then session.setMode("ace/mode/coffee")
  130. else session.setMode("ace/mode/text")
  131. $("[name='agent[options][language]']").on 'change', setSyntax
  132. setSyntax()
  133. session.setValue($source.val())
  134. updateFromEditors: ->
  135. $(".ace-editor").each ->
  136. $source = $($(this).data('source'))
  137. $source.val($(this).data('ace-editor').getSession().getValue())
  138. enableDryRunButton: ->
  139. $(".agent-dry-run-button").prop('disabled', false).off().on "click", @invokeDryRun
  140. disableDryRunButton: ->
  141. $(".agent-dry-run-button").prop('disabled', true)
  142. invokeDryRun: (e) =>
  143. e.preventDefault()
  144. @updateFromEditors()
  145. Utils.handleDryRunButton(e.target)
  146. $ ->
  147. Utils.registerPage(AgentEditPage, forPathsMatching: /^agents/)