-
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
Slider updates - "separate" handle logic from the slider and allow for vertical or horizontal orientation #1816
Closed
Closed
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,9 @@ vjs.Slider = vjs.Component.extend({ | |
this.bar = this.getChild(this.options_['barName']); | ||
this.handle = this.getChild(this.options_['handleName']); | ||
|
||
// Set a horizontal or vertical class on the slider depending on the slider type | ||
this.vertical(!!this.options()['vertical']); | ||
|
||
this.on('mousedown', this.onMouseDown); | ||
this.on('touchstart', this.onMouseDown); | ||
this.on('focus', this.onFocus); | ||
|
@@ -78,56 +81,82 @@ vjs.Slider.prototype.update = function(){ | |
// If scrubbing, we could use a cached value to make the handle keep up with the user's mouse. | ||
// On HTML5 browsers scrubbing is really smooth, but some flash players are slow, so we might want to utilize this later. | ||
// var progress = (this.player_.scrubbing) ? this.player_.getCache().currentTime / this.player_.duration() : this.player_.currentTime() / this.player_.duration(); | ||
var progress = this.getPercent(); | ||
var handle = this.handle; | ||
var bar = this.bar; | ||
|
||
var barProgress, | ||
progress = this.getPercent(), | ||
handle = this.handle, | ||
bar = this.bar; | ||
// If there's no bar... | ||
if (!bar) return; | ||
|
||
// Protect against no duration and other division issues | ||
if (isNaN(progress)) { progress = 0; } | ||
|
||
barProgress = progress; | ||
|
||
// If there is a handle, we need to account for the handle in our calculation for progress bar | ||
// so that it doesn't fall short of or extend past the handle. | ||
if (handle) { | ||
var barProgress = this.updateHandlePosition(progress); | ||
|
||
var box = this.el_, | ||
boxWidth = box.offsetWidth, | ||
// Convert to a percentage for setting | ||
var percentage = vjs.round(barProgress * 100, 2) + '%'; | ||
|
||
handleWidth = handle.el().offsetWidth, | ||
// Set the new bar width or height | ||
if (this.options()['vertical']) { | ||
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 think this should check |
||
bar.el().style.height = percentage; | ||
} else { | ||
bar.el().style.width = percentage; | ||
} | ||
}; | ||
|
||
// The width of the handle in percent of the containing box | ||
// In IE, widths may not be ready yet causing NaN | ||
handlePercent = (handleWidth) ? handleWidth / boxWidth : 0, | ||
/** | ||
* Update the handle position. | ||
*/ | ||
vjs.Slider.prototype.updateHandlePosition = function(progress) { | ||
var handle = this.handle; | ||
if (!handle) return; | ||
|
||
var vertical = this.options()['vertical']; | ||
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.
|
||
var box = this.el_; | ||
|
||
var boxSize, handleSize; | ||
if (vertical) { | ||
boxSize = box.offsetHeight; | ||
handleSize = handle.el().offsetHeight; | ||
} else { | ||
boxSize = box.offsetWidth; | ||
handleSize = handle.el().offsetWidth; | ||
} | ||
|
||
// Get the adjusted size of the box, considering that the handle's center never touches the left or right side. | ||
// There is a margin of half the handle's width on both sides. | ||
boxAdjustedPercent = 1 - handlePercent, | ||
// The width of the handle in percent of the containing box | ||
// In IE, widths may not be ready yet causing NaN | ||
var handlePercent = (handleSize) ? handleSize / boxSize : 0, | ||
|
||
// Adjust the progress that we'll use to set widths to the new adjusted box width | ||
adjustedProgress = progress * boxAdjustedPercent; | ||
// Get the adjusted size of the box, considering that the handle's center never touches the left or right side. | ||
// There is a margin of half the handle's width on both sides. | ||
boxAdjustedPercent = 1 - handlePercent, | ||
|
||
// The bar does reach the left side, so we need to account for this in the bar's width | ||
barProgress = adjustedProgress + (handlePercent / 2); | ||
// Adjust the progress that we'll use to set widths to the new adjusted box width | ||
adjustedProgress = progress * boxAdjustedPercent; | ||
|
||
// Move the handle from the left based on the adjected progress | ||
handle.el().style.left = vjs.round(adjustedProgress * 100, 2) + '%'; | ||
} | ||
// The bar does reach the left side, so we need to account for this in the bar's width | ||
var barProgress = adjustedProgress + (handlePercent / 2); | ||
|
||
var percentage = vjs.round(adjustedProgress * 100, 2) + '%'; | ||
|
||
// Set the new bar width | ||
if (bar) { | ||
bar.el().style.width = vjs.round(barProgress * 100, 2) + '%'; | ||
if (vertical) { | ||
handle.el().style.bottom = percentage; | ||
} else { | ||
handle.el().style.left = percentage; | ||
} | ||
|
||
return barProgress; | ||
}; | ||
|
||
vjs.Slider.prototype.calculateDistance = function(event){ | ||
var el, box, boxX, boxY, boxW, boxH, handle, pageX, pageY; | ||
|
||
el = this.el_; | ||
box = vjs.findPosition(el); | ||
boxW = boxH = el.offsetWidth; | ||
boxW = el.offsetWidth; | ||
boxH = el.offsetHeight; | ||
handle = this.handle; | ||
|
||
if (this.options()['vertical']) { | ||
|
@@ -199,6 +228,23 @@ vjs.Slider.prototype.onClick = function(event){ | |
event.preventDefault(); | ||
}; | ||
|
||
vjs.Slider.prototype.vertical_ = false; | ||
vjs.Slider.prototype.vertical = function(bool) { | ||
if (bool === undefined) { | ||
return this.vertical_; | ||
} | ||
|
||
this.vertical_ = !!bool; | ||
|
||
if (this.vertical_) { | ||
this.addClass('vjs-slider-vertical'); | ||
} else { | ||
this.addClass('vjs-slider-horizontal'); | ||
} | ||
|
||
return this; | ||
}; | ||
|
||
/** | ||
* SeekBar Behavior includes play progress bar, and seek handle | ||
* Needed so it can determine seek position based on handle position/size | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Doesn't look like this handle var is being used anymore.