From d692dbc342890a0e72fd835c96e918ff32983f55 Mon Sep 17 00:00:00 2001 From: jlukic Date: Mon, 13 Jul 2015 16:44:08 -0400 Subject: [PATCH] #2526 - Redo methods for determining boundaries exceeded to be more coherent --- src/definitions/modules/popup.js | 112 +++++++++++++++++-------------- 1 file changed, 62 insertions(+), 50 deletions(-) diff --git a/src/definitions/modules/popup.js b/src/definitions/modules/popup.js index 3419fef7b..4ecbb4614 100644 --- a/src/definitions/modules/popup.js +++ b/src/definitions/modules/popup.js @@ -120,7 +120,7 @@ $.fn.popup = function(parameters) { : $body ; } - if( $offsetParent.is('html') ) { + if( $offsetParent.is('html') && $offsetParent[0] !== $body[0] ) { module.debug('Setting page as offset parent'); $offsetParent = $body; } @@ -441,6 +441,9 @@ $.fn.popup = function(parameters) { $module.removeData(metadata.variation); return $module.data(metadata.variation) || settings.variation; }, + popupOffset: function() { + return $popup.offset(); + }, calculations: function() { var targetElement = $target[0], @@ -495,10 +498,10 @@ $.fn.popup = function(parameters) { // calculate screen boundaries screen = calculations.screen; calculations.boundary = { - top : screen.scroll.top - settings.jitter, - bottom : screen.scroll.top + screen.height + settings.jitter, - left : screen.scroll.left - settings.jitter, - right : screen.scroll.left + screen.width + settings.jitter + top : screen.scroll.top, + bottom : screen.scroll.top + screen.height, + left : screen.scroll.left, + right : screen.scroll.left + screen.width }; return calculations; }, @@ -526,6 +529,30 @@ $.fn.popup = function(parameters) { } return false; }, + distanceFromBoundary: function(offset, calculations) { + var + distanceFromBoundary = {}, + popup, + boundary + ; + offset = offset || module.get.offset(); + calculations = calculations || module.get.calculations(); + + // shorthand + popup = calculations.popup; + boundary = calculations.boundary; + + if(offset) { + distanceFromBoundary = { + top : (offset.top - boundary.top), + left : (offset.left - boundary.left), + right : (boundary.right - (offset.left + popup.width) ), + bottom : (boundary.bottom - (offset.top + popup.height) ) + }; + module.verbose('Distance from boundaries determined', offset, distanceFromBoundary); + } + return distanceFromBoundary; + }, offsetParent: function($target) { var element = ($target !== undefined) @@ -553,40 +580,6 @@ $.fn.popup = function(parameters) { : $() ; }, - offstagePosition: function(position, calculations) { - var - offset = $popup.offset(), - offstage = {}, - offstagePositions = [], - popup, - boundary - ; - position = position || false; - calculations = calculations || module.get.calculations(); - // shorthand - popup = calculations.popup; - boundary = calculations.boundary; - - if(offset && position) { - offstage = { - top : (offset.top < boundary.top), - bottom : (offset.top + popup.height > boundary.bottom), - right : (offset.left + popup.width > boundary.right), - left : (offset.left < boundary.left) - }; - module.verbose('Offstage positions determined', offset, offstage); - } - // return only boundaries that have been surpassed - $.each(offstage, function(direction, isOffstage) { - if(isOffstage) { - offstagePositions.push(direction); - } - }); - return (offstagePositions.length > 0) - ? offstagePositions.join(' ') - : false - ; - }, positions: function() { return { 'top left' : false, @@ -672,9 +665,9 @@ $.fn.popup = function(parameters) { target, popup, parent, - computedPosition, positioning, - offstagePosition + popupOffset, + distanceFromBoundary ; calculations = calculations || module.get.calculations(); position = position || $module.data(metadata.position) || settings.position; @@ -804,12 +797,14 @@ $.fn.popup = function(parameters) { .addClass(position) .addClass(className.loading) ; - // check if is offstage - offstagePosition = module.get.offstagePosition(position, calculations); - // recursively find new positioning - if(offstagePosition) { - module.debug('Popup cant fit into viewport', position, offstagePosition); + popupOffset = module.get.popupOffset(); + + // see if any boundaries are surpassed with this tentative position + distanceFromBoundary = module.get.distanceFromBoundary(popupOffset, calculations); + + if( module.is.offstage(distanceFromBoundary, position) ) { + module.debug('Position is outside viewport', position); if(searchDepth < settings.maxSearchDepth) { searchDepth++; position = module.get.nextPosition(position); @@ -828,7 +823,6 @@ $.fn.popup = function(parameters) { return false; } } - module.debug('Position is on stage', position); module.remove.attempts(); module.set.fluidWidth(calculations); @@ -957,6 +951,24 @@ $.fn.popup = function(parameters) { }, is: { + offstage: function(distanceFromBoundary, position) { + var + offstage = [] + ; + // return boundaries that have been surpassed + $.each(distanceFromBoundary, function(direction, distance) { + if(distance < -settings.jitter) { + module.debug('Position exceeds allowable distance from edge', direction, distance, position); + offstage.push(direction); + } + }); + if(offstage.length > 0) { + return true; + } + else { + return false; + } + }, active: function() { return $module.hasClass(className.active); }, @@ -1171,7 +1183,7 @@ $.fn.popup.settings = { name : 'Popup', // module settings - debug : true, + debug : false, verbose : false, performance : true, namespace : 'popup', @@ -1267,8 +1279,8 @@ $.fn.popup.settings = { // distance away from activating element in px distanceAway : 0, - // number of pixels an element is allowed to be "offstage" (allows for rounding errors) - jitter : 2, + // number of pixels an element is allowed to be "offstage" for a position to be chosen (allows for rounding) + jitter : 1, // offset on aligning axis from calculated position offset : 0,