Keine Beschreibung http://j1x-huginn.herokuapp.com

application.js.coffee.erb 5.9KB

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