forked from Colin-Fredericks/hx-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhx-min.js
1 lines (1 loc) · 13.5 KB
/
hx-min.js
1
var HXGlobalJS=(function(hxLocalOptions,HXPUPTimer){var hxDefaultOptions={makeTOC:false,collapsedNav:false,makeSmiles:true,openPageDiscussion:false,resizeMaps:true,markExternalLinks:false,highlightColor:'#ff0',highlightBackground:'rgba(0,0,0,0)',highlightState:true,highlightCode:true,slickOptions:{arrows:true,dots:true,infinite:true,slidesToShow:3,slidesToScroll:3},slickNavOptions:{asNavFor:'.hx-bigslider',variableWidth:true,focusOnSelect:true,slidesToShow:3,slidesToScroll:1},slickBigOptions:{asNavFor:'.hx-navslider',arrows:false,dots:true,fade:true,adaptiveHeight:true,slidesToShow:1,slidesToScroll:1},PUPOptions:{width:800,effect:'fade',effectlength:200,myPosition:'center',atPosition:'center',ofTarget:window},VidLinkOptions:{hideLinkAfter:5,effect:'slide',hide:{'direction':'down'},show:{'direction':'down'},speed:500,location:'bl'}};var courseAssetURL=getAssetURL(window.location.href,'complete');logThatThing(courseAssetURL);var courseSite=getAssetURL(window.location.href,'site');if(courseSite.indexOf('studio')>-1){console.log('Running HXJS in studio is probably not great.');return}var courseInfo=getCourseInfo(window.location.href);var courseLogID=courseInfo.institution+'.'+courseInfo.id+'_'+courseInfo.run;logThatThing({'HX.js':'enabled'});logThatThing({'course log id':courseLogID});$.getMultiScripts=function(arr,path){var _arr=$.map(arr,function(scr){return $.getScript((path||'')+scr)});_arr.push($.Deferred(function(deferred){$(deferred.resolve)}));return $.when.apply($,_arr)};var scriptArray=[];var hxOptions={};scriptArray.push('hxGlobalOptions.js');var codeblocks=$('code');if(codeblocks.length){logThatThing({'code_block':'found'});scriptArray.push('prism.js')}var slider=$('.hx-slider');var navslider=$('.hx-navslider');var bigslider=$('.hx-bigslider');if(slider.length||(navslider.length&&bigslider.length)){logThatThing({'image_slider':'found'});scriptArray.push('slick.js')}var theMaps=$('map');if(theMaps.length){logThatThing({'image_map':'found'});scriptArray.push('imageMapResizer.min.js')}var loadVideoStuff=true;if(typeof hxLocalOptions.dontLoadVideoStuff!==undefined){if(hxLocalOptions.dontLoadVideoStuff===true){loadVideoStuff=false;console.log('skipping loading video js')}}var allVideos=$('.video');if(loadVideoStuff){if(allVideos.length){logThatThing({'video':'found'});scriptArray.push('HXVideoLinks.js');var HXVL;if(HXPUPTimer.length!==0){scriptArray.push('HXPopUpProblems.js');var HXPUP}}}$.getMultiScripts(scriptArray,courseAssetURL).done(function(){logThatThing({'Loaded scripts':scriptArray});if(hxGlobalOptions){hxOptions=setDefaultOptions(hxLocalOptions,hxGlobalOptions,hxDefaultOptions)}else{hxOptions=setDefaultOptions(hxLocalOptions,{},hxDefaultOptions)}keepGoing(hxOptions)}).fail(function(){logThatThing('Did not load scripts.');hxOptions=setDefaultOptions(hxLocalOptions,{},hxDefaultOptions);keepGoing(hxOptions)});function keepGoing(hxOptions){if(allVideos.length&&loadVideoStuff){$('head').append($('<link rel="stylesheet" href="'+courseAssetURL+'VideoLinks.css" type="text/css" />'));HXVL=new HXVideoLinks(hxOptions.VidLinkOptions);if(HXPUPTimer.length!==0){HXPUP=new HXPopUpProblems(hxDefaultOptions.PUPOptions,HXPUPTimer)}}if(theMaps.length&&hxOptions.resizeMaps){$('map').imageMapResize()}if(codeblocks.length&&hxOptions.highlightCode){$('head').append($('<link rel="stylesheet" href="'+courseAssetURL+'prism.css" type="text/css" />'));$('.submit, .reset').on('click tap',function(){var rehighlight=function(mutationsList){for(var mutation of mutationsList){if(mutation.type=='childList'){$.when(observer.disconnect()).done(function(){$.when(Prism.highlightAllUnder(target)).done(function(){setTimeout(function(){observer.observe(target,config)},500)})});break}}};var target=this.closest('.xblock');var config={childList:true};var observer=new MutationObserver(rehighlight);observer.observe(target,config)})}var allTimeLinks=$('a.hx-vidtime');allTimeLinks.on('click tap',function(){var thisTime=hmsToTime($(this).attr('data-time'));var href=$(this).attr('href');var anchor=href.slice(href.indexOf('#video'));var vidNumber=anchor.replace('#video','');var unitNumber=href.slice(href.indexOf('/jump_to_id/')+13,href.indexOf('#video'));var startsWithHash=href.indexOf('#')===0?true:false;if(startsWithHash){logThatThing({'link starts video at time':thisTime});HXVL.jumpToTime(vidNumber,thisTime )}else{localStorage.HXVideoLinkGo="true";localStorage.HXVideoLinkUnit=unitNumber;localStorage.HXVideoLinkNumber=vidNumber;localStorage.HXVideoLinkTime=thisTime;logThatThing({'storing video info for jump':{'unit':unitNumber,'video number':vidNumber,'time':thisTime}})}});if(hxOptions.collapsedNav){$('.sequence-nav').hide();$('.sequence > .path').hide();$('h3.unit-title').hide()}if(hxOptions.makeSmiles){var mehFace=$('<span class="hx-smileystack hx-meh fa-stack fa-lg"><span class="fa fa-circle fa-stack-2x"></span><span class="fa fa-meh-o fa-stack-2x"></span></span>');var smileFace=$('<span class="hx-smileystack hx-smile fa-stack fa-lg"><span class="fa fa-circle fa-stack-2x"></span><span class="fa fa-smile-o fa-stack-2x"></span></span>');var spacer=$('<span class="hx-smileyspacer"></span>');$('.hx-smileystack').remove();$('.hx-smileyspacer').remove();$('.sequence-bottom').prepend(spacer);$('.sequence-bottom').append(mehFace);$(mehFace).on('click tap',function(){$(mehFace).remove();$('.sequence-bottom').append(smileFace);logThatThing('Smile!')})}var tintButton=$('#hx-tint');if(tintButton.length){$(tintButton).on('click tap',function(){$('p').animate({'color':'blue'})})}if(hxOptions.openPageDiscussion){$('.discussion-show.btn').click()}if(hxOptions.markExternalLinks){logThatThing('marking external links');$('.vert .xblock a, .static_tab_wrapper .xblock a').each(function(i,linky){var destination=$(linky).attr('href');if(typeof destination!=='undefined'){if(destination.includes('edx.org')||destination.includes('jump_to_id')||destination.includes('/courses/')||destination.includes('cloudfront.net')||destination.includes('edx-cdn.org')||destination.includes('edxapp')||destination.includes('javascript:void')||destination.slice(0,1)=='#'){}else{$(linky).append(' <span class="fa fa-external-link"><span class="sr">External link</span></span>')}}})}if(hxOptions.makeTOC){if($('.edx-notes-wrapper-content').length){$('.edx-notes-wrapper-content:first-of-type').prepend('<div id="autoTOC" class="hx-autotoc"></div>')}else{$('#seq_content .xblock:first-of-type').prepend('<div id="autoTOC" class="hx-autotoc"></div>')}var autoTOC='<h3>Table of Contents</h3><ul>';var allHeaders=$('h3, h4').filter(function(){return $(this).is(':visible')});var TOCList=$('#autoTOC ul');allHeaders.each(function(i){$(this).attr('id','TOCLink'+i);var TOCEntry=$(this).text();var TOCLevel;if($(this).is('h3')){TOCLevel=3;if($(allHeaders[i-1]).is('h3')||i===0){autoTOC+='<li class="autotoc'+TOCLevel+'"><a href="#TOCLink'+i+'">'+TOCEntry+'</a></li>'}else if($(allHeaders[i-1]).is('h4')){autoTOC+='</ul></li><li class="autotoc'+TOCLevel+'"><a href="#TOCLink'+i+'">'+TOCEntry+'</a></li>'}}if($(this).is('h4')){TOCLevel=4;if($(allHeaders[i-1]).is('h3')){if(i>0){autoTOC.slice(0,autoTOC.length-5)}autoTOC+='<ul><li class="autotoc'+TOCLevel+'"><a href="#TOCLink'+i+'">'+TOCEntry+'</a></li>'}else if($(allHeaders[i-1]).is('h4')){autoTOC+='<li class="autotoc'+TOCLevel+'"><a href="#TOCLink'+i+'">'+TOCEntry+'</a></li>'}}});autoTOC+='</ul>';$('#autoTOC').append(autoTOC)}$('[class^=hx-togglebutton]').each(function(){var myNumber=getClassNumber(this.className,'hx-togglebutton');$(this).attr('aria-controls','hx-toggletarget'+myNumber);if($('.hx-toggletarget'+myNumber+':visible').length>0){$(this).attr('aria-expanded','true');$('.hx-toggletarget'+myNumber).attr('aria-hidden','false')}else{$(this).attr('aria-expanded','false');$('[class^=hx-toggletarget]').attr('aria-hidden','true')}});$('[class^=hx-togglebutton]').on('click tap',function(){var myNumber=getClassNumber(this.className,'hx-togglebutton');$('.hx-toggletarget'+myNumber).slideToggle('fast');if($(this).attr('aria-expanded')==='true'){console.log('was visible');$(this).attr('aria-expanded','false');$('.hx-toggletarget'+myNumber).attr('aria-hidden','true')}else{console.log('was not visible');$(this).attr('aria-expanded','true');$('.hx-toggletarget'+myNumber).attr('aria-hidden','false')}logThatThing({'Toggle button':'pressed','Toggle number':myNumber})});$('[class^=hx-highlighter]').on('click tap',function(){var myNumber=getClassNumber(this.className,'hx-highlighter');if(hxOptions.highlightState){$('.hx-highlight'+myNumber).animate({backgroundColor:hxOptions.highlightColor},200)}else{$('.hx-highlight'+myNumber).animate({backgroundColor:hxOptions.highlightBackground},200)}hxOptions.highlightState=!hxOptions.highlightState;logThatThing({'Highlight button':'pressed','Highlight number':myNumber})});var popUpOpener=$('.hx-popup-opener');if(popUpOpener.length){$('map').each(function(index){var tempList=[];$(this).find('area').each(function(index){tempList.push('<li class="'+this.className.split(/\s+/)[0]+' hx-popup-opener" title="'+this.title+'"><a href="javascript:;">'+this.title+'</a></li>')});var listHTML='<ul>'+tempList.join('')+'</ul>';listHTML='<h4>Clickable Areas:</h4>'+listHTML;$(this).after(listHTML)});popUpOpener=$('.hx-popup-opener');popUpOpener.on('click tap',function(){var myClass=this.className;var boxName=myClass.split(/\s+/)[0];$('div.'+boxName).dialog({dialogClass:"hx-popup-dialog",title:$(this).attr('title'),show:{effect:'fade',duration:200},hide:{effect:'fade',duration:100},buttons:{"Close":function(){$(this).dialog("close")}}},function(boxName){$('div.'+boxName).css({'display':''});alert(boxName)});logThatThing({'Pop-up Dialog':'opened','Dialog':boxName})})}var allFootnotes=$('span[class^="hx-footnote"]');if(allFootnotes.length){var thisFootnote,thisNumber,thisTarget,footnoteComponents,destinationComponent;for(var i=0;i<allFootnotes.length;i+=1){thisFootnote=allFootnotes[i];thisNumber=getClassNumber(thisFootnote.className,'hx-footnote');thisTarget=$('div.hx-footnote-target'+thisNumber);$(thisFootnote).addClass('hx-footnote-style');$(thisFootnote).wrap('<sup></sup>');footnoteComponents=$('h3:contains("Footnote")');destinationComponent=$(footnoteComponents[footnoteComponents.length-1]).parent();$(thisTarget).detach().appendTo(destinationComponent);$(thisFootnote).wrap('<a href="#hxfoot'+thisNumber+'" name="hxfootback'+thisNumber+'"></a>').wrap();thisTarget.prepend('<a name="hxfoot'+thisNumber+'"></a>');thisTarget.append('<p><a href="#hxfootback'+thisNumber+'">(back)</a></p>')}}if(slider.length){$('head').append($('<link rel="stylesheet" href="'+courseAssetURL+'slick.css" type="text/css" />'));$('head').append($('<link rel="stylesheet" href="'+courseAssetURL+'slick-theme.css" type="text/css" />'));slider.slick(hxOptions.slickOptions);logThatThing({'slider':'created'})}if(navslider.length&&bigslider.length){navslider.slick(hxOptions.slickNavOptions);bigslider.slick(hxOptions.slickBigOptions);logThatThing({'paired slider':'created'})}}function getAssetURL(windowURL,option){windowURL=windowURL.replace('%2B','+');windowURL=windowURL.replace('%3A',':');var courseSiteURL=windowURL.match(/https:\/\/.+\//)[0];if(option=='site'){return courseSiteURL}var staticFolderURL=windowURL.replace('courses/course','asset');if(staticFolderURL.search('xblock/block-v1')>-1){staticFolderURL=staticFolderURL.replace('xblock/block','asset');staticFolderURL=staticFolderURL.replace('+type@','/')}var pluslocation=staticFolderURL.indexOf('+');var finalLocation=staticFolderURL.indexOf('/',pluslocation);staticFolderURL=staticFolderURL.slice(0,finalLocation);staticFolderURL=staticFolderURL+'+type@asset+block/';if(option=='partial'){return staticFolderURL.replace(courseSiteURL,'')}return staticFolderURL}function getCourseInfo(windowURL){var partialURL=getAssetURL(windowURL,'partial');var courseInfo={};partialURL=partialURL.split(':')[1];courseInfo.institution=partialURL.split('+')[0];courseInfo.id=partialURL.split('+')[1];courseInfo.run=partialURL.split('+')[2];return courseInfo}function getClassNumber(className,importantClass){var allClasses=className.split(/\s+/);for(var i=0;i<allClasses.length;i+=1){if(allClasses[i].indexOf(importantClass)===0){return allClasses[i].slice(importantClass.length)}}return -1}function setDefaultOptions(localOptions,globalOptions,fallbackOptions){var tempOptions={};if(!localOptions&&!globalOptions){return fallbackOptions}else if(!localOptions){tempOptions=$.extend(true,{},fallbackOptions,globalOptions)}else if(!globalOptions){tempOptions=$.extend(true,{},fallbackOptions,localOptions)}else{tempOptions=$.extend(true,{},fallbackOptions,globalOptions,localOptions)}return tempOptions}(function($){$.fn.hxKonami=function(callback,code){if(code===undefined){code="38,38,40,40,37,39,37,39,66,65"}return this.each(function(){var kkeys=[];$(this).keydown(function(e){kkeys.push(e.keyCode);while(kkeys.length>code.split(',').length){kkeys.shift()}if(kkeys.toString().indexOf(code)>=0){kkeys=[];callback(e)}})})}})(jQuery);$(window).hxKonami(function(){alert('+30 Lives');logThatThing({'easter egg':'Konami Code'})});function hmsToTime(hms){hms=hms.toString();var hmsArray=hms.split(':');var time=0;if(hmsArray.length==3){time=3600*parseInt(hmsArray[0])+60*parseInt(hmsArray[1])+Number(hmsArray[2])}else if(hmsArray.length==2){time=60*parseInt(hmsArray[0])+Number(hmsArray[1])}else if(hmsArray.length==1){time=Number(hmsArray[0])}return time}this.getAssetURL=getAssetURL;this.hmsToTime=hmsToTime;function logThatThing(ThatThing){console.log(JSON.stringify(ThatThing));Logger.log(courseLogID+'.hxjs',ThatThing)}this.logThatThing=logThatThing});if(typeof hxLocalOptions==='undefined'){var hxLocalOptions={}}if(typeof HXPUPTimer==='undefined'){var HXPUPTimer=[]}$(document).ready(function(){HXGlobalJS(hxLocalOptions,HXPUPTimer)});