-
Notifications
You must be signed in to change notification settings - Fork 7.5k
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1091,6 +1091,67 @@ 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; | ||
|
||
if (widthOrHeight !== 'width' && widthOrHeight !== 'height') { | ||
log.warn('currentDimension only accepts width or height value'); | ||
return false; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It probably should return There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, we probably should throw an error in this case. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have thrown an exception. |
||
} | ||
|
||
if (typeof window.getComputedStyle !== 'undefined') { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. safer to check that it's equal to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok. |
||
const computedStyle = window.getComputedStyle(this.el_, null); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok. |
||
computedWidthOrHeight = computedStyle.getPropertyValue(widthOrHeight) || computedStyle[ widthOrHeight ]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Very minor style comment: spaces inside There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be fixed during merge; so, ignore me! |
||
} | ||
|
||
if (this.el_.currentStyle) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should be an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok. |
||
// ie 8 doesn't support computed style, shim it | ||
// return clientWidth or clientHeight instead for better accuracy | ||
const rule = 'client' + widthOrHeight.substr(0, 1).toUpperCase() + widthOrHeight.substr(1); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. would const rule = `client${toTitleCase(widthOrHeight)}` There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have converted to offsetWidth. |
||
computedWidthOrHeight = this.el_[rule]; | ||
} | ||
|
||
// remove 'px' from variable and parse as integer | ||
computedWidthOrHeight = parseInt(computedWidthOrHeight, 10); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we should use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok. |
||
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. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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(){ | ||
var container = document.createElement('div'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we try to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok. |
||
var comp = new Component(getFakePlayer(), {}); | ||
var el = comp.el(); | ||
var fixture = document.getElementById('qunit-fixture'); | ||
|
||
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%'); | ||
|
||
var computedWidth = TestHelpers.getComputedStyle(el, 'width'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we should probably hard code these values since otherwise we're basically testing that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok 👍 |
||
var computedHeight = TestHelpers.getComputedStyle(el, 'height'); | ||
|
||
ok(computedWidth === comp.currentWidth() + 'px', 'matches computed width'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it would be better to use the equal(comp.currentWidth() + 'px', computedWidth, 'matches computed width'); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok. |
||
ok(computedHeight === comp.currentHeight() + 'px', 'matches computed height'); | ||
|
||
ok(computedWidth === comp.currentDimension('width') + 'px', 'matches computed width'); | ||
ok(computedHeight === comp.currentDimension('height') + 'px', 'matches computed height'); | ||
|
||
ok(computedWidth === comp.currentDimensions()['width'] + 'px', 'matches computed width'); | ||
ok(computedHeight === comp.currentDimensions()['height'] + 'px', 'matches computed width'); | ||
|
||
}); | ||
|
||
test('should use a defined content el for appending children', function(){ | ||
class CompWithContent extends Component {} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also, the default for this variable should probably be
0
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok.