agent-edit-page.js.coffee 6.0 KB

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