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

Normalising and Improving #19

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
276 changes: 135 additions & 141 deletions javascripts/videojs.playlist.js
Original file line number Diff line number Diff line change
@@ -1,148 +1,142 @@
(function() {

videojs.plugin('playlist', function(options) {
//this.L="vjs_common_one";


console.log(this);
var id=this.el().id;

//console.log('begin playlist plugin with video id:'+id);

//console.log(this);
//var id=this.tag.id;
//assign variables
var tracks=document.querySelectorAll("#"+id+"-vjs-playlist .vjs-track"),
trackCount=tracks.length,
player=this,
currentTrack=tracks[0],
index=0,
play=true,
onTrackSelected=options.onTrackSelected;

//manually selecting track
for(var i=0; i<trackCount; i++){
tracks[i].onclick = function(){
//var track=this;
//index=this.getAttribute('data-index');
//console.log("a is clicked and index position is"+this.getAttribute('data-index')+"the data-src is "+this.getAttribute('data-src'));
//console.log("a is clicked and index position is"+index+"the data-src is "+this.getAttribute('data-src'));

trackSelect(this);
}
}

// for continuous play
if(typeof options.continuous=='undefined' || options.continuous==true){
//console.log('options.continuous==true');

player.on("ended", function(){
//console.log('on ended');

index++;
if(index>=trackCount){
//console.log('go to beginning');
index=0;
}
else;// console.log('trigger click next track');
tracks[index].click();

});// on ended
}
else;// console.log('dont play next!');

//track select function for onended and manual selecting tracks
var trackSelect=function(track){

//get new src
var src=track.getAttribute('data-src');
index=parseInt(track.getAttribute('data-index')) || index;
//console.log('track select click src:'+src);
(function(videojs) {
'use strict';

var defaults = {
continuous: true,
debug: false,
setTrack: 0,
hash: true,
},
playlist;

/**
* Initialize the plugin.
* @param options (optional) {object} configuration for the plugin
*/
playlist = function (options) {
var settings = videojs.util.mergeOptions(defaults, options),
player = this,
player_id = player.el().id,
tracks = document.querySelectorAll('#' + player_id + '-vjs-playlist .vjs-track'),
trackCount = tracks.length,
index = settings.setTrack,
play = true,
//track select function for onended and manual selecting tracks
trackSelect = function(track) {
// if from a click
if (track instanceof MouseEvent) {
track = this;
}

//get new src
var src = track.getAttribute('data-src'),
type = track.getAttribute('data-type'),
index = parseInt(track.getAttribute('data-index')) || index;

log('track select click src - ' + src);

player.src([
{type: type, src: src}
]);

if (play) {
player.play();
}

//remove 'currentTrack' CSS class
for(var i = 0; i < trackCount; i++){
if (tracks[i].classList.contains('current-track')) {
tracks[i].className = tracks[i].className.replace(/\bcurrent-track\b/, 'nonPlayingTrack');
}
}
//add 'current-track' CSS class
track.className = track.className + ' current-track';

if(typeof settings.onTrackSelected === 'function') {
settings.onTrackSelected.apply(track);
}

if (!settings.hash) {
return false;
}

},
log = function(message) {
if (settings.debug) {
console.log('Video.js Playlist Plugin: ', message);
}
};

log('begin with video id - ' + player_id);

// manually selecting track
for (var i = 0; i < trackCount; i++) {
tracks[i].onclick = trackSelect;
}

if(player.techName=='youtube'){
player.src([
{ type: type="video/youtube", src: src}
]);
// for continuous play
if(settings.continuous === true) {
log('continuous');

player.on('ended', function() {
log('on ended');

index++;
if(index >= trackCount) {
log('go to beginning');
index = 0;
} else {
log('trigger click next track');
tracks[index].click();
}
});
} else {
log('dont play next!');
}
else{

if(player.el().firstChild.tagName=="AUDIO" || (typeof options.mediaType!='undefined' && options.mediaType=="audio") ){

player.src([
{ type: "audio/mp4", src: src+".m4a" },
{ type: "audio/webm", src: src+".webm" },
{ type: type="video/youtube", src: src},
{ type: "audio/ogg", src: src+".ogg" }
/*{ type: "audio/mpeg", src: src+".mp3" },
{ type: "audio/ogg", src: src+".oga" }*/
]);
}
else{
//console.log("video");
player.src([
{ type: "video/mp4", src: src+".mp4" },
{ type: type="video/youtube", src: src},
{ type: "video/webm", src: src+".webm" }
//{ type: "video/ogv", src: src+".ogv" }
]);

if (window.location.hash) {
for (var i = 0; i < trackCount; i++) {
if (tracks[i].getAttribute('href') === window.location.hash) {
index = i;
break;
}
}
}



if(play) player.play();

//remove 'currentTrack' CSS class
for(var i=0; i<trackCount; i++){
if(tracks[i].classList.contains('currentTrack')){
tracks[i].className=tracks[i].className.replace(/\bcurrentTrack\b/,'nonPlayingTrack');
play = false;
log('setTrack - ' + index);
trackSelect(tracks[index]);
play = true;

return {
tracks: tracks,
trackCount: trackCount,
play: function() {
return play;
},
index: function() {
return index;
},
prev: function() {
var j = index - 1;
this.goTo(j);
},
next: function(){
var j = index + 1;
this.goTo(j);
},
goTo: function(j) {
log('go to - ' + j);

if(j < 0 || j > trackCount) {
j = 0;
}

trackSelect(tracks[j]);
}
}
//add 'currentTrack' CSS class
track.className = track.className + " currentTrack";
if(typeof onTrackSelected === 'function') onTrackSelected.apply(track);

}

//if want to start at track other than 1st track
if(typeof options.setTrack!='undefined' ){
options.setTrack=parseInt(options.setTrack);
currentTrack=tracks[options.setTrack];
index=options.setTrack;
play=false;
//console.log('options.setTrack index'+index);
trackSelect(tracks[index]);
play=true;
}
if (window.location.hash) {
var hash = window.location.hash.substring(9);
play = false;
trackSelect(tracks[hash]);
}

var data={
tracks: tracks,
trackCount: trackCount,
play:function(){
return play;
},
index:function(){
return index;
},
prev:function(){
var j=index-1;
//console.log('j'+j);
if(j<0 || j>trackCount) j=0;
trackSelect(tracks[j]);
},
next:function(){
var j=index+1;
//console.log('j'+j);
if(j<0 || j>trackCount) j=0;
trackSelect(tracks[j]);
}
};
};
return data;
});
//return videojsplugin;
})();

// register the plugin
videojs.plugin('playlist', playlist);
})(window.videojs);
Loading