Created
September 12, 2013 08:59
-
-
Save kane-thornwyrd/6534734 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
/** | |
* A Quick and Dirty snippet to create iFrame Popups out of things… | |
* Require jQuery || Ember.js | |
* | |
* @class ModalIFrame | |
* | |
* @param {DOMElement} el The element acting as a launcher. | |
* @param {Object} options Defaults: | |
* { | |
* openEvent: 'click', | |
* launcherElement: 'a', | |
* closeText: '⊗', | |
* hideAnimation: 'slideUp', | |
* showAnimation: 'slideDown' | |
* } | |
*/ | |
var ModalIFrame = function(el, options){ | |
this.el = el; | |
options = typeof options !== 'undefined' ? options : {}; | |
this.options = $.extend(true, options, ModalIFrame.prototype.defaults); | |
this | |
.initIFrame() | |
.bindEvents() | |
; | |
}; | |
ModalIFrame.prototype = { | |
defaults: { | |
openEvent: 'click', | |
launcherElement: 'a', | |
closeText: '⊗', | |
hideAnimation: 'slideUp', | |
showAnimation: 'slideDown' | |
}, | |
initIFrame: function(){ | |
this._modal = document.createElement( 'div' ); | |
this._modal.classList.add('modalIFrame'); | |
$(this._modal).hide(); | |
var decodiv = document.createElement( 'div' ); | |
this._iframe = document.createElement( 'iframe' ); | |
this._modalCloseButton = document.createElement( 'a' ); | |
this._modalCloseButton.innerText = this.options.closeText; | |
this._modalCloseButton.href = '#'; | |
this._modalCloseButton.classList.add('closeModalIFrame'); | |
$(decodiv).append(this._modalCloseButton, this._iframe); | |
$(this._modal).append(decodiv); | |
$('body').append(this._modal); | |
return this; | |
}, | |
bindEvents: function(){ | |
$(this.el).on( | |
this.options.openEvent, | |
this.options.launcherElement, | |
this.getOpenCallback(this) | |
); | |
$(document).on('keydown', this.getCloseCallback(this)); | |
$(this._modal).on('click', '.closeModalIFrame', this.getCloseCallback(this)); | |
}, | |
getOpenCallback: function(self){ | |
return function openModal(event){ | |
event.preventDefault(); | |
self._iframe.src = this.href; | |
self.showModal(500); | |
return false; | |
}; | |
}, | |
getCloseCallback: function(self){ | |
return function closeModal(event){ | |
if(event.type === 'keydown' && event.keyCode === 27 && self.displayed){ | |
self.hideModal(200); | |
} | |
if(event.type === 'click'){ | |
event.preventDefault(); | |
self.hideModal(200); | |
return false; | |
} | |
}; | |
}, | |
hideModal: function(duration){ | |
this.displayed = false; | |
$(this._modal)[this.options.hideAnimation](duration); | |
return this; | |
}, | |
showModal: function(duration){ | |
this.displayed = true; | |
$(this._modal)[this.options.showAnimation](duration); | |
return this; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Deserve refactoring.