|
88 | 88 | tempBodyMargin = '',
|
89 | 89 | keepScrollingClass = false,
|
90 | 90 | hadScrollbar = false,
|
| 91 | + windowRefocused = false, |
91 | 92 |
|
92 | 93 | elementEventNamespace,
|
93 | 94 | id,
|
|
251 | 252 | .off(eventNamespace)
|
252 | 253 | ;
|
253 | 254 | $window.off(elementEventNamespace);
|
| 255 | + $context.off(elementEventNamespace); |
254 | 256 | $dimmer.off(elementEventNamespace);
|
255 | 257 | $closeIcon.off(elementEventNamespace);
|
256 | 258 | if ($inputs) {
|
|
272 | 274 | return nodes;
|
273 | 275 | },
|
274 | 276 | shouldRefresh = false,
|
275 |
| - shouldRefreshInputs = false |
| 277 | + shouldRefreshInputs = false, |
| 278 | + ignoreAutofocus = true |
276 | 279 | ;
|
277 | 280 | mutations.every(function (mutation) {
|
278 | 281 | if (mutation.type === 'attributes') {
|
279 |
| - if (observeAttributes && (mutation.attributeName === 'disabled' || $(mutation.target).find(':input').addBack(':input').length > 0)) { |
| 282 | + if (observeAttributes && (mutation.attributeName === 'disabled' || $(mutation.target).find(':input').addBack(':input').filter(':visible').length > 0)) { |
280 | 283 | shouldRefreshInputs = true;
|
281 | 284 | }
|
282 | 285 | } else {
|
|
287 | 290 | $removedInputs = $(collectNodes(mutation.removedNodes)).filter('a[href], [tabindex], :input');
|
288 | 291 | if ($addedInputs.length > 0 || $removedInputs.length > 0) {
|
289 | 292 | shouldRefreshInputs = true;
|
| 293 | + ignoreAutofocus = false; |
290 | 294 | }
|
291 | 295 | }
|
292 | 296 |
|
|
298 | 302 | module.refresh();
|
299 | 303 | }
|
300 | 304 | if (shouldRefreshInputs) {
|
301 |
| - module.refreshInputs(); |
| 305 | + module.refreshInputs(ignoreAutofocus); |
302 | 306 | }
|
303 | 307 | });
|
304 | 308 | observer.observe(element, {
|
|
326 | 330 | $allModals = $otherModals.add($module);
|
327 | 331 | },
|
328 | 332 |
|
329 |
| - refreshInputs: function () { |
| 333 | + refreshInputs: function (ignoreAutofocus) { |
330 | 334 | if ($inputs) {
|
331 | 335 | $inputs
|
332 | 336 | .off('keydown' + elementEventNamespace)
|
|
335 | 339 | $inputs = $module.find('a[href], [tabindex], :input:enabled').filter(':visible').filter(function () {
|
336 | 340 | return $(this).closest('.disabled').length === 0;
|
337 | 341 | });
|
338 |
| - if ($inputs.length === 0) { |
339 |
| - $inputs = $module; |
| 342 | + if ($inputs.filter(':input').length === 0) { |
| 343 | + $inputs = $module.add($inputs); |
340 | 344 | $module.attr('tabindex', -1);
|
341 | 345 | } else {
|
342 | 346 | $module.removeAttr('tabindex');
|
|
347 | 351 | $inputs.last()
|
348 | 352 | .on('keydown' + elementEventNamespace, module.event.inputKeyDown.last)
|
349 | 353 | ;
|
350 |
| - if (settings.autofocus && $inputs.filter(':focus').length === 0) { |
| 354 | + if (!ignoreAutofocus && settings.autofocus && $inputs.filter(':focus').length === 0) { |
351 | 355 | module.set.autofocus();
|
352 | 356 | }
|
353 | 357 | },
|
|
385 | 389 | .on('resize' + elementEventNamespace, module.event.resize)
|
386 | 390 | .on('focus' + elementEventNamespace, module.event.focus)
|
387 | 391 | ;
|
| 392 | + $context |
| 393 | + .on('click' + elementEventNamespace, module.event.click) |
| 394 | + ; |
388 | 395 | },
|
389 | 396 | scrollLock: function () {
|
390 | 397 | // touch events default to passive, due to changes in chrome to optimize mobile perf
|
|
542 | 549 | }
|
543 | 550 | },
|
544 | 551 | focus: function () {
|
545 |
| - if ($dimmable.dimmer('is active') && module.is.active() && settings.autofocus) { |
| 552 | + windowRefocused = true; |
| 553 | + }, |
| 554 | + click: function (event) { |
| 555 | + if (windowRefocused && document.activeElement !== event.target && $dimmable.dimmer('is active') && module.is.active() && settings.autofocus && $(document.activeElement).closest(selector.modal).length === 0) { |
546 | 556 | requestAnimationFrame(module.set.autofocus);
|
547 | 557 | }
|
| 558 | + windowRefocused = false; |
548 | 559 | },
|
549 | 560 | },
|
550 | 561 |
|
|
1054 | 1065 | var
|
1055 | 1066 | $autofocus = $inputs.filter('[autofocus]'),
|
1056 | 1067 | $rawInputs = $inputs.filter(':input'),
|
1057 |
| - $input = $autofocus.length > 0 |
1058 |
| - ? $autofocus.first() |
| 1068 | + $input = ($autofocus.length > 0 |
| 1069 | + ? $autofocus |
1059 | 1070 | : ($rawInputs.length > 0
|
1060 | 1071 | ? $rawInputs
|
1061 |
| - : $inputs.filter(':not(i.close)') |
1062 |
| - ).first() |
| 1072 | + : $module) |
| 1073 | + ).first() |
1063 | 1074 | ;
|
1064 |
| - // check if only the close icon is remaining |
1065 |
| - if ($input.length === 0 && $inputs.length > 0) { |
1066 |
| - $input = $inputs.first(); |
1067 |
| - } |
1068 |
| - if ($input.length > 0) { |
1069 |
| - $input.trigger('focus'); |
1070 |
| - } |
| 1075 | + $input.trigger('focus'); |
1071 | 1076 | },
|
1072 | 1077 | bodyMargin: function () {
|
1073 | 1078 | var position = module.can.leftBodyScrollbar() ? 'left' : 'right';
|
|
0 commit comments