Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
$ = jQuery
class Modal
@ESC_KEY_CODE: 27
constructor: (@modal) ->
@_addDialogBackground()
@_listenForDialogDismiss()
show: ->
@modal.removeClass('invisible')
@modal.show()
@modal.trigger('show')
$('.modal-dialog-background').show()
@modal.find('input[type!="submit"]:visible,textarea:visible,select:visible').eq(0).focus()
hide: ->
@modal.hide()
$('.modal-dialog-background').hide()
if @modal.attr('data-close-callback')
@modal.trigger('close-callback')
_addDialogBackground: ->
if $('.modal-dialog-background').length == 0
$('body').append $('<div>').addClass('modal-dialog-background')
_listenForDialogDismiss: ->
@modal.on 'click', '[data-dismiss="modal"]', (e) =>
e.preventDefault()
@hide()
unless @modal.attr('data-no-escape')
$('.modal-dialog-background').click =>
@hide()
$(document).keyup (e) =>
@hide() if e.keyCode == Modal.ESC_KEY_CODE
$.fn.extend
modal: (option) ->
return @each ()->
data = new Modal($(@))
if typeof option == 'string'
data[option]()
else
data.show()
$ ->
$('body').on 'click', '[data-toggle="modal"]', (e) ->
e.preventDefault()
target = $(@).attr('data-target') || $($(@).attr('href'))
if $(@).attr('data-load')
target.load($(@).attr('data-load'))
target.modal()
if $('div[data-auto-show="true"]')
$('div[data-auto-show="true"]').modal()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment