Created
April 3, 2013 18:50
-
-
Save plapier/5304088 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ = 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