make mathjax host a config option, revamp script loading, fragment examples #531

This commit is contained in:
Hakim El Hattab
2013-08-12 23:15:19 -04:00
parent 3901904057
commit 3e23b8b690
2 changed files with 109 additions and 20 deletions

View File

@ -9,27 +9,10 @@ var RevealMath = window.RevealMath || (function(){
var loaded = false;
var config = Reveal.getConfig().math || {};
config.host = config.host || 'http://cdn.mathjax.org/mathjax/latest/MathJax.js';
config.mode = config.mode || 'TeX-AMS_HTML-full';
var head = document.querySelector( 'head' );
var script = document.createElement( 'script' );
script.type = 'text/javascript';
script.src = 'http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=' + config.mode;
// Detect when the script has loaded
script.onload = onScriptLoad;
// IE
script.onreadystatechange = function() {
if ( this.readyState === 'loaded' ) {
onScriptLoad.call();
}
}
// Normal browsers
head.appendChild( script );
function onScriptLoad() {
loadScript( config.host + '?config=' + config.mode, function() {
// Conditioned just in case both onload and readystate fire
if( loaded === false ) {
@ -48,11 +31,42 @@ var RevealMath = window.RevealMath || (function(){
// This will only typeset equations that have not yet been
// processed, as well as equations that have change since
// last being processed.
MathJax.Hub.Update( event.currentSlide );
MathJax.Hub.Update( event.currentSlide, function() {
Reveal.layout();
} );
} );
}
} );
function loadScript( url, callback ) {
var head = document.querySelector( 'head' );
var script = document.createElement( 'script' );
script.type = 'text/javascript';
script.src = url;
// Wrapper for callback to make sure it only fires once
var finish = function() {
if( typeof callback === 'function' ) {
callback.call();
callback = null;
}
}
script.onload = finish;
// IE
script.onreadystatechange = function() {
if ( this.readyState === 'loaded' ) {
finish.call();
}
}
// Normal browsers
head.appendChild( script );
}
})();