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

Commit

Permalink
refactor(bidi): add mdUtil.bidi() method
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasBurleson committed Feb 28, 2016
1 parent 5cf32d0 commit d5a887c
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 25 deletions.
3 changes: 2 additions & 1 deletion src/components/menu/js/menuServiceProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,8 @@ function MenuProvider($$interimElementProvider) {
throw new Error('Invalid target mode "' + positionMode.top + '" specified for md-menu on Y axis.');
}

var rtl = $document[0].dir == 'rtl' || $document[0].body.dir == 'rtl';
var rtl = ($mdUtil.bidi() == 'rtl');

switch (positionMode.left) {
case 'target':
position.left = existingOffsets.left + originNodeRect.left - alignTargetRect.left;
Expand Down
33 changes: 9 additions & 24 deletions src/components/sticky/sticky.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,18 +197,10 @@ function MdSticky($document, $mdConstant, $$rAF, $mdUtil, $compile) {
current = current.offsetParent;
}
item.height = item.element.prop('offsetHeight');
var ltr = !($document[0].dir == 'rtl' || $document[0].body.dir == 'rtl');
if(ltr) {
item.clone.css('margin-left', item.left + 'px');
if ($mdUtil.floatingScrollbars()) {
item.clone.css('margin-right', '0');
}
} else {
item.clone.css('margin-right', item.right + 'px');
if ($mdUtil.floatingScrollbars()) {
item.clone.css('margin-left', '0');
}
}

var defaultVal = $mdUtil.floatingScrollbars() ? '0' : undefined;
$mdUtil.bidi(item.clone, 'margin-left', item.left, defaultVal);
$mdUtil.bidi(item.clone, 'margin-right', defaultVal, item.right);
}

// As we scroll, push in and select the correct sticky element.
Expand Down Expand Up @@ -314,18 +306,11 @@ function MdSticky($document, $mdConstant, $$rAF, $mdUtil, $compile) {
}
} else {
item.translateY = amount;
var ltr = !($document[0].dir == 'rtl' || $document[0].body.dir == 'rtl');
if(ltr) {
item.clone.css(
$mdConstant.CSS.TRANSFORM,
'translate3d(' + item.left + 'px,' + amount + 'px,0)'
);
} else {
item.clone.css(
$mdConstant.CSS.TRANSFORM,
'translateY(' + amount + 'px)'
);
}

$mdUtil.bidi( item.clone, $mdConstant.CSS.TRANSFORM,
'translate3d(' + item.left + 'px,' + amount + 'px,0)',
'translateY(' + amount + 'px)'
);
}
}
}
Expand Down
29 changes: 29 additions & 0 deletions src/core/util/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,35 @@ function UtilFactory($document, $timeout, $compile, $rootScope, $$mdAnimate, $in
return new Date().getTime();
},

/**
* 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');

// If accessor
if ( arguments.length == 0 ) return ltr ? 'ltr' : 'rtl';

// If mutator
if ( ltr && angular.isDefined(lValue)) {
angular.element(element).css(property, validate(lValue));
}
else if ( !ltr && angular.isDefined(rValue)) {
angular.element(element).css(property, validate(rValue) );
}

// Internal utils

function validate(value) {
return !value ? '0' :
hasPx(value) ? value : value + 'px';
}
function hasPx(value) {
return String(value).indexOf('px') > -1;
}
},

clientRect: function(element, offsetParent, isOffsetRect) {
var node = getNode(element);
offsetParent = getNode(offsetParent || node.offsetParent || document.body);
Expand Down

0 comments on commit d5a887c

Please sign in to comment.