agent-edit-page.js.coffee 7.7 KB

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