diff options
author | Hakim El Hattab <hakim.elhattab@gmail.com> | 2018-10-04 14:48:01 +0200 |
---|---|---|
committer | Hakim El Hattab <hakim.elhattab@gmail.com> | 2018-10-04 14:48:01 +0200 |
commit | 29b0e86089eb3ec0d4bb5811c9b723dfcf36703c (patch) | |
tree | 986f986df26127d945374e2b8f004ec1cb1a9568 /js | |
parent | d5cf3fa13c899014008314d41292880fa8b699bd (diff) | |
download | perl-software-in-gnu-guix-29b0e86089eb3ec0d4bb5811c9b723dfcf36703c.tar perl-software-in-gnu-guix-29b0e86089eb3ec0d4bb5811c9b723dfcf36703c.tar.gz |
remove head.min.js in favor of simple built-in script loader
Diffstat (limited to 'js')
-rw-r--r-- | js/reveal.js | 90 |
1 files changed, 68 insertions, 22 deletions
diff --git a/js/reveal.js b/js/reveal.js index ac3135d..ca2a9a0 100644 --- a/js/reveal.js +++ b/js/reveal.js @@ -436,41 +436,28 @@ scriptsToPreload = 0; // Called once synchronous scripts finish loading - function proceed() { + function afterSynchronousScriptsLoaded() { + // Load asynchronous scripts if( scriptsAsync.length ) { - // Load asynchronous scripts - head.js.apply( null, scriptsAsync ); + scriptsAsync.forEach( function( s ) { + loadScript( s.src, s.callback ); + } ); } start(); } - function loadScript( s ) { - head.ready( s.src.match( /([\w\d_\-]*)\.?js(\?[\w\d.=&]*)?$|[^\\\/]*$/i )[0], function() { - // Extension may contain callback functions - if( typeof s.callback === 'function' ) { - s.callback.apply( this ); - } - - if( --scriptsToPreload === 0 ) { - proceed(); - } - }); - } - for( var i = 0, len = config.dependencies.length; i < len; i++ ) { var s = config.dependencies[i]; // Load if there's no condition or the condition is truthy if( !s.condition || s.condition() ) { if( s.async ) { - scriptsAsync.push( s.src ); + scriptsAsync.push( s ); } else { - scripts.push( s.src ); + scripts.push( s ); } - - loadScript( s ); } } @@ -478,12 +465,71 @@ scriptsToPreload = scripts.length; // Load synchronous scripts - head.js.apply( null, scripts ); + scripts.forEach( function( s ) { + loadScript( s.src, function() { + + if( typeof s.callback === 'function' ) s.callback(); + + if( --scriptsToPreload === 0 ) { + + afterSynchronousScriptsLoaded(); + + } + + } ); + } ); } else { - proceed(); + afterSynchronousScriptsLoaded(); + } + + } + + /** + * Loads a JavaScript file from the given URL and executes it. + * + * @param {string} url Address of the .js file to load + * @param {function} callback Method to invoke when the script + * has loaded and executed + */ + function loadScript( url, callback ) { + + var script = document.createElement( 'script' ); + script.type = 'text/javascript'; + script.async = false; + script.defer = false; + script.src = url; + + if( callback ) { + + // Success callback + script.onload = script.onreadystatechange = function( event ) { + if( event.type === "load" || (/loaded|complete/.test( script.readyState ) ) ) { + + // Kill event listeners + script.onload = script.onreadystatechange = script.onerror = null; + + callback(); + + } + }; + + // Error callback + script.onerror = function( err ) { + + // Kill event listeners + script.onload = script.onreadystatechange = script.onerror = null; + + callback( new Error( 'Failed loading script: ' + script.src + '\n' + err) ); + + }; + } + // Append the script at the end of <head> + var head = document.querySelector( 'head' ); + head.insertBefore( script, head.lastChild ); + } /** |