utils.js.coffee 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 = if button.form then $(':input[name!="_method"]', button.form).serialize() else '') ->
  34. $(button).prop('disabled', true)
  35. cleanup = -> $(button).prop('disabled', false)
  36. url = $(button).data('action-url')
  37. with_event_mode = $(button).data('with-event-mode')
  38. if with_event_mode is 'no'
  39. return @invokeDryRun(url, data, cleanup)
  40. Utils.showDynamicModal """
  41. <h5>Event to send#{if with_event_mode is 'maybe' then ' (Optional)' else ''}</h5>
  42. <form class="dry-run-form" method="post">
  43. <div class="form-group">
  44. <textarea rows="10" name="event" class="payload-editor" data-height="200">
  45. {}
  46. </textarea>
  47. </div>
  48. <div class="form-group">
  49. <input value="Dry Run" class="btn btn-primary" type="submit" />
  50. </div>
  51. </form>
  52. """,
  53. body: (body) =>
  54. form = $(body).find('.dry-run-form')
  55. payload_editor = form.find('.payload-editor')
  56. if previous = $(button).data('payload')
  57. payload_editor.text(previous)
  58. window.setupJsonEditor(payload_editor)
  59. form.submit (e) =>
  60. e.preventDefault()
  61. json = $(e.target).find('.payload-editor').val()
  62. json = '{}' if json == ''
  63. try
  64. payload = JSON.parse(json)
  65. throw true unless payload.constructor is Object
  66. if Object.keys(payload).length == 0
  67. json = ''
  68. else
  69. json = JSON.stringify(payload)
  70. catch
  71. alert 'Invalid JSON object.'
  72. return
  73. if json == ''
  74. if with_event_mode is 'yes'
  75. alert 'Event is required for this agent to run.'
  76. return
  77. dry_run_data = data
  78. $(button).data('payload', null)
  79. else
  80. dry_run_data = "event=#{encodeURIComponent(json)}&#{data}"
  81. $(button).data('payload', json)
  82. $(body).closest('[role=dialog]').on 'hidden.bs.modal', =>
  83. @invokeDryRun(url, dry_run_data, cleanup)
  84. .modal('hide')
  85. title: 'Dry Run'
  86. onHide: cleanup
  87. @invokeDryRun: (url, data, callback) ->
  88. $('body').css(cursor: 'progress')
  89. $.ajax type: 'POST', url: url, dataType: 'json', data: data
  90. .always =>
  91. $('body').css(cursor: 'auto')
  92. .done (json) =>
  93. Utils.showDynamicModal """
  94. <h5>Log</h5>
  95. <pre class="agent-dry-run-log"></pre>
  96. <h5>Events</h5>
  97. <pre class="agent-dry-run-events"></pre>
  98. <h5>Memory</h5>
  99. <pre class="agent-dry-run-memory"></pre>
  100. """,
  101. body: (body) ->
  102. $(body).
  103. find('.agent-dry-run-log').text(json.log).end().
  104. find('.agent-dry-run-events').text(json.events).end().
  105. find('.agent-dry-run-memory').text(json.memory)
  106. title: 'Dry Run Results',
  107. onHide: callback
  108. .fail (xhr, status, error) ->
  109. alert('Error: ' + error)
  110. callback()