Skip to content

Instantly share code, notes, and snippets.

@brettbartylla
Created September 6, 2017 00:03
Show Gist options
  • Save brettbartylla/29e25c4f81875da0b413314f710ee7c1 to your computer and use it in GitHub Desktop.
Save brettbartylla/29e25c4f81875da0b413314f710ee7c1 to your computer and use it in GitHub Desktop.
jQuery I wrote that dynamically builds HTML5 players and inserts them inline on a page. Video content (IDs) came from Brightcove. I've removed the original videos and am using hardcoded values for this example.
(function (context) {
'use strict';
var require = context.require;
var requirejs = context.requirejs;
var define = context.define;
define(['require','jquery','lib/Util','GlobalEventDispatcher','GlobalResizeListener'],function (require) {
//define any required libraries - see config.js for registered names
var $ = require('jquery'), //require jQuery
util = require('lib/Util'), //require util for $debug
GlobalEventDispatcher = require('GlobalEventDispatcher'),
GlobalResizeListener = require('GlobalResizeListener');
//global vars
//navigation variables gathered as data options from CMS
var nowShowing,
nextVid,
prevVid;
//check each column for naviation attributes
$('.componentClass').each(function(){
if($(this).attr('data-nextlabel')){
nextVid = $(this).attr('data-nextlabel');
}
if($(this).attr('data-prevlabel')){
prevVid = $(this).attr('data-prevlabel');
}
if($(this).attr('data-prevlabel')){
nowShowing = $(this).attr('data-showlabel');
}
});
//video play button clicked
$(".js-videoPlayer-trigger").click(function(e){
//if the trigger belongs to the columns component
if($(this).parents('.componentClass').length){
e.preventDefault();
var self = $(this);
//get videoID from trigger of video clicked, this will = playerData.videoID, pass this value
//id of video that needs to play
var currentVID = $(this).parents('.MMM--tableBlock-li-inner-item-100').attr('data-video-id');
var getContainer;
//this grabs the wrapper coming from the bottom of the rG-68-Titling component
var videoWrapper = $(this).parents().children().find('.externalVidWrapper');
//then fire all code below
//object holding player data
var playerData = {
'accountID' : '2635130879001',
'playerID' : '3cff688e-cc46-424e-b997-538049729228',
'videoID' : currentVID,
'uniqueID': 'myPlayer1'
};
//variables that build the player
var playerHTML,
videoContainer,
navControls,
videoArray =[];
var videoProto = function(aID,pID,vID,uID){
//constructor
this.aID = aID;
this.pID = pID;
this.vID = vID;
this.uID = uID;
//function building player object
videoProto.prototype.buildPlayer = function(){
playerHTML= '<video style="height:100%; width:100%; position:relative;" id=\"' + uID + '\" data-video-id=\"' + vID + '\" data-account=\"' + aID + '\" data-player=\"' + pID + '\" data-embed=\"default\" class=\"video-js\" controls></video>';
//build flag
videoObj.nowShowing();
return playerHTML;
};
//function building player object
videoProto.prototype.buildNav = function(){
//check if more than 1 player is on page
$('.MMM--tableBlock-li-inner-item-100').each(function(){
if($(this).attr('data-video-id')){
videoArray.push($(this).attr('data-video-id'));
}
});
//if the videoArray contains more than one video, show next/prev controls
if(videoArray.length > 1){
navControls = videoWrapper.children().closest('.js-videoNavcontrols');
navControls.addClass('showNav');
//clear nav controls
$('.showNav').html('');
$('.showNav').prepend('<div class="navAnchor"><span class="prv"></span><span class="seperator">|</span><span class="nxt"></span><i class="MMM--icn MMM--icn_close mix-MMM--icn_psTheme MMM--videoCloseBtn"><span class="MMM--icn-text"></span></div');
}
};
videoProto.prototype.getPlayerPosition = function(){
//if playing the first video in array, disable the previous video link
if(videoArray[0] == vID){
var p = $('.prv').children();
var n = $('.nxt').children();
$(p).remove();
$(n).remove();
$('.nxt').prepend($('<a class="next" target="_self"> <span>' + nextVid +' &gt;'+ '</span></a>'));
$('.prv').prepend($('<span>' +'&lt; '+ prevVid +' ' + '</span>'));
}
//if its not the first or last video show both navigation options
if(videoArray[0] != vID && videoArray.slice(-1)[0] != vID){
var p = $('.prv').children();
var n = $('.nxt').children();
$(p).remove();
$(n).remove();
$('.prv').prepend($('<a class="previous" target="_self"> <span>' +'&lt; ' + prevVid +' ' + '</span></a>'));
$('.nxt').prepend($('<a class="next" target="_self"> <span>' + nextVid +' &gt;'+ '</span></a>'));
}
if(vID == videoArray[videoArray.length-1]){
var p = $('.prv').children();
var n = $('.nxt').children();
$(p).remove();
$(n).remove();
$('.nxt').prepend($('<span>' +' ' + nextVid + ' &gt;'+'</span>'));
$('.prv').prepend($('<a class="previous" target="_self"> <span>' + '&lt; ' +prevVid +' ' +'</span></a>'));
}
};
//function appending player object to page
videoProto.prototype.getContainer = function(){
//get div holding videoWrapper
videoContainer = $(videoWrapper).children().closest('.js-videoObjectContainer');
$(videoWrapper).removeClass('MMM--isVisuallyHidden');
//get height of column, slide element down out of video's way
var slideHeight = $(videoContainer).height();
var colList = $(videoContainer).siblings().closest('.MMM--columnList');
return videoContainer;
};
//function appending player object to page
videoProto.prototype.appendPlayer = function(){
//insert player
$(videoContainer).html(playerHTML);
//determins which navigation controls to show / hide
videoObj.getPlayerPosition();
};
//if close icon clicked
$(document).on('click','.MMM--videoCloseBtn',function(){
//empty video player
playerHTML = '';
//insert empty player
$(videoContainer).html(playerHTML)
//hide video container
$(videoContainer).parent().addClass('MMM--isVisuallyHidden');
//remove any pre existing now showing flags
$('.MMM--nowShow').each(function(){
$(this).remove();
});
//clear nav controls
$('.showNav').html('');
});
//function appending player object to page
videoProto.prototype.buildScript = function(){
// add and execute the player script tag
var s = document.createElement('script');
s.src = "//players.brightcove.net/" + playerData.accountID + "/" + playerData.playerID + "_default/index.min.js";
document.body.appendChild(s);
s.onload = callback;
};
//plays the video
function callback() {
//video player object
var myPlayer = videojs('myPlayer1');
myPlayer.play();
}
//function appending now playing flag
videoProto.prototype.nowShowing = function(){
//remove and replace now showing flag
$('.MMM--tableBlock-li-inner-item-100').each(function(){
var el = $(this);
if($(this).attr('data-video-id') == vID){
//remove any pre existing now showing flags
$('.MMM--nowShow').each(function(){
$(this).remove();
});
$(el).children().children(':first').prepend("<div class='MMM--nowShow'>"+nowShowing+"</div>");
}
});
};
//navigation control
$(document).on('click','.next',function(){
//use the next video ID as the new video ID
vID = videoArray[($.inArray(vID, videoArray) + 1) % videoArray.length];
//build flag
videoObj.nowShowing();
//builds the script to call brightcove, calls function to play video
videoObj.buildScript();
//combines the videos id's and HTML to create an html5 player
videoObj.buildPlayer();
//gets the element that the html5 player will be inserted into
videoObj.getContainer();
//appends the video player object to container
videoObj.appendPlayer();
});
//if previous link clicked
$(document).on('click','.previous',function(){
e.preventDefault();
//use the previous video ID as the new video ID
vID = videoArray[($.inArray(vID, videoArray) - 1) % videoArray.length];
//build flag
videoObj.nowShowing();
//builds the script to call brightcove, calls function to play video
videoObj.buildScript();
//combines the videos id's and HTML to create an html5 player
videoObj.buildPlayer();
//gets the element that the html5 player will be inserted into
videoObj.getContainer();
//appends the video player object to container
videoObj.appendPlayer();
});
}; //end videoProto function
//create video 0bject from prototype
var videoObj = new videoProto(playerData.accountID, playerData.playerID, playerData.videoID, playerData.uniqueID);
//builds the script to call brightcove, calls function to play video
videoObj.buildScript();
//combines the videos id's and HTML to create an html5 player
videoObj.buildPlayer();
//gets the element that the html5 player will be inserted into
videoObj.getContainer();
//builds navigation controls if there are more than 1 video in que
videoObj.buildNav();
//appends the video player object to container
videoObj.appendPlayer();
}//end if
});
});
}(MMMRequire));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment