Skip to content
Browse files

SAKIII-5617 - Remove MathJax from the initial pageload

  • Loading branch information...
1 parent c923464 commit 5ae93dc37b626e7e11db26529600a464e12c109d Nicolaas Matthijs committed May 28, 2012
Showing with 40 additions and 21 deletions.
  1. +40 −21 dev/lib/sakai/sakai.api.util.js
View
61 dev/lib/sakai/sakai.api.util.js
@@ -43,26 +43,6 @@ define(
var sakai_util = {
startup : function(meData) {
- // I know this is hideous
- (function () {
- var script = document.createElement("script");
- script.type = "text/javascript";
- script.src = "/dev/lib/MathJax/MathJax.js";
-
- var config =
- 'MathJax.Hub.Config({' +
- 'messageStyle: "none",' +
- 'config: "default.js",' +
- 'styles: {"#MathJax_Message": {display: "none !important"}}' +
- '}); ' +
- 'MathJax.Hub.Startup.onload();';
-
- if (window.opera) {script.innerHTML = config;}
- else {script.text = config;}
-
- $("head")[0].appendChild(script);
- })();
-
// Start polling to keep session alive when logged in
if (meData.user.userid) {
setInterval(function() {
@@ -1837,6 +1817,29 @@ define(
}
},
+ loadedMathJax: false,
+ loadMathJax: function(){
+ if (!sakai_util.loadedMathJax){
+ sakai_util.loadedMathJax = true;
+ var script = document.createElement("script");
+ script.type = "text/javascript";
+ script.src = "/dev/lib/MathJax/MathJax.js";
+
+ var config =
+ 'MathJax.Hub.Config({' +
+ 'messageStyle: "none",' +
+ 'config: "default.js",' +
+ 'styles: {"#MathJax_Message": {display: "none !important"}}' +
+ '}); ' +
+ 'MathJax.Hub.Startup.onload();';
+
+ if (window.opera) {script.innerHTML = config;}
+ else {script.text = config;}
+
+ $("head")[0].appendChild(script);
+ }
+ },
+
/**
* Runs MathJax over an element replacing any math TeX with rendered
* formulas
@@ -1848,7 +1851,23 @@ define(
{
element = element[0];
}
- MathJax.Hub.Queue(["Typeset", MathJax.Hub, element]);
+ // Check whether a MathJax formula is available
+ var elementContent = $(element).html();
+ if (elementContent && elementContent.indexOf("$$") !== -1){
+ // Check whether MathJax has already been loaded
+ if (!window["MathJax"] || !MathJax.Hub){
+ sakai_util.loadMathJax();
+ }
+ // Try to render the formula. This will fail if MathJax hasn't finished
+ // loading yet. If that's the case, the system will retry after 200ms
+ try {
+ MathJax.Hub.Queue(["Typeset", MathJax.Hub, element]);
+ } catch (err){
+ setTimeout(function(){
+ sakai_util.renderMath(element);
+ }, 200);
+ }
+ }
},
// :?=&;\/?@+$<>#%'"''{}|\\^[]'

0 comments on commit 5ae93dc

Please sign in to comment.
Something went wrong with that request. Please try again.