Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add player.currentWidth and player.currentHeight* methods for getting computed style #3144

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions src/js/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -1091,6 +1091,64 @@ class Component {
return parseInt(this.el_['offset' + toTitleCase(widthOrHeight)], 10);
}

/**
* Get width or height of computed style
* @param {String} widthOrHeight 'width' or 'height'
* @return {Number|Boolean} The bolean false if nothing was set
* @method currentDimension
*/
currentDimension(widthOrHeight) {
let computedWidthOrHeight = 0;

if (widthOrHeight !== 'width' && widthOrHeight !== 'height') {
throw new Error('currentDimension only accepts width or height value');
}

if (typeof window.getComputedStyle === 'function') {
const computedStyle = window.getComputedStyle(this.el_);
computedWidthOrHeight = computedStyle.getPropertyValue(widthOrHeight) || computedStyle[ widthOrHeight ];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very minor style comment: spaces inside [ and ].

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be fixed during merge; so, ignore me!

} else if (this.el_.currentStyle) {
// ie 8 doesn't support computed style, shim it
// return clientWidth or clientHeight instead for better accuracy
const rule = `offset${toTitleCase(widthOrHeight)}`;
computedWidthOrHeight = this.el_[rule];
}

// remove 'px' from variable and parse as integer
computedWidthOrHeight = parseFloat(computedWidthOrHeight);
return computedWidthOrHeight;
}

/**
* Get an object which contains width and height values of computed style
* @return {Object} The dimensions of element
* @method currentDimensions
*/
currentDimensions() {
return {
width: this.currentDimension('width'),
height: this.currentDimension('height')
};
}

/**
* Get width of computed style
* @return {Integer}
* @method currentWidth
*/
currentWidth() {
return this.currentDimension('width');
}

/**
* Get height of computed style
* @return {Integer}
* @method currentHeight
*/
currentHeight() {
return this.currentDimension('height');
}

/**
* Emit 'tap' events when touch events are supported
* This is used to support toggling the controls through a tap on the video.
Expand Down
28 changes: 28 additions & 0 deletions test/unit/component.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,34 @@ test('should change the width and height of a component', function(){
ok(comp.height() === 0, 'forced height was removed');
});

test('should get the computed dimensions', function(){
const container = document.createElement('div');
const comp = new Component(getFakePlayer(), {});
const el = comp.el();
const fixture = document.getElementById('qunit-fixture');

const computedWidth = '500px';
const computedHeight = '500px';

fixture.appendChild(container);
container.appendChild(el);
// Container of el needs dimensions or the component won't have dimensions
container.style.width = '1000px';
container.style.height = '1000px';

comp.width('50%');
comp.height('50%');

equal(comp.currentWidth() + 'px', computedWidth, 'matches computed width');
equal(comp.currentHeight() + 'px', computedHeight, 'matches computed height');

equal(comp.currentDimension('width') + 'px', computedWidth, 'matches computed width');
equal(comp.currentDimension('height') + 'px', computedHeight, 'matches computed height');

equal(comp.currentDimensions()['width'] + 'px', computedWidth, 'matches computed width');
equal(comp.currentDimensions()['height'] + 'px', computedHeight, 'matches computed width');

});

test('should use a defined content el for appending children', function(){
class CompWithContent extends Component {}
Expand Down