Skip to content

Commit

Permalink
fix(chore): check if context exists in parent windows
Browse files Browse the repository at this point in the history
This PR supports context in parent window objects
  • Loading branch information
lubber-de authored Feb 19, 2023
1 parent d8dd970 commit 2321380
Show file tree
Hide file tree
Showing 12 changed files with 180 additions and 48 deletions.
16 changes: 13 additions & 3 deletions src/definitions/behaviors/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
;

Expand All @@ -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,
Expand Down
36 changes: 22 additions & 14 deletions src/definitions/behaviors/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand All @@ -51,6 +63,7 @@
moduleNamespace = namespace + '-module',

$module = $(this),
$context = settings.context ? contextCheck(settings.context, window) : $module,

element = this,
instance = $module.data(moduleNamespace),
Expand All @@ -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();
},

Expand All @@ -94,8 +99,11 @@

destroy: function () {
module.verbose('Destroying previous module', instance);
$module
$context
.off(eventNamespace)
;
$module
.removeData(metadata.storedText)
.removeData(moduleNamespace)
;
},
Expand Down
15 changes: 14 additions & 1 deletion src/definitions/behaviors/visibility.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,

Expand Down
15 changes: 14 additions & 1 deletion src/definitions/modules/dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
;

Expand All @@ -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),
Expand Down
16 changes: 14 additions & 2 deletions src/definitions/modules/flyout.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
;

Expand All @@ -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,
Expand Down Expand Up @@ -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();
Expand Down
14 changes: 13 additions & 1 deletion src/definitions/modules/modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
;

Expand All @@ -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,
Expand Down
18 changes: 15 additions & 3 deletions src/definitions/modules/nag.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
$.fn.nag = function (parameters) {
var
$allModules = $(this),
$body = $('body'),
moduleSelector = $allModules.selector || '',

time = Date.now(),
Expand All @@ -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 () {
Expand All @@ -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),
Expand Down
23 changes: 17 additions & 6 deletions src/definitions/modules/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
;
Expand All @@ -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,
Expand Down
16 changes: 14 additions & 2 deletions src/definitions/modules/sidebar.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand All @@ -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),
Expand Down Expand Up @@ -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);
Expand Down
27 changes: 16 additions & 11 deletions src/definitions/modules/sticky.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
;

Expand All @@ -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,

Expand Down Expand Up @@ -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);
}
Expand Down
Loading

0 comments on commit 2321380

Please sign in to comment.