--- loncom/html/adm/jsMath/plugins/autoload.js 2006/05/17 22:19:38 1.3 +++ loncom/html/adm/jsMath/plugins/autoload.js 2007/10/09 21:29:28 1.4 @@ -9,18 +9,16 @@ * * You can control the items to look for via the variables * - * jsMath.Autoload.findMathElements * jsMath.Autoload.findTeXstrings * jsMath.Autoload.findLaTeXstrings * jsMath.Autoload.findCustomStrings * jsMath.Autoload.findCustomSettings * - * which control whether to look for SPAN and DIV elements of class - * "math", TeX strings that will be converted by jsMath.ConvertTeX(), or - * LaTeX strings that will be converted by jsMath.ConvertLaTeX(). By - * default, the first is true and the second and third are false. The - * findCustomStrings can be used to specify your own delimiters for - * in-line and display mathematics, e.g + * which control whether to look for TeX strings that will be converted + * by jsMath.ConvertTeX(), or LaTeX strings that will be converted by + * jsMath.ConvertLaTeX(). By default, the first is true and the second + * and third are false. The findCustomStrings can be used to specify your + * own delimiters for in-line and display mathematics, e.g. * * jsMath.Autoload.findCustomStrings = [ * '[math],'[/math]', // start and end in-line math @@ -95,12 +93,61 @@ jsMath.Add(jsMath.Autoload,{ Script: { - iframe: null, // the hidden iframe + request: null, // XMLHttpRequest object (if we can get it) + iframe: null, // the hidden iframe (if not) /* - * Load an external JavaScript file asynchronously + * Get XMLHttpRequest object, if possible, and look up the URL root + * (MSIE can't use xmlReuest to load local files, so avoid that) + */ + Init: function () { + if (!(document.URL && document.URL.match(/^file:\/\/.*\\/))) { + if (window.XMLHttpRequest) {try {this.request = new XMLHttpRequest} catch (err) {}} + if (!this.request && window.ActiveXObject) { + var xml = ["MSXML2.XMLHTTP.5.0","MSXML2.XMLHTTP.4.0","MSXML2.XMLHTTP.3.0", + "MSXML2.XMLHTTP","Microsoft.XMLHTTP"]; + for (var i = 0; i < xml.length && !this.request; i++) { + try {this.request = new ActiveXObject(xml[i])} catch (err) {} + } + } + } + this.Root(); + }, + + /* + * Load an external JavaScript file */ Load: function (url) { + if (this.request) { + setTimeout(function () {jsMath.Autoload.Script.xmlLoad(url)},1); + } else { + this.startLoad(url); + } + }, + + /* + * Load an external JavaScript file via XMLHttpRequest + */ + xmlLoad: function (url) { + try { + this.request.open("GET",jsMath.Autoload.root+url,false); + this.request.send(null); + } catch (err) { + throw "autoload: can't load the file '"+url+"'\n" + + "Message: "+err.message; + } + if (this.request.status && this.request.status >= 400) { + throw "autoload: can't load the file '"+url+"'\n" + + "Error status: "+this.request.status; + } + window.eval(this.request.responseText); + this.endLoad(); + }, + + /* + * Load an external JavaScript file via jsMath-autoload.html + */ + startLoad: function (url) { this.iframe = document.createElement('iframe'); this.iframe.style.visibility = 'hidden'; this.iframe.style.position = 'absolute'; @@ -144,6 +191,7 @@ jsMath.Add(jsMath.Autoload,{ if (data.length == 1) {jsMath[name](data[0])} else {jsMath[name](data[0],data[1],data[2],data[3])} } + this.queue = []; }, AfterLoad: function () {jsMath.Autoload.Script.RunStack()}, @@ -157,7 +205,7 @@ jsMath.Add(jsMath.Autoload,{ if (script) { for (var i = 0; i < script.length; i++) { var src = script[i].src; - if (src && src.match('(^|/)plugins/autoload.js$')) { + if (src && src.match('(^|/|\\\\)plugins/autoload.js$')) { jsMath.Autoload.root = src.replace(/plugins\/autoload.js$/,''); break; } @@ -176,8 +224,9 @@ jsMath.Add(jsMath.Autoload,{ */ Check: function () { if (this.checked) return; this.checked = 1; - if (this.findTeXstrings || this.findLaTeXstrings || - this.findCustomStrings || this.findCustomSettings) { + if ((this.findTeXstrings || this.findLaTeXstrings || + this.findCustomStrings || this.findCustomSettings) && + (!jsMath.tex2math || !jsMath.tex2math.loaded)) { this.Script.tex2math = 1; this.Script.Load('plugins/tex2math.js'); } else { @@ -185,48 +234,48 @@ jsMath.Add(jsMath.Autoload,{ this.Check2(); } }, + ReCheck: function () { + if (jsMath.loaded) return; + this.InitStubs(); + this.checked = 0; + this.Script.queue = []; + this.Check(); + }, /* * Once tex2math is loaded, use it to check for math that * needs to be tagged for jsMath, and load jsMath if it is needed */ Check2: function () { - this.Script.tex2math = 0; - this.needsJsMath = 0; if (this.checkElement == null) {this.checkElement = null} - if (this.findMathElements) { - this.needsJsMath = this.areMathElements(this.checkElement); - } - jsMath.tex2math.callback = this.tex2mathCallback; - if (this.findTeXstrings && !this.needsJsMath) { - jsMath.tex2math.ConvertTeX(this.checkElement); - } - if (this.findLaTeXstrings && !this.needsJsMath) { - jsMath.tex2math.ConvertLaTeX(this.checkElement); - } - if (this.findCustomSettings && !this.needsJsMath) { - jsMath.tex2math.Convert(this.checkElement,this.findCustomSettings); - } - if (this.findCustomStrings && !this.needsJsMath) { + this.Script.tex2math = 0; this.needsJsMath = 0; + if (this.checkElement == null) {this.checkElement = null} + + if (this.findTeXstrings) {jsMath.tex2math.ConvertTeX(this.checkElement)} + if (this.findLaTeXstrings) {jsMath.tex2math.ConvertLaTeX(this.checkElement)} + if (this.findCustomSettings) {jsMath.tex2math.Convert(this.checkElement,this.findCustomSettings)} + if (this.findCustomStrings) { var s = this.findCustomStrings; jsMath.tex2math.CustomSearch(s[0],s[1],s[2],s[3]); jsMath.tex2math.ConvertCustom(this.checkElement); } - jsMath.tex2math.callback = null; + this.needsJsMath = this.areMathElements(this.checkElement); if (this.needsJsMath) { this.LoadJsMath(); } else { - jsMath.Autoload.Script = null; jsMath.Process = function () {}; jsMath.ProcessBeforeShowing = function () {}; - jsMath.Synchronize = function () {}; jsMath.ConvertTeX = function () {}; jsMath.ConvertTeX2 = function () {}; jsMath.ConvertLaTeX = function () {}; jsMath.ConvertCustom = function () {}; jsMath.CustomSearch = function () {}; jsMath.Macro = function () {}; - jsMath.Autoload.Run = function () {}; + jsMath.Synchronize = function (code,data) { + if (typeof(code) == 'string') {eval(code)} else {code(data)} + }; + jsMath.Autoload.Script.RunStack(); // perform pending commands + jsMath.Autoload.setMessage(); } }, @@ -240,26 +289,9 @@ jsMath.Add(jsMath.Autoload,{ }, /* - * jsMath.Autoload.Run() can be called to perform the - * tex2math calls given by the Autoload parameters. + * jsMath.Autoload.Run() is now longer needed */ - Run: function (data) {this.Script.Push('Autorun',[data])}, - - Autorun: function () { - if (this.findTeXstrings) {jsMath.ConvertTeX(this.checkElement)} - if (this.findLaTeXstrings) {jsMath.ConvertLaTeX(this.checkElement)} - if (this.findCustomSettings) { - jsMath.Synchronize(function () { - jsMath.tex2math.Convert(jsMath.Autoload.checkElement, - jsMath.Autoload.findCustomSettings); - }); - } - if (this.findCustomStrings) { - var s = this.findCustomStrings; - jsMath.CustomSearch(s[0],s[1],s[2],s[3]); - jsMath.ConvertCustom(this.checkElement); - } - }, + Run: function (data) {}, /* * Look to see if there are SPAN or DIV elements of class "math". @@ -283,7 +315,10 @@ jsMath.Add(jsMath.Autoload,{ * and then do any pending commands. */ LoadJsMath: function () { + if (this.loading) return; + if (jsMath.loaded) {this.afterLoad(); return} if (this.root) { + this.loading = 1; this.setMessage('Loading jsMath...'); this.Script.AfterLoad = this.afterLoad; this.Script.Load('jsMath.js'); @@ -292,6 +327,7 @@ jsMath.Add(jsMath.Autoload,{ } }, afterLoad: function () { + jsMath.Autoload.loading = 0; if (jsMath.tex2math.window) {jsMath.tex2math.window.jsMath = jsMath} // // Handle MSIE bug where jsMath.window both is and is not the actual window @@ -300,12 +336,12 @@ jsMath.Add(jsMath.Autoload,{ var fonts = jsMath.Autoload.loadFonts; if (fonts) { if (typeof(fonts) != 'object') {fonts = [fonts]} - for (var i in fonts) {jsMath.Font.Load(fonts[i])} + for (var i = 0; i < fonts.length; i++) {jsMath.Font.Load(fonts[i])} } var files = jsMath.Autoload.loadFiles; if (files) { if (typeof(files) != 'object') {files = [files]} - for (var i in files) {jsMath.Setup.Script(files[i])} + for (var i = 0; i < files.length; i++) {jsMath.Setup.Script(files[i])} } jsMath.Synchronize(function () {jsMath.Autoload.Script.RunStack()}); jsMath.Autoload.setMessage(); @@ -320,44 +356,45 @@ jsMath.Add(jsMath.Autoload,{ if (!document.body.hasChildNodes) {document.body.appendChild(this.div)} else {document.body.insertBefore(this.div,document.body.firstChild)} var style = { - position:'absolute', bottom:'1px', left:'2px', + position:'fixed', bottom:'1px', left:'2px', backgroundColor:'#E6E6E6', border:'solid 1px #959595', margin:'0px', padding:'1px 8px', zIndex:102, color:'black', fontSize:'75%', width:'auto' }; for (var id in style) {this.div.style[id] = style[id]} this.div.appendChild(jsMath.document.createTextNode(message)); - } else { + } else if (this.div) { this.div.firstChild.nodeValue = ""; this.div.style.visibility = 'hidden'; } - } - -}); + }, -/* - * Queue these so we can do them after jsMath has been loaded - */ -jsMath.Add(jsMath,{ - Process: function (data) {jsMath.Autoload.Script.Push('Process',[data])}, - ProcessBeforeShowing: function (data) {jsMath.Autoload.Script.Push('ProcessBeforeShowing',[data])}, - ConvertTeX: function (data) {jsMath.Autoload.Script.Push('ConvertTeX',[data])}, - ConvertTeX2: function (data) {jsMath.Autoload.Script.Push('ConvertTeX2',[data])}, - ConvertLaTeX: function (data) {jsMath.Autoload.Script.Push('ConvertLaTeX',[data])}, - ConvertCustom: function (data) {jsMath.Autoload.Script.Push('ConvertCustom',[data])}, - CustomSearch: function (d1,d2,d3,d4) {jsMath.Autoload.Script.Push('CustomSearch',[d1,d2,d3,d4])}, - Synchronize: function (data) {jsMath.Autoload.Script.Push('Synchronize',[data])}, - Macro: function (cs,def,params) {jsMath.Autoload.Script.Push('Macro',[cs,def,params])}, - Autorun: function () {jsMath.Autoload.Autorun()} + /* + * Queue these so we can do them after jsMath has been loaded + */ + stubs: { + Process: function (data) {jsMath.Autoload.Script.Push('Process',[data])}, + ProcessBeforeShowing: function (data) {jsMath.Autoload.Script.Push('ProcessBeforeShowing',[data])}, + ConvertTeX: function (data) {jsMath.Autoload.Script.Push('ConvertTeX',[data])}, + ConvertTeX2: function (data) {jsMath.Autoload.Script.Push('ConvertTeX2',[data])}, + ConvertLaTeX: function (data) {jsMath.Autoload.Script.Push('ConvertLaTeX',[data])}, + ConvertCustom: function (data) {jsMath.Autoload.Script.Push('ConvertCustom',[data])}, + CustomSearch: function (d1,d2,d3,d4) {jsMath.Autoload.Script.Push('CustomSearch',[d1,d2,d3,d4])}, + Synchronize: function (data) {jsMath.Autoload.Script.Push('Synchronize',[data])}, + Macro: function (cs,def,params) {jsMath.Autoload.Script.Push('Macro',[cs,def,params])} + }, + + InitStubs: function () {jsMath.Add(jsMath,jsMath.Autoload.stubs)} + }); /* * Initialize */ -if (jsMath.Autoload.findMathElements == null) {jsMath.Autoload.findMathElements = 1} if (jsMath.Autoload.findTeXstrings == null) {jsMath.Autoload.findTeXstrings = 0} if (jsMath.Autoload.findLaTeXstrings == null) {jsMath.Autoload.findLaTeXstrings = 0} -jsMath.Autoload.Script.Root(); +jsMath.Autoload.Script.Init(); +jsMath.Autoload.InitStubs(); if (document.body) {jsMath.Autoload.Check()}