application.js.coffee.erb 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #= require jquery
  2. #= require jquery_ujs
  3. #= require bootstrap
  4. #= require select2
  5. #= require json2
  6. #= require jquery.json-editor
  7. #= require latlon_and_geo
  8. #= require ./worker-checker
  9. #= require_self
  10. window.setupJsonEditor = ($editor = $(".live-json-editor")) ->
  11. JSONEditor.prototype.ADD_IMG = '<%= image_path 'json-editor/add.png' %>'
  12. JSONEditor.prototype.DELETE_IMG = '<%= image_path 'json-editor/delete.png' %>'
  13. if $editor.length
  14. jsonEditor = new JSONEditor($editor, $editor.data('width') || 400, $editor.data('height') || 500)
  15. jsonEditor.doTruncation true
  16. jsonEditor.showFunctionButtons()
  17. return jsonEditor
  18. hideSchedule = ->
  19. $(".schedule-region select").hide()
  20. $(".schedule-region .cannot-be-scheduled").show()
  21. showSchedule = ->
  22. $(".schedule-region select").show()
  23. $(".schedule-region .cannot-be-scheduled").hide()
  24. hideLinks = ->
  25. $(".link-region .select2-container").hide()
  26. $(".link-region .propagate-immediately").hide()
  27. $(".link-region .cannot-receive-events").show()
  28. showLinks = ->
  29. $(".link-region .select2-container").show()
  30. $(".link-region .propagate-immediately").show()
  31. $(".link-region .cannot-receive-events").hide()
  32. showEventDescriptions()
  33. hideEventCreation = ->
  34. $(".event-related-region").hide()
  35. showEventCreation = ->
  36. $(".event-related-region").show()
  37. showEventDescriptions = ->
  38. if $("#agent_source_ids").val()
  39. $.getJSON "/agents/event_descriptions", { ids: $("#agent_source_ids").val().join(",") }, (json) =>
  40. if json.description_html?
  41. $(".event-descriptions").show().html(json.description_html)
  42. else
  43. $(".event-descriptions").hide()
  44. else
  45. $(".event-descriptions").html("").hide()
  46. $(document).ready ->
  47. # JSON Editor
  48. window.jsonEditor = setupJsonEditor()
  49. # Flash
  50. if $(".flash").length
  51. setTimeout((-> $(".flash").slideUp(-> $(".flash").remove())), 5000)
  52. # Agent Navigation
  53. $agentNavigate = $('#agent-navigate')
  54. $agentNavigate.typeahead(
  55. minLength: 0,
  56. items: 15,
  57. source: agentNames
  58. ).on("change", (e) ->
  59. if agentPaths[$agentNavigate.val()]
  60. $('#agent-navigate').closest(".navbar-search").find(".spinner").show()
  61. navigationData = agentPaths[$agentNavigate.val()]
  62. if !(navigationData instanceof Object) || !navigationData.method || navigationData.method == 'GET'
  63. window.location = navigationData.url || navigationData
  64. else
  65. $("<a href='#{navigationData.url}' data-method='#{navigationData.method}'></a>").appendTo($("body")).click()
  66. ).on("focus", (e) ->
  67. $agentNavigate.val ''
  68. ).on("blur", (e) ->
  69. $agentNavigate.val ''
  70. )
  71. # Pressing '/' selects the search box.
  72. $("body").on "keypress", (e) ->
  73. if e.keyCode == 47 # The '/' key
  74. if e.target.nodeName == "BODY"
  75. e.preventDefault()
  76. $agentNavigate.focus()
  77. # Agent Show
  78. fetchLogs = (e) ->
  79. agentId = $(e.target).closest("[data-agent-id]").data("agent-id")
  80. e.preventDefault()
  81. $("#logs .spinner").show()
  82. $("#logs .refresh, #logs .clear").hide()
  83. $.get "/agents/#{agentId}/logs", (html) =>
  84. $("#logs .logs").html html
  85. $("#logs .spinner").stop(true, true).fadeOut ->
  86. $("#logs .refresh, #logs .clear").show()
  87. clearLogs = (e) ->
  88. if confirm("Are you sure you want to clear all logs for this Agent?")
  89. agentId = $(e.target).closest("[data-agent-id]").data("agent-id")
  90. e.preventDefault()
  91. $("#logs .spinner").show()
  92. $("#logs .refresh, #logs .clear").hide()
  93. $.post "/agents/#{agentId}/logs/clear", { "_method": "DELETE" }, (html) =>
  94. $("#logs .logs").html html
  95. $("#show-tabs li a.recent-errors").removeClass 'recent-errors'
  96. $("#logs .spinner").stop(true, true).fadeOut ->
  97. $("#logs .refresh, #logs .clear").show()
  98. $(".agent-show #show-tabs a[href='#logs'], #logs .refresh").on "click", fetchLogs
  99. $(".agent-show #logs .clear").on "click", clearLogs
  100. if tab = window.location.href.match(/tab=(\w+)\b/i)?[1]
  101. if tab in ["details", "logs"]
  102. $(".agent-show .nav-tabs li a[href='##{tab}']").click()
  103. # Editing Agents
  104. $("#agent_source_ids").on "change", showEventDescriptions
  105. $("#agent_type").on "change", ->
  106. if window.jsonEditor?
  107. $("#agent-spinner").fadeIn();
  108. $("#agent_source_ids").select2("val", {});
  109. $(".event-descriptions").html("").hide()
  110. $.getJSON "/agents/type_details", { type: $(@).val() }, (json) =>
  111. if json.can_be_scheduled
  112. showSchedule()
  113. else
  114. hideSchedule()
  115. if json.can_receive_events
  116. showLinks()
  117. else
  118. hideLinks()
  119. if json.can_create_events
  120. showEventCreation()
  121. else
  122. hideEventCreation()
  123. $(".description").html(json.description_html) if json.description_html?
  124. if $("#agent_options").hasClass("showing-default") || $("#agent_options").val().match(/\A\s*(\{\s*\}|)\s*\Z/g)
  125. window.jsonEditor.json = json.options
  126. window.jsonEditor.rebuild()
  127. $("#agent-spinner").stop(true, true).fadeOut();
  128. $("#agent_type").change() if $("#agent_type").length
  129. # Select2 Selects
  130. $(".select2").select2(width: 'resolve')
  131. if $(".schedule-region")
  132. if $(".schedule-region").data("can-be-scheduled") == true
  133. showSchedule()
  134. else
  135. hideSchedule()
  136. if $(".link-region")
  137. if $(".link-region").data("can-receive-events") == true
  138. showLinks()
  139. else
  140. hideLinks()
  141. if $(".event-related-region")
  142. if $(".event-related-region").data("can-create-events") == true
  143. showEventCreation()
  144. else
  145. hideEventCreation()