diff --git a/lib/modal-native.js b/lib/modal-native.js index 02a9a694..01cd6c0f 100644 --- a/lib/modal-native.js +++ b/lib/modal-native.js @@ -19,7 +19,7 @@ } })(function(){ - + //MODAL DEFINITION var Modal = function(element, options) { // element is the trigger button / options.target is the modal options = options || {}; @@ -33,149 +33,251 @@ this.duration = options.duration || 300; // the default modal fade duration option this.options.duration = (this.isIE && this.isIE < 10) ? 0 : this.duration; + this.scrollbarWidth = 0; this.dialog = this.modal.querySelector('.modal-dialog'); this.timer = 0; - this.init() - } - - Modal.prototype.init = function() { - if ( this.options.content && this.options.content !== undefined ) { - this.content( this.options.content ); - } - this.dismiss(); - this.keydown(); - this.trigger(); - if (!(this.isIE && this.isIE < 9)) { this.resize(); } // filter IE9- only - } - - Modal.prototype.open = function() { - this._open(); - } - - Modal.prototype.close = function() { - this._close(); - } - - Modal.prototype._open = function() { - var self = this; - - if ( this.options.backdrop ) { - this.createOverlay(); - } else { this.overlay = null } - - document.body.className += ' modal-open'; - this.modal.style.display = 'block'; - - clearTimeout(self.modal.getAttribute('data-timer')); - this.timer = setTimeout( function() { - - if ( self.overlay !== null ) { - self._resize(); - self.overlay.className += ' in'; + this.init(); + }; + + var getWindowWidth = function() { + var htmlRect = document.documentElement.getBoundingClientRect(), + fullWindowWidth = window.innerWidth || (htmlRect.right - Math.abs(htmlRect.left)); + return fullWindowWidth; + }; + Modal.prototype = { + + init : function() { + + this.actions(); + this.trigger(); + if ( this.options.content && this.options.content !== undefined ) { + this.content( this.options.content ); } - self.modal.className += ' in'; - self.modal.setAttribute('aria-hidden', false); - }, self.options.duration/2); - this.modal.setAttribute('data-timer',self.timer); - - this.opened = true; - } - - Modal.prototype._close = function() { - var self = this; - - this.modal.className = this.modal.className.replace(' in',''); - this.modal.setAttribute('aria-hidden', true); - - if ( this.overlay ) { this.overlay.className = this.overlay.className.replace(' in',''); } - document.body.className = document.body.className.replace(' modal-open',''); - - clearTimeout(self.modal.getAttribute('data-timer')); - this.timer = setTimeout( function() { - self.modal.style.display = 'none'; - self.removeOverlay(); - }, self.options.duration/2); - this.modal.setAttribute('data-timer',self.timer); - - this.opened = false; - } - - Modal.prototype.content = function( content ) { - return this.modal.querySelector('.modal-content').innerHTML = content; - } - - Modal.prototype.createOverlay = function() { - var backdrop = document.createElement('div'), overlay = document.querySelector('.modal-backdrop'); - backdrop.setAttribute('class','modal-backdrop fade'); - - if ( overlay ) { - this.overlay = overlay; - } else { - this.overlay = backdrop; - document.body.appendChild(backdrop); - } - } - - Modal.prototype.removeOverlay = function() { - var overlay = document.querySelector('.modal-backdrop'); - if ( overlay !== null && overlay !== undefined ) { - document.body.removeChild(overlay) - } - } - - Modal.prototype.keydown = function() { - var self = this; - document.addEventListener('keydown', function(e) { - if (self.options.keyboard && e.which == 27) { - self.close(); - } - }, false); - } - - Modal.prototype.trigger = function() { - var self = this; - var triggers = document.querySelectorAll('[data-toggle="modal"]'), tgl = triggers.length, i = 0; - for ( i;i document.documentElement.clientHeight; + this.scrollbarWidth = this.measureScrollbar(); + }, + + this.setScrollbar = function () { + var bodyStyle = window.getComputedStyle(document.body), bodyPad = parseInt((bodyStyle.paddingRight), 10); + if (this.bodyIsOverflowing) { document.body.style.paddingRight = (bodyPad + this.scrollbarWidth) + 'px'; } + }, + + this.resetScrollbar = function () { + document.body.style.paddingRight = ''; + }, + + this.measureScrollbar = function () { // thx walsh + var scrollDiv = document.createElement('div'); + scrollDiv.className = 'modal-scrollbar-measure'; + document.body.appendChild(scrollDiv); + var scrollbarWidth = scrollDiv.offsetWidth - scrollDiv.clientWidth; + document.body.removeChild(scrollDiv); + return scrollbarWidth; + }, + + this.addClass = function(el,c) { + if (el.classList) { el.classList.add(c); } else { el.className += ' '+c; } + }, + + this.removeClass = function(el,c) { + if (el.classList) { el.classList.remove(c); } else { el.className = el.className.replace(c,'').replace(/^\s+|\s+$/g,''); } } - }) - } - + } + }; + + // DATA API var Modals = document.querySelectorAll('.modal'), mdl = Modals.length, i = 0; for ( i;i