Project

General

Profile

« Previous | Next » 

Revision 4307

upgrade to MapBuilder 1.5rc2 - includes support for Firefox 3 compatibility (yes, it is also EOLed)

View differences:

Context.js
1 1
/*
2 2
License: LGPL as per: http://www.gnu.org/copyleft/lesser.html
3
$Id$
3
$Id: Context.js 3887 2008-02-27 18:18:53Z ahocevar $
4 4
*/
5 5

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

  
6 9
/**
7
 * Stores a Web Map Context (WMC) document as defined by the Open GIS Consortium
8
 * http://opengis.org and extensions the the WMC.  
10
 * Stores a Web Map Context (WMC) document as defined by the Open Geospatial Consortium
11
 * (http://www.opengeospatial.org/) and extensions the the WMC.
9 12
 *
10 13
 * Listeners supported by this model:
11 14
 * "refresh" called when window parameters (width/height, bbox) are changed
......
16 19
 * @author Cameron Shorter
17 20
 * @param modelNode Pointer to the xml node for this model from the config file.
18 21
 * @param parent    The parent model for the object.
19
  * 
22
  *
20 23
 */
21 24
function Context(modelNode, parent) {
22 25
  // Inherit the ModelBase functions and parameters
......
26 29

  
27 30
  /**
28 31
   * Change a Layer's visibility.
29
   * @param layerName  The name of the layer that is to be changed
32
   * @param layerId  The name of the layer that is to be changed
30 33
   * @param hidden     String with the value to be set; 1=hidden, 0=visible.
31 34
   */
32
  this.setHidden=function(layerName, hidden){
35
  this.setHidden=function(layerId, hidden){
33 36
    // Set the hidden attribute in the Context
34 37
    var hiddenValue = "0";
35 38
    if (hidden) hiddenValue = "1";
36
      
37
    var layer=this.doc.selectSingleNode("/wmc:ViewContext/wmc:LayerList/wmc:Layer[wmc:Name='"+layerName+"']");
39

  
40
    var layer = this.getLayer(layerId);
38 41
    if (layer) layer.setAttribute("hidden", hiddenValue);
39 42
    // Call the listeners
40
    this.callListeners("hidden", layerName);
43
    this.callListeners("hidden", layerId);
41 44
  }
42 45

  
43 46
  /**
44 47
   * Get the layer's visiblity attribute value.
45
   * @param layerName  The name of the layer that is to be changed
48
   * @param layerId  The name of the layer that is to be changed
46 49
   * @return hidden  String with the value; 1=hidden, 0=visible.
47 50
   */
48
  this.getHidden=function(layerName){
51
  this.getHidden=function(layerId){
49 52
    var hidden=1;
50
    var layer=this.doc.selectSingleNode("/wmc:ViewContext/wmc:LayerList/wmc:Layer[wmc:Name='"+layerName+"']");
53
    var layer = this.getLayer(layerId);
51 54
    if (layer) hidden = layer.getAttribute("hidden");
52 55
    return hidden;
53 56
  }
......
57 60
   * @return BoundingBox array with the sequence (xmin,ymin,xmax,ymax).
58 61
   */
59 62
  this.getBoundingBox=function() {
63
    var bbox = new Array();
64
    // Extract BoundingBox from the context
60 65
    var boundingBox=this.doc.selectSingleNode("/wmc:ViewContext/wmc:General/wmc:BoundingBox");
61
    bbox = new Array();
62 66
    bbox[0]=parseFloat(boundingBox.getAttribute("minx"));
63 67
    bbox[1]=parseFloat(boundingBox.getAttribute("miny"));
64 68
    bbox[2]=parseFloat(boundingBox.getAttribute("maxx"));
......
72 76
   */
73 77
  this.setBoundingBox=function(boundingBox) {
74 78
    // Set BoundingBox in context
75
    //bbox=this.doc.documentElement.getElementsByTagName("BoundingBox").item(0);
76 79
    var bbox=this.doc.selectSingleNode("/wmc:ViewContext/wmc:General/wmc:BoundingBox");
77 80
    bbox.setAttribute("minx", boundingBox[0]);
78 81
    bbox.setAttribute("miny", boundingBox[1]);
......
88 91
   */
89 92
  this.initBbox=function(objRef) {
90 93
    // Set BoundingBox in context from URL CGI params
91
    if (window.cgiArgs["bbox"]) {     //set as minx,miny,maxx,maxy
94
    if (window.cgiArgs["bbox"]) {   //set as minx,miny,maxx,maxy
92 95
      var bbox = window.cgiArgs["bbox"].split(',');
93
      var ul = new Array(parseFloat(bbox[0]),parseFloat(bbox[3]));
94
      var lr = new Array(parseFloat(bbox[2]),parseFloat(bbox[1]));
95
      objRef.extent.zoomToBox(ul, lr);
96
      objRef.setBoundingBox(bbox);
96 97
    }
97 98
  }
98
  //PGC this.addListener( "loadModel", this.initBbox, this );
99
  this.addListener( "contextLoaded", this.initBbox, this );
99
  this.addListener( "loadModel", this.initBbox, this );
100
  //this.addListener( "contextLoaded", this.initBbox, this );
100 101

  
101 102
  /**
102 103
   * Set the aoi param and call the refresh listeners
......
113 114
  //MA this.addListener( "contextLoaded", this.initAoi, this );
114 115

  
115 116
  /**
116
   * Set the Spacial Reference System for the context document.
117
   * Set the Spatial Reference System for the context document.
117 118
   * @param srs The Spatial Reference System.
118 119
   */
119 120
  this.setSRS=function(srs) {
......
123 124
  }
124 125

  
125 126
  /**
126
   * Get the Spacial Reference System from the context document.
127
   * Get the Spatial Reference System from the context document.
127 128
   * @return srs The Spatial Reference System.
128 129
   */
129 130
  this.getSRS=function() {
130 131
    var bbox=this.doc.selectSingleNode("/wmc:ViewContext/wmc:General/wmc:BoundingBox");
131 132
    srs=bbox.getAttribute("SRS");
133
    srs = srs ? srs : 'EPSG:4326';
132 134
    return srs;
133 135
  }
134 136

  
135 137
  /**
138
   * Get the Projection object from the context document.
139
   * @return Proj Object of  The Spatial Reference System.
140
   */
141
  this.initProj=function(objRef) {
142
    objRef.proj=new OpenLayers.Projection(objRef.getSRS());
143
  }
144
  this.addFirstListener( "loadModel", this.initProj, this );
145

  
146
  /**
136 147
   * Get the Window width.
137 148
   * @return width The width of map window from the context document
138 149
   */
......
143 154

  
144 155
  /**
145 156
   * Set the Window width.
146
   * @param width The width of map window (therefore of map layer images).
157
   * @param width The width of map window to set in the context document
147 158
   */
148 159
  this.setWindowWidth=function(width) {
149 160
    var win=this.doc.selectSingleNode("/wmc:ViewContext/wmc:General/wmc:Window");
......
171 182
  }
172 183

  
173 184
  /**
185
   * Returns the width/height of the map window as an array
186
   */
187
  this.getWindowSize=function() {
188
    var win=this.doc.selectSingleNode("/wmc:ViewContext/wmc:General/wmc:Window");
189
    return new Array(win.getAttribute("width"), win.getAttribute("height"));
190
  }
191

  
192
  /**
193
   * Set the Window width and height in one function call to avoid a resize event in between
194
   * setting width and height, because that causes checkBbox to be triggered, which adjusts the
195
   * bbox then when it should not yet be adjusted.
196
   * Added by VTS for dynamic map window resizing (AutoResize tool)
197
   * @param size Size of the map window as (width, height) array
198
   */
199
  this.setWindowSize=function(size) {
200
    var width = size[0];
201
    var height = size[1];
202
    var win=this.doc.selectSingleNode("/wmc:ViewContext/wmc:General/wmc:Window");
203
    win.setAttribute("width", width);
204
    win.setAttribute("height", height);
205
    this.callListeners("resize");
206
  }
207

  
208
  /**
209
   * Returns the Layer node with the specified name from the list of nodes
210
   * selected by the nodeSelectXpath from the capabilities doc.
211
   * @param featureName name of the featureType to look up
212
   * @return the Layer node with the specified name.
213
   */
214
  this.getFeatureNode = function(featureName) {
215
    return this.doc.selectSingleNode(this.nodeSelectXpath+"[wmc:Name='"+featureName+"']");
216
  }
217

  
218
  /**
174 219
   * Returns the serverUrl for the layer passed in as the feature argument.
175 220
   * @param requestName ignored for context docs (only GetMap supported)
176 221
   * @param method ignored for context docs (only GET supported)
177 222
   * @param feature the Layer node from the context doc
178
   * @return URL for the GetMap request 
223
   * @return URL for the GetMap request
179 224
   */
180 225
  this.getServerUrl = function(requestName, method, feature) {
181 226
    return feature.selectSingleNode("wmc:Server/wmc:OnlineResource").getAttribute("xlink:href");
......
186 231
   * @param feature the Layer node from the context doc
187 232
   * @return the WMS GetMap version for the Layer.
188 233
   */
189
  this.getVersion = function(feature) {  
234
  this.getVersion = function(feature) {
190 235
    return feature.selectSingleNode("wmc:Server").getAttribute("version");
191 236
  }
192 237

  
......
204 249
   * @return the list with queryable layers
205 250
   */
206 251
  this.getQueryableLayers = function() {
207
    var listNodeArray = this.doc.selectNodes("/wmc:ViewContext/wmc:LayerList/wmc:Layer[attribute::queryable='1']/wmc:Name");
252
    var listNodeArray = this.doc.selectNodes("/wmc:ViewContext/wmc:LayerList/wmc:Layer[attribute::queryable='1']");
208 253
    return listNodeArray;
209 254
  }
210 255

  
......
220 265

  
221 266
  /**
222 267
   * Method to get a layer with the specified name in the context doc
223
   * @param layerName the layer to be returned
268
   * @param layerId the id of the layer to be returned
224 269
   * @return the list with all layers
225 270
   */
226
  this.getLayer = function(layerName) {
227
    var layer = this.doc.selectSingleNode("/wmc:ViewContext/wmc:LayerList/wmc:Layer[wmc:Name='"+layerName+"']");
271
  this.getLayer = function(layerId) {
272
    var layer = this.doc.selectSingleNode("/wmc:ViewContext/wmc:LayerList/wmc:Layer[@id='"+layerId+"']");
273
    if (layer == null) {
274
      layer = this.doc.selectSingleNode("/wmc:ViewContext/wmc:LayerList/wmc:Layer[wmc:Name='"+layerId+"']");
275
    }
228 276
    //TBD: add in time stamp
229 277
    return layer;
230 278
  }
231 279

  
232 280
  /**
281
   * Method to get a layer id with the specified id/name in the context doc
282
   * @param layerName the name of the layer of which the id is to be returned
283
   * @return the id of the layer || false
284
   */
285
  this.getLayerIdByName = function(layerName) {
286
    var layer = this.getLayer(layerName);
287
    var id;
288
    if (layer) {
289
      id = layer.getAttribute("id");
290
    }
291

  
292
    return id || false;
293
  }
294

  
295
  /**
233 296
   * Method to add a Layer to the LayerList
234 297
   * @param layerNode the Layer node from another context doc or capabiltiies doc
235 298
   */
236 299
  this.addLayer = function(objRef, layerNode) {
300

  
237 301
    var parentNode = objRef.doc.selectSingleNode("/wmc:ViewContext/wmc:LayerList");
238
    parentNode.appendChild(layerNode.cloneNode(true));
302
    parentNode.appendChild(layerNode);
303

  
304
    // Generate layer id if layer doesn't have an id
305
    if (!layerNode.getAttribute("id")) {
306
      var randomNumber = Math.round(10000 * Math.random());
307
      id = Mapbuilder.getProperty(layerNode, "wmc:Name") + "_" + randomNumber; 
308
      layerNode.setAttribute("id", id);
309
    }
310

  
239 311
    objRef.modified = true;
240 312
    //objRef.callListeners("refresh");
241 313
  }
242 314
  this.addFirstListener( "addLayer", this.addLayer, this );
243 315

  
316
 /**
317
   * Method to add a Sld tag to the StyleList
318
   * @param layerName the Layer name from another context doc or capabiltiies doc
319
   */
320
  this.addSLD = function(objRef,sldNode) {
321
    // alert("context addSLD : "+objRef.id);
322
    var layerName=Mapbuilder.getProperty(sldNode, "//Name");
323
    var parentNode = objRef.doc.selectSingleNode("//wmc:Layer[wmc:Name='"+layerName+"']");
324
    parentNode.appendChild(sldNode.cloneNode(true));
325
    objRef.modified = true;
326
    var attribs=[];
327
    attribs["sld_body"]=(new XMLSerializer()).serializeToString(objRef.doc.selectSingleNode("//wmc:Layer[wmc:Name='"+layerName+"']/wmc:StyleList/wmc:Style/wmc:SLD/wmc:StyledLayerDescriptor"));
328
	objRef.map.mbMapPane.refreshLayer(objRef.map.mbMapPane,layerName,attribs);
329
	
330
  }
331
  this.addFirstListener( "addSLD", this.addSLD, this );
332

  
244 333
  /**
245 334
   * Method to remove a Layer from the LayerList
246
   * @param layerName the Layer to be deleted
335
   * @param layerId the Layer to be deleted
247 336
   */
248
  this.deleteLayer = function(objRef, layerName) {
249
    var deletedNode = objRef.getLayer(layerName);
337
  this.deleteLayer = function(objRef, layerId) {
338
    var deletedNode = objRef.getLayer(layerId);
250 339
    if (!deletedNode) {
251
      alert("node note found; unable to delete node:"+layerName);
340
      alert(mbGetMessage("nodeNotFound", layerId));
252 341
      return;
253 342
    }
254 343
    deletedNode.parentNode.removeChild(deletedNode);
......
258 347

  
259 348
  /**
260 349
   * Method to move a Layer in the LayerList up
261
   * @param layerName the layer to be moved
350
   * @param layerId the layer to be moved
262 351
   */
263
  this.moveLayerUp = function(objRef, layerName) {
264
    var movedNode = objRef.getLayer(layerName);
352
  this.moveLayerUp = function(objRef, layerId) {
353
    var movedNode = objRef.getLayer(layerId);
265 354
    var sibNode = movedNode.selectSingleNode("following-sibling::*");
266 355
    if (!sibNode) {
267
      alert("can't move node past beginning of list:"+layerName);
356
      alert(mbGetMessage("cantMoveUp", layerId));
268 357
      return;
269 358
    }
270 359
    movedNode.parentNode.insertBefore(sibNode,movedNode);
......
274 363

  
275 364
  /**
276 365
   * Method to move a Layer in the LayerList down
277
   * @param layerName the layer to be moved
366
   * @param layerId the layer to be moved
278 367
   */
279
  this.moveLayerDown = function(objRef, layerName) {
280
    var movedNode = objRef.getLayer(layerName);
368
  this.moveLayerDown = function(objRef, layerId) {
369
    var movedNode = objRef.getLayer(layerId);
281 370
    var listNodeArray = movedNode.selectNodes("preceding-sibling::*");  //preceding-sibling axis contains all previous siblings
282 371
    var sibNode = listNodeArray[listNodeArray.length-1];
283 372
    if (!sibNode) {
284
      alert("can't move node past beginning of list:"+layerName);
373
      alert(mbGetMessage("cantMoveDown", layerId));
285 374
      return;
286 375
    }
287 376
    movedNode.parentNode.insertBefore(movedNode,sibNode);
......
291 380

  
292 381
  /**
293 382
   * Adds a node to the Context document extension element.  The extension element
294
   * will be created if it doesn't already exist.  
383
   * will be created if it doesn't already exist.
295 384
   * @param extensionNode the node to be appended in the extension element.
296 385
   * @return the ndoe added to the extension element
297 386
   */
......
315 404

  
316 405
  /**
317 406
   * Parses a Dimension element from the Context document as a loadModel listener.
318
   * This results in an XML structure with one element for each GetMap time value 
407
   * This results in an XML structure with one element for each GetMap time value
319 408
   * parameter and added to the Context extrension element.
320
   * @param objRef a pointer to this object 
409
   * @param objRef a pointer to this object
321 410
   */
322 411
  this.initTimeExtent = function( objRef ) {
323 412
    //only the first one selected is used as the timestamp source
......
327 416
    for (var i=0; i<timeNodes.length; ++i) {
328 417
      var extentNode = timeNodes[i];
329 418
      objRef.timestampList = createElementWithNS(objRef.doc,"TimestampList",mbNsUrl);
330
      var layerName = extentNode.parentNode.parentNode.selectSingleNode("wmc:Name").firstChild.nodeValue;
331
      objRef.timestampList.setAttribute("layerName", layerName);
332
      //alert("found time dimension, extent:"+extentNode.firstChild.nodeValue);
333
      var times = extentNode.firstChild.nodeValue.split(",");   //comma separated list of arguments
419
      var layerId;
420
      var layerNode = extentNode.parentNode.parentNode;
421
      if (layerNode.selectSingleNode("@id")) {
422
        layerId = Mapbuilder.getProperty(layerNode, "@id");
423
      } else {
424
        layerId = Mapbuilder.getProperty(layerNode, "wmc:Name");
425
      }
426
      objRef.timestampList.setAttribute("layerId", layerId);
427
      //alert("found time dimension, extent:"+getNodeValue(extentNode));
428
      var times = getNodeValue(extentNode).split(",");   //comma separated list of arguments
334 429
      for (var j=0; j<times.length; ++j) {
335 430
        var params = times[j].split("/");     // parses start/end/period
336 431
        if (params.length==3) {
......
363 458
          objRef.timestampList.appendChild(timestamp);
364 459
        }
365 460
      }
366
     objRef.setExtension(objRef.timestampList);  
461
     objRef.setExtension(objRef.timestampList);
367 462
    }
368 463
  }
369 464
  this.addFirstListener( "loadModel", this.initTimeExtent, this );
465
  
466
  /**
467
   * clear the time extent created by initTimeExtent
468
   * @param objRef reference to this model
469
   */
470
  this.clearTimeExtent = function( objRef ) {
471
    var tsList = objRef.timestampList;
472
    if (tsList) {
473
      tsList.parentNode.removeChild(tsList);
474
    }
475
  }
476
  this.addListener("newModel", this.clearTimeExtent, this);
370 477

  
371 478
  /**
372 479
   * Returns the current timestamp value.
373
   * @param layerName the name of the Layer from which the timestamp list was generated
480
   * @param layerId the name of the Layer from which the timestamp list was generated
374 481
   * @return the current timestamp value.
375 482
   */
376
  this.getCurrentTimestamp = function( layerName ) {
483
  this.getCurrentTimestamp = function( layerId ) {
377 484
    var index = this.getParam("timestamp");
378
    return this.timestampList.childNodes[index].firstChild.nodeValue;
485
    return getNodeValue(this.timestampList.childNodes[index]);
379 486
  }
487

  
488
  // PL -BRGM
489
  /**
490
   * Change a Layer's opacity
491
   * @param layerId  The name of the layer that is to be changed
492
   * @param Opacity     Value of the opacity
493
   */
494
  this.setOpacity=function(layerId, Opacity){
495
    // Set the hidden attribute in the Context
496
    var layer = this.getLayer(layerId);
497
    if (layer) layer.setAttribute("opacity", Opacity);
498
    // Call the listeners
499
    this.callListeners("opacity", layerId);
500
  }
501

  
502
  /**
503
   * Get the layer's opacity attribute value.
504
   * @param layerId  The name of the layer that is to be changed
505
   * @return hidden  String with the value; 1=hidden, 0=visible.
506
   */
507
  this.getOpacity=function(layerId){
508
    var opacity=1;
509
    var layer = this.getLayer(layerId);
510
    if (layer) opacity = layer.getAttribute("opacity");
511
    return opacity;
512
  }
513
  // PL -END
380 514
}
381

  

Also available in: Unified diff