Nessuna descrizione http://j1x-huginn.herokuapp.com

agent-edit-page.js.coffee 7.4KB

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