diff --git a/modules/oojs-ui/OO.ui.Element.js b/modules/oojs-ui/OO.ui.Element.js index a212000cbb..903ef25276 100644 --- a/modules/oojs-ui/OO.ui.Element.js +++ b/modules/oojs-ui/OO.ui.Element.js @@ -82,22 +82,22 @@ OO.ui.Element.getJQuery = function ( context, frame ) { * Get the document of an element. * * @static - * @param {jQuery|HTMLElement|HTMLDocument|Window} context Context to bind the function to + * @param {jQuery|HTMLElement|HTMLDocument|Window} obj Object to get the document for * @returns {HTMLDocument} Document object * @throws {Error} If context is invalid */ -OO.ui.Element.getDocument = function ( context ) { +OO.ui.Element.getDocument = function ( obj ) { var doc = // jQuery - selections created "offscreen" won't have a context, so .context isn't reliable - ( context[0] && context[0].ownerDocument ) || + ( obj[0] && obj[0].ownerDocument ) || // Empty jQuery selections might have a context - context.context || + obj.context || // HTMLElement - context.ownerDocument || + obj.ownerDocument || // Window - context.document || + obj.document || // HTMLDocument - ( context.nodeType === 9 && context ); + ( obj.nodeType === 9 && obj ); if ( doc ) { return doc; @@ -110,14 +110,38 @@ OO.ui.Element.getDocument = function ( context ) { * Get the window of an element or document. * * @static - * @param {jQuery|HTMLElement|HTMLDocument|Window} context Context to bind the function to + * @param {jQuery|HTMLElement|HTMLDocument|Window} obj Context to get the window for * @returns {Window} Window object */ -OO.ui.Element.getWindow = function ( context ) { - var doc = this.getDocument( context ); +OO.ui.Element.getWindow = function ( obj ) { + var doc = this.getDocument( obj ); return doc.parentWindow || doc.defaultView; }; +/** + * Get the direction of an element or document. + * + * @static + * @param {jQuery|HTMLElement|HTMLDocument|Window} obj Context to get the direction for + * @returns {string} Text direction, either `ltr` or `rtl` + */ +OO.ui.Element.getDir = function ( obj ) { + var isDoc, isWin; + + if ( obj instanceof jQuery ) { + obj = obj[0]; + } + isDoc = obj.nodeType === 9; + isWin = obj.document !== undefined; + if ( isDoc || isWin ) { + if ( isWin ) { + obj = obj.document; + } + obj = obj.body; + } + return $( obj ).css( 'direction' ); +}; + /** * Get the offset between two frames. * diff --git a/modules/oojs-ui/layouts/OO.ui.GridLayout.js b/modules/oojs-ui/layouts/OO.ui.GridLayout.js index 729dabd2f5..fe62da90c2 100644 --- a/modules/oojs-ui/layouts/OO.ui.GridLayout.js +++ b/modules/oojs-ui/layouts/OO.ui.GridLayout.js @@ -139,7 +139,7 @@ OO.ui.GridLayout.prototype.update = function () { 'top': Math.round( top * 100 ) + '%' }; // If RTL, reverse: - if ( this.$.frame.dir === 'rtl' ) { + if ( OO.ui.Element.getDir( this.$.context ) === 'rtl' ) { dimensions.right = Math.round( left * 100 ) + '%'; } else { dimensions.left = Math.round( left * 100 ) + '%'; diff --git a/modules/oojs-ui/layouts/OO.ui.PanelLayout.js b/modules/oojs-ui/layouts/OO.ui.PanelLayout.js index 5c7ca6163f..9d9dc33f78 100644 --- a/modules/oojs-ui/layouts/OO.ui.PanelLayout.js +++ b/modules/oojs-ui/layouts/OO.ui.PanelLayout.js @@ -34,7 +34,7 @@ OO.ui.PanelLayout = function OoUiPanelLayout( config ) { } // Add directionality class: - this.$element.addClass( 'oo-ui-' + this.$.frame.dir ); + this.$element.addClass( 'oo-ui-' + OO.ui.Element.getDir( this.$.context ) ); }; /* Inheritance */