agent-edit-page.js.coffee 6.3KB

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