class @Utils
@navigatePath: (path) ->
path = "/" + path unless path.match(/^\//)
window.location.href = path
@currentPath: ->
window.location.href.replace(/https?:\/\/.*?\//g, '')
@registerPage: (klass, options = {}) ->
if options.forPathsMatching?
if Utils.currentPath().match(options.forPathsMatching)
window.currentPage = new klass()
else
new klass()
@showDynamicModal: (content = '', { title, body, onHide } = {}) ->
$("body").append """
"""
modal = document.querySelector('#dynamic-modal')
$(modal).find('.modal-title').text(title || '').end().on 'hidden.bs.modal', ->
$('#dynamic-modal').remove()
onHide?()
body?(modal.querySelector('.modal-body'))
$(modal).modal('show')
@handleDryRunButton: (button, data = if button.form then $(':input[name!="_method"]', button.form).serialize() else '') ->
$(button).prop('disabled', true)
cleanup = -> $(button).prop('disabled', false)
url = $(button).data('action-url')
with_event_mode = $(button).data('with-event-mode')
if with_event_mode is 'no'
return @invokeDryRun(url, data, cleanup)
Utils.showDynamicModal """
Event to send#{if with_event_mode is 'maybe' then ' (Optional)' else ''}
""",
body: (body) =>
form = $(body).find('.dry-run-form')
payload_editor = form.find('.payload-editor')
if previous = $(button).data('payload')
payload_editor.text(previous)
window.setupJsonEditor(payload_editor)
form.submit (e) =>
e.preventDefault()
json = $(e.target).find('.payload-editor').val()
json = '{}' if json == ''
try
payload = JSON.parse(json)
throw true unless payload.constructor is Object
if Object.keys(payload).length == 0
json = ''
else
json = JSON.stringify(payload)
catch
alert 'Invalid JSON object.'
return
if json == ''
if with_event_mode is 'yes'
alert 'Event is required for this agent to run.'
return
dry_run_data = data
$(button).data('payload', null)
else
dry_run_data = "event=#{encodeURIComponent(json)}{data}"
$(button).data('payload', json)
$(body).closest('[role=dialog]').on 'hidden.bs.modal', =>
@invokeDryRun(url, dry_run_data, cleanup)
.modal('hide')
$(body).closest('[role=dialog]').on 'shown.bs.modal', ->
$(this).find('.btn-primary').focus()
title: 'Dry Run'
onHide: cleanup
@invokeDryRun: (url, data, callback) ->
$('body').css(cursor: 'progress')
$.ajax type: 'POST', url: url, dataType: 'json', data: data
.always =>
$('body').css(cursor: 'auto')
.done (json) =>
Utils.showDynamicModal """
""",
body: (body) ->
$(body).
find('.agent-dry-run-log').text(json.log).end().
find('.agent-dry-run-events').text(json.events).end().
find('.agent-dry-run-memory').text(json.memory)
active = if json.events.match(/^\[?\s*\]?$/) then 'tabLog' else 'tabEvents'
$('#resultTabs a[href="#' + active + '"]').tab('show')
title: 'Dry Run Results',
onHide: callback
.fail (xhr, status, error) ->
alert('Error: ' + error)
callback()