Project

General

Profile

1
/*
2
License: LGPL as per: http://www.gnu.org/copyleft/lesser.html
3
$Id: Config.js 3893 2008-02-29 18:22:13Z ahocevar $
4
*/
5

    
6
// Ensure this object's dependancies are loaded.
7
mapbuilder.loadScript(baseDir+"/model/ModelBase.js");
8

    
9
/**
10
 * The config object is the parent model of all mapbuilder objects.
11
 * The application creates a global object called 'config' which represents
12
 * the mapbuilder configuration xml file passed in as a parameter.
13
 * Config is a model like any other model.  
14
 * Any mapbuilder object can be de-referenced by using the 'config.objects' 
15
 * property as in config.objects.idValueFromConfig.
16
 * The schema for the config can be found at /mabuilder/lib/schemas/config.xsd
17
 *
18
 * @constructor
19
 * @base ModelBase
20
 * @author adair
21
 * @requires Sarissa
22
 * @param url URL of the configuration file.
23
 */
24
function Config(url) {
25
/**
26
 * open the application specific configuration document, passed in aas the url argument.
27
 */
28
  this.doc = Sarissa.getDomDocument();
29
  this.doc.async = false;
30
  this.doc.validateOnParse=false;  //IE6 SP2 parsing bug
31
  if(_SARISSA_IS_SAFARI)
32
  {
33
  var xmlhttp = new XMLHttpRequest();
34
  xmlhttp.open("GET", url, false);
35
  xmlhttp.send(null);
36
  this.doc = xmlhttp.responseXML;
37
  }else
38
   {
39
  this.doc.load(url);
40
  }
41
  if (Sarissa.getParseErrorText(this.doc) != Sarissa.PARSED_OK){
42
    alert("error loading config document: " + url );//+ " - " + Sarissa.getParseErrorText(this.doc) );
43
  }
44
  this.url = url;
45
  this.namespace = "xmlns:mb='"+mbNsUrl+"'";
46
  if(! _SARISSA_IS_SAFARI ){ 
47
  this.doc.setProperty("SelectionLanguage", "XPath");
48
  Sarissa.setXpathNamespaces(this.doc, this.namespace);}
49

    
50
/**
51
 * Set the serializeUrl and proxyUrl values from a global configuration document
52
 * Optional, these can also be set in individual config docs.
53
 */
54
  var configDoc = Sarissa.getDomDocument();
55
  configDoc.async = false;
56
  configDoc.validateOnParse=false;  //IE6 SP2 parsing bug
57
  if(_SARISSA_IS_SAFARI){
58
    var xmlhttp = new XMLHttpRequest();
59
    xmlhttp.open("GET", baseDir+"/"+mbServerConfig, false);
60
    xmlhttp.send(null);
61
    configDoc = xmlhttp.responseXML;
62
  }else
63
   {
64
    configDoc.load(baseDir+"/"+mbServerConfig);
65
   }
66
  if (Sarissa.getParseErrorText(configDoc) != Sarissa.PARSED_OK) {
67
    //alert("error loading server config document: " + baseDir+"/"+mbServerConfig );
68
  } else {
69
  if(! _SARISSA_IS_SAFARI ){
70
    configDoc.setProperty("SelectionLanguage", "XPath");
71
    Sarissa.setXpathNamespaces(configDoc, this.namespace);
72
    }
73
    this.proxyUrl = Mapbuilder.getProperty(configDoc, "/mb:MapbuilderConfig/mb:proxyUrl", this.proxyUrl);
74
    this.serializeUrl = Mapbuilder.getProperty(configDoc, "/mb:MapbuilderConfig/mb:serializeUrl", this.serializeUrl);
75
  }
76
  configDoc = null;
77

    
78
  /**
79
   * Dynamic loading of the javascript files for objects defined in the Config file.
80
   * @private
81
   */
82
  this.loadConfigScripts=function(){
83
    // load css stylesheets that were requested before skindir was known.
84
    if (mapbuilder.cssToLoad) {
85
      for (var i=0; i<mapbuilder.cssToLoad.length; i++) {
86
        loadCss(mapbuilder.cssToLoad[i]);
87
      }
88
      mapbuilder.cssToLoad = null;
89
    }
90
    
91
    // Load script files for all components that don't have <scriptfile> specified
92
    // in the config file.
93
    mapbuilder.loadScriptsFromXpath(this.doc.selectNodes("//mb:models/*"),"model/");
94
    mapbuilder.loadScriptsFromXpath(this.doc.selectNodes("//mb:widgets/*"),"widget/");
95
    mapbuilder.loadScriptsFromXpath(this.doc.selectNodes("//mb:tools/*"),"tool/");
96

    
97
    //TBD: Deprecate the following block and move into loadScriptsFromXpath instead.
98
    //load all scriptfiles called for in the config file.  There seems to be a 
99
    //problem if this is done anywhere except in the page <HEAD> element.
100
    var scriptFileNodes = this.doc.selectNodes("//mb:scriptFile");
101
    for (var i=0; i<scriptFileNodes.length; i++ ) {
102
      scriptFile = getNodeValue(scriptFileNodes[i]);
103
      mapbuilder.loadScript(scriptFile);
104
    }
105
  }
106

    
107
  /**
108
  * multilingual support; defaults to english 
109
  * Set via a "language" parameter in the URL, 
110
  * or by setting a global "language" Javascript variable in the page <HEAD>.
111
  * Retrieve the language value from the global conifg object as "config.lang"
112
  */
113
  this.defaultLang = "en";
114
  this.lang = this.defaultLang;
115
  if (window.cgiArgs["language"]) {
116
    this.lang = window.cgiArgs["language"];
117
  } else if (window.language) {
118
    this.lang = window.language;
119
  }
120

    
121
  //set some global application properties
122
  var modelNode = this.doc.documentElement;
123
  this.skinDir = Mapbuilder.getProperty(modelNode, "mb:skinDir");
124
  this.proxyUrl = Mapbuilder.getProperty(modelNode, "mb:proxyUrl", this.proxyUrl);
125
  this.serializeUrl = Mapbuilder.getProperty(modelNode, "mb:serializeUrl", this.serializeUrl);
126

    
127
  /**
128
   * Convenience method to load widgetText from a dir.
129
   * Has the possible side-effect of changing config.lang to config.defaultLang
130
   * if the widgetText for the selected language is not found.
131
   * @param config the config object
132
   * @param dir    the base dir for the widget text
133
   * @private
134
   */
135
  function loadWidgetText(config, dir) {
136
    var widgetFile = "/widgetText.xml";
137
    var widgetText;
138
    var widgetTextUrl = dir + "/" + config.lang + widgetFile;
139
    widgetText = Sarissa.getDomDocument();
140
    widgetText.async = false;
141
    widgetText.validateOnParse=false;  //IE6 SP2 parsing bug
142
    
143
    if (typeof(inlineXSL)!="undefined") {
144
      var xmlString = inlineXSL[widgetTextUrl];
145
      xmlString = xmlString.replace(/DOUBLE_QUOTE/g,"\"");
146
      widgetText = (new DOMParser()).parseFromString(xmlString, "text/xml");
147
    }
148
    else {
149
      if (_SARISSA_IS_SAFARI) {
150
        var xmlhttp = new XMLHttpRequest();
151
        xmlhttp.open("GET", widgetTextUrl, false);
152
        xmlhttp.send(null);
153
        widgetText = xmlhttp.responseXML;
154
      }
155
      else {
156
        try {
157
          widgetText.load(widgetTextUrl);
158
        }
159
        catch (ignoredErr) {}
160
      }
161
    }      
162
    if (Sarissa.getParseErrorText(widgetText) != Sarissa.PARSED_OK) {
163
      var errMsg = "Error loading widgetText document: " + widgetTextUrl;
164
      if (config.lang == config.defaultLang) {
165
        alert(errMsg);
166
      }
167
      else {
168
        // Try to fall back on default language
169
        alert(errMsg + "\nFalling back on default language=\"" + config.defaultLang + "\"");
170
        config.lang = config.defaultLang;
171
        widgetTextUrl = dir + "/" + config.lang + widgetFile;
172
        if(_SARISSA_IS_SAFARI) {
173
          var xmlhttp = new XMLHttpRequest();
174
          xmlhttp.open("GET", widgetTextUrl, false);
175
          xmlhttp.send(null);
176
          widgetText = xmlhttp.responseXML;
177
        }
178
        else {
179
          widgetText.load(widgetTextUrl);
180
        }
181
        if (Sarissa.getParseErrorText(widgetText) != Sarissa.PARSED_OK) {
182
          alert("Falling back on default language failed!");
183
        }
184
      }
185
    }
186
    if(! _SARISSA_IS_SAFARI) {
187
      widgetText.setProperty("SelectionLanguage", "XPath");
188
      Sarissa.setXpathNamespaces(widgetText, config.namespace);
189
    }
190
    return widgetText;
191
  }
192
  
193
  // Load widgetText
194
  this.widgetText = loadWidgetText(this, baseDir + "/text");
195
  // Try to load userWidgetText
196
  userWidgetTextDir = modelNode.selectSingleNode("mb:userWidgetTextDir");
197
  if (userWidgetTextDir) {
198
    var userWidgetText = loadWidgetText(this, getNodeValue(userWidgetTextDir));
199
    if (userWidgetText) {
200
      // User has specified userWidgetText, merge with widgetText
201
      var userWidgets = userWidgetText.selectSingleNode("/mb:WidgetText/mb:widgets");
202
      var configWidgets = this.widgetText.selectSingleNode("/mb:WidgetText/mb:widgets");
203
      if (userWidgets && configWidgets) {
204
        // Merge <widgets/> texts
205
        Sarissa.copyChildNodes(userWidgets, configWidgets, true);
206
      }
207
      var userMessages = userWidgetText.selectSingleNode("/mb:WidgetText/mb:messages");
208
      var configMessages = this.widgetText.selectSingleNode("/mb:WidgetText/mb:messages");
209
      if (userMessages && configMessages) {
210
        // Merge <messages/> texts
211
        Sarissa.copyChildNodes(userMessages, configMessages, true);
212
      }
213
    }
214
  }
215

    
216
  /**
217
  * the objects property holds a reference to every mapbuilder javascript object
218
  * created.  Each object is added as a property of config.objects using the
219
  * value of the object id from the configuration file
220
  */
221
  this.objects = new Object();
222

    
223
  // Inherit the ModelBase functions and parameters
224
  ModelBase.apply(this, new Array(modelNode));
225

    
226
  /**
227
   * Load a model and its child models, widgets and tools.
228
   * This function can be called at any time to load a new model or replace an
229
   * existing model object.
230
   * @param modelId   the id of the model in config XML to be updated
231
   * @param modelUrl  URL of the XML model document to be loaded
232
   */
233
  this.loadModel = function( modelId, modelUrl ) {
234
    var model = this.objects[modelId];
235
    if (model && modelUrl) {
236
      var httpPayload = new Object();
237
      httpPayload.method = model.method;
238
      httpPayload.url = modelUrl;
239
      model.newRequest(model,httpPayload);
240
    } else {
241
      alert(mbGetMessage("errorLoadingModel", modelId, modelUrl));
242
    }
243
  }
244

    
245
  /**
246
   * Repaint the widget passed in.  
247
   * This function can be called at any time to paint the widget.
248
   * @param widget   a pointer to the widget object to be painted.
249
   */
250
  this.paintWidget = function( widget ) {
251
    if (widget) {
252
      widget.paint(widget, widget.id);
253
    } else {
254
      alert(mbGetMessage("errorPaintingWidget"));
255
    }
256
  }
257
}
258

    
259
/**
260
* Initialise the global config object for Mozilla browsers.
261
*/
262
if (document.readyState==null){
263
  // Mozilla
264
  mapbuilder.setLoadState(MB_LOAD_CONFIG);
265
  config=new Config(mbConfigUrl);
266
  config[config.id] = config;
267
  config.loadConfigScripts();
268
}
(1-1/19)