-
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
Default dimensions #2482
Default dimensions #2482
Changes from 5 commits
229a60a
c705b59
b81bba2
26e55c3
01191ed
cc2511a
c020041
58088ba
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 |
---|---|---|
|
@@ -15,6 +15,7 @@ import log from './utils/log.js'; | |
import toTitleCase from './utils/to-title-case.js'; | ||
import { createTimeRange } from './utils/time-ranges.js'; | ||
import { bufferedPercent } from './utils/buffer.js'; | ||
import * as stylesheet from './utils/stylesheet.js'; | ||
import FullscreenApi from './fullscreen-api.js'; | ||
import MediaError from './media-error.js'; | ||
import safeParseTuple from 'safe-json-parse/tuple'; | ||
|
@@ -274,8 +275,10 @@ class Player extends Component { | |
// Add a style element in the player that we'll use to set the width/height | ||
// of the player in a way that's still overrideable by CSS, just like the | ||
// video element | ||
this.styleEl_ = document.createElement('style'); | ||
el.appendChild(this.styleEl_); | ||
this.styleEl_ = stylesheet.getStyleElement('vjs-styles-dimensions'); | ||
let defaultsStyleEl = document.querySelector('.vjs-styles-defaults'); | ||
let head = document.querySelector('head'); | ||
head.insertBefore(this.styleEl_, defaultsStyleEl ? defaultsStyleEl.nextSibling : head.firstChild); | ||
|
||
// Pass in the width/height/aspectRatio options which will update the style el | ||
this.width(this.options_.width); | ||
|
@@ -448,17 +451,16 @@ class Player extends Component { | |
// Ensure the right class is still on the player for the style element | ||
this.addClass(idClass); | ||
|
||
// Create the width/height CSS | ||
var css = `.${idClass} { width: ${width}px; height: ${height}px; }`; | ||
// Add the aspect ratio CSS for when using a fluid layout | ||
css += `.${idClass}.vjs-fluid { padding-top: ${ratioMultiplier * 100}%; }`; | ||
stylesheet.setTextContent(this.styleEl_, ` | ||
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'm using one template string here to add both the previous css selectors to the style element. 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. Beautiful. |
||
.${idClass} { | ||
width: ${width}px; | ||
height: ${height}px; | ||
} | ||
|
||
// Update the style el | ||
if (this.styleEl_.styleSheet){ | ||
this.styleEl_.styleSheet.cssText = css; | ||
} else { | ||
this.styleEl_.innerHTML = css; | ||
} | ||
.${idClass}.vjs-fluid { | ||
padding-top: ${ratioMultiplier * 100}%; | ||
} | ||
`); | ||
} | ||
|
||
/** | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import document from 'global/document'; | ||
|
||
export let getStyleElement = function(className) { | ||
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 should probably be named 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. Make sense. I guess it could possibly look to see if a style element with the same class name exists and return that and create one, otherwise, or something, but I don't think that's that useful currently. 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. Yeah, since each player needs its own style element I don't think we need a getter. Which reminds me, should we have something that cleans up the style el when the player is disposed? Previously I think it was just relying on the style el being removed with the player 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. probably. I can add that in along with this rename. 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. Updated. |
||
let style = document.createElement('style'); | ||
style.className = className; | ||
|
||
return style; | ||
}; | ||
|
||
export let setTextContent = function(el, content) { | ||
if (el.styleSheet) { | ||
el.styleSheet.cssText = content; | ||
} else { | ||
el.textContent = content; | ||
} | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -172,7 +172,6 @@ test('should set the width, height, and aspect ratio via a css class', function( | |
}; | ||
|
||
// Initial state | ||
ok(player.styleEl_.parentNode === player.el(), 'player has a style element'); | ||
ok(!getStyleText(player.styleEl_), 'style element should be empty when the player is given no dimensions'); | ||
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're no longer adding this element to the player. The tests below should cover whether the styles are applied correctly or not. |
||
|
||
// Set only the width | ||
|
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.
Should we worry about scenarios where
defaultsStyleEl
is the only thing in the head. Or if there is no head for some reason?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.
I don't think it's worth worrying about if there's no head. I think looking for the
defaultsStyleEl
and prepending to HEAD otherwise, is quick and easy enough to do.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.
Sounds good.