1 /* Copyright (c) 2006 MetaCarta, Inc., published under the BSD license.
 
   2  * See http://svn.openlayers.org/trunk/openlayers/license.txt for the full
 
   3  * text of the license. */
 
   4 // @require: OpenLayers/Layer/Markers.js
 
   9 OpenLayers.Layer.Text = Class.create();
 
  10 OpenLayers.Layer.Text.prototype = 
 
  11   Object.extend( new OpenLayers.Layer.Markers(), {
 
  13     /** store url of text file
 
  17     /** @type Array(OpenLayers.Feature) */
 
  20     /** @type OpenLayers.Feature */
 
  21     selectedFeature: null,
 
  26     * @param {String} name
 
  27     * @param {String} location
 
  29     initialize: function(name, location) {
 
  30         OpenLayers.Layer.Markers.prototype.initialize.apply(this, [name]);
 
  31         this.location = location;
 
  32         this.features = new Array();
 
  33         new Ajax.Request(location, 
 
  34           { method: 'get', onComplete:this.parseData.bind(this) } );
 
  43         OpenLayers.Layer.Markers.prototype.destroy.apply(this, arguments);
 
  46     /** WFS layer is never a base class. 
 
  49     isBaseLayer: function() {
 
  55      * @param {?} ajaxRequest
 
  57     parseData: function(ajaxRequest) {
 
  58         var text = ajaxRequest.responseText;
 
  59         var lines = text.split('\n');
 
  61         // length - 1 to allow for trailing new line
 
  62         for (var lcv = 0; lcv < (lines.length - 1); lcv++) {
 
  63             var currLine = lines[lcv].replace(/^\s*/,'').replace(/\s*$/,'');
 
  65             if (currLine.charAt(0) != '#') { /* not a comment */
 
  68                     //First line is columns
 
  69                     columns = currLine.split('\t');
 
  71                     var vals = currLine.split('\t');
 
  72                     var location = new OpenLayers.LonLat(0,0);
 
  74                     var icon, iconSize, iconOffset;
 
  76                     for (var valIndex = 0; valIndex < vals.length; valIndex++) {
 
  78                             if (columns[valIndex] == 'point') {
 
  79                                 var coords = vals[valIndex].split(',');
 
  80                                 location.lat = parseFloat(coords[0]);
 
  81                                 location.lon = parseFloat(coords[1]);
 
  83                             } else if (columns[valIndex] == 'lat') {
 
  84                                 location.lat = parseFloat(vals[valIndex]);
 
  86                             } else if (columns[valIndex] == 'lon') {
 
  87                                 location.lon = parseFloat(vals[valIndex]);
 
  89                             } else if (columns[valIndex] == 'title')
 
  90                                 title = vals[valIndex];
 
  91                             else if (columns[valIndex] == 'image' ||
 
  92                                      columns[valIndex] == 'icon')
 
  94                             else if (columns[valIndex] == 'iconSize') {
 
  95                                 var size = vals[valIndex].split(',');
 
  96                                 iconSize = new OpenLayers.Size(parseFloat(size[0]),
 
  98                             } else if (columns[valIndex] == 'iconOffset') {
 
  99                                 var offset = vals[valIndex].split(',');
 
 100                                 iconOffset = new OpenLayers.Pixel(parseFloat(offset[0]),
 
 101                                                            parseFloat(offset[1]));
 
 102                             } else if (columns[valIndex] == 'title') {
 
 103                                 title = vals[valIndex];
 
 104                             } else if (columns[valIndex] == 'description') {
 
 105                                 description = vals[valIndex];
 
 110                       var data = new Object();
 
 112                           data.icon = new OpenLayers.Icon(url, 
 
 116                           data.icon = OpenLayers.Marker.defaultIcon();
 
 118                           //allows for the case where the image url is not 
 
 119                           // specified but the size is. use a default icon
 
 120                           // but change the size
 
 121                           if (iconSize != null) {
 
 122                               data.icon.setSize(iconSize);
 
 126                       if ((title != null) && (description != null)) {
 
 127                           data['popupContentHTML'] = '<h2>'+title+'</h2><p>'+description+'</p>';
 
 129                       var feature = new OpenLayers.Feature(this, location, data);
 
 130                       this.features.append(feature);
 
 131                       var marker = feature.createMarker();
 
 132                       marker.events.register('click', feature, this.markerClick);
 
 133                       this.addMarker(marker);
 
 143     markerClick: function(evt) {
 
 144         sameMarkerClicked = (this == this.layer.selectedFeature);
 
 145         this.layer.selectedFeature = (!sameMarkerClicked) ? this : null;
 
 146         for(var i=0; i < this.layer.map.popups.length; i++) {
 
 147             this.layer.map.removePopup(this.layer.map.popups[i]);
 
 149         if (!sameMarkerClicked) {
 
 150             this.layer.map.addPopup(this.createPopup()); 
 
 158     clearFeatures: function() {
 
 159         if (this.features != null) {
 
 160             while(this.features.length > 0) {
 
 161                 var feature = this.features[0];
 
 162                 this.features.remove(feature);
 
 168     /** @final @type String */
 
 169     CLASS_NAME: "OpenLayers.Text"