Created
August 27, 2015 15:38
-
-
Save guillermoroblesjr/cb005b79989dbe6aacc6 to your computer and use it in GitHub Desktop.
A simple modal constructor with Private and Public methods
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
| /* | |
| * How to use: | |
| * var modal = new Modal({ | |
| * modalBackground: $('#manual-mode-modal'), | |
| * modalDialog: $('#manual-mode-modal-wrapper') | |
| * }); | |
| * | |
| * modal.openModal(); | |
| * modal.closeModal(); | |
| */ | |
| define(function(require){ | |
| 'use strict'; | |
| var $ = require('jquery'); | |
| var _ = require('lodash'); | |
| //////////////////////////////////////////////////////////////////////// | |
| // Private API | |
| //////////////////////////////////////////////////////////////////////// | |
| var _Private = function(){ | |
| 'use strict'; | |
| }; | |
| _Private.prototype = Object.create(Object.prototype); | |
| _Private.prototype.constructor = _Private; | |
| _Private.prototype.openModalBackground = function(instance){ | |
| 'use strict'; | |
| instance.modalBackground.removeClass('hide'); | |
| }; | |
| _Private.prototype.closeModalBackground = function(instance){ | |
| 'use strict'; | |
| instance.modalBackground.addClass('hide'); | |
| }; | |
| _Private.prototype.displayModalDialog = function(instance){ | |
| 'use strict'; | |
| if ( instance.modalDialog === null || instance.modalDialog === undefined ) { | |
| return; | |
| } | |
| instance.modalDialog.removeClass('hide'); | |
| }; | |
| _Private.prototype.hideModalDialog = function(instance){ | |
| 'use strict'; | |
| if ( instance.modalDialog === null || instance.modalDialog === undefined ) { | |
| return; | |
| } | |
| instance.modalDialog.addClass('hide'); | |
| }; | |
| _Private.prototype.verticallyCenterModal = function(instance){ | |
| 'use strict'; | |
| if ( instance.modalDialog === null || instance.modalDialog === undefined ) { | |
| return; | |
| } | |
| var bg_height = instance.modalBackground.outerHeight(); | |
| var modal_height = instance.modalDialog.outerHeight(); | |
| var modalTopOffset = _.round( (bg_height / 2) - (modal_height / 2) ); | |
| instance.modalDialog.css('top', modalTopOffset + 'px'); | |
| }; | |
| _Private.prototype.runBeforeOpenModal = function(instance){ | |
| 'use strict'; | |
| if ( instance.runBeforeOpenModal === null || instance.runBeforeOpenModal === undefined ) { | |
| return; | |
| } | |
| instance.runBeforeOpenModal( instance ); | |
| }; | |
| _Private.prototype.runAfterCloseModal = function(instance){ | |
| 'use strict'; | |
| if ( instance.runAfterCloseModal === null || instance.runAfterCloseModal === undefined ) { | |
| return; | |
| } | |
| instance.runAfterCloseModal( instance ); | |
| }; | |
| var _private = new _Private(); | |
| //////////////////////////////////////////////////////////////////////// | |
| // Public API | |
| //////////////////////////////////////////////////////////////////////// | |
| var Fn = function(options){ | |
| 'use strict'; | |
| this.modalBackground = options.modalBackground || null; | |
| this.modalDialog = options.modalDialog || null; | |
| this.runBeforeOpenModal = options.runBeforeOpenModal || null; | |
| this.runAfterCloseModal = options.runAfterCloseModal || null; | |
| this.data = options.data || null; | |
| return this; | |
| }; | |
| Fn.prototype = Object.create(Object.prototype); | |
| Fn.prototype.constructor = Fn; | |
| Fn.prototype.openModal = function(){ | |
| 'use strict'; | |
| _private.runBeforeOpenModal( this ); | |
| _private.openModalBackground( this ); | |
| _private.verticallyCenterModal( this ); | |
| _private.displayModalDialog( this ); | |
| }; | |
| Fn.prototype.closeModal = function(){ | |
| 'use strict'; | |
| _private.closeModalBackground( this ); | |
| _private.hideModalDialog( this ); | |
| _private.runAfterCloseModal( this ); | |
| }; | |
| return Fn; | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment