Last active
December 21, 2015 03:58
-
-
Save timgremore/6245930 to your computer and use it in GitHub Desktop.
Simple jQuery modal plugin
This file contains 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
# Thanks to https://github.com/codrops/ModalWindowEffects/blob/master/js/modalEffects.js | |
# for the example | |
# version 0.1 | |
$.fn.extend | |
modal: (options) -> | |
settings = | |
overlay: "#overlay" | |
dataAttr: 'modal' | |
visibleClass: "visible" # class used to show modals | |
force: false # if object is actual modal, display immediately | |
debug: false | |
settings = $.extend settings, options | |
log = (msg) -> | |
console?.log msg if settings.debug | |
removeModals = () -> | |
# make sure only one modal is visible at a time (TODO: rethink this) | |
$("[data-#{settings.dataAttr}]").each () -> | |
$this = $(this) | |
$("##{$this.data(settings.dataAttr)}").removeClass(settings.visibleClass) | |
return @each () -> | |
$this = $(this) | |
if settings.force | |
$modal = $this | |
else | |
$modal = $("#" + $this.data(settings.dataAttr)) | |
$overlay = $(settings.overlay) | |
log "Searching dom for ##{$this.data(settings.dataAttr)}" | |
$modal.find('.close-modal').on 'click', (evt) -> | |
$modal.removeClass(settings.visibleClass) | |
$overlay.removeClass(settings.visibleClass) | |
evt.preventDefault() | |
if settings.force | |
$overlay.addClass(settings.visibleClass) | |
else | |
$this.on 'click', (evt) -> | |
$modal.addClass(settings.visibleClass) | |
$overlay.addClass(settings.visibleClass) | |
evt.preventDefault() | |
$overlay.on 'click', (evt) -> | |
$modal.removeClass(settings.visibleClass) | |
$overlay.removeClass(settings.visibleClass) | |
evt.preventDefault() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment