diff --git a/src/definitions/behaviors/api.js b/src/definitions/behaviors/api.js index a2e09b575e..972476dc66 100644 --- a/src/definitions/behaviors/api.js +++ b/src/definitions/behaviors/api.js @@ -36,7 +36,19 @@ query = arguments[0], methodInvoked = typeof query === 'string', queryArguments = [].slice.call(arguments, 1), + contextCheck = function (context, win) { + var $context; + if ([window, document].indexOf(context) >= 0) { + $context = $(context); + } else { + $context = $(win.document).find(context); + if ($context.length === 0) { + $context = win.frameElement ? contextCheck(context, win.parent) : window; + } + } + return $context; + }, returnedValue ; @@ -62,9 +74,7 @@ $form = $module.closest(selector.form), // context used for state - $context = settings.stateContext - ? ([window, document].indexOf(settings.stateContext) < 0 ? $(document).find(settings.stateContext) : $(settings.stateContext)) - : $module, + $context = settings.stateContext ? contextCheck(settings.stateContext, window) : $module, // request details ajaxSettings, diff --git a/src/definitions/behaviors/state.js b/src/definitions/behaviors/state.js index 58d3a88cd8..e0ede3ca44 100644 --- a/src/definitions/behaviors/state.js +++ b/src/definitions/behaviors/state.js @@ -31,7 +31,19 @@ query = arguments[0], methodInvoked = typeof query === 'string', queryArguments = [].slice.call(arguments, 1), + contextCheck = function (context, win) { + var $context; + if ([window, document].indexOf(context) >= 0) { + $context = $(context); + } else { + $context = $(win.document).find(context); + if ($context.length === 0) { + $context = win.frameElement ? contextCheck(context, win.parent) : window; + } + } + return $context; + }, returnedValue ; $allModules.each(function () { @@ -51,6 +63,7 @@ moduleNamespace = namespace + '-module', $module = $(this), + $context = settings.context ? contextCheck(settings.context, window) : $module, element = this, instance = $module.data(moduleNamespace), @@ -68,19 +81,11 @@ } // bind events with delegated events - if (settings.context && moduleSelector !== '') { - ([window, document].indexOf(settings.context) < 0 ? $(document).find(settings.context) : $(settings.context)) - .on(moduleSelector, 'mouseenter' + eventNamespace, module.change.text) - .on(moduleSelector, 'mouseleave' + eventNamespace, module.reset.text) - .on(moduleSelector, 'click' + eventNamespace, module.toggle.state) - ; - } else { - $module - .on('mouseenter' + eventNamespace, module.change.text) - .on('mouseleave' + eventNamespace, module.reset.text) - .on('click' + eventNamespace, module.toggle.state) - ; - } + $context + .on('mouseenter' + eventNamespace, module.change.text) + .on('mouseleave' + eventNamespace, module.reset.text) + .on('click' + eventNamespace, module.toggle.state) + ; module.instantiate(); }, @@ -94,8 +99,11 @@ destroy: function () { module.verbose('Destroying previous module', instance); - $module + $context .off(eventNamespace) + ; + $module + .removeData(metadata.storedText) .removeData(moduleNamespace) ; }, diff --git a/src/definitions/behaviors/visibility.js b/src/definitions/behaviors/visibility.js index ec673242a1..06cb737042 100755 --- a/src/definitions/behaviors/visibility.js +++ b/src/definitions/behaviors/visibility.js @@ -30,6 +30,19 @@ query = arguments[0], methodInvoked = typeof query === 'string', queryArguments = [].slice.call(arguments, 1), + contextCheck = function (context, win) { + var $context; + if ([window, document].indexOf(context) >= 0) { + $context = $(context); + } else { + $context = $(win.document).find(context); + if ($context.length === 0) { + $context = win.frameElement ? contextCheck(context, win.parent) : window; + } + } + + return $context; + }, returnedValue, moduleCount = $allModules.length, @@ -53,7 +66,7 @@ $window = $(window), $module = $(this), - $context = [window, document].indexOf(settings.context) < 0 ? $(document).find(settings.context) : $(settings.context), + $context = contextCheck(settings.context, window), $placeholder, diff --git a/src/definitions/modules/dropdown.js b/src/definitions/modules/dropdown.js index e39b0b4d8a..834fba2af2 100644 --- a/src/definitions/modules/dropdown.js +++ b/src/definitions/modules/dropdown.js @@ -32,6 +32,19 @@ query = arguments[0], methodInvoked = typeof query === 'string', queryArguments = [].slice.call(arguments, 1), + contextCheck = function (context, win) { + var $context; + if ([window, document].indexOf(context) >= 0) { + $context = $(context); + } else { + $context = $(win.document).find(context); + if ($context.length === 0) { + $context = win.frameElement ? contextCheck(context, win.parent) : window; + } + } + + return $context; + }, returnedValue ; @@ -56,7 +69,7 @@ moduleNamespace = 'module-' + namespace, $module = $(this), - $context = [window, document].indexOf(settings.context) < 0 ? $document.find(settings.context) : $(settings.context), + $context = contextCheck(settings.context, window), $text = $module.find(selector.text), $search = $module.find(selector.search), $sizer = $module.find(selector.sizer), diff --git a/src/definitions/modules/flyout.js b/src/definitions/modules/flyout.js index 1031202bf2..2f6138ccc9 100644 --- a/src/definitions/modules/flyout.js +++ b/src/definitions/modules/flyout.js @@ -36,7 +36,19 @@ query = arguments[0], methodInvoked = typeof query === 'string', queryArguments = [].slice.call(arguments, 1), + contextCheck = function (context, win) { + var $context; + if ([window, document].indexOf(context) >= 0) { + $context = $body; + } else { + $context = $(win.document).find(context); + if ($context.length === 0) { + $context = win.frameElement ? contextCheck(context, win.parent) : $body; + } + } + return $context; + }, returnedValue ; @@ -57,7 +69,7 @@ moduleNamespace = 'module-' + namespace, $module = $(this), - $context = [window, document].indexOf(settings.context) < 0 ? $document.find(settings.context) : $body, + $context = contextCheck(settings.context, window), $closeIcon = $module.find(selector.close), $inputs, $focusedElement, @@ -527,7 +539,7 @@ }, refresh: function () { module.verbose('Refreshing selector cache'); - $context = [window, document].indexOf(settings.context) < 0 ? $document.find(settings.context) : $body; + $context = contextCheck(settings.context, window); module.refreshFlyouts(); $pusher = $context.children(selector.pusher); module.clear.cache(); diff --git a/src/definitions/modules/modal.js b/src/definitions/modules/modal.js index fa3ad50fb0..b097a3c843 100755 --- a/src/definitions/modules/modal.js +++ b/src/definitions/modules/modal.js @@ -34,7 +34,19 @@ query = arguments[0], methodInvoked = typeof query === 'string', queryArguments = [].slice.call(arguments, 1), + contextCheck = function (context, win) { + var $context; + if ([window, document].indexOf(context) >= 0) { + $context = $body; + } else { + $context = $(win.document).find(context); + if ($context.length === 0) { + $context = win.frameElement ? contextCheck(context, win.parent) : $body; + } + } + return $context; + }, returnedValue ; @@ -54,7 +66,7 @@ moduleNamespace = 'module-' + namespace, $module = $(this), - $context = [window, document].indexOf(settings.context) < 0 ? $document.find(settings.context) : $body, + $context = contextCheck(settings.context, window), isBody = $context[0] === $body[0], $closeIcon = $module.find(selector.closeIcon), $inputs, diff --git a/src/definitions/modules/nag.js b/src/definitions/modules/nag.js index b0eeccdab4..dabe71d15d 100644 --- a/src/definitions/modules/nag.js +++ b/src/definitions/modules/nag.js @@ -22,6 +22,7 @@ $.fn.nag = function (parameters) { var $allModules = $(this), + $body = $('body'), moduleSelector = $allModules.selector || '', time = Date.now(), @@ -30,6 +31,19 @@ query = arguments[0], methodInvoked = typeof query === 'string', queryArguments = [].slice.call(arguments, 1), + contextCheck = function (context, win) { + var $context; + if ([window, document].indexOf(context) >= 0) { + $context = $(context); + } else { + $context = $(win.document).find(context); + if ($context.length === 0) { + $context = win.frameElement ? contextCheck(context, win.parent) : $body; + } + } + + return $context; + }, returnedValue ; $allModules.each(function () { @@ -47,9 +61,7 @@ $module = $(this), - $context = settings.context - ? ([window, document].indexOf(settings.context) < 0 ? $(document).find(settings.context) : $(settings.context)) - : $('body'), + $context = settings.context ? contextCheck(settings.context, window) : $body, element = this, instance = $module.data(moduleNamespace), diff --git a/src/definitions/modules/popup.js b/src/definitions/modules/popup.js index d24100a20c..4b592ef176 100644 --- a/src/definitions/modules/popup.js +++ b/src/definitions/modules/popup.js @@ -38,6 +38,19 @@ query = arguments[0], methodInvoked = typeof query === 'string', queryArguments = [].slice.call(arguments, 1), + contextCheck = function (context, win) { + var $context; + if ([window, document].indexOf(context) >= 0) { + $context = $(context); + } else { + $context = $(win.document).find(context); + if ($context.length === 0) { + $context = win.frameElement ? contextCheck(context, win.parent) : $body; + } + } + + return $context; + }, returnedValue ; @@ -57,12 +70,10 @@ moduleNamespace = 'module-' + namespace, $module = $(this), - $context = [window, document].indexOf(settings.context) < 0 ? $document.find(settings.context) : $(settings.context), - $scrollContext = [window, document].indexOf(settings.scrollContext) < 0 ? $document.find(settings.scrollContext) : $(settings.scrollContext), - $boundary = [window, document].indexOf(settings.boundary) < 0 ? $document.find(settings.boundary) : $(settings.boundary), - $target = settings.target - ? ([window, document].indexOf(settings.target) < 0 ? $document.find(settings.target) : $(settings.target)) - : $module, + $context = contextCheck(settings.context, window), + $scrollContext = contextCheck(settings.scrollContext, window), + $boundary = contextCheck(settings.boundary, window), + $target = settings.target ? contextCheck(settings.target, window) : $module, $popup, $offsetParent, diff --git a/src/definitions/modules/sidebar.js b/src/definitions/modules/sidebar.js index 6cfef97b56..986328bd21 100644 --- a/src/definitions/modules/sidebar.js +++ b/src/definitions/modules/sidebar.js @@ -36,7 +36,19 @@ query = arguments[0], methodInvoked = typeof query === 'string', queryArguments = [].slice.call(arguments, 1), + contextCheck = function (context, win) { + var $context; + if ([window, document].indexOf(context) >= 0) { + $context = $body; + } else { + $context = $(win.document).find(context); + if ($context.length === 0) { + $context = win.frameElement ? contextCheck(context, win.parent) : $body; + } + } + return $context; + }, returnedValue; $allModules.each(function () { @@ -55,7 +67,7 @@ moduleNamespace = 'module-' + namespace, $module = $(this), - $context = [window, document].indexOf(settings.context) < 0 ? $document.find(settings.context) : $body, + $context = contextCheck(settings.context, window), isBody = $context[0] === $body[0], $sidebars = $module.children(selector.sidebar), @@ -277,7 +289,7 @@ refresh: function () { module.verbose('Refreshing selector cache'); - $context = [window, document].indexOf(settings.context) < 0 ? $document.find(settings.context) : $body; + $context = contextCheck(settings.context, window); module.refreshSidebars(); $pusher = $context.children(selector.pusher); $fixed = $context.children(selector.fixed); diff --git a/src/definitions/modules/sticky.js b/src/definitions/modules/sticky.js index ce25933784..e78db9caf6 100755 --- a/src/definitions/modules/sticky.js +++ b/src/definitions/modules/sticky.js @@ -31,6 +31,19 @@ query = arguments[0], methodInvoked = typeof query === 'string', queryArguments = [].slice.call(arguments, 1), + contextCheck = function (context, win) { + var $context; + if ([window, document].indexOf(context) >= 0) { + $context = $(context); + } else { + $context = $(win.document).find(context); + if ($context.length === 0) { + $context = win.frameElement ? contextCheck(context, win.parent) : window; + } + } + + return $context; + }, returnedValue ; @@ -49,7 +62,7 @@ $module = $(this), $window = $(window), - $scroll = [window, document].indexOf(settings.scrollContext) < 0 ? $document.find(settings.scrollContext) : $(settings.scrollContext), + $scroll = contextCheck(settings.scrollContext, window), $container, $context, @@ -127,19 +140,11 @@ }, determineContainer: function () { - if (settings.container) { - $container = [window, document].indexOf(settings.container) < 0 ? $document.find(settings.container) : $(settings.container); - } else { - $container = $module.offsetParent(); - } + $container = settings.container ? contextCheck(settings.container, window) : $module.offsetParent(); }, determineContext: function () { - if (settings.context) { - $context = [window, document].indexOf(settings.context) < 0 ? $document.find(settings.context) : $(settings.context); - } else { - $context = $container; - } + $context = settings.context ? contextCheck(settings.context, window) : $container; if ($context.length === 0) { module.error(error.invalidContext, settings.context, $module); } diff --git a/src/definitions/modules/tab.js b/src/definitions/modules/tab.js index 6a6aa70113..bab3365a27 100644 --- a/src/definitions/modules/tab.js +++ b/src/definitions/modules/tab.js @@ -37,7 +37,19 @@ query = arguments[0], methodInvoked = typeof query === 'string', queryArguments = [].slice.call(arguments, 1), + contextCheck = function (context, win) { + var $context; + if ([window, document].indexOf(context) >= 0) { + $context = $(context); + } else { + $context = $(win.document).find(context); + if ($context.length === 0) { + $context = win.frameElement ? contextCheck(context, win.parent) : window; + } + } + return $context; + }, initializedHistory = false, returnedValue ; @@ -152,7 +164,7 @@ $context = $reference.parent(); module.verbose('Determined parent element for creating context', $context); } else if (settings.context) { - $context = [window, document].indexOf(settings.context) < 0 ? $document.find(settings.context) : $(settings.context); + $context = contextCheck(settings.context, window); module.verbose('Using selector for tab context', settings.context, $context); } else { $context = $('body'); diff --git a/src/definitions/modules/toast.js b/src/definitions/modules/toast.js index d5f71a8afc..98a75965d0 100644 --- a/src/definitions/modules/toast.js +++ b/src/definitions/modules/toast.js @@ -22,6 +22,7 @@ $.fn.toast = function (parameters) { var $allModules = $(this), + $body = $('body'), moduleSelector = $allModules.selector || '', time = Date.now(), @@ -30,6 +31,19 @@ query = arguments[0], methodInvoked = typeof query === 'string', queryArguments = [].slice.call(arguments, 1), + contextCheck = function (context, win) { + var $context; + if ([window, document].indexOf(context) >= 0) { + $context = $(context); + } else { + $context = $(win.document).find(context); + if ($context.length === 0) { + $context = win.frameElement ? contextCheck(context, win.parent) : $body; + } + } + + return $context; + }, returnedValue ; $allModules.each(function () { @@ -55,9 +69,7 @@ $progressBar, $animationObject, $close, - $context = settings.context - ? ([window, document].indexOf(settings.context) < 0 ? $(document).find(settings.context) : $(settings.context)) - : $('body'), + $context = settings.context ? contextCheck(settings.context, window) : $body, isToastComponent = $module.hasClass('toast') || $module.hasClass('message') || $module.hasClass('card'),