Revision 4307
Added by ben leinfelder over 15 years ago
ModelBase.js | ||
---|---|---|
1 | 1 |
/* |
2 | 2 |
License: LGPL as per: http://www.gnu.org/copyleft/lesser.html |
3 |
$Id$ |
|
3 |
$Id: ModelBase.js 3953 2008-03-31 10:25:24Z oterral $
|
|
4 | 4 |
*/ |
5 | 5 |
|
6 |
// Ensure this object's dependancies are loaded. |
|
7 |
mapbuilder.loadScript(baseDir+"/util/Listener.js"); |
|
8 |
|
|
6 | 9 |
/** |
7 | 10 |
* Base Model class to be inherited by all Model objects and provdes methods |
8 | 11 |
* and properties common to all models. |
... | ... | |
32 | 35 |
this.id = "MbModel_" + mbIds.getId(); |
33 | 36 |
} |
34 | 37 |
|
35 |
//get the human readable title for the model |
|
36 |
var titleNode = modelNode.selectSingleNode("mb:title"); |
|
37 |
if (titleNode) { |
|
38 |
this.title = titleNode.firstChild.nodeValue; |
|
39 |
} else { |
|
40 |
this.title = this.id; |
|
38 |
/** |
|
39 |
* Convenient access to Mapbuilder.getProperty |
|
40 |
* @param property property to get |
|
41 |
* @param default value to use if property is not set |
|
42 |
* @return the value for the property |
|
43 |
*/ |
|
44 |
this.getProperty = function(property, defaultValue) { |
|
45 |
return Mapbuilder.getProperty(modelNode, property, defaultValue); |
|
41 | 46 |
} |
42 | 47 |
|
43 |
// set an empty debug property which turns of alert messages for a |
|
44 |
// particular model |
|
45 |
if(modelNode.selectSingleNode("mb:debug"))this.debug="true"; |
|
48 |
//get the human readable title for the model |
|
49 |
this.title = this.getProperty("mb:title", this.id); |
|
46 | 50 |
|
51 |
//set a debug property in config to see alerts for a particular model |
|
52 |
this.debug = Mapbuilder.parseBoolean(this.getProperty("mb:debug", false)); |
|
53 |
|
|
47 | 54 |
/** |
48 | 55 |
* set the initial model URL in config. |
49 | 56 |
* the URL can also be passed in as a URL parameter by using the model ID |
... | ... | |
51 | 58 |
**/ |
52 | 59 |
if (window.cgiArgs[this.id]) { |
53 | 60 |
this.url = window.cgiArgs[this.id]; |
54 |
} else if (window[this.id]) { |
|
61 |
} else if (window[this.id] && typeof window[this.id] == "string") {
|
|
55 | 62 |
this.url = window[this.id]; |
56 | 63 |
} else if (modelNode.url) { |
57 | 64 |
this.url = modelNode.url; |
58 | 65 |
} else { |
59 | 66 |
var defaultModel = modelNode.selectSingleNode("mb:defaultModelUrl"); |
60 |
if (defaultModel) this.url = defaultModel.firstChild.nodeValue;
|
|
67 |
if (defaultModel) this.url = getNodeValue(defaultModel);
|
|
61 | 68 |
} |
62 | 69 |
|
63 | 70 |
//set the method property |
64 |
var method = modelNode.selectSingleNode("mb:method"); |
|
65 |
if (method) { |
|
66 |
this.method = method.firstChild.nodeValue; |
|
67 |
} else { |
|
68 |
this.method = "get"; |
|
69 |
} |
|
71 |
this.method = this.getProperty("mb:method", "get"); |
|
70 | 72 |
|
71 | 73 |
//set the namespace property |
72 |
var namespace = modelNode.selectSingleNode("mb:namespace"); |
|
73 |
if (namespace) { |
|
74 |
this.namespace = namespace.firstChild.nodeValue; |
|
75 |
} |
|
74 |
this.namespace = this.getProperty("mb:namespace"); |
|
76 | 75 |
|
77 | 76 |
var templateAttr = modelNode.attributes.getNamedItem("template"); |
78 | 77 |
if (templateAttr) { |
79 |
this.template = (templateAttr.nodeValue=="true")?true:false;
|
|
78 |
this.template = Mapbuilder.parseBoolean(templateAttr.nodeValue);
|
|
80 | 79 |
this.modelNode.removeAttribute("template"); |
81 | 80 |
} |
82 | 81 |
|
83 | 82 |
//get the xpath to select nodes from the parent doc |
84 |
var nodeSelectXpath = modelNode.selectSingleNode("mb:nodeSelectXpath"); |
|
85 |
if (nodeSelectXpath) { |
|
86 |
this.nodeSelectXpath = nodeSelectXpath.firstChild.nodeValue; |
|
87 |
} |
|
88 |
|
|
83 |
this.nodeSelectXpath = this.getProperty("mb:nodeSelectXpath"); |
|
84 |
|
|
89 | 85 |
/** |
86 |
* Widgets can place configurations in a model. This is an associative |
|
87 |
* array with the widgetId of the widget that places its configuration |
|
88 |
* here as key. |
|
89 |
*/ |
|
90 |
this.config = new Array(); |
|
91 |
|
|
92 |
/** |
|
90 | 93 |
* Get the value of a node as selected by an XPath expression.1 |
91 | 94 |
* @param objRef Reference to this node. |
92 | 95 |
* @param xpath XPath of the node to update. |
... | ... | |
96 | 99 |
if (!objRef.doc) return null; |
97 | 100 |
node=objRef.doc.selectSingleNode(xpath); |
98 | 101 |
if(node && node.firstChild){ |
99 |
return node.firstChild.nodeValue;
|
|
102 |
return getNodeValue(node);
|
|
100 | 103 |
}else{ |
101 | 104 |
return null; |
102 | 105 |
} |
... | ... | |
157 | 160 |
var xmlHttp = new XMLHttpRequest(); |
158 | 161 |
|
159 | 162 |
var sUri = objRef.url; |
160 |
if ( sUri.indexOf("http://")==0 ) { |
|
161 |
if (objRef.method == "get") { |
|
163 |
if ( sUri.indexOf("http://")==0 || sUri.indexOf("https://")==0) {
|
|
164 |
if (objRef.method.toLowerCase() == "get") {
|
|
162 | 165 |
sUri = getProxyPlusUrl(sUri); |
163 | 166 |
} else { |
164 | 167 |
sUri = config.proxyUrl; |
165 | 168 |
} |
166 | 169 |
} |
170 |
//alert( "ModelBase:"+objRef.method + " to:"+ sUri+ " " + objRef.url) |
|
171 |
|
|
167 | 172 |
xmlHttp.open(objRef.method, sUri, objRef.async); |
168 |
if (objRef.method == "post") { |
|
173 |
if (objRef.method.toLowerCase() == "post") {
|
|
169 | 174 |
xmlHttp.setRequestHeader("content-type",objRef.contentType); |
170 | 175 |
xmlHttp.setRequestHeader("serverUrl",objRef.url); |
171 | 176 |
} |
172 | 177 |
|
173 |
xmlHttp.onreadystatechange = function() { |
|
178 |
xmlHttp.onreadystatechange = function() {
|
|
174 | 179 |
objRef.setParam("modelStatus",httpStatusMsg[xmlHttp.readyState]); |
175 | 180 |
if (xmlHttp.readyState==4) { |
176 | 181 |
if (xmlHttp.status >= 400) { //http errors status start at 400 |
177 |
var errorMsg = "error loading document: " + sUri + " - " + xmlHttp.statusText + "-" + xmlHttp.responseText;
|
|
182 |
var errorMsg = mbGetMessage("errorLoadingDocument", sUri, xmlHttp.statusText, xmlHttp.responseText);
|
|
178 | 183 |
alert(errorMsg); |
179 | 184 |
objRef.setParam("modelStatus",errorMsg); |
180 | 185 |
return; |
181 | 186 |
} else { |
182 | 187 |
//alert(xmlHttp.getResponseHeader("Content-Type")); |
183 |
if ( null==xmlHttp.responseXML ) { |
|
184 |
alert( "null XML response:" + xmlHttp.responseText ); |
|
185 |
} else { |
|
186 |
objRef.doc = xmlHttp.responseXML; |
|
188 |
//if ( null==xmlHttp.responseXML ) { |
|
189 |
// alert( "null XML response:" + xmlHttp.responseText ); |
|
190 |
//} else { |
|
191 |
// Problem with IE is that sometimes the XML files do not get loaded as XML for some reason (especially from disk) |
|
192 |
// So we need to deal with it here |
|
193 |
|
|
194 |
if( (xmlHttp.responseXML != null) && (xmlHttp.responseXML.root != null) && (xmlHttp.responseXML.root.children.length>0) ) { |
|
195 |
objRef.doc = xmlHttp.responseXML; |
|
196 |
if( Sarissa.getParseErrorText(objRef.doc) == Sarissa.PARSED_OK ) { |
|
197 |
objRef.finishLoading(); |
|
198 |
} else { |
|
199 |
alert(mbGetMessage("parseError", Sarissa.getParseErrorText(objRef.doc))); |
|
200 |
} |
|
201 |
return; |
|
202 |
} |
|
203 |
|
|
204 |
if( xmlHttp.responseText != null ) { |
|
205 |
// if that's the case, the xml file is in the responseText |
|
206 |
// we have to load it manually |
|
207 |
objRef.doc = Sarissa.getDomDocument(); |
|
208 |
objRef.doc.async = false; |
|
209 |
objRef.doc = (new DOMParser()).parseFromString( xmlHttp.responseText.replace(/>\s+</g, "><"), "text/xml") |
|
210 |
if( objRef.doc == null ) { |
|
211 |
alert(mbGetMessage("documentParseError", Sarissa.getParseErrorText(objRef.doc))); |
|
212 |
// debugger; |
|
213 |
} else { |
|
214 |
if( Sarissa.getParseErrorText(objRef.doc) == Sarissa.PARSED_OK ) { |
|
215 |
objRef.finishLoading(); |
|
216 |
} else { |
|
217 |
alert(mbGetMessage("parseError", Sarissa.getParseErrorText(objRef.doc))); |
|
218 |
} |
|
219 |
} |
|
220 |
return; |
|
221 |
} |
|
187 | 222 |
//if (objRef.doc.documentElement.nodeName.search(/exception/i)>=0) { |
188 | 223 |
// objRef.setParam("modelStatus",-1); |
189 |
// alert("Exception:"+Sarissa.serialize(xmlHttp.responseText));
|
|
224 |
// alert("Exception:"+(new XMLSerializer()).serializeToString(xmlHttp.responseText));
|
|
190 | 225 |
//} |
191 |
} |
|
192 |
objRef.finishLoading(); |
|
226 |
//}
|
|
227 |
//objRef.finishLoading();
|
|
193 | 228 |
} |
194 | 229 |
} |
195 | 230 |
} |
231 |
|
|
232 |
var postData = objRef.postData || ""; |
|
233 |
if (typeof postData == "object") { |
|
234 |
postData = new XMLSerializer().serializeToString(postData); |
|
235 |
} |
|
236 |
xmlHttp.send(postData); |
|
196 | 237 |
|
197 |
xmlHttp.send(objRef.postData); |
|
198 |
|
|
199 | 238 |
if (!objRef.async) { |
200 | 239 |
if (xmlHttp.status >= 400) { //http errors status start at 400 |
201 |
var errorMsg = "error loading document: " + sUri + " - " + xmlHttp.statusText + "-" + xmlHttp.responseText;
|
|
240 |
var errorMsg = mbGetMessage("errorLoadingDocument", sUri, xmlHttp.statusText, xmlHttp.responseText);
|
|
202 | 241 |
alert(errorMsg); |
203 | 242 |
this.objRef.setParam("modelStatus",errorMsg); |
204 | 243 |
return; |
205 | 244 |
} else { |
206 | 245 |
//alert(xmlHttp.getResponseHeader("Content-Type")); |
207 |
if ( null==xmlHttp.responseXML ) alert( "null XML response:" + xmlHttp.responseText );
|
|
246 |
if ( null==xmlHttp.responseXML ) alert(mbGetMessage("nullXmlResponse", xmlHttp.responseText));
|
|
208 | 247 |
objRef.doc = xmlHttp.responseXML; |
209 | 248 |
objRef.finishLoading(); |
210 | 249 |
} |
... | ... | |
214 | 253 |
} |
215 | 254 |
} |
216 | 255 |
} |
256 |
this.addListener("reloadModel",this.loadModelDoc, this); |
|
217 | 257 |
|
218 | 258 |
/** |
219 | 259 |
* Set the model's XML document using an XML object as a parameter. |
... | ... | |
237 | 277 |
this.finishLoading = function() { |
238 | 278 |
// the following two lines are needed for IE; set the namespace for selection |
239 | 279 |
if(this.doc){ |
280 |
if(! _SARISSA_IS_SAFARI){ |
|
240 | 281 |
this.doc.setProperty("SelectionLanguage", "XPath"); |
241 | 282 |
if(this.namespace) Sarissa.setXpathNamespaces(this.doc, this.namespace); |
283 |
} |
|
242 | 284 |
|
243 | 285 |
// Show the newly loaded XML document |
244 |
if(this.debug) alert("Loading Model:"+this.id+" "+Sarissa.serialize(this.doc));
|
|
245 |
this.callListeners("contextLoaded"); //PGC |
|
286 |
if(this.debug) mbDebugMessage(this, "Loading Model:"+this.id+" "+(new XMLSerializer()).serializeToString(this.doc));
|
|
287 |
|
|
246 | 288 |
this.callListeners("loadModel"); |
247 | 289 |
} |
248 | 290 |
} |
... | ... | |
265 | 307 |
// assign it an id |
266 | 308 |
if (objRef.template) { |
267 | 309 |
var parentNode = objRef.modelNode.parentNode; |
268 |
var newConfigNode; |
|
269 | 310 |
if(_SARISSA_IS_IE) { |
270 |
newConfigNode = parentNode.appendChild(modelNode.cloneNode(true)); |
|
271 |
} else { |
|
272 |
newConfigNode = parentNode.appendChild(objRef.modelNode.ownerDocument.importNode(objRef.modelNode,true)); |
|
311 |
var newConfigNode = parentNode.appendChild(modelNode.cloneNode(true)); |
|
273 | 312 |
} |
313 |
else { |
|
314 |
var newConfigNode = parentNode.appendChild(objRef.modelNode.ownerDocument.importNode(objRef.modelNode,true)); |
|
315 |
} |
|
274 | 316 |
newConfigNode.removeAttribute("id"); //this will get created automatically |
317 |
newConfigNode.setAttribute("createByTemplate","true"); |
|
275 | 318 |
//set defaultModelUrl config properties |
276 | 319 |
model = objRef.createObject(newConfigNode); |
277 | 320 |
model.callListeners("init"); |
... | ... | |
292 | 335 |
*/ |
293 | 336 |
this.deleteTemplates = function() { |
294 | 337 |
if (this.templates) { |
338 |
var model; |
|
295 | 339 |
while( model=this.templates.pop() ) { |
296 | 340 |
model.setParam("newModel"); |
297 | 341 |
var parentNode = this.modelNode.parentNode; |
... | ... | |
299 | 343 |
} |
300 | 344 |
} |
301 | 345 |
} |
302 |
|
|
303 |
/** |
|
346 |
/** |
|
304 | 347 |
* save the model by posting it to the serializeUrl, which is defined as a |
305 | 348 |
* property of config. |
306 | 349 |
* @param objRef Pointer to this object. |
307 | 350 |
*/ |
308 | 351 |
this.saveModel = function(objRef) { |
309 | 352 |
if (config.serializeUrl) { |
310 |
var response = postLoad(config.serializeUrl, objRef.doc); |
|
353 |
var response = postGetLoad(config.serializeUrl, objRef.doc ,"text/xml","",""); |
|
354 |
if(! _SARISSA_IS_SAFARI){ |
|
311 | 355 |
response.setProperty("SelectionLanguage", "XPath"); |
312 | 356 |
Sarissa.setXpathNamespaces(response, "xmlns:xlink='http://www.w3.org/1999/xlink'"); |
357 |
} |
|
313 | 358 |
var onlineResource = response.selectSingleNode("//OnlineResource"); |
314 | 359 |
var fileUrl = onlineResource.attributes.getNamedItem("xlink:href").nodeValue; |
315 | 360 |
objRef.setParam("modelSaved", fileUrl); |
316 | 361 |
} else { |
317 |
alert("serializeUrl must be specified in config to save a model");
|
|
362 |
alert(mbGetMessage("noSerializeUrl"));
|
|
318 | 363 |
} |
319 | 364 |
} |
320 | 365 |
|
... | ... | |
328 | 373 |
*/ |
329 | 374 |
this.createObject = function(configNode) { |
330 | 375 |
var objectType = configNode.nodeName; |
331 |
var evalStr = "new " + objectType + "(configNode,this);"; |
|
332 |
var newObject = eval( evalStr ); |
|
333 |
if ( newObject ) { |
|
376 |
//var evalStr = "new " + objectType + "(configNode,this);"; |
|
377 |
//var newObject = eval( evalStr ); |
|
378 |
//hint from Alex Russel alex@dojotoolkit.org so we can compress it |
|
379 |
|
|
380 |
// If model/tool/widget doesn't exist, exit |
|
381 |
if (!window[objectType]) { |
|
382 |
alert(mbGetMessage("errorCreatingObject", objectType)); |
|
383 |
return false; |
|
384 |
} |
|
385 |
|
|
386 |
var newObject = new window[objectType](configNode, this); |
|
387 |
if (newObject) { |
|
334 | 388 |
config.objects[newObject.id] = newObject; |
335 | 389 |
return newObject; |
336 |
} else {
|
|
337 |
alert("error creating object:" + objectType);
|
|
390 |
} else { |
|
391 |
alert(mbGetMessage("errorCreatingObject", objectType));
|
|
338 | 392 |
} |
339 | 393 |
} |
340 | 394 |
|
... | ... | |
347 | 401 |
//loop through all nodes selected from config |
348 | 402 |
var configObjects = this.modelNode.selectNodes( objectXpath ); |
349 | 403 |
for (var i=0; i<configObjects.length; i++ ) { |
404 |
if(configObjects[i].nodeName != "#text" && configObjects[i].nodeName != "#comment" ) |
|
405 |
{ |
|
350 | 406 |
this.createObject( configObjects[i]); |
407 |
} |
|
351 | 408 |
} |
352 | 409 |
} |
353 | 410 |
|
... | ... | |
368 | 425 |
* @param objRef Pointer to this object. |
369 | 426 |
*/ |
370 | 427 |
this.refresh = function(objRef) { |
371 |
objRef.setParam("refresh",true);
|
|
428 |
objRef.setParam("refresh"); |
|
372 | 429 |
} |
373 | 430 |
this.addListener("loadModel",this.refresh, this); |
374 | 431 |
|
Also available in: Unified diff
upgrade to MapBuilder 1.5rc2 - includes support for Firefox 3 compatibility (yes, it is also EOLed)