Преглед изворни кода

remove head.min.js in favor of simple built-in script loader

Hakim El Hattab пре 6 година
родитељ
комит
29b0e86089

+ 0 - 1
demo.html

@@ -384,7 +384,6 @@ Reveal.addEventListener( 'customevent', function() {
 
 		</div>
 
-		<script src="lib/js/head.min.js"></script>
 		<script src="js/reveal.js"></script>
 
 		<script>

+ 0 - 1
index.html

@@ -29,7 +29,6 @@
 			</div>
 		</div>
 
-		<script src="lib/js/head.min.js"></script>
 		<script src="js/reveal.js"></script>
 
 		<script>

+ 68 - 22
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 );
+
 	}
 
 	/**

Разлика између датотеке није приказан због своје велике величине
+ 0 - 6
lib/js/head.min.js


+ 0 - 1
plugin/markdown/example.html

@@ -109,7 +109,6 @@
             </div>
 		</div>
 
-		<script src="../../lib/js/head.min.js"></script>
 		<script src="../../js/reveal.js"></script>
 
 		<script>

+ 1 - 0
test/assets/external-script-a.js

@@ -0,0 +1 @@
+window.externalScriptSequence += 'A';

+ 1 - 0
test/assets/external-script-b.js

@@ -0,0 +1 @@
+window.externalScriptSequence += 'B';

+ 1 - 0
test/assets/external-script-c.js

@@ -0,0 +1 @@
+window.externalScriptSequence += 'C';

+ 1 - 0
test/assets/external-script-d.js

@@ -0,0 +1 @@
+window.externalScriptSequence += 'D';

+ 0 - 1
test/examples/embedded-media.html

@@ -34,7 +34,6 @@
 
 		</div>
 
-		<script src="../../lib/js/head.min.js"></script>
 		<script src="../../js/reveal.js"></script>
 
 		<script>

+ 0 - 1
test/examples/math.html

@@ -159,7 +159,6 @@
 
 		</div>
 
-		<script src="../../lib/js/head.min.js"></script>
 		<script src="../../js/reveal.js"></script>
 
 		<script>

+ 0 - 1
test/examples/slide-backgrounds.html

@@ -122,7 +122,6 @@
 
 		</div>
 
-		<script src="../../lib/js/head.min.js"></script>
 		<script src="../../js/reveal.js"></script>
 
 		<script>

+ 0 - 1
test/examples/slide-transitions.html

@@ -81,7 +81,6 @@
 
 		</div>
 
-		<script src="../../lib/js/head.min.js"></script>
 		<script src="../../js/reveal.js"></script>
 
 		<script>

+ 74 - 0
test/test-async-dependencies.html

@@ -0,0 +1,74 @@
+<!doctype html>
+<html lang="en">
+
+	<head>
+		<meta charset="utf-8">
+
+		<title>reveal.js - Test Async Dependencies</title>
+
+		<link rel="stylesheet" href="../css/reveal.css">
+		<link rel="stylesheet" href="qunit-2.5.0.css">
+	</head>
+
+	<body style="overflow: auto;">
+
+		<div id="qunit"></div>
+		<div id="qunit-fixture"></div>
+
+		<div class="reveal" style="display: none;">
+
+			<div class="slides">
+
+				<section>Slide content</section>
+
+			</div>
+
+		</div>
+
+		<script src="../js/reveal.js"></script>
+		<script src="qunit-2.5.0.js"></script>
+
+		<script>
+			window.externalScriptSequence = '';
+
+			QUnit.module( 'Async Dependencies' );
+
+			var scriptCount = 0;
+
+			QUnit.test( 'Async scripts are loaded', function( assert ) {
+				assert.expect( 5 );
+				var done = assert.async( 5 );
+
+				function callback( event ) {
+					if( window.externalScriptSequence.length === 1 ) {
+						assert.ok( window.externalScriptSequence === 'A', 'first callback was sync script' );
+					}
+					else {
+						assert.ok( true, 'async script loaded' );
+					}
+
+					if( window.externalScriptSequence.length === 4 ) {
+						assert.ok( 	window.externalScriptSequence.indexOf( 'A' ) !== -1 &&
+									window.externalScriptSequence.indexOf( 'B' ) !== -1 &&
+									window.externalScriptSequence.indexOf( 'C' ) !== -1 &&
+									window.externalScriptSequence.indexOf( 'D' ) !== -1, 'four unique scripts were loaded' );
+					}
+
+					done();
+					scriptCount ++;
+				}
+
+				Reveal.initialize({
+					dependencies: [
+						{ src: 'assets/external-script-a.js', async: false, callback: callback },
+						{ src: 'assets/external-script-b.js', async: true, callback: callback },
+						{ src: 'assets/external-script-c.js', async: true, callback: callback },
+						{ src: 'assets/external-script-d.js', async: true, callback: callback }
+					]
+				});
+			});
+
+		</script>
+
+	</body>
+</html>

+ 54 - 0
test/test-dependencies.html

@@ -0,0 +1,54 @@
+<!doctype html>
+<html lang="en">
+
+	<head>
+		<meta charset="utf-8">
+
+		<title>reveal.js - Test Dependencies</title>
+
+		<link rel="stylesheet" href="../css/reveal.css">
+		<link rel="stylesheet" href="qunit-2.5.0.css">
+	</head>
+
+	<body style="overflow: auto;">
+
+		<div id="qunit"></div>
+		<div id="qunit-fixture"></div>
+
+		<div class="reveal" style="display: none;">
+
+			<div class="slides">
+
+				<section>Slide content</section>
+
+			</div>
+
+		</div>
+
+		<script src="../js/reveal.js"></script>
+		<script src="qunit-2.5.0.js"></script>
+
+		<script>
+			window.externalScriptSequence = '';
+
+			Reveal.addEventListener( 'ready', function() {
+
+				QUnit.module( 'Dependencies' );
+
+				QUnit.test( 'Load synchronous scripts', function( assert ) {
+					assert.strictEqual( window.externalScriptSequence, 'ABC', 'Loaded and executed in order' );
+				});
+
+			} );
+
+			Reveal.initialize({
+				dependencies: [
+					{ src: 'assets/external-script-a.js' },
+					{ src: 'assets/external-script-b.js' },
+					{ src: 'assets/external-script-c.js' }
+				]
+			});
+		</script>
+
+	</body>
+</html>

+ 0 - 1
test/test-markdown-element-attributes.html

@@ -122,7 +122,6 @@
 
 		</div>
 
-		<script src="../lib/js/head.min.js"></script>
 		<script src="../js/reveal.js"></script>
 		<script src="../plugin/markdown/marked.js"></script>
 		<script src="../plugin/markdown/markdown.js"></script>

+ 0 - 1
test/test-markdown-external.html

@@ -23,7 +23,6 @@
 
 		</div>
 
-		<script src="../lib/js/head.min.js"></script>
 		<script src="../js/reveal.js"></script>
 		<script src="../plugin/highlight/highlight.js"></script>
 		<script src="../plugin/markdown/marked.js"></script>

+ 0 - 1
test/test-markdown-options.html

@@ -31,7 +31,6 @@
 
 		</div>
 
-		<script src="../lib/js/head.min.js"></script>
 		<script src="../js/reveal.js"></script>
 		<script src="qunit-2.5.0.js"></script>
 

+ 0 - 1
test/test-markdown-slide-attributes.html

@@ -116,7 +116,6 @@
 
 		</div>
 
-		<script src="../lib/js/head.min.js"></script>
 		<script src="../js/reveal.js"></script>
 		<script src="../plugin/markdown/marked.js"></script>
 		<script src="../plugin/markdown/markdown.js"></script>

+ 0 - 1
test/test-markdown.html

@@ -40,7 +40,6 @@
 
 		</div>
 
-		<script src="../lib/js/head.min.js"></script>
 		<script src="../js/reveal.js"></script>
 		<script src="../plugin/markdown/marked.js"></script>
 		<script src="../plugin/markdown/markdown.js"></script>

+ 0 - 1
test/test-pdf.html

@@ -73,7 +73,6 @@
 
 		</div>
 
-		<script src="../lib/js/head.min.js"></script>
 		<script src="../js/reveal.js"></script>
 		<script src="qunit-2.5.0.js"></script>
 

+ 0 - 1
test/test.html

@@ -76,7 +76,6 @@
 
 		</div>
 
-		<script src="../lib/js/head.min.js"></script>
 		<script src="../js/reveal.js"></script>
 		<script src="qunit-2.5.0.js"></script>