-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for common module systems
- Loading branch information
Showing
6 changed files
with
138 additions
and
120 deletions.
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
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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
{ | ||
"name": "progressor", | ||
"main": "progressor.js", | ||
"version": "0.3.0", | ||
"version": "1.0.0", | ||
"homepage": "https://github.com/ejb/progressor", | ||
"authors": [ | ||
"ejb <[email protected]>" | ||
|
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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
{ | ||
"name": "progressor.js", | ||
"description": "Lightweight, customisable progress bars for HTML5 video & audio", | ||
"version": "0.3.0", | ||
"version": "1.0.0", | ||
"homepage": "https://github.com/ejb/progressor.js", | ||
"author": "Elliot Bentley <[email protected]> (http://ejb.github.io)", | ||
"keywords": [ | ||
|
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 |
---|---|---|
@@ -1,131 +1,133 @@ | ||
function Progressor( options ){ | ||
this._media = options.media; | ||
this._bar = options.bar; | ||
this._text = options.text; | ||
this._time = options.time; | ||
this.initProgressBar(); | ||
this.initMedia(); | ||
}; | ||
(function(name, definition) { | ||
if (typeof module != 'undefined') module.exports = definition(); | ||
else if (typeof define == 'function' && typeof define.amd == 'object') define(definition); | ||
else this[name] = definition(); | ||
}('progressor', function() { | ||
function Progressor( options ){ | ||
this._media = options.media; | ||
this._bar = options.bar; | ||
this._text = options.text; | ||
this._time = options.time; | ||
this.initProgressBar(); | ||
this.initMedia(); | ||
}; | ||
|
||
|
||
Progressor.prototype.initMedia = function() { | ||
this._media.addEventListener('timeupdate', this.updateProgress.bind(this), false); | ||
this._media.addEventListener('timeupdate', this.updateTimeCount.bind(this), false); | ||
this.addClickEvents(); | ||
this.updateTimeCount(this._media); | ||
}; | ||
Progressor.prototype.initMedia = function() { | ||
this._media.addEventListener('timeupdate', this.updateProgress.bind(this), false); | ||
this._media.addEventListener('timeupdate', this.updateTimeCount.bind(this), false); | ||
this.addClickEvents(); | ||
this.updateTimeCount(this._media); | ||
}; | ||
|
||
Progressor.prototype.initProgressBar = function(){ | ||
this._textBox = document.createElement('span'); | ||
this._textBox.textContent = this._text || ""; | ||
this._bar.style.position = "relative"; | ||
this._bar.style.zIndex = 1; | ||
this._bar.className = this._bar.className + " progressor"; | ||
Progressor.prototype.initProgressBar = function(){ | ||
this._textBox = document.createElement('span'); | ||
this._textBox.textContent = this._text || ""; | ||
this._bar.style.position = "relative"; | ||
this._bar.style.zIndex = 1; | ||
this._bar.className = this._bar.className + " progressor"; | ||
|
||
this._progress = document.createElement('div'); | ||
this._progress.className = "progressor-progress"; | ||
this._progress.style.width = "0%"; | ||
this._progress.style.height = "100%"; | ||
this._progress.style.position = "absolute"; | ||
this._progress.style.top = 0; | ||
this._progress.style.zIndex = -1; | ||
this._progress = document.createElement('div'); | ||
this._progress.className = "progressor-progress"; | ||
this._progress.style.width = "0%"; | ||
this._progress.style.height = "100%"; | ||
this._progress.style.position = "absolute"; | ||
this._progress.style.top = 0; | ||
this._progress.style.zIndex = -1; | ||
|
||
this._bar.style.webkitUserSelect = "none"; | ||
this._bar.style.userSelect = "none"; | ||
this._bar.appendChild ( this._textBox ); | ||
this._bar.appendChild( this._progress ); | ||
}; | ||
this._bar.style.webkitUserSelect = "none"; | ||
this._bar.style.userSelect = "none"; | ||
this._bar.appendChild ( this._textBox ); | ||
this._bar.appendChild( this._progress ); | ||
}; | ||
|
||
Progressor.prototype.updateProgress = function() { | ||
this.updateTimeCount(); | ||
var value = 0; | ||
if (this._media.currentTime > 0) { | ||
value =(100 / this._media.duration) * this._media.currentTime; | ||
} | ||
// this._bar.getElementsByTagName('div')[0].clientWidth = value + "%"; | ||
this._bar.getElementsByTagName('div')[0].style.width = value + "%"; | ||
}; | ||
|
||
Progressor.prototype.formatTime = function ( time ) { | ||
var minutes = Math.floor(time / 60); | ||
var seconds = ("0" + Math.round( time - minutes * 60 ) ).slice(-2); | ||
return minutes+":"+seconds; | ||
} | ||
Progressor.prototype.updateProgress = function() { | ||
this.updateTimeCount(); | ||
var value = 0; | ||
if (this._media.currentTime > 0) { | ||
value =(100 / this._media.duration) * this._media.currentTime; | ||
} | ||
// this._bar.getElementsByTagName('div')[0].clientWidth = value + "%"; | ||
this._bar.getElementsByTagName('div')[0].style.width = value + "%"; | ||
}; | ||
|
||
Progressor.prototype.updateTimeCount = function(){ | ||
if ( this._time ) { | ||
var currTime = this.formatTime ( this._media.currentTime ); | ||
var totalTime = this.formatTime ( this._media.duration ); | ||
if ( isNaN( this._media.duration ) === true ) { totalTime = "00:00" }; | ||
this._time.innerHTML = currTime + "/" + totalTime; | ||
Progressor.prototype.formatTime = function ( time ) { | ||
var minutes = Math.floor(time / 60); | ||
var seconds = ("0" + Math.round( time - minutes * 60 ) ).slice(-2); | ||
return minutes+":"+seconds; | ||
} | ||
}; | ||
|
||
Progressor.prototype.updateTimeCount = function(){ | ||
if ( this._time ) { | ||
var currTime = this.formatTime ( this._media.currentTime ); | ||
var totalTime = this.formatTime ( this._media.duration ); | ||
if ( isNaN( this._media.duration ) === true ) { totalTime = "00:00" }; | ||
this._time.innerHTML = currTime + "/" + totalTime; | ||
} | ||
}; | ||
|
||
Progressor.prototype.timeFromCursorPosition = function(element, event, duration){ | ||
var dimensions = element.getBoundingClientRect(); | ||
var pixelsOfBar = event.clientX - dimensions.left; | ||
var percentToSecs = pixelsOfBar / dimensions.width; | ||
return percentToSecs * duration; | ||
}; | ||
|
||
Progressor.prototype.setMediaProgress = function(event){ | ||
this._media.currentTime = this.timeFromCursorPosition( | ||
this._bar, | ||
event, | ||
this._media.duration | ||
); | ||
this.updateProgress(); | ||
|
||
}; | ||
Progressor.prototype.timeFromCursorPosition = function(element, event, duration){ | ||
var dimensions = element.getBoundingClientRect(); | ||
var pixelsOfBar = event.clientX - dimensions.left; | ||
var percentToSecs = pixelsOfBar / dimensions.width; | ||
return percentToSecs * duration; | ||
}; | ||
|
||
Progressor.prototype.remove = function(){ | ||
function clearEvents(oldElement){ | ||
var newElement = oldElement.cloneNode(true); | ||
oldElement.parentNode.replaceChild(newElement, oldElement); | ||
} | ||
this._time.innerHTML = ""; | ||
this._bar.removeChild(this._textBox); | ||
this._bar.removeChild(this._progress); | ||
this._bar.style.position = ""; | ||
this._bar.style.zIndex = ""; | ||
this._bar.style.webkitUserSelect = ""; | ||
this._bar.style.userSelect = ""; | ||
this._bar.classList.remove("progressor"); | ||
clearEvents( this._bar ); | ||
clearEvents( this._media ); | ||
} | ||
Progressor.prototype.setMediaProgress = function(event){ | ||
this._media.currentTime = this.timeFromCursorPosition( | ||
this._bar, | ||
event, | ||
this._media.duration | ||
); | ||
this.updateProgress(); | ||
|
||
}; | ||
|
||
Progressor.prototype.addClickEvents = function(){ | ||
var isMouseDown = false, | ||
wasPlaying = false, | ||
mouseEventRefresh = ''; | ||
var mouseDown = function(e){ | ||
isMouseDown = true; | ||
wasPlaying = !this._media.paused; | ||
this._media.pause(); | ||
this.setMediaProgress(e); | ||
} | ||
var mouseUp = function(e){ | ||
clearInterval(mouseEventRefresh); | ||
isMouseDown = false; | ||
if (wasPlaying == true) { | ||
this._media.play(); | ||
wasPlaying = false; | ||
}; | ||
} | ||
var mouseMove = function(e){ | ||
if ( isMouseDown === true ) { | ||
mouseEventRefresh = setInterval( this.setMediaProgress(e) , 1000 ); | ||
Progressor.prototype.remove = function(){ | ||
function clearEvents(oldElement){ | ||
var newElement = oldElement.cloneNode(true); | ||
oldElement.parentNode.replaceChild(newElement, oldElement); | ||
} | ||
this._time.innerHTML = ""; | ||
this._bar.removeChild(this._textBox); | ||
this._bar.removeChild(this._progress); | ||
this._bar.style.position = ""; | ||
this._bar.style.zIndex = ""; | ||
this._bar.style.webkitUserSelect = ""; | ||
this._bar.style.userSelect = ""; | ||
this._bar.classList.remove("progressor"); | ||
clearEvents( this._bar ); | ||
clearEvents( this._media ); | ||
} | ||
this._bar.addEventListener("mousedown", mouseDown.bind(this) ); | ||
document.addEventListener("mouseup", mouseUp.bind(this) ); | ||
document.addEventListener("mousemove", mouseMove.bind(this) ); | ||
}; | ||
|
||
var progressor = { | ||
init : function(){ | ||
console.error("'progressor.init()' is deprecated. Please use 'Progressor()'."); | ||
} | ||
} | ||
Progressor.prototype.addClickEvents = function(){ | ||
var isMouseDown = false, | ||
wasPlaying = false, | ||
mouseEventRefresh = ''; | ||
var mouseDown = function(e){ | ||
isMouseDown = true; | ||
wasPlaying = !this._media.paused; | ||
this._media.pause(); | ||
this.setMediaProgress(e); | ||
} | ||
var mouseUp = function(e){ | ||
clearInterval(mouseEventRefresh); | ||
isMouseDown = false; | ||
if (wasPlaying == true) { | ||
this._media.play(); | ||
wasPlaying = false; | ||
}; | ||
} | ||
var mouseMove = function(e){ | ||
if ( isMouseDown === true ) { | ||
mouseEventRefresh = setInterval( this.setMediaProgress(e) , 1000 ); | ||
} | ||
} | ||
this._bar.addEventListener("mousedown", mouseDown.bind(this) ); | ||
document.addEventListener("mouseup", mouseUp.bind(this) ); | ||
document.addEventListener("mousemove", mouseMove.bind(this) ); | ||
}; | ||
|
||
return Progressor; | ||
})); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.2/require.min.js"></script> | ||
|
||
<script> | ||
require(['../progressor'], function(Progressor) { | ||
if (typeof Progressor === 'function') { | ||
alert('Progressor loaded in OK'); | ||
} else { | ||
alert('Progressor did not load in correctly'); | ||
} | ||
}); | ||
</script> |