application.js.coffee.erb 5.8 KB

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