Skip to content
This repository was archived by the owner on Oct 8, 2021. It is now read-only.

Commit 78a24f2

Browse files
author
Gabriel Schulhof
committed
Helpers: Add getFocusElement() and safelyBlur()
1 parent 91440df commit 78a24f2

File tree

3 files changed

+47
-37
lines changed

3 files changed

+47
-37
lines changed

js/helpers.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,42 @@ define( [ "jquery", "./ns", "jquery-ui/jquery.ui.core" ], function( jQuery ) {
200200
}
201201
},
202202

203+
// IE9 sometimes throws an exception when one attempts to access document.activeElement
204+
getFocusElement: function( theDocument ) {
205+
try {
206+
return theDocument.activeElement;
207+
} catch( anException ) {}
208+
},
209+
210+
// Able to handle an element, a window, or a document
211+
// Given a window or a document, it will safely blur its active element
212+
// Given an element, it will safely blur it
213+
safelyBlur: function( entity ) {
214+
var theElement, theDocument, theWindow;
215+
216+
// document and window retrieval inspired by the widget factory
217+
if ( entity.style ) {
218+
theElement = entity;
219+
theDocument = element.ownerDocument;
220+
theWindow = ehtDocument.defaultView || theDocument.parentWindow;
221+
} else {
222+
if ( entity.document ) {
223+
theWindow = entity;
224+
theDocument = entity.document;
225+
theElement = $.mobile.getFocusElement( theDocument );
226+
} else {
227+
theDocument = entity;
228+
theWindow = theDocument.defaultView || theDocument.parentWindow;
229+
theElement = $.mobile.getFocusElement( theDocument );
230+
}
231+
}
232+
233+
if ( theElement && theElement !== theWindow &&
234+
theElement.nodeName.toLowerCase() !== "body" ) {
235+
$.event.trigger( "blur", undefined, theElement );
236+
}
237+
},
238+
203239
loading: function() {
204240
// If this is the first call to this function, instantiate a loader widget
205241
var loader = this.loading._widget || $( $.mobile.loader.prototype.defaultHtml ).loader(),

js/widgets/pagecontainer.js

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
define( [
66
"jquery",
77
"../core",
8+
"../helpers",
89
"../navigation/path",
910
"../navigation/base",
1011
"../events/navigate",
@@ -936,7 +937,7 @@ define( [
936937
transition: function( toPage, triggerData, settings ) {
937938
var fromPage, url, pageUrl, fileUrl, active, activeIsInitialPage, historyDir,
938939
pageTitle, isDialog, alreadyThere, newPageTitle, params, cssTransitionDeferred,
939-
beforeTransition, focusElement;
940+
beforeTransition;
940941

941942
// If we are in the midst of a transition, queue the current request.
942943
// We'll call changePage() once we're done with the current transition
@@ -1032,19 +1033,8 @@ define( [
10321033
historyDir = settings.direction === "back" ? -1 : 1;
10331034
}
10341035

1035-
// We blur the focussed element to cause the virtual keyboard to disappear, but we must
1036-
// wrap the retrieval of the focussed element in a try/catch block since IE9 throws
1037-
// "Unspecified error" if document.activeElement is undefined when we are in an IFrame.
1038-
try {
1039-
focusElement = this.document[ 0 ].activeElement;
1040-
}
1041-
catch( e ) {}
1042-
1043-
if ( focusElement &&
1044-
focusElement !== this.window[ 0 ] &&
1045-
focusElement.nodeName.toLowerCase() !== "body" ) {
1046-
$( focusElement ).blur();
1047-
}
1036+
// We blur the focused element to cause the virtual keyboard to disappear
1037+
$.mobile.safelyBlur( this.document[ 0 ] );
10481038

10491039
// Record whether we are at a place in history where a dialog used to be -
10501040
// if so, do not add a new history entry and do not change the hash either

js/widgets/popup.js

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ define( [
1616
"../links",
1717
"../widget",
1818
"../support",
19+
"../helpers",
1920
"../events/navigate",
2021
"../navigation/path",
2122
"../navigation/history",
@@ -26,17 +27,6 @@ define( [
2627
//>>excludeEnd("jqmBuildExclude");
2728
(function( $, undefined ) {
2829

29-
// IE9 sometimes throws an exception when one attempts to access document.activeElement
30-
function getFocusElement( document ) {
31-
var focusElement;
32-
33-
try {
34-
focusElement = document.activeElement;
35-
} catch( e ) {}
36-
37-
return focusElement;
38-
}
39-
4030
function fitSegmentInsideSegment( windowSize, segmentSize, offset, desired ) {
4131
var returnValue = desired;
4232

@@ -309,9 +299,10 @@ $.widget( "mobile.popup", {
309299
if ( targetElement !== ui.container[ 0 ] ) {
310300
target = $( targetElement );
311301
if ( !$.contains( ui.container[ 0 ], targetElement ) ) {
312-
$( getFocusElement( this.document[ 0 ] ) ).one( "focus", $.proxy( function() {
313-
this._safelyBlur( targetElement );
314-
}, this ) );
302+
$( $.mobile.getFocusElement( this.document[ 0 ] ) ).one( "focus",
303+
$.proxy( function() {
304+
$.mobile.safelyBlur( targetElement );
305+
}, this ) );
315306
ui.focusElement.focus();
316307
theEvent.preventDefault();
317308
theEvent.stopImmediatePropagation();
@@ -642,25 +633,18 @@ $.widget( "mobile.popup", {
642633
}
643634
},
644635

645-
_safelyBlur: function( currentElement ){
646-
if ( currentElement !== this.window[ 0 ] &&
647-
currentElement.nodeName.toLowerCase() !== "body" ) {
648-
$( currentElement ).blur();
649-
}
650-
},
651-
652636
_openPrerequisitesComplete: function() {
653637
var id = this.element.attr( "id" ),
654638
firstFocus = this._ui.container.find( ":focusable" ).first(),
655-
focusElement = getFocusElement( this.document[ 0 ] );
639+
focusElement = $.mobile.getFocusElement( this.document[ 0 ] );
656640

657641
this._ui.container.addClass( "ui-popup-active" );
658642
this._isOpen = true;
659643
this._resizeScreen();
660644

661645
// Check to see if currElement is not a child of the container. If it's not, blur
662646
if ( focusElement && !$.contains( this._ui.container[ 0 ], focusElement ) ) {
663-
this._safelyBlur( focusElement );
647+
$.mobile.safelyBlur( focusElement );
664648
}
665649
if ( firstFocus.length > 0 ) {
666650
this._ui.focusElement = firstFocus;

0 commit comments

Comments
 (0)