refactor shrink.

This commit is contained in:
Christopher Jeffrey 2013-07-17 05:03:50 -05:00
parent 1ae6a19b35
commit 8e81ef015b
1 changed files with 56 additions and 52 deletions

View File

@ -1962,12 +1962,12 @@ Box.prototype._getShrinkBox = function(xi, xl, yi, yl) {
return { xi: xi, xl: xi, yi: yi, yl: yi };
}
var i, el, mxi = 0, mxl = 0, myi = 0, myl = 0;
var i, el, ret, mxi = 0, mxl = 0, myi = 0, myl = 0;
for (i = 0; i < this.children.length; i++) {
el = this.children[i];
var ret = el.render(true);
ret = el.render(true);
if (!ret) continue;
if (ret.xi < mxi) mxi = ret.xi;
@ -1976,31 +1976,25 @@ Box.prototype._getShrinkBox = function(xi, xl, yi, yl) {
if (ret.yl > myl) myl = ret.yl;
}
var xll = xl, yll = yl;
if (this.options.left == null && this.options.right != null) {
if (this.options.width == null) xi = xl - (mxl - mxi);
} else {
if (this.options.width == null) xl = mxl;
if (this.options.width == null
&& (this.options.left == null
|| this.options.right == null)) {
if (this.options.left == null && this.options.right != null) {
xi = xl - (mxl - mxi);
} else {
xl = mxl;
}
}
if (this.options.top == null && this.options.bottom != null) {
if (this.options.height == null) yi = yl - (myl - myi);
} else {
if (this.options.height == null) yl = myl;
}
// Recenter shrunken elements.
if (xl < xll && this.options.left === 'center') {
xll = (xll - xl) / 2 | 0;
xi += xll;
xl += xll;
}
if (yl < yll && this.options.top === 'center') {
yll = (yll - yl) / 2 | 0;
yi += yll;
yl += yll;
if (this.options.height == null
&& (this.options.top == null
|| this.options.bottom == null)
&& this.childBase == null) {
if (this.options.top == null && this.options.bottom != null) {
yi = yl - (myl - myi);
} else {
yl = myl;
}
}
return { xi: xi, xl: xl, yi: yi, yl: yl };
@ -2009,9 +2003,7 @@ Box.prototype._getShrinkBox = function(xi, xl, yi, yl) {
Box.prototype._getShrinkContent = function(xi, xl, yi, yl, content) {
var hw = this._getShrinkSize(content)
, h = hw.height
, w = hw.width
, xll = xl
, yll = yl;
, w = hw.width;
if (this.options.width == null
&& (this.options.left == null
@ -2034,26 +2026,16 @@ Box.prototype._getShrinkContent = function(xi, xl, yi, yl, content) {
}
}
// Recenter shrunken elements.
if (xl < xll && this.options.left === 'center') {
xll = (xll - xl) / 2 | 0;
xi += xll;
xl += xll;
}
if (yl < yll && this.options.top === 'center') {
yll = (yll - yl) / 2 | 0;
yi += yll;
yl += yll;
}
return { xi: xi, xl: xl, yi: yi, yl: yl };
};
Box.prototype._getShrink = function(xi, xl, yi, yl, content) {
var shrinkBox = this._getShrinkBox(xi, xl, yi, yl)
, shrinkContent = this._getShrinkContent(xi, xl, yi, yl, content);
, shrinkContent = this._getShrinkContent(xi, xl, yi, yl, content)
, xll = xl
, yll = yl;
// Figure out which one is bigger and use it.
if (shrinkBox.xl - shrinkBox.xi > shrinkContent.xl - shrinkContent.xi) {
xi = shrinkBox.xi;
xl = shrinkBox.xl;
@ -2070,17 +2052,39 @@ Box.prototype._getShrink = function(xi, xl, yi, yl, content) {
yl = shrinkContent.yl;
}
// Add padding if we're shrinking.
if (this.padding) {
if (this.options.left == null && this.options.right != null) {
if (this.options.width == null) xi -= this.padding * 2;
} else {
if (this.options.width == null) xl += this.padding * 2;
// Recenter shrunken elements.
if (xl < xll && this.options.left === 'center') {
xll = (xll - xl) / 2 | 0;
xi += xll;
xl += xll;
}
if (yl < yll && this.options.top === 'center') {
yll = (yll - yl) / 2 | 0;
yi += yll;
yl += yll;
}
// Add padding, doesn't seem to be necessary.
if (0 && this.padding) {
if (this.options.width == null
&& (this.options.left == null
|| this.options.right == null)) {
if (this.options.left == null && this.options.right != null) {
xi -= this.padding * 2;
} else {
xl += this.padding * 2;
}
}
if (this.options.top == null && this.options.bottom != null) {
if (this.options.height == null) yi -= this.padding * 2;
} else {
if (this.options.height == null) yl += this.padding * 2;
if (this.options.height == null
&& (this.options.top == null
|| this.options.bottom == null)
&& this.childBase == null) {
if (this.options.top == null && this.options.bottom != null) {
yi -= this.padding * 2;
} else {
yl += this.padding * 2;
}
}
}