Skip to content

Instantly share code, notes, and snippets.

@kane-thornwyrd
Created September 12, 2013 08:59
Show Gist options
  • Save kane-thornwyrd/6534734 to your computer and use it in GitHub Desktop.
Save kane-thornwyrd/6534734 to your computer and use it in GitHub Desktop.
/**
* 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;
}
};
@kane-thornwyrd
Copy link
Author

Deserve refactoring.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment