Project

General

Profile

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

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

    
37
/**
38
 * Set the serializeUrl and proxyUrl values from a global configuration document
39
 * Optional, these can also be set in individual config docs.
40
 */
41
  var configDoc = Sarissa.getDomDocument();
42
  configDoc.async = false;
43
  configDoc.validateOnParse=false;  //IE6 SP2 parsing bug
44
  configDoc.load(baseDir+"/"+mbServerConfig);
45
  if (configDoc.parseError < 0) {
46
    //alert("error loading server config document: " + baseDir+"/"+mbServerConfig );
47
  } else {
48
    configDoc.setProperty("SelectionLanguage", "XPath");
49
    Sarissa.setXpathNamespaces(configDoc, this.namespace);
50
    var node = configDoc.selectSingleNode("/mb:MapbuilderConfig/mb:proxyUrl");
51
    if (node) this.proxyUrl = node.firstChild.nodeValue;
52
    node = configDoc.selectSingleNode("/mb:MapbuilderConfig/mb:serializeUrl");
53
    if (node) this.serializeUrl = node.firstChild.nodeValue;
54
  }
55
  configDoc = null;
56

    
57
  /**
58
   * Dynamic loading of the javascript files for objects defined in the Config file.
59
   * @private
60
   */
61
  this.loadConfigScripts=function(){
62
    // Load script files for all components that don't have <scriptfile> specified
63
    // in the config file.
64
    mapbuilder.loadScriptsFromXpath(this.doc.selectNodes("//mb:models/*"),"model/");
65
    mapbuilder.loadScriptsFromXpath(this.doc.selectNodes("//mb:widgets/*"),"widget/");
66
    mapbuilder.loadScriptsFromXpath(this.doc.selectNodes("//mb:tools/*"),"tool/");
67

    
68
    //TBD: Deprecate the following block and move into loadScriptsFromXpath instead.
69
    //load all scriptfiles called for in the config file.  There seems to be a 
70
    //problem if this is done anywhere except in the page <HEAD> element.
71
    var scriptFileNodes = this.doc.selectNodes("//mb:scriptFile");
72
    for (var i=0; i<scriptFileNodes.length; i++ ) {
73
      scriptFile = scriptFileNodes[i].firstChild.nodeValue;
74
      mapbuilder.loadScript(scriptFile);
75
    }
76
  }
77

    
78
  /**
79
  * multilingual support; defaults to english 
80
  * Set via a "language" parameter in the URL, 
81
  * or by setting a global "language" Javascript variable in the page <HEAD>.
82
  * Retrieve the language value from the global conifg object as "config.lang"
83
  */
84
  this.lang = "en";
85
  if (window.cgiArgs["language"]) {
86
    this.lang = window.cgiArgs["language"];
87
  } else if (window.language) {
88
    this.lang = window.language;
89
  }
90

    
91
  //set some global application properties
92
  var modelNode = this.doc.documentElement;
93
  this.skinDir = modelNode.selectSingleNode("mb:skinDir").firstChild.nodeValue;
94
  var proxyUrl = modelNode.selectSingleNode("mb:proxyUrl");
95
  if (proxyUrl) this.proxyUrl = proxyUrl.firstChild.nodeValue;
96
  var serializeUrl = modelNode.selectSingleNode("mb:serializeUrl");
97
  if (serializeUrl) this.serializeUrl = serializeUrl.firstChild.nodeValue;
98

    
99
  var widgetText = modelNode.selectSingleNode("mb:widgetTextUrl");
100
  if (widgetText) {
101
    var widgetTextUrl = this.skinDir + "/" + this.lang + "/" + widgetText.firstChild.nodeValue;
102
    this.widgetText = Sarissa.getDomDocument();
103
    this.widgetText.async = false;
104
    this.widgetText.validateOnParse=false;  //IE6 SP2 parsing bug
105
    this.widgetText.load(widgetTextUrl);
106
    if (this.widgetText.parseError < 0){
107
      alert("error loading widgetText document: " + widgetTextUrl );//+ " - " + Sarissa.getParseErrorText(this.doc) );
108
    }
109
    this.widgetText.setProperty("SelectionLanguage", "XPath");
110
    Sarissa.setXpathNamespaces(this.widgetText, this.namespace);
111
  }
112

    
113
  /**
114
  * the objects property holds a reference to every mapbuilder javascript object
115
  * created.  Each object is added as a property of config.objects using the
116
  * value of the object id from the configuration file
117
  */
118
  this.objects = new Object();
119

    
120
  // Inherit the ModelBase functions and parameters
121
  ModelBase.apply(this, new Array(modelNode));
122

    
123
  /**
124
   * Load a model and its child models, widgets and tools.
125
   * This function can be called at any time to load a new model or replace an
126
   * existing model object.
127
   * @param modelId   the id of the model in config XML to be updated
128
   * @param modelUrl  URL of the XML model document to be loaded
129
   */
130
  this.loadModel = function( modelId, modelUrl ) {
131
    var model = this.objects[modelId];
132
    if (model && modelUrl) {
133
      var httpPayload = new Object();
134
      httpPayload.method = model.method;
135
      httpPayload.url = modelUrl;
136
      model.newRequest(model,httpPayload);
137
    } else {
138
      alert("config loadModel error:"+modelId+":"+modelUrl);
139
    }
140
  }
141

    
142
  /**
143
   * Repaint the widget passed in.  
144
   * This function can be called at any time to paint the widget.
145
   * @param widget   a pointer to the widget object to be painted.
146
   */
147
  this.paintWidget = function( widget ) {
148
    if (widget) {
149
      widget.paint(widget, true);
150
    } else {
151
      alert("config paintWidget error: widget does not exist");
152
    }
153
  }
154
}
155

    
156
/**
157
* Initialise the global config object for Mozilla browsers.
158
*/
159
if (document.readyState==null){
160
  // Mozilla
161
  mapbuilder.setLoadState(MB_LOAD_CONFIG);
162
  config=new Config(mbConfigUrl);
163
  config[config.id] = config;
164
  config.loadConfigScripts();
165
}
(1-1/18)