application.js.coffee.erb 6.9KB

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