Project

General

Profile

1
/*
2
License: LGPL as per: http://www.gnu.org/copyleft/lesser.html
3
$Id$
4
*/
5

    
6
/** get a time stamp at start of the page load */
7
var mbTimerStart = new Date();
8

    
9
/** the global config object */
10
var config;
11

    
12
/** URL of Mapbuilder's lib/ directory. */
13
var baseDir;
14

    
15
/** mapbuilder environement settings, defaults to a .xml extension but is 
16
  auto-configured by the ant build script to .jsp for tomcat environment 
17
  the URL to this file will be pre-pended with the baseDir value.
18
*/
19
var mbServerConfig = "mapbuilderConfig.xml";
20
var mbNsUrl = "http://mapbuilder.sourceforge.net/mapbuilder";
21

    
22
// LoadState Constants
23
var MB_UNLOADED=0;    // Scripts not loaded yet
24
var MB_LOAD_CORE=1;   // Loading scripts loaded defined in Mapbuilder
25
var MB_LOAD_CONFIG=2; // Loading scripts loaded defined in Config
26
var MB_LOADED=3;      // All scripts loaded
27

    
28
/**
29
 * This Object bootstraps the Mapbuilder libraries by loading the core
30
 * script files.
31
 * When Config.js is loaded, the script files for objects described in the
32
 * Mapbuilder config file are loaded.
33
 * Objects which have dependencies will trigger the dependancies to load
34
 * when they are loaded.
35
 *
36
 * @constructor
37
 * @author Cameron Shorter
38
 * @requires Config
39
 * @requires Listener
40
 * @requires ModelBase
41
 * @requires Sarissa
42
 * @requires Util
43
 */
44
function Mapbuilder() {
45

    
46
  /**
47
   * Determines which Mapbuilder scripts are loading.
48
   * TBD: Is it possible to use enumerated types in JS?
49
   */
50
  this.loadState=MB_UNLOADED;
51

    
52
  /** Array of objects that are loading.  Don't continue initialisation until
53
   * all objects have loaded. */
54
  this.loadingScripts=new Array();
55

    
56
  /** Timer to periodically check if scripts have loaded. */
57
  this.scriptsTimer=null;
58

    
59
  /**
60
   * Called periodically and moves onto the next loadState when this round of
61
   * scripts have loaded.
62
   * For IE clients, object.readyState is used to check if scripts are loaded.
63
   * Mozilla works fine without this function - I think it is single threaded.
64
   */
65
  this.checkScriptsLoaded=function() {
66
    if (document.readyState!=null){
67
      // IE client
68

    
69
      // Scripts are removed from array when they have loaded
70
      while(this.loadingScripts.length>0
71
        &&(this.loadingScripts[0].readyState=="loaded"
72
          ||this.loadingScripts[0].readyState=="complete"
73
          ||this.loadingScripts[0].readyState==null))
74
      {
75
        this.loadingScripts.shift();
76
      }
77
      if (this.loadingScripts.length==0){
78
        this.setLoadState(this.loadState+1);
79
      }
80
    }
81
    else{
82
      // Mozilla client
83
      if(this.loadState==MB_LOAD_CORE && config!=null){
84
        // Config has finished loading
85
        this.setLoadState(MB_LOAD_CONFIG);
86
      }
87
    }
88
  }
89

    
90
  /**
91
   * Move onto loading the next set of scripts.
92
   * @param newState The new loading state.
93
   */
94
  this.setLoadState=function(newState){
95
    this.loadState=newState;
96
    switch (newState){
97
      case MB_LOAD_CORE:
98
        this.loadScript(baseDir+"/util/overlib/overlib.js");
99
        this.loadScript(baseDir+"/util/sarissa/Sarissa.js");
100
        this.loadScript(baseDir+"/util/sarissa/sarissa_ieemu_xpath.js");
101
        //this.loadScript(baseDir+"/util/sarissa/sarissa_dhtml.js");
102
        //this.loadScript(baseDir+"/util/sarissa/sarissa_ieemu_xslt.js");//all deprecated
103
        this.loadScript(baseDir+"/util/Util.js");
104
        this.loadScript(baseDir+"/util/Listener.js");
105
        this.loadScript(baseDir+"/model/ModelBase.js");
106
        this.loadScript(baseDir+"/model/Config.js");
107
        break;
108
      case MB_LOAD_CONFIG:
109
         if(document.readyState){
110
          // IE
111
          config=new Config(mbConfigUrl);
112
          config.loadConfigScripts();
113
        }else{
114
          // Mozilla
115
          this.setLoadState(MB_LOADED);
116
        }
117
        break;
118
      case MB_LOADED:
119
        clearInterval(this.scriptsTimer);
120
        break;
121
    }
122
  }
123

    
124
  /**
125
   * Dynamically load a script file if it has not already been loaded.
126
   * @param url The url of the script.
127
   */
128
  this.loadScript=function(url){
129
    if(!document.getElementById(url)){
130
      var script = document.createElement('script');
131
      script.defer = false;   //not sure of effect of this?
132
      script.type = "text/javascript";
133
      script.src = url;
134
      script.id = url;
135
      document.getElementsByTagName('head')[0].appendChild(script);
136
      this.loadingScripts.push(script);
137
    }
138
  }
139

    
140
  /**
141
   * Internal function to load scripts for components that don't have <scriptfile>
142
   * specified in the config file.
143
   * @param xPath Xpath match of components from the Config file.
144
   * @param dir The directory the script is located in.
145
   */
146
  this.loadScriptsFromXpath=function(nodes,dir) {
147
    //var nodes = this.doc.selectNodes(xPath);
148
    for (var i=0; i<nodes.length; i++) {
149
      if (nodes[i].selectSingleNode("mb:scriptFile")==null){
150
        scriptFile = baseDir+"/"+dir+nodes[i].nodeName+".js";
151
        this.loadScript(scriptFile);
152
      }
153
    }
154
  }
155

    
156
  //derive the baseDir value by looking for the script tag that loaded this file
157
  var head = document.getElementsByTagName('head')[0];
158
  var nodes = head.childNodes;
159
  for (var i=0; i<nodes.length; ++i ){
160
    var src = nodes.item(i).src;
161
    if (src) {
162
      var index = src.indexOf("/Mapbuilder.js");
163
      if (index>=0) {
164
        baseDir = src.substring(0, index);
165
      }
166
    }
167
  }
168

    
169
  // Start loading core scripts.
170
  this.setLoadState(MB_LOAD_CORE);
171

    
172
  // Start a timer which periodically calls checkScriptsLoaded().
173
  this.scriptsTimer=setInterval('mapbuilder.checkScriptsLoaded()',100);
174
}
175

    
176
/**
177
 * copied from sarissa, a function to check browser security setting in IE, 
178
 * opens a help page if ActiveX objects are disabled.
179
 */
180
function checkIESecurity()
181
{
182
  var testPrefixes = ["Msxml2.DOMDocument.5.0", "Msxml2.DOMDocument.4.0", "Msxml2.DOMDocument.3.0", "MSXML2.DOMDocument", "MSXML.DOMDocument", "Microsoft.XMLDOM"];
183
  // found progID flag
184
  var bFound = false;
185
  for(var i=0; i < testPrefixes.length && !bFound; i++) {
186
    try {
187
      var oDoc = new ActiveXObject(testPrefixes[i]);
188
      bFound = true;
189
    }
190
    catch (e) {
191
      //trap; try next progID
192
    }
193
  }
194
  if (!bFound) window.open("/mapbuilder/docs/help/IESetup.html");  //TBD: set this URL in config
195
}
196

    
197
if (navigator.userAgent.toLowerCase().indexOf("msie") > -1) checkIESecurity();
198
var mapbuilder=new Mapbuilder();
199

    
200
/**
201
 * Initialise Mapbuilder if script has been loaded, else wait to be called
202
 * again.
203
 */
204
function mapbuilderInit(){
205
  if(mapbuilder && mapbuilder.loadState==MB_LOADED){
206
    clearInterval(mbTimerId);
207
    config.init(config);
208
    config.callListeners("init");
209
    var mbTimerStop = new Date();
210
    //alert("load time:"+(mbTimerStop.getTime()-mbTimerStart.getTime()) );
211
    if (window.mbInit) window.mbInit();
212
    config.callListeners("loadModel");
213
  }
214
}
215

    
216
/** Timer used when checking if scripts have loaded. */
217
var mbTimerId;
218

    
219
/**
220
 * Mapbuilder's main initialisation script.
221
 * This should be called from the main html file using:
222
 *   <body onload="mbDoLoad()">
223
 * @param initFunction Optional - A function reference that will be called after 
224
 * config.init() has been called.  This is to give application code a chance to
225
 * do initialization and be guaranteed that all objects exist (inlcuding config).
226
 */
227
function mbDoLoad(initFunction) {
228
  // See if scripts have been loaded every 100msecs, then call config.init().
229
  mbTimerId=setInterval('mapbuilderInit()',100);
230
  if (initFunction) window.mbInit = initFunction;
231
}
(6-6/15)