utils.js.coffee 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. class @Utils
  2. @navigatePath: (path) ->
  3. path = "/" + path unless path.match(/^\//)
  4. window.location.href = path
  5. @currentPath: ->
  6. window.location.href.replace(/https?:\/\/.*?\//g, '')
  7. @registerPage: (klass, options = {}) ->
  8. if options.forPathsMatching?
  9. if Utils.currentPath().match(options.forPathsMatching)
  10. window.currentPage = new klass()
  11. else
  12. new klass()
  13. @showDynamicModal: (content = '', { title, body, onHide } = {}) ->
  14. $("body").append """
  15. <div class="modal fade" tabindex="-1" id='dynamic-modal' role="dialog" aria-labelledby="dynamic-modal-label" aria-hidden="true">
  16. <div class="modal-dialog modal-lg">
  17. <div class="modal-content">
  18. <div class="modal-header">
  19. <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
  20. <h4 class="modal-title" id="dynamic-modal-label"></h4>
  21. </div>
  22. <div class="modal-body">#{content}</div>
  23. </div>
  24. </div>
  25. </div>
  26. """
  27. modal = document.querySelector('#dynamic-modal')
  28. $(modal).find('.modal-title').text(title || '').end().on 'hidden.bs.modal', ->
  29. $('#dynamic-modal').remove()
  30. onHide?()
  31. body?(modal.querySelector('.modal-body'))
  32. $(modal).modal('show')
  33. @handleDryRunButton: (button, data = $(button.form).serialize()) ->
  34. $(button).prop('disabled', true)
  35. $('body').css(cursor: 'progress')
  36. $.ajax type: 'POST', url: $(button).data('action-url'), dataType: 'json', data: data
  37. .always =>
  38. $('body').css(cursor: 'auto')
  39. .done (json) =>
  40. Utils.showDynamicModal """
  41. <h5>Log</h5>
  42. <pre class="agent-dry-run-log"></pre>
  43. <h5>Events</h5>
  44. <pre class="agent-dry-run-events"></pre>
  45. <h5>Memory</h5>
  46. <pre class="agent-dry-run-memory"></pre>
  47. """,
  48. body: (body) ->
  49. $(body).
  50. find('.agent-dry-run-log').text(json.log).end().
  51. find('.agent-dry-run-events').text(json.events).end().
  52. find('.agent-dry-run-memory').text(json.memory)
  53. title: 'Dry Run Results',
  54. onHide: -> $(button).prop('disabled', false)
  55. .fail (xhr, status, error) ->
  56. alert('Error: ' + error)
  57. $(button).prop('disabled', false)