Annotation of loncom/html/adm/jsMath/plugins/autoload.js, revision 1.2

1.1       albertel    1: /*
                      2:  *  autoload.js
                      3:  *  
                      4:  *  Part of the jsMath package for mathematics on the web.
                      5:  *
                      6:  *  This file is a plugin that checks if a page contains any math
                      7:  *  that must be processed by jsMath, and only loads jsMath.js
                      8:  *  when there is.
                      9:  *  
                     10:  *  You can control the items to look for via the variables
                     11:  *  
                     12:  *      jsMath.Autoload.findMathElements
                     13:  *      jsMath.Autoload.findTeXstrings
                     14:  *      jsMath.Autoload.findLaTeXstrings
1.2     ! albertel   15:  *      jsMath.Autoload.findCustomStrings
        !            16:  *      jsMath.Autoload.findCustomSettings
1.1       albertel   17:  *  
                     18:  *  which control whether to look for SPAN and DIV elements of class
                     19:  *  "math", TeX strings that will be converted by jsMath.ConvertTeX(), or
                     20:  *  LaTeX strings that will be converted by jsMath.ConvertLaTeX().  By
1.2     ! albertel   21:  *  default, the first is true and the second and third are false.  The
        !            22:  *  findCustomStrings can be used to specify your own delimiters for
        !            23:  *  in-line and display mathematics, e.g
        !            24:  *  
        !            25:  *      jsMath.Autoload.findCustomStrings = [
        !            26:  *         '[math],'[/math]',          // start and end in-line math
        !            27:  *         '[display]','[/display]'    // start and end display math
        !            28:  *      ];
        !            29:  *  
        !            30:  *  Finally, findCustomSettings can be set to an object reference whose
        !            31:  *  name:value pairs control the individual search settings for tex2math.  
        !            32:  *  (See the plugins/tex2math.js file for more details).
1.1       albertel   33:  *  
                     34:  *  If any math strings are found, jsMath.js will be loaded automatically, 
1.2     ! albertel   35:  *  but not loaded otherwise.  If any of the last four are set and TeX math
1.1       albertel   36:  *  strings are found, then plugins/tex2ath.js will be loaded
                     37:  *  automatically.  jsMath.Autoload.needsJsMath will be set to true or
1.2     ! albertel   38:  *  false depending on whether jsMath needed to be loaded.
1.1       albertel   39:  *  
                     40:  *  The value of jsMath.Autoload.element controls the element to be
                     41:  *  searched by the autoload plug-in.  If unset, the complete document will
                     42:  *  be searched.  If set to a string, the element with that name will be
                     43:  *  searched.  If set to a DOM object, that object and its children will
                     44:  *  be searched.
                     45:  *  
                     46:  *  Finally, there are two additional parameters that control files to
                     47:  *  be loaded after jsMath.js, should it be needed.  These are
                     48:  *  
                     49:  *      jsMath.Autoload.loadFonts
                     50:  *      jsMath.Autoload.loadFiles
                     51:  *  
                     52:  *  If jsMath.js is loaded, the fonts contained in the loadFonts array
                     53:  *  will be loaded, and the JavaScript files listed in the loadFiles array
                     54:  *  will be run.  Relative URL's are loaded based from the URL containing
                     55:  *  jsMath.js.
                     56:  *  
1.2     ! albertel   57:  *  The autoload plugin can be loaded in the document HEAD or in the BODY. 
        !            58:  *  If it is loaded in the HEAD, you will need to call jsMath.Autoload.Check()
        !            59:  *  at the end of the BODY (say in the window.onload handler) in order to
        !            60:  *  get it to check the page for math that needs to be tagged, otherwise load
        !            61:  *  the file at the bottom of the BODY and it will run the check automatically.
        !            62:  *
        !            63:  *  You can call jsMath.Autoload.Run() after the check has been performed
        !            64:  *  in order to call the appropriate tex2math routines for the given Autoload
        !            65:  *  settings.  You can call jsMath.Autoload.Run() even when jsMath isn't loaded.
        !            66:  *  
1.1       albertel   67:  *  ---------------------------------------------------------------------
                     68:  *
1.2     ! albertel   69:  *  Copyright 2004-2006 by Davide P. Cervone
        !            70:  * 
        !            71:  *  Licensed under the Apache License, Version 2.0 (the "License");
        !            72:  *  you may not use this file except in compliance with the License.
        !            73:  *  You may obtain a copy of the License at
        !            74:  * 
        !            75:  *      http://www.apache.org/licenses/LICENSE-2.0
        !            76:  * 
        !            77:  *  Unless required by applicable law or agreed to in writing, software
        !            78:  *  distributed under the License is distributed on an "AS IS" BASIS,
        !            79:  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
        !            80:  *  See the License for the specific language governing permissions and
        !            81:  *  limitations under the License.
1.1       albertel   82:  */
                     83: 
                     84: /*************************************************************************/
                     85: 
                     86: /*
                     87:  *  Make sure jsMath.Autoload is available
                     88:  */
1.2     ! albertel   89: if (jsMath == null) {jsMath = {}}
1.1       albertel   90: if (jsMath.Autoload == null) {jsMath.Autoload = {}}
1.2     ! albertel   91: jsMath.Add = function (dst,src) {for (var id in src) {dst[id] = src[id]}},
        !            92: jsMath.document = document; // tex2math needs this
        !            93: 
        !            94: jsMath.Add(jsMath.Autoload,{
        !            95:   
        !            96:   Script: {
        !            97: 
        !            98:     iframe: null,  // the hidden iframe
        !            99: 
        !           100:     /*
        !           101:      *  Load an external JavaScript file asynchronously
        !           102:      */
        !           103:     Load: function (url) {
        !           104:       this.iframe = document.createElement('iframe');
        !           105:       this.iframe.style.visibility = 'hidden';
        !           106:       this.iframe.style.position = 'absolute';
        !           107:       this.iframe.style.width  = '0px';
        !           108:       this.iframe.style.height = '0px';
        !           109:       if (document.body.firstChild) {
        !           110:         document.body.insertBefore(this.iframe,document.body.firstChild);
        !           111:       } else {
        !           112:         document.body.appendChild(this.iframe);
        !           113:       }
        !           114:       this.url = url; setTimeout('jsMath.Autoload.Script.setURL()',100);
        !           115:     },
        !           116:     endLoad: function () {setTimeout('jsMath.Autoload.Script.AfterLoad()',1)},
        !           117: 
        !           118:     /*
        !           119:      *  Use location.replace() to avoid browsers placing the file in
        !           120:      *  the history (and messing up the BACK button action).  Not
        !           121:      *  completely effective in Firefox 1.0.x.  Safari won't handle
        !           122:      *  replace() if the document is local (not sure if that's really
        !           123:      *  the issue, but that's the only time I see it).
        !           124:      */
        !           125:     setURL: function () {
        !           126:       var url = jsMath.Autoload.root+"jsMath-autoload.html";
        !           127:       var doc = this.iframe.contentDocument;
        !           128:       if (!doc && this.iframe.contentWindow) {doc = this.iframe.contentWindow.document}
        !           129:       if (navigator.vendor == "Apple Computer, Inc." &&
        !           130:           document.location.protocol == 'file:') {doc = null}
        !           131:       if (doc) {doc.location.replace(url)} else {this.iframe.src = url}
        !           132:     },
        !           133: 
        !           134:     /*
        !           135:      *  Queue items that need to be postponed until jsMath has run
        !           136:      */
        !           137:     queue: [],
        !           138:     Push: function (name,data) {this.queue[this.queue.length] = [name,data]},
        !           139:     RunStack: function () {
        !           140:       if (this.tex2math) {jsMath.Autoload.Check2(); return}
        !           141:       for (var i = 0; i < this.queue.length; i++) {
        !           142:         var name = this.queue[i][0];
        !           143:         var data = this.queue[i][1];
        !           144:         if (data.length == 1) {jsMath[name](data[0])}
        !           145:           else {jsMath[name](data[0],data[1],data[2],data[3])}
        !           146:       }
        !           147:     },
        !           148:   
        !           149:     AfterLoad: function () {jsMath.Autoload.Script.RunStack()},
        !           150: 
        !           151:     /*
        !           152:      *  Look up the jsMath root directory, if it is not already supplied
        !           153:      */
        !           154:     Root: function () {
        !           155:       if (jsMath.Autoload.root) return;
        !           156:       var script = document.getElementsByTagName('script');
        !           157:       if (script) {
        !           158:         for (var i = 0; i < script.length; i++) {
        !           159:           var src = script[i].src;
        !           160:           if (src && src.match('(^|/)plugins/autoload.js$')) {
        !           161:             jsMath.Autoload.root = src.replace(/plugins\/autoload.js$/,'');
        !           162:             break;
        !           163:           }
        !           164:         }
        !           165:       }
        !           166:     }
1.1       albertel  167: 
1.2     ! albertel  168:   },
        !           169:   
        !           170:   /**************************************************************/
        !           171:   
        !           172:   /*
        !           173:    *  Load tex2math first (so we can call its search functions
        !           174:    *  to look to see if anything needs to be turned into math)
        !           175:    *  if it is needed, otherwise go on to the second check.
        !           176:    */
        !           177:   Check: function () {
        !           178:     if (this.checked) return; this.checked = 1;
        !           179:     if (this.findTeXstrings || this.findLaTeXstrings ||
        !           180:         this.findCustomStrings || this.findCustomSettings) {
        !           181:       this.Script.tex2math = 1;
        !           182:       this.Script.Load('plugins/tex2math.js');
        !           183:     } else {
        !           184:       if (!jsMath.tex2math) {jsMath.tex2math = {}}
        !           185:       this.Check2();
        !           186:     }
        !           187:   },
1.1       albertel  188: 
1.2     ! albertel  189:   /*
        !           190:    *  Once tex2math is loaded, use it to check for math that
        !           191:    *  needs to be tagged for jsMath, and load jsMath if it is needed
        !           192:    */
        !           193:   Check2: function () {
        !           194:     this.Script.tex2math = 0;
        !           195:     this.needsJsMath = 0;
        !           196:     if (this.findMathElements) {
        !           197:       this.needsJsMath = this.areMathElements(this.checkElement);
        !           198:     }
        !           199:     jsMath.tex2math.callback = this.tex2mathCallback;
        !           200:     if (this.findTeXstrings && !this.needsJsMath) {
        !           201:       jsMath.tex2math.ConvertTeX(this.checkElement);
        !           202:     }
        !           203:     if (this.findLaTeXstrings && !this.needsJsMath) {
        !           204:       jsMath.tex2math.ConvertLaTeX(this.checkElement);
        !           205:     }
        !           206:     if (this.findCustomSettings && !this.needsJsMath) {
        !           207:       jsMath.tex2math.Convert(this.checkElement,this.findCustomSettings);
        !           208:     }
        !           209:     if (this.findCustomStrings && !this.needsJsMath) {
        !           210:       var s = this.findCustomStrings;
        !           211:       jsMath.tex2math.CustomSearch(s[0],s[1],s[2],s[3]);
        !           212:       jsMath.tex2math.ConvertCustom(this.checkElement);
        !           213:     }
        !           214:     jsMath.tex2math.callback = null;
1.1       albertel  215: 
1.2     ! albertel  216:     if (this.needsJsMath) {
        !           217:       this.LoadJsMath();
        !           218:     } else {
        !           219:       jsMath.Autoload.Script = null;
        !           220:       jsMath.Process = function () {};
        !           221:       jsMath.ProcessBeforeShowing = function () {};
        !           222:       jsMath.Synchronize = function () {};
        !           223:       jsMath.ConvertTeX = function () {};
        !           224:       jsMath.ConvertTeX2 = function () {};
        !           225:       jsMath.ConvertLaTeX = function () {};
        !           226:       jsMath.ConvertCustom = function () {};
        !           227:       jsMath.CustomSearch = function () {};
        !           228:       jsMath.Macro = function () {};
        !           229:       jsMath.Autoload.Run = function () {};
1.1       albertel  230:     }
1.2     ! albertel  231:   },
1.1       albertel  232: 
1.2     ! albertel  233:   /*
        !           234:    *  A callback used in the tex2math searches to signal that
        !           235:    *  some math has been found.
        !           236:    */
        !           237:   tex2mathCallback: function () {
        !           238:     jsMath.Autoload.needsJsMath = 1;
        !           239:     return false;
        !           240:   },
        !           241: 
        !           242:   /*
        !           243:    *  jsMath.Autoload.Run() can be called to perform the 
        !           244:    *  tex2math calls given by the Autoload parameters.
        !           245:    */
        !           246:   Run: function (data) {this.Script.Push('Autorun',[data])},
        !           247: 
        !           248:   Autorun: function () {
        !           249:     if (this.findTeXstrings) {jsMath.ConvertTeX(this.checkElement)}
        !           250:     if (this.findLaTeXstrings) {jsMath.ConvertLaTeX(this.checkElement)}
        !           251:     if (this.findCustomSettings) {
        !           252:       jsMath.Synchronize(function () {
        !           253:         jsMath.tex2math.Convert(jsMath.Autoload.checkElement,
        !           254:                                 jsMath.Autoload.findCustomSettings);
        !           255:       });
        !           256:     }
        !           257:     if (this.findCustomStrings) {
        !           258:       var s = this.findCustomStrings;
        !           259:       jsMath.CustomSearch(s[0],s[1],s[2],s[3]);
        !           260:       jsMath.ConvertCustom(this.checkElement);
        !           261:     }
        !           262:   },
1.1       albertel  263: 
1.2     ! albertel  264:   /*
        !           265:    *  Look to see if there are SPAN or DIV elements of class "math".
        !           266:    */
        !           267:   areMathElements: function (obj) {
        !           268:     if (!obj) {obj = document}
        !           269:     if (typeof(obj) == 'string') {obj = document.getElementById(obj)}
        !           270:     if (!obj.getElementsByTagName) {return false}
        !           271:     var math = obj.getElementsByTagName('div');
        !           272:     for (var k = 0; k < math.length; k++) 
        !           273:       {if (math[k].className == 'math') {return true}}
        !           274:     math = obj.getElementsByTagName('span');
        !           275:     for (var k = 0; k < math.length; k++) 
        !           276:       {if (math[k].className == 'math') {return true}}
        !           277:     return false;
        !           278:   },
        !           279: 
        !           280:   /*
        !           281:    *  When math tags are found, load the jsMath.js file,
        !           282:    *  and afterward, load any auxiliary files or fonts,
        !           283:    *  and then do any pending commands.
        !           284:    */
        !           285:   LoadJsMath: function () {
        !           286:     if (this.root) {
        !           287:       this.setMessage('Loading jsMath...');
        !           288:       this.Script.AfterLoad = this.afterLoad;
        !           289:       this.Script.Load('jsMath.js');
        !           290:     } else {
        !           291:       alert("Can't determine URL for jsMath.js");
        !           292:     }
        !           293:   },
        !           294:   afterLoad: function () {
        !           295:     if (jsMath.tex2math.window) {jsMath.tex2math.window.jsMath = jsMath}
        !           296:     //
        !           297:     //  Handle MSIE bug where jsMath.window both is and is not the actual window
        !           298:     //
        !           299:     if (jsMath.browser == 'MSIE') {window.onscroll = jsMath.window.onscroll};
        !           300:     var fonts = jsMath.Autoload.loadFonts;
        !           301:     if (fonts) {
        !           302:       if (typeof(fonts) != 'object') {fonts = [fonts]}
        !           303:       for (var i in fonts) {jsMath.Font.Load(fonts[i])}
        !           304:     }
        !           305:     var files = jsMath.Autoload.loadFiles;
        !           306:     if (files) {
        !           307:       if (typeof(files) != 'object') {files = [files]}
        !           308:       for (var i in files) {jsMath.Setup.Script(files[i])}
        !           309:     }
        !           310:     jsMath.Synchronize(function () {jsMath.Autoload.Script.RunStack()});
        !           311:     jsMath.Autoload.setMessage();
        !           312:   },
        !           313: 
        !           314:   /*
        !           315:    *  Display a message in a small box at the bottom of the screen
        !           316:    */
        !           317:   setMessage: function (message) {
        !           318:     if (message) {
        !           319:       this.div = document.createElement('div');
        !           320:       if (!document.body.hasChildNodes) {document.body.appendChild(this.div)}
        !           321:         else {document.body.insertBefore(this.div,document.body.firstChild)}
        !           322:       var style = {
        !           323:         position:'absolute', bottom:'1px', left:'2px',
        !           324:         backgroundColor:'#E6E6E6', border:'solid 1px #959595',
        !           325:         margin:'0px', padding:'1px 8px', zIndex:102,
        !           326:         color:'black', fontSize:'75%', width:'auto'
        !           327:       };
        !           328:       for (var id in style) {this.div.style[id] = style[id]}
        !           329:       this.div.appendChild(jsMath.document.createTextNode(message));
        !           330:     } else {
        !           331:       this.div.firstChild.nodeValue = "";
        !           332:       this.div.style.visibility = 'hidden';
1.1       albertel  333:     }
                    334:   }
1.2     ! albertel  335:   
        !           336: });
1.1       albertel  337: 
                    338: /*
1.2     ! albertel  339:  *  Queue these so we can do them after jsMath has been loaded
1.1       albertel  340:  */
1.2     ! albertel  341: jsMath.Add(jsMath,{
        !           342:   Process: function (data) {jsMath.Autoload.Script.Push('Process',[data])},
        !           343:   ProcessBeforeShowing: function (data) {jsMath.Autoload.Script.Push('ProcessBeforeShowing',[data])},
        !           344:   ConvertTeX: function (data) {jsMath.Autoload.Script.Push('ConvertTeX',[data])},
        !           345:   ConvertTeX2: function (data) {jsMath.Autoload.Script.Push('ConvertTeX2',[data])},
        !           346:   ConvertLaTeX: function (data) {jsMath.Autoload.Script.Push('ConvertLaTeX',[data])},
        !           347:   ConvertCustom: function (data) {jsMath.Autoload.Script.Push('ConvertCustom',[data])},
        !           348:   CustomSearch: function (d1,d2,d3,d4) {jsMath.Autoload.Script.Push('CustomSearch',[d1,d2,d3,d4])},
        !           349:   Synchronize: function (data) {jsMath.Autoload.Script.Push('Synchronize',[data])},
        !           350:   Macro: function (cs,def,params) {jsMath.Autoload.Script.Push('Macro',[cs,def,params])},
        !           351:   Autorun: function () {jsMath.Autoload.Autorun()}
        !           352: });
1.1       albertel  353: 
                    354: /*
1.2     ! albertel  355:  *  Initialize
1.1       albertel  356:  */
                    357: 
                    358: if (jsMath.Autoload.findMathElements == null) {jsMath.Autoload.findMathElements = 1}
                    359: if (jsMath.Autoload.findTeXstrings == null)   {jsMath.Autoload.findTeXstrings = 0}
                    360: if (jsMath.Autoload.findLaTeXstrings == null) {jsMath.Autoload.findLaTeXstrings = 0}
                    361: 
1.2     ! albertel  362: jsMath.Autoload.Script.Root();
        !           363: if (document.body) {jsMath.Autoload.Check()}

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>