diff --git a/src/components/panel/panel.js b/src/components/panel/panel.js index 234f4e2a54c..b053121e82f 100644 --- a/src/components/panel/panel.js +++ b/src/components/panel/panel.js @@ -676,7 +676,7 @@ angular * @description * Sets the value of the offset in the x-direction. * - * @param {string} offsetX + * @param {string|number} offsetX * @returns {!MdPanelPosition} */ @@ -686,7 +686,7 @@ angular * @description * Sets the value of the offset in the y-direction. * - * @param {string} offsetY + * @param {string|number} offsetY * @returns {!MdPanelPosition} */ @@ -2532,11 +2532,11 @@ MdPanelPosition.prototype._validateXPosition = function(xPosition) { /** * Sets the value of the offset in the x-direction. This will add to any * previously set offsets. - * @param {string|function(MdPanelPosition): string} offsetX + * @param {string|number|function(MdPanelPosition): string} offsetX * @returns {!MdPanelPosition} */ MdPanelPosition.prototype.withOffsetX = function(offsetX) { - this._translateX.push(offsetX); + this._translateX.push(addUnits(offsetX)); return this; }; @@ -2544,11 +2544,11 @@ MdPanelPosition.prototype.withOffsetX = function(offsetX) { /** * Sets the value of the offset in the y-direction. This will add to any * previously set offsets. - * @param {string|function(MdPanelPosition): string} offsetY + * @param {string|number|function(MdPanelPosition): string} offsetY * @returns {!MdPanelPosition} */ MdPanelPosition.prototype.withOffsetY = function(offsetY) { - this._translateY.push(offsetY); + this._translateY.push(addUnits(offsetY)); return this; }; @@ -2664,9 +2664,8 @@ MdPanelPosition.prototype.getActualPosition = function() { MdPanelPosition.prototype._reduceTranslateValues = function(translateFn, values) { return values.map(function(translation) { - // TODO(crisbeto): this should add the units after #9609 is merged. var translationValue = angular.isFunction(translation) ? - translation(this) : translation; + addUnits(translation(this)) : translation; return translateFn + '(' + translationValue + ')'; }, this).join(' '); }; @@ -3186,7 +3185,6 @@ function getElement(el) { return angular.element(queryResult); } - /** * Gets the computed values for an element's translateX and translateY in px. * @param {!angular.JQLite|!Element} el @@ -3214,3 +3212,12 @@ function getComputedTranslations(el, property) { return output; } + +/** + * Adds units to a number value. + * @param {string|number} value + * @return {string} + */ +function addUnits(value) { + return angular.isNumber(value) ? value + 'px' : value; +} diff --git a/src/components/panel/panel.spec.js b/src/components/panel/panel.spec.js index b32051b3150..f35a8d086a2 100644 --- a/src/components/panel/panel.spec.js +++ b/src/components/panel/panel.spec.js @@ -1804,6 +1804,26 @@ describe('$mdPanel', function() { .toBeApproximately(middleOfPage + parseInt(offset)); }); + it('horizontally with an integer value', function() { + var left = '50px'; + var offset = -15; + + var position = mdPanelPosition + .absolute() + .left(left) + .withOffsetX(offset); + + config['position'] = position; + + openPanel(config); + + var panelRect = document.querySelector(PANEL_EL) + .getBoundingClientRect(); + + expect(panelRect.left) + .toBeApproximately(parseInt(left) + offset); + }); + it('vertically', function() { var top = '50px'; var offset = '-15px'; @@ -1873,6 +1893,50 @@ describe('$mdPanel', function() { expect(middleOfPanel) .toBeApproximately(middleOfPage + parseInt(offset)); }); + + it('vertically with an integer value', function() { + var top = '50px'; + var offset = -15; + + var position = mdPanelPosition + .absolute() + .top(top) + .withOffsetY(offset); + + config['position'] = position; + + openPanel(config); + + var panelRect = document.querySelector(PANEL_EL) + .getBoundingClientRect(); + + expect(panelRect.top) + .toBeApproximately(parseInt(top) + offset); + }); + + it('with a function that does not return units', function() { + var left = '50px'; + var offset = -15; + var obj = { + getOffsetX: function() { + return offset; + } + }; + + var position = mdPanelPosition + .absolute() + .left(left) + .withOffsetX(obj.getOffsetX); + + config['position'] = position; + + openPanel(config); + + var panelRect = document.querySelector(PANEL_EL) + .getBoundingClientRect(); + + expect(panelRect.left).toBeApproximately(parseInt(left) + offset); + }); }); describe('should absolutely position the panel at', function() {