Skip to content
This repository has been archived by the owner on Sep 5, 2024. It is now read-only.

Commit

Permalink
fix(rtl): support applying dir="rtl" on DOM elements other than the b…
Browse files Browse the repository at this point in the history
…ody (#11579)

Create a single function to check text direction based on dir attribute on DOM element or document.

Fixes #11016 Fixes #9754
  • Loading branch information
marosoft authored and jelbourn committed Jul 11, 2019
1 parent 0077d3e commit bc7833b
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 14 deletions.
5 changes: 2 additions & 3 deletions src/components/gridList/grid-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ angular.module('material.components.gridList', ['material.core'])
* </md-grid-list>
* </hljs>
*/
function GridListDirective($interpolate, $mdConstant, $mdGridLayout, $mdMedia) {
function GridListDirective($interpolate, $mdConstant, $mdGridLayout, $mdMedia, $mdUtil) {
return {
restrict: 'E',
controller: GridListController,
Expand Down Expand Up @@ -270,8 +270,7 @@ function GridListDirective($interpolate, $mdConstant, $mdGridLayout, $mdMedia) {

// The width and horizontal position of each tile is always calculated the same way, but the
// height and vertical position depends on the rowMode.
var ltr = document.dir != 'rtl' && document.body.dir != 'rtl';
var style = ltr ? {
var style = (!$mdUtil.isRtl(attrs)) ? {
left: POSITION({ unit: hUnit, offset: position.col, gutter: gutter }),
width: DIMENSION({ unit: hUnit, span: spans.col, gutter: gutter }),
// resets
Expand Down
2 changes: 1 addition & 1 deletion src/components/menu/js/menuServiceProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ function MenuProvider($$interimElementProvider) {
throw new Error('Invalid target mode "' + positionMode.top + '" specified for md-menu on Y axis.');
}

var rtl = ($mdUtil.bidi() === 'rtl');
var rtl = $mdUtil.isRtl(el);

switch (positionMode.left) {
case 'target':
Expand Down
2 changes: 1 addition & 1 deletion src/components/panel/panel.js
Original file line number Diff line number Diff line change
Expand Up @@ -2541,7 +2541,7 @@ function MdPanelPosition($injector) {
this._$window = $injector.get('$window');

/** @private {boolean} */
this._isRTL = $injector.get('$mdUtil').bidi() === 'rtl';
this._isRTL = $injector.get('$mdUtil').isRtl();

/** @private @const {!angular.$mdConstant} */
this._$mdConstant = $injector.get('$mdConstant');
Expand Down
2 changes: 1 addition & 1 deletion src/components/slider/slider.js
Original file line number Diff line number Diff line change
Expand Up @@ -647,7 +647,7 @@ function SliderDirective($$rAF, $window, $mdAria, $mdUtil, $mdConstant, $mdThemi
var size = vertical ? sliderDimensions.height : sliderDimensions.width;
var calc = (position - offset) / size;

if (!vertical && $mdUtil.bidi() === 'rtl') {
if (!vertical && $mdUtil.isRtl(attr)) {
calc = 1 - calc;
}

Expand Down
2 changes: 1 addition & 1 deletion src/components/tabs/js/tabsController.js
Original file line number Diff line number Diff line change
Expand Up @@ -962,6 +962,6 @@ function MdTabsController ($scope, $element, $window, $mdConstant, $mdTabInkRipp
}

function isRtl() {
return ($mdUtil.bidi() === 'rtl');
return $mdUtil.isRtl($attrs);
}
}
9 changes: 5 additions & 4 deletions src/components/virtualRepeat/virtual-repeater.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ function VirtualRepeatContainerController($$rAF, $mdUtil, $mdConstant, $parse, $
this.oldElementSize = null;
/** @type {!number} Maximum amount of pixels allowed for a single DOM element */
this.maxElementPixels = $mdConstant.ELEMENT_MAX_PIXELS;
/** @type {string} Direction of the text */
this.ltr = !$mdUtil.isRtl(this.$attrs);

if (this.$attrs.mdTopIndex) {
/** @type {function(angular.Scope): number} Binds to topIndex on AngularJS scope */
Expand Down Expand Up @@ -385,13 +387,12 @@ VirtualRepeatContainerController.prototype.resetScroll = function() {


VirtualRepeatContainerController.prototype.handleScroll_ = function() {
var ltr = document.dir !== 'rtl' && document.body.dir !== 'rtl';
if (!ltr && !this.maxSize) {
if (!this.ltr && !this.maxSize) {
this.scroller.scrollLeft = this.scrollSize;
this.maxSize = this.scroller.scrollLeft;
}
var offset = this.isHorizontal() ?
(ltr?this.scroller.scrollLeft : this.maxSize - this.scroller.scrollLeft)
(this.ltr ? this.scroller.scrollLeft : this.maxSize - this.scroller.scrollLeft)
: this.scroller.scrollTop;
if (this.scrollSize < this.size) return;
if (offset > this.scrollSize - this.size) {
Expand All @@ -405,7 +406,7 @@ VirtualRepeatContainerController.prototype.handleScroll_ = function() {
var numItems = Math.max(0, Math.floor(offset / itemSize) - NUM_EXTRA);

var transform = (this.isHorizontal() ? 'translateX(' : 'translateY(') +
(!this.isHorizontal() || ltr ? (numItems * itemSize) : - (numItems * itemSize)) + 'px)';
(!this.isHorizontal() || this.ltr ? (numItems * itemSize) : - (numItems * itemSize)) + 'px)';

this.scrollOffset = offset;
this.offsetter.style.webkitTransform = transform;
Expand Down
30 changes: 27 additions & 3 deletions src/core/util/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,36 @@ function UtilFactory($document, $timeout, $compile, $rootScope, $$mdAnimate, $in
return $options.getOption ? $options.getOption(optionName) : $options[optionName];
},

/**
* Determines the current 'dir'ectional value based on the value of 'dir'
* attribute of the element. If that is not defined, it will try to use
* a 'dir' attribute of the body or html tag.
*
* @param {Object=} attrs a hash object with key-value pairs of normalized
* attribute names and their corresponding attribute values.
* @returns {boolean} true if the element's passed in attributes,
* the document, or the body indicates RTL mode, false otherwise.
*/
isRtl: function(attrs) {
var dir = angular.isDefined(attrs) && attrs.hasOwnProperty('dir') && attrs.dir;

switch (dir) {
case 'ltr':
return false;

case 'rtl':
return true;
}

return ($document[0].dir === 'rtl' || $document[0].body.dir === 'rtl');
},

/**
* Bi-directional accessor/mutator used to easily update an element's
* property based on the current 'dir'ectional value.
*/
bidi : function(element, property, lValue, rValue) {
var ltr = !($document[0].dir == 'rtl' || $document[0].body.dir == 'rtl');
bidi: function(element, property, lValue, rValue) {
var ltr = !this.isRtl();

// If accessor
if (arguments.length == 0) return ltr ? 'ltr' : 'rtl';
Expand All @@ -111,7 +135,7 @@ function UtilFactory($document, $timeout, $compile, $rootScope, $$mdAnimate, $in
},

bidiProperty: function (element, lProperty, rProperty, value) {
var ltr = !($document[0].dir == 'rtl' || $document[0].body.dir == 'rtl');
var ltr = !this.isRtl();

var elem = angular.element(element);

Expand Down

0 comments on commit bc7833b

Please sign in to comment.