Skip to content

Commit

Permalink
fix(ionHeaderBar): make it align after elements properly load
Browse files Browse the repository at this point in the history
Addresses #945.
  • Loading branch information
ajoslin committed Apr 8, 2014
1 parent 6a1ac35 commit d00aaa5
Showing 1 changed file with 29 additions and 28 deletions.
57 changes: 29 additions & 28 deletions js/views/headerBarView.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,39 +22,40 @@
return;
}

var i, c, childSize;
var childNodes = this.el.childNodes;
var leftWidth = 0;
var rightWidth = 0;
var isCountingRightWidth = false;
var self = this;
//We have to rAF here so all of the elements have time to initialize
ionic.requestAnimationFrame(function() {
var i, c, childSize;
var childNodes = self.el.childNodes;
var leftWidth = 0;
var rightWidth = 0;
var isCountingRightWidth = false;

// Compute how wide the left children are
// Skip all titles (there may still be two titles, one leaving the dom)
// Once we encounter a titleEl, realize we are now counting the right-buttons, not left
for(i = 0; i < childNodes.length; i++) {
c = childNodes[i];
if (c.tagName && c.tagName.toLowerCase() == 'h1') {
isCountingRightWidth = true;
continue;
}
// Compute how wide the left children are
// Skip all titles (there may still be two titles, one leaving the dom)
// Once we encounter a titleEl, realize we are now counting the right-buttons, not left
for(i = 0; i < childNodes.length; i++) {
c = childNodes[i];
if (c.tagName && c.tagName.toLowerCase() == 'h1') {
isCountingRightWidth = true;
continue;
}

childSize = null;
if(c.nodeType == 3) {
childSize = ionic.DomUtil.getTextBounds(c);
} else if(c.nodeType == 1) {
childSize = c.getBoundingClientRect();
}
if(childSize) {
if (isCountingRightWidth) {
rightWidth += childSize.width;
} else {
leftWidth += childSize.width;
childSize = null;
if(c.nodeType == 3) {
childSize = ionic.DomUtil.getTextBounds(c).width;
} else if(c.nodeType == 1) {
childSize = c.offsetWidth;
}
if(childSize) {
if (isCountingRightWidth) {
rightWidth += childSize;
} else {
leftWidth += childSize;
}
}
}
}

var self = this;
ionic.requestAnimationFrame(function() {
var margin = Math.max(leftWidth, rightWidth) + 10;

// Size and align the header titleEl based on the sizes of the left and
Expand Down

0 comments on commit d00aaa5

Please sign in to comment.