Skip to content

Commit

Permalink
Add RTL feature-detect to fix behavior in recent Chrome versions
Browse files Browse the repository at this point in the history
WebKit used to universally keep scrollbars on the right side in RTL mode,
but Chrome now swaps the scrollbar to the left side like other browsers.
Safari (6, at least) still leaves it on the right.
  • Loading branch information
Kenneth G. Franqueiro committed Apr 2, 2014
1 parent 76b3b1f commit 35f9504
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 17 deletions.
41 changes: 29 additions & 12 deletions List.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,16 @@ function(kernel, declare, dom, listen, has, miscUtil, TouchScroll, hasClass, put
return document.getElementById(id);
}

function getScrollbarSize(node, dimension){
function cleanupTestElement(element){
element.className = "";
document.body.removeChild(element);
}

function getScrollbarSize(element, dimension){
// Used by has tests for scrollbar width/height
var body = document.body,
size;

put(body, node, ".dgrid-scrollbar-measure");
size = node["offset" + dimension] - node["client" + dimension];

put(node, "!dgrid-scrollbar-measure");
body.removeChild(node);

put(document.body, element, ".dgrid-scrollbar-measure");
var size = element["offset" + dimension] - element["client" + dimension];
cleanupTestElement(element);
return size;
}
has.add("dom-scrollbar-width", function(global, doc, element){
Expand All @@ -31,6 +30,23 @@ function(kernel, declare, dom, listen, has, miscUtil, TouchScroll, hasClass, put
return getScrollbarSize(element, "Height");
});

has.add("dom-rtl-scrollbar-left", function(global, doc, element){
var div = put("div"),
isLeft;

put(document.body, element, ".dgrid-scrollbar-measure[dir=rtl]");
put(element, div);

// position: absolute makes IE always report child's offsetLeft as 0,
// but it conveniently makes other browsers reset to 0 as base, and all
// versions of IE are known to move the scrollbar to the left side for rtl
isLeft = !!has("ie") || div.offsetLeft >= has("dom-scrollbar-width");
cleanupTestElement(element);
put(div, "!");
element.removeAttribute("dir");
return isLeft;
});

// var and function for autogenerating ID when one isn't provided
var autogen = 0;
function generateId(){
Expand Down Expand Up @@ -249,7 +265,8 @@ function(kernel, declare, dom, listen, has, miscUtil, TouchScroll, hasClass, put
put(domNode, footerNode);

if(isRTL){
domNode.className += " dgrid-rtl" + (has("webkit") ? "" : " dgrid-rtl-nonwebkit");
domNode.className += " dgrid-rtl" +
(has("dom-rtl-scrollbar-left") ? " dgrid-rtl-swap" : "");
}

listen(bodyNode, "scroll", function(event){
Expand Down Expand Up @@ -340,7 +357,7 @@ function(kernel, declare, dom, listen, has, miscUtil, TouchScroll, hasClass, put
// a rule to account for scrollbar width in all grid headers.
miscUtil.addCssRule(".dgrid-header-row", "right: " + scrollbarWidth + "px");
// add another for RTL grids
miscUtil.addCssRule(".dgrid-rtl-nonwebkit .dgrid-header-row", "left: " + scrollbarWidth + "px");
miscUtil.addCssRule(".dgrid-rtl-swap .dgrid-header-row", "left: " + scrollbarWidth + "px");
}
}

Expand Down
10 changes: 5 additions & 5 deletions css/dgrid_rtl.css
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
/* rtl support
IMPORTANT:
WebKit places the scrollbars on the RIGHT, even when in RTL mode :(
Safari places the scrollbars on the right even when in RTL mode.
.dgrid-rtl is added to the domNode if the grid's isRTL is true
.dgrid-rtl-nonwebkit is also added to the domNode if
the grid's isRTL is true AND the client isn't WebKit
.dgrid-rtl-swap is also added to the domNode if isRTL is true and the
client swaps the vertical scrollbar to the left side
*/
.dgrid-rtl-nonwebkit .dgrid-header-row {
.dgrid-rtl-swap .dgrid-header-row {
right: 0;
left: 17px;
}

.dgrid-rtl-nonwebkit .dgrid-header-scroll {
.dgrid-rtl-swap .dgrid-header-scroll {
left: 0px;
right: auto;
}
Expand Down

0 comments on commit 35f9504

Please sign in to comment.