Skip to content

Commit 2321380

Browse files
authored
fix(chore): check if context exists in parent windows
This PR supports context in parent window objects
1 parent d8dd970 commit 2321380

File tree

12 files changed

+180
-48
lines changed

12 files changed

+180
-48
lines changed

src/definitions/behaviors/api.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,19 @@
3636
query = arguments[0],
3737
methodInvoked = typeof query === 'string',
3838
queryArguments = [].slice.call(arguments, 1),
39+
contextCheck = function (context, win) {
40+
var $context;
41+
if ([window, document].indexOf(context) >= 0) {
42+
$context = $(context);
43+
} else {
44+
$context = $(win.document).find(context);
45+
if ($context.length === 0) {
46+
$context = win.frameElement ? contextCheck(context, win.parent) : window;
47+
}
48+
}
3949

50+
return $context;
51+
},
4052
returnedValue
4153
;
4254

@@ -62,9 +74,7 @@
6274
$form = $module.closest(selector.form),
6375

6476
// context used for state
65-
$context = settings.stateContext
66-
? ([window, document].indexOf(settings.stateContext) < 0 ? $(document).find(settings.stateContext) : $(settings.stateContext))
67-
: $module,
77+
$context = settings.stateContext ? contextCheck(settings.stateContext, window) : $module,
6878

6979
// request details
7080
ajaxSettings,

src/definitions/behaviors/state.js

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,19 @@
3131
query = arguments[0],
3232
methodInvoked = typeof query === 'string',
3333
queryArguments = [].slice.call(arguments, 1),
34+
contextCheck = function (context, win) {
35+
var $context;
36+
if ([window, document].indexOf(context) >= 0) {
37+
$context = $(context);
38+
} else {
39+
$context = $(win.document).find(context);
40+
if ($context.length === 0) {
41+
$context = win.frameElement ? contextCheck(context, win.parent) : window;
42+
}
43+
}
3444

45+
return $context;
46+
},
3547
returnedValue
3648
;
3749
$allModules.each(function () {
@@ -51,6 +63,7 @@
5163
moduleNamespace = namespace + '-module',
5264

5365
$module = $(this),
66+
$context = settings.context ? contextCheck(settings.context, window) : $module,
5467

5568
element = this,
5669
instance = $module.data(moduleNamespace),
@@ -68,19 +81,11 @@
6881
}
6982

7083
// bind events with delegated events
71-
if (settings.context && moduleSelector !== '') {
72-
([window, document].indexOf(settings.context) < 0 ? $(document).find(settings.context) : $(settings.context))
73-
.on(moduleSelector, 'mouseenter' + eventNamespace, module.change.text)
74-
.on(moduleSelector, 'mouseleave' + eventNamespace, module.reset.text)
75-
.on(moduleSelector, 'click' + eventNamespace, module.toggle.state)
76-
;
77-
} else {
78-
$module
79-
.on('mouseenter' + eventNamespace, module.change.text)
80-
.on('mouseleave' + eventNamespace, module.reset.text)
81-
.on('click' + eventNamespace, module.toggle.state)
82-
;
83-
}
84+
$context
85+
.on('mouseenter' + eventNamespace, module.change.text)
86+
.on('mouseleave' + eventNamespace, module.reset.text)
87+
.on('click' + eventNamespace, module.toggle.state)
88+
;
8489
module.instantiate();
8590
},
8691

@@ -94,8 +99,11 @@
9499

95100
destroy: function () {
96101
module.verbose('Destroying previous module', instance);
97-
$module
102+
$context
98103
.off(eventNamespace)
104+
;
105+
$module
106+
.removeData(metadata.storedText)
99107
.removeData(moduleNamespace)
100108
;
101109
},

src/definitions/behaviors/visibility.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,19 @@
3030
query = arguments[0],
3131
methodInvoked = typeof query === 'string',
3232
queryArguments = [].slice.call(arguments, 1),
33+
contextCheck = function (context, win) {
34+
var $context;
35+
if ([window, document].indexOf(context) >= 0) {
36+
$context = $(context);
37+
} else {
38+
$context = $(win.document).find(context);
39+
if ($context.length === 0) {
40+
$context = win.frameElement ? contextCheck(context, win.parent) : window;
41+
}
42+
}
43+
44+
return $context;
45+
},
3346
returnedValue,
3447

3548
moduleCount = $allModules.length,
@@ -53,7 +66,7 @@
5366
$window = $(window),
5467

5568
$module = $(this),
56-
$context = [window, document].indexOf(settings.context) < 0 ? $(document).find(settings.context) : $(settings.context),
69+
$context = contextCheck(settings.context, window),
5770

5871
$placeholder,
5972

src/definitions/modules/dropdown.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,19 @@
3232
query = arguments[0],
3333
methodInvoked = typeof query === 'string',
3434
queryArguments = [].slice.call(arguments, 1),
35+
contextCheck = function (context, win) {
36+
var $context;
37+
if ([window, document].indexOf(context) >= 0) {
38+
$context = $(context);
39+
} else {
40+
$context = $(win.document).find(context);
41+
if ($context.length === 0) {
42+
$context = win.frameElement ? contextCheck(context, win.parent) : window;
43+
}
44+
}
45+
46+
return $context;
47+
},
3548
returnedValue
3649
;
3750

@@ -56,7 +69,7 @@
5669
moduleNamespace = 'module-' + namespace,
5770

5871
$module = $(this),
59-
$context = [window, document].indexOf(settings.context) < 0 ? $document.find(settings.context) : $(settings.context),
72+
$context = contextCheck(settings.context, window),
6073
$text = $module.find(selector.text),
6174
$search = $module.find(selector.search),
6275
$sizer = $module.find(selector.sizer),

src/definitions/modules/flyout.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,19 @@
3636
query = arguments[0],
3737
methodInvoked = typeof query === 'string',
3838
queryArguments = [].slice.call(arguments, 1),
39+
contextCheck = function (context, win) {
40+
var $context;
41+
if ([window, document].indexOf(context) >= 0) {
42+
$context = $body;
43+
} else {
44+
$context = $(win.document).find(context);
45+
if ($context.length === 0) {
46+
$context = win.frameElement ? contextCheck(context, win.parent) : $body;
47+
}
48+
}
3949

50+
return $context;
51+
},
4052
returnedValue
4153
;
4254

@@ -57,7 +69,7 @@
5769
moduleNamespace = 'module-' + namespace,
5870

5971
$module = $(this),
60-
$context = [window, document].indexOf(settings.context) < 0 ? $document.find(settings.context) : $body,
72+
$context = contextCheck(settings.context, window),
6173
$closeIcon = $module.find(selector.close),
6274
$inputs,
6375
$focusedElement,
@@ -527,7 +539,7 @@
527539
},
528540
refresh: function () {
529541
module.verbose('Refreshing selector cache');
530-
$context = [window, document].indexOf(settings.context) < 0 ? $document.find(settings.context) : $body;
542+
$context = contextCheck(settings.context, window);
531543
module.refreshFlyouts();
532544
$pusher = $context.children(selector.pusher);
533545
module.clear.cache();

src/definitions/modules/modal.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,19 @@
3434
query = arguments[0],
3535
methodInvoked = typeof query === 'string',
3636
queryArguments = [].slice.call(arguments, 1),
37+
contextCheck = function (context, win) {
38+
var $context;
39+
if ([window, document].indexOf(context) >= 0) {
40+
$context = $body;
41+
} else {
42+
$context = $(win.document).find(context);
43+
if ($context.length === 0) {
44+
$context = win.frameElement ? contextCheck(context, win.parent) : $body;
45+
}
46+
}
3747

48+
return $context;
49+
},
3850
returnedValue
3951
;
4052

@@ -54,7 +66,7 @@
5466
moduleNamespace = 'module-' + namespace,
5567

5668
$module = $(this),
57-
$context = [window, document].indexOf(settings.context) < 0 ? $document.find(settings.context) : $body,
69+
$context = contextCheck(settings.context, window),
5870
isBody = $context[0] === $body[0],
5971
$closeIcon = $module.find(selector.closeIcon),
6072
$inputs,

src/definitions/modules/nag.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
$.fn.nag = function (parameters) {
2323
var
2424
$allModules = $(this),
25+
$body = $('body'),
2526
moduleSelector = $allModules.selector || '',
2627

2728
time = Date.now(),
@@ -30,6 +31,19 @@
3031
query = arguments[0],
3132
methodInvoked = typeof query === 'string',
3233
queryArguments = [].slice.call(arguments, 1),
34+
contextCheck = function (context, win) {
35+
var $context;
36+
if ([window, document].indexOf(context) >= 0) {
37+
$context = $(context);
38+
} else {
39+
$context = $(win.document).find(context);
40+
if ($context.length === 0) {
41+
$context = win.frameElement ? contextCheck(context, win.parent) : $body;
42+
}
43+
}
44+
45+
return $context;
46+
},
3347
returnedValue
3448
;
3549
$allModules.each(function () {
@@ -47,9 +61,7 @@
4761

4862
$module = $(this),
4963

50-
$context = settings.context
51-
? ([window, document].indexOf(settings.context) < 0 ? $(document).find(settings.context) : $(settings.context))
52-
: $('body'),
64+
$context = settings.context ? contextCheck(settings.context, window) : $body,
5365

5466
element = this,
5567
instance = $module.data(moduleNamespace),

src/definitions/modules/popup.js

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,19 @@
3838
query = arguments[0],
3939
methodInvoked = typeof query === 'string',
4040
queryArguments = [].slice.call(arguments, 1),
41+
contextCheck = function (context, win) {
42+
var $context;
43+
if ([window, document].indexOf(context) >= 0) {
44+
$context = $(context);
45+
} else {
46+
$context = $(win.document).find(context);
47+
if ($context.length === 0) {
48+
$context = win.frameElement ? contextCheck(context, win.parent) : $body;
49+
}
50+
}
51+
52+
return $context;
53+
},
4154

4255
returnedValue
4356
;
@@ -57,12 +70,10 @@
5770
moduleNamespace = 'module-' + namespace,
5871

5972
$module = $(this),
60-
$context = [window, document].indexOf(settings.context) < 0 ? $document.find(settings.context) : $(settings.context),
61-
$scrollContext = [window, document].indexOf(settings.scrollContext) < 0 ? $document.find(settings.scrollContext) : $(settings.scrollContext),
62-
$boundary = [window, document].indexOf(settings.boundary) < 0 ? $document.find(settings.boundary) : $(settings.boundary),
63-
$target = settings.target
64-
? ([window, document].indexOf(settings.target) < 0 ? $document.find(settings.target) : $(settings.target))
65-
: $module,
73+
$context = contextCheck(settings.context, window),
74+
$scrollContext = contextCheck(settings.scrollContext, window),
75+
$boundary = contextCheck(settings.boundary, window),
76+
$target = settings.target ? contextCheck(settings.target, window) : $module,
6677

6778
$popup,
6879
$offsetParent,

src/definitions/modules/sidebar.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,19 @@
3636
query = arguments[0],
3737
methodInvoked = typeof query === 'string',
3838
queryArguments = [].slice.call(arguments, 1),
39+
contextCheck = function (context, win) {
40+
var $context;
41+
if ([window, document].indexOf(context) >= 0) {
42+
$context = $body;
43+
} else {
44+
$context = $(win.document).find(context);
45+
if ($context.length === 0) {
46+
$context = win.frameElement ? contextCheck(context, win.parent) : $body;
47+
}
48+
}
3949

50+
return $context;
51+
},
4052
returnedValue;
4153

4254
$allModules.each(function () {
@@ -55,7 +67,7 @@
5567
moduleNamespace = 'module-' + namespace,
5668

5769
$module = $(this),
58-
$context = [window, document].indexOf(settings.context) < 0 ? $document.find(settings.context) : $body,
70+
$context = contextCheck(settings.context, window),
5971
isBody = $context[0] === $body[0],
6072

6173
$sidebars = $module.children(selector.sidebar),
@@ -277,7 +289,7 @@
277289

278290
refresh: function () {
279291
module.verbose('Refreshing selector cache');
280-
$context = [window, document].indexOf(settings.context) < 0 ? $document.find(settings.context) : $body;
292+
$context = contextCheck(settings.context, window);
281293
module.refreshSidebars();
282294
$pusher = $context.children(selector.pusher);
283295
$fixed = $context.children(selector.fixed);

src/definitions/modules/sticky.js

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,19 @@
3131
query = arguments[0],
3232
methodInvoked = typeof query === 'string',
3333
queryArguments = [].slice.call(arguments, 1),
34+
contextCheck = function (context, win) {
35+
var $context;
36+
if ([window, document].indexOf(context) >= 0) {
37+
$context = $(context);
38+
} else {
39+
$context = $(win.document).find(context);
40+
if ($context.length === 0) {
41+
$context = win.frameElement ? contextCheck(context, win.parent) : window;
42+
}
43+
}
44+
45+
return $context;
46+
},
3447
returnedValue
3548
;
3649

@@ -49,7 +62,7 @@
4962

5063
$module = $(this),
5164
$window = $(window),
52-
$scroll = [window, document].indexOf(settings.scrollContext) < 0 ? $document.find(settings.scrollContext) : $(settings.scrollContext),
65+
$scroll = contextCheck(settings.scrollContext, window),
5366
$container,
5467
$context,
5568

@@ -127,19 +140,11 @@
127140
},
128141

129142
determineContainer: function () {
130-
if (settings.container) {
131-
$container = [window, document].indexOf(settings.container) < 0 ? $document.find(settings.container) : $(settings.container);
132-
} else {
133-
$container = $module.offsetParent();
134-
}
143+
$container = settings.container ? contextCheck(settings.container, window) : $module.offsetParent();
135144
},
136145

137146
determineContext: function () {
138-
if (settings.context) {
139-
$context = [window, document].indexOf(settings.context) < 0 ? $document.find(settings.context) : $(settings.context);
140-
} else {
141-
$context = $container;
142-
}
147+
$context = settings.context ? contextCheck(settings.context, window) : $container;
143148
if ($context.length === 0) {
144149
module.error(error.invalidContext, settings.context, $module);
145150
}

0 commit comments

Comments
 (0)