]> git.openstreetmap.org Git - rails.git/blob - public/lib/OpenLayers/Layer/Text.js
new calls
[rails.git] / public / lib / OpenLayers / Layer / Text.js
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
5
6 /**
7 * @class
8 */
9 OpenLayers.Layer.Text = Class.create();
10 OpenLayers.Layer.Text.prototype = 
11   Object.extend( new OpenLayers.Layer.Markers(), {
12
13     /** store url of text file
14     * @type str */
15     location:null,
16
17     /** @type Array(OpenLayers.Feature) */
18     features: null,
19
20     /** @type OpenLayers.Feature */
21     selectedFeature: null,
22
23     /**
24     * @constructor
25     *
26     * @param {String} name
27     * @param {String} location
28     */
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) } );
35     },
36
37     /**
38      * 
39      */
40     destroy: function() {
41         this.clearFeatures();
42         this.features = null;
43         OpenLayers.Layer.Markers.prototype.destroy.apply(this, arguments);
44     },
45     
46     /** WFS layer is never a base class. 
47      * @type Boolean
48      */
49     isBaseLayer: function() {
50         return false;
51     },
52     
53     
54     /**
55      * @param {?} ajaxRequest
56      */
57     parseData: function(ajaxRequest) {
58         var text = ajaxRequest.responseText;
59         var lines = text.split('\n');
60         var columns;
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*$/,'');
64         
65             if (currLine.charAt(0) != '#') { /* not a comment */
66             
67                 if (!columns) {
68                     //First line is columns
69                     columns = currLine.split('\t');
70                 } else {
71                     var vals = currLine.split('\t');
72                     var location = new OpenLayers.LonLat(0,0);
73                     var title; var url;
74                     var icon, iconSize, iconOffset;
75                     var set = false;
76                     for (var valIndex = 0; valIndex < vals.length; valIndex++) {
77                         if (vals[valIndex]) {
78                             if (columns[valIndex] == 'point') {
79                                 var coords = vals[valIndex].split(',');
80                                 location.lat = parseFloat(coords[0]);
81                                 location.lon = parseFloat(coords[1]);
82                                 set = true;
83                             } else if (columns[valIndex] == 'lat') {
84                                 location.lat = parseFloat(vals[valIndex]);
85                                 set = true;
86                             } else if (columns[valIndex] == 'lon') {
87                                 location.lon = parseFloat(vals[valIndex]);
88                                 set = true;
89                             } else if (columns[valIndex] == 'title')
90                                 title = vals[valIndex];
91                             else if (columns[valIndex] == 'image' ||
92                                      columns[valIndex] == 'icon')
93                                 url = vals[valIndex];
94                             else if (columns[valIndex] == 'iconSize') {
95                                 var size = vals[valIndex].split(',');
96                                 iconSize = new OpenLayers.Size(parseFloat(size[0]),
97                                                            parseFloat(size[1]));
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];
106                             }
107                         }
108                     }
109                     if (set) {
110                       var data = new Object();
111                       if (url != null) {
112                           data.icon = new OpenLayers.Icon(url, 
113                                                           iconSize, 
114                                                           iconOffset);
115                       } else {
116                           data.icon = OpenLayers.Marker.defaultIcon();
117
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);
123                           }
124                         
125                       }
126                       if ((title != null) && (description != null)) {
127                           data['popupContentHTML'] = '<h2>'+title+'</h2><p>'+description+'</p>';
128                       }
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);
134                     }
135                 }
136             }
137         }
138     },
139     
140     /**
141      * @param {Event} evt
142      */
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]);
148         }
149         if (!sameMarkerClicked) {
150             this.layer.map.addPopup(this.createPopup()); 
151         }
152         Event.stop(evt);
153     },
154
155     /**
156      * 
157      */
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);
163                 feature.destroy();
164             }
165         }        
166     },
167     
168     /** @final @type String */
169     CLASS_NAME: "OpenLayers.Text"
170 });
171      
172