utils.js.coffee 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. $.ajax url,
  41. method: 'GET',
  42. data:
  43. with_event_mode: with_event_mode
  44. source_ids: $.map($(".link-region select option:selected"), (el) -> $(el).val() )
  45. success: (modal_data) =>
  46. Utils.showDynamicModal modal_data,
  47. body: (body) =>
  48. form = $(body).find('.dry-run-form')
  49. payload_editor = form.find('.payload-editor')
  50. if previous = $(button).data('payload')
  51. payload_editor.text(previous)
  52. editor = window.setupJsonEditor(payload_editor)[0]
  53. $(body).find('.dry-run-event-sample').click (e) =>
  54. e.preventDefault()
  55. editor.json = $(e.currentTarget).data('payload')
  56. editor.rebuild()
  57. form.submit (e) =>
  58. e.preventDefault()
  59. json = $(e.target).find('.payload-editor').val()
  60. json = '{}' if json == ''
  61. try
  62. payload = JSON.parse(json.replace(/\\\\([n|r|t])/g, "\\$1"))
  63. throw true unless payload.constructor is Object
  64. if Object.keys(payload).length == 0
  65. json = ''
  66. else
  67. json = JSON.stringify(payload)
  68. catch
  69. alert 'Invalid JSON object.'
  70. return
  71. if json == ''
  72. if with_event_mode is 'yes'
  73. alert 'Event is required for this agent to run.'
  74. return
  75. dry_run_data = data
  76. $(button).data('payload', null)
  77. else
  78. dry_run_data = "event=#{encodeURIComponent(json)}&#{data}"
  79. $(button).data('payload', json)
  80. $(body).closest('[role=dialog]').on 'hidden.bs.modal', =>
  81. @invokeDryRun(url, dry_run_data, cleanup)
  82. .modal('hide')
  83. $(body).closest('[role=dialog]').on 'shown.bs.modal', ->
  84. $(this).find('.btn-primary').focus()
  85. title: 'Dry Run'
  86. onHide: cleanup
  87. @invokeDryRun: (url, data, callback) ->
  88. $('body').css(cursor: 'progress')
  89. $.ajax type: 'POST', url: url, dataType: 'html', data: data
  90. .always =>
  91. $('body').css(cursor: 'auto')
  92. .done (modal_data) =>
  93. Utils.showDynamicModal modal_data,
  94. title: 'Dry Run Results',
  95. onHide: callback
  96. .fail (xhr, status, error) ->
  97. alert('Error: ' + error)
  98. callback()
  99. @select2TagClickHandler: (e, elem) ->
  100. if e.which == 1
  101. window.location = $(elem).attr('href')
  102. else
  103. window.open($(elem).attr('href'))
  104. # _.escape from underscore: https://github.com/jashkenas/underscore/blob/1e68f06610fa4ecb7f2c45d1eb2ad0173d6a2cc1/underscore.js#L1411-L1436
  105. escapeMap =
  106. '&': '&amp;'
  107. '<': '&lt;'
  108. '>': '&gt;'
  109. '"': '&quot;'
  110. '\'': '&#x27;'
  111. '`': '&#x60;'
  112. createEscaper = (map) ->
  113. escaper = (match) ->
  114. map[match]
  115. # Regexes for identifying a key that needs to be escaped.
  116. source = '(?:' + Object.keys(map).join('|') + ')'
  117. testRegexp = RegExp(source)
  118. replaceRegexp = RegExp(source, 'g')
  119. (string) ->
  120. string = if string == null then '' else '' + string
  121. if testRegexp.test(string) then string.replace(replaceRegexp, escaper) else string
  122. @escape = createEscaper(escapeMap)