Changeset 7922
- Timestamp:
- 09/02/08 10:53:32 (3 months ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
sandbox/jvanulden/openlayers/lib/OpenLayers/Control/LayerSwitcher.js
r7906 r7922 73 73 * Property: activeLayer 74 74 */ 75 activeLayer: null, 75 activeLayer: null, 76 77 /** 78 * Property: useLegendGraphics 79 */ 80 useLegendGraphics: false, 76 81 77 82 /** … … 166 171 167 172 // populate div with current info 168 this.redraw(); 169 173 this.redraw(); 174 170 175 return this.div; 171 176 }, … … 276 281 // layers if they are visible 277 282 var checked = (baseLayer) ? (layer == this.map.baseLayer) 278 : layer.getVisibility(); 279 283 : layer.getVisibility(); 284 285 var layerWrapper = document.createElement("div"); 286 layerWrapper.id = "layer_" + layer.id; 287 280 288 // create input element 281 289 var inputElem = document.createElement("input"); … … 309 317 titleDiv.id = "title_" + layer.id; 310 318 titleDiv.style.backgroundColor = "#e1e1e1"; 311 titleDiv.style.width = "2 15px";319 titleDiv.style.width = "220px"; 312 320 titleDiv.style.padding = "2px"; 313 321 titleDiv.style.border = "solid 1px #e1e1e1"; … … 364 372 365 373 // set the default opacity 366 layer.setOpacity("1.0"); 367 368 // create legend div 369 var legendDiv = document.createElement("div"); 370 legendDiv.style.position = "relative"; 371 372 // create legend img element 373 var legendImg = document.createElement("img"); 374 legendImg.id = "leg_" + layer.id; 375 legendImg.style.display = "none"; 376 374 layer.setOpacity("1.0"); 375 377 376 var context = { 377 'layer': layer, 378 378 'inputElem': inputElem, 379 'layer': layer,380 379 'titleDiv': titleDiv, 381 'legendImg': legendImg,382 380 'layerSwitcher': this 383 }; 384 385 // if the service doesn't return an image, insert a transparent gif 386 OpenLayers.Event.observe(legendImg, "error", 387 OpenLayers.Function.bind(this.onLegendImgError, context) 388 ); 381 }; 389 382 390 383 // bind events to the layer operation buttons … … 435 428 'layer': layer, 436 429 'inputElem': inputElem, 437 'legendDiv': legendDiv,438 430 'titleDiv': titleDiv, 439 431 'labelSpan': labelSpan … … 442 434 var groupDiv = (baseLayer) ? this.baseLayersDiv 443 435 : this.dataLayersDiv; 444 445 groupDiv.appendChild(titleDiv); 436 437 groupDiv.appendChild(layerWrapper); 438 layerWrapper.appendChild(titleDiv); 446 439 titleDiv.appendChild(inputElem); 447 440 titleDiv.appendChild(buttonSpan); … … 455 448 buttonSpan.appendChild(opacityPlusButton); 456 449 titleDiv.appendChild(labelSpan); 457 legendDiv.appendChild(legendImg); 458 groupDiv.appendChild(legendDiv); 459 groupDiv.appendChild(br); 460 450 groupDiv.appendChild(br); 451 461 452 } 462 453 … … 469 460 this.baseLbl.style.display = (containsBaseLayers) ? "" : "none"; 470 461 471 // load the legend images 472 this.loadLegendImages(); 462 if(this.useLegendGraphics){ this.getLegendGraphics(this.useLegendGraphics) }; 473 463 474 464 return this.div; … … 477 467 /** 478 468 * Method: 479 * Load legend images 469 * Load legend images using OGC GetLegendGraphic request 470 * 471 * NOTE: this will only work for layers that are hosted 472 * on an OGC WMS service. 480 473 * 481 474 * Parameters: 482 * e - {Event} 483 */ 484 loadLegendImages: function(e) 475 * enableRedraw - If true legend graphics are redrawn with 476 * layerSwitcher, if false legend graphics 477 * are only drawn on request. 478 */ 479 getLegendGraphics: function(enableRedraw) 485 480 { 481 this.useLegendGraphics = (enableRedraw) ? true : false; 482 486 483 var len = this.map.layers.length; 487 484 for (var i=0; i <len; i++) 488 485 { 489 486 var layer = this.map.layers[i]; 490 var img = OpenLayers.Util.getElement("leg_" + layer.id); 487 var layerWrapper = OpenLayers.Util.getElement("layer_" + layer.id); 488 var legendImg = document.createElement("img"); 491 489 492 if(img && layer.isBaseLayer == false) 490 var context = { 491 'legendImg': legendImg, 492 'layerSwitcher': this 493 }; 494 495 // if the service doesn't return an image, insert a transparent gif 496 OpenLayers.Event.observe(legendImg, "error", 497 OpenLayers.Function.bind(this.onLegendImgError, context) 498 ); 499 500 if(layer.isBaseLayer == false) 493 501 { 494 502 // build getLegendGraphic request 495 var url= layer.getFullRequestString({503 var legendGraphicURL = layer.getFullRequestString({ 496 504 REQUEST: "GetLegendGraphic", 497 505 LAYER: layer.params.LAYERS, … … 499 507 WIDTH: "150"}); 500 508 501 img.src = url;502 img.style.display = "inline";509 legendImg.src = legendGraphicURL; 510 layerWrapper.appendChild(legendImg); 503 511 } 504 512 } 513 }, 514 515 /** 516 * Method: onLegendImgError 517 * Load blank image if getLegendGraphic request fails 518 * 519 * Parameters: 520 * e - {Event} 521 */ 522 onLegendImgError: function(e) 523 { 524 this.legendImg.src = "../img/blank.gif"; 525 526 OpenLayers.Event.stop(e); 505 527 }, 506 528 … … 528 550 } 529 551 } 552 530 553 OpenLayers.Event.stop(e); 531 554 }, 532 533 /**534 * Method: onLegendImgError535 * Load blank image if none found536 *537 * Parameters:538 * e - {Event}539 */540 onLegendImgError: function(e)541 {542 this.legendImg.src = "../img/blank.gif";543 544 OpenLayers.Event.stop(e);545 },546 555 547 556 /** … … 763 772 this.div.style.color = "#333"; 764 773 this.div.style.backgroundColor = "transparent"; 765 766 774 767 775 OpenLayers.Event.observe(this.div, "mouseup", … … 776 784 this.layersDiv = document.createElement("div"); 777 785 this.layersDiv.id = this.id + "_layersDiv"; 778 this.layersDiv.style.paddingTop = " 10px";779 this.layersDiv.style.paddingLeft = " 10px";780 this.layersDiv.style.paddingBottom = " 5px";786 this.layersDiv.style.paddingTop = "5px"; 787 this.layersDiv.style.paddingLeft = "5px"; 788 this.layersDiv.style.paddingBottom = "10px"; 781 789 this.layersDiv.style.paddingRight = "75px"; 782 790 this.layersDiv.style.backgroundColor = this.activeColor; … … 798 806 this.dataLayersDiv = document.createElement("div"); 799 807 this.dataLayersDiv.style.paddingLeft = "5px"; 800 808 801 809 if (this.ascending) { 802 810 this.layersDiv.appendChild(this.baseLbl); … … 810 818 this.layersDiv.appendChild(this.baseLayersDiv); 811 819 } 812 820 813 821 this.div.appendChild(this.layersDiv); 814 822 … … 818 826 blend: false}); 819 827 820 OpenLayers.Rico.Corner.changeOpacity(this.layersDiv, 0.9 0);828 OpenLayers.Rico.Corner.changeOpacity(this.layersDiv, 0.95); 821 829 822 830 var imgLocation = OpenLayers.Util.getImagesLocation();
