# Dialog windows
#
class window.Alchemy.Dialog
DEFAULTS:
header_height: 36
size: '400x300'
padding: true
title: ''
modal: true
overflow: 'visible'
ready: ->
closed: ->
# Arguments:
# - url: The url to load the content from via ajax
# - options: A object holding options
# - size: The maximum size of the Dialog
# - title: The title of the Dialog
constructor: (@url, @options = {}) ->
@options = $.extend({}, @DEFAULTS, @options)
@$document = $(document)
@$window = $(window)
@$body = $('body')
size = @options.size.split('x')
@width = parseInt(size[0], 10)
@height = parseInt(size[1], 10)
@build()
# Opens the Dialog and loads the content via ajax.
open: ->
@dialog.trigger 'Alchemy.DialogOpen'
@bind_close_events()
window.requestAnimationFrame =>
@dialog_container.addClass('open')
@overlay.addClass('open') if @overlay?
@$body.addClass('prevent-scrolling')
Alchemy.currentDialogs.push(this)
@load()
true
# Closes the Dialog and removes it from the DOM
close: ->
@dialog.trigger 'DialogClose.Alchemy'
@$document.off 'keydown'
@dialog_container.removeClass('open')
@overlay.removeClass('open') if @overlay?
@$document.on 'webkitTransitionEnd transitionend oTransitionEnd', =>
@$document.off 'webkitTransitionEnd transitionend oTransitionEnd'
@dialog_container.remove()
@overlay.remove() if @overlay?
@$body.removeClass('prevent-scrolling')
Alchemy.currentDialogs.pop(this)
if @options.closed?
@options.closed()
true
# Loads the content via ajax and replaces the Dialog body with server response.
load: ->
@show_spinner()
$.get @url, (data) =>
@replace(data)
.fail (xhr) =>
@show_error(xhr)
true
# Reloads the Dialog content
reload: ->
@dialog_body.empty()
@load()
# Replaces the dialog body with given content and initializes it.
replace: (data) ->
@remove_spinner()
@dialog_body.hide()
@dialog_body.html(data)
@init()
@dialog[0].dispatchEvent(new CustomEvent(
"DialogReady.Alchemy",
bubbles: true
detail:
body: @dialog_body[0]
))
if @options.ready?
@options.ready(@dialog_body)
@dialog_body.show()
true
# Adds a spinner into Dialog body
show_spinner: ->
@spinner = new Alchemy.Spinner('medium')
@spinner.spin(@dialog_body[0])
# Removes the spinner from Dialog body
remove_spinner: ->
@spinner.stop()
# Initializes the Dialog body
init: ->
Alchemy.GUI.init(@dialog_body)
@watch_remote_forms()
# Watches ajax requests inside of dialog body and replaces the content accordingly
watch_remote_forms: ->
form = $('[data-remote="true"]', @dialog_body)
form.bind "ajax:success", (event) =>
xhr = event.detail[2]
content_type = xhr.getResponseHeader('Content-Type')
if content_type.match(/javascript/)
return
else
@dialog_body.html(xhr.responseText)
@init()
return
form.bind "ajax:error", (event, b, c) =>
statusText = event.detail[1]
xhr = event.detail[2]
@show_error(xhr, statusText)
return
# Displays an error message
show_error: (xhr, status_message, $container = @dialog_body) ->
error_type = "warning"
switch xhr.status
when 0
error_header = "The server does not respond."
error_body = "Please check server and try again."
when 403
error_header = "You are not authorized!"
error_body = "Please close this window."
else
error_type = "error"
if status_message
error_header = status_message
console.error(xhr.responseText)
else
error_header = "#{xhr.statusText} (#{xhr.status})"
error_body = "Please check log and try again."
$errorDiv = $(" #{error_body}#{error_header}