Project

General

Profile

Download (8.2 KB) Statistics
| Branch: | Tag: | Revision:
1
/* Copyright (c) 2006-2013 by OpenLayers Contributors (see authors.txt for
2
 * full list of contributors). Published under the 2-clause BSD license.
3
 * See license.txt in the OpenLayers distribution or repository for the
4
 * full text of the license. */
5

    
6

    
7
/**
8
 * @requires OpenLayers/Control.js
9
 * @requires OpenLayers/Control/ArgParser.js
10
 * @requires OpenLayers/Lang.js
11
 */
12

    
13
/**
14
 * Class: OpenLayers.Control.Permalink
15
 * The Permalink control is hyperlink that will return the user to the 
16
 * current map view. By default it is drawn in the lower right corner of the
17
 * map. The href is updated as the map is zoomed, panned and whilst layers
18
 * are switched.
19
 * 
20
 * Inherits from:
21
 *  - <OpenLayers.Control>
22
 */
23
OpenLayers.Control.Permalink = OpenLayers.Class(OpenLayers.Control, {
24
    
25
    /**
26
     * APIProperty: argParserClass
27
     * {Class} The ArgParser control class (not instance) to use with this
28
     *     control.
29
     */
30
    argParserClass: OpenLayers.Control.ArgParser,
31

    
32
    /** 
33
     * Property: element 
34
     * {DOMElement}
35
     */
36
    element: null,
37
    
38
    /** 
39
     * APIProperty: anchor
40
     * {Boolean} This option changes 3 things:
41
     *     the character '#' is used in place of the character '?',
42
     *     the window.href is updated if no element is provided.
43
     *     When this option is set to true it's not recommend to provide
44
     *     a base without provide an element.
45
     */
46
    anchor: false,
47

    
48
    /** 
49
     * APIProperty: base
50
     * {String}
51
     */
52
    base: '',
53

    
54
    /** 
55
     * APIProperty: displayProjection
56
     * {<OpenLayers.Projection>} Requires proj4js support.  Projection used
57
     *     when creating the coordinates in the link. This will reproject the
58
     *     map coordinates into display coordinates. If you are using this
59
     *     functionality, the permalink which is last added to the map will
60
     *     determine the coordinate type which is read from the URL, which
61
     *     means you should not add permalinks with different
62
     *     displayProjections to the same map. 
63
     */
64
    displayProjection: null, 
65

    
66
    /**
67
     * Constructor: OpenLayers.Control.Permalink
68
     *
69
     * Parameters: 
70
     * element - {DOMElement} 
71
     * base - {String} 
72
     * options - {Object} options to the control.
73
     *
74
     * Or for anchor:
75
     * options - {Object} options to the control.
76
     */
77
    initialize: function(element, base, options) {
78
        if (element !== null && typeof element == 'object' && !OpenLayers.Util.isElement(element)) {
79
            options = element;
80
            this.base = document.location.href;
81
            OpenLayers.Control.prototype.initialize.apply(this, [options]);
82
            if (this.element != null) {
83
                this.element = OpenLayers.Util.getElement(this.element);
84
            }
85
        }
86
        else {
87
            OpenLayers.Control.prototype.initialize.apply(this, [options]);
88
            this.element = OpenLayers.Util.getElement(element);
89
            this.base = base || document.location.href;
90
        }
91
    },
92
    
93
    /**
94
     * APIMethod: destroy
95
     */
96
    destroy: function()  {
97
        if (this.element && this.element.parentNode == this.div) {
98
            this.div.removeChild(this.element);
99
            this.element = null;
100
        }
101
        if (this.map) {
102
            this.map.events.unregister('moveend', this, this.updateLink);
103
        }
104

    
105
        OpenLayers.Control.prototype.destroy.apply(this, arguments); 
106
    },
107

    
108
    /**
109
     * Method: setMap
110
     * Set the map property for the control. 
111
     * 
112
     * Parameters:
113
     * map - {<OpenLayers.Map>} 
114
     */
115
    setMap: function(map) {
116
        OpenLayers.Control.prototype.setMap.apply(this, arguments);
117

    
118
        //make sure we have an arg parser attached
119
        for(var i=0, len=this.map.controls.length; i<len; i++) {
120
            var control = this.map.controls[i];
121
            if (control.CLASS_NAME == this.argParserClass.CLASS_NAME) {
122
                
123
                // If a permalink is added to the map, and an ArgParser already
124
                // exists, we override the displayProjection to be the one
125
                // on the permalink. 
126
                if (control.displayProjection != this.displayProjection) {
127
                    this.displayProjection = control.displayProjection;
128
                }    
129
                
130
                break;
131
            }
132
        }
133
        if (i == this.map.controls.length) {
134
            this.map.addControl(new this.argParserClass(
135
                { 'displayProjection': this.displayProjection }));       
136
        }
137

    
138
    },
139

    
140
    /**
141
     * Method: draw
142
     *
143
     * Returns:
144
     * {DOMElement}
145
     */    
146
    draw: function() {
147
        OpenLayers.Control.prototype.draw.apply(this, arguments);
148
          
149
        if (!this.element && !this.anchor) {
150
            this.element = document.createElement("a");
151
            this.element.innerHTML = OpenLayers.i18n("Permalink");
152
            this.element.href="";
153
            this.div.appendChild(this.element);
154
        }
155
        this.map.events.on({
156
            'moveend': this.updateLink,
157
            'changelayer': this.updateLink,
158
            'changebaselayer': this.updateLink,
159
            scope: this
160
        });
161
        
162
        // Make it so there is at least a link even though the map may not have
163
        // moved yet.
164
        this.updateLink();
165
        
166
        return this.div;
167
    },
168
   
169
    /**
170
     * Method: updateLink 
171
     */
172
    updateLink: function() {
173
        var separator = this.anchor ? '#' : '?';
174
        var href = this.base;
175
        var anchor = null;
176
        if (href.indexOf("#") != -1 && this.anchor == false) {
177
            anchor = href.substring( href.indexOf("#"), href.length);
178
        }
179
        if (href.indexOf(separator) != -1) {
180
            href = href.substring( 0, href.indexOf(separator) );
181
        }
182
        var splits = href.split("#");
183
        href = splits[0] + separator+ OpenLayers.Util.getParameterString(this.createParams());
184
        if (anchor) {
185
            href += anchor;
186
        }
187
        if (this.anchor && !this.element) {
188
            window.location.href = href;
189
        }
190
        else {
191
            this.element.href = href;
192
        }
193
    }, 
194
    
195
    /**
196
     * APIMethod: createParams
197
     * Creates the parameters that need to be encoded into the permalink url.
198
     * 
199
     * Parameters:
200
     * center - {<OpenLayers.LonLat>} center to encode in the permalink.
201
     *     Defaults to the current map center.
202
     * zoom - {Integer} zoom level to encode in the permalink. Defaults to the
203
     *     current map zoom level.
204
     * layers - {Array(<OpenLayers.Layer>)} layers to encode in the permalink.
205
     *     Defaults to the current map layers.
206
     * 
207
     * Returns:
208
     * {Object} Hash of parameters that will be url-encoded into the
209
     * permalink.
210
     */
211
    createParams: function(center, zoom, layers) {
212
        center = center || this.map.getCenter();
213
          
214
        var params = OpenLayers.Util.getParameters(this.base);
215
        
216
        // If there's still no center, map is not initialized yet. 
217
        // Break out of this function, and simply return the params from the
218
        // base link.
219
        if (center) { 
220

    
221
            //zoom
222
            params.zoom = zoom || this.map.getZoom(); 
223

    
224
            //lon,lat
225
            var lat = center.lat;
226
            var lon = center.lon;
227
            
228
            if (this.displayProjection) {
229
                var mapPosition = OpenLayers.Projection.transform(
230
                  { x: lon, y: lat }, 
231
                  this.map.getProjectionObject(), 
232
                  this.displayProjection );
233
                lon = mapPosition.x;  
234
                lat = mapPosition.y;  
235
            }       
236
            params.lat = Math.round(lat*100000)/100000;
237
            params.lon = Math.round(lon*100000)/100000;
238
    
239
            //layers        
240
            layers = layers || this.map.layers;  
241
            params.layers = '';
242
            for (var i=0, len=layers.length; i<len; i++) {
243
                var layer = layers[i];
244
    
245
                if (layer.isBaseLayer) {
246
                    params.layers += (layer == this.map.baseLayer) ? "B" : "0";
247
                } else {
248
                    params.layers += (layer.getVisibility()) ? "T" : "F";           
249
                }
250
            }
251
        }
252

    
253
        return params;
254
    }, 
255

    
256
    CLASS_NAME: "OpenLayers.Control.Permalink"
257
});
(27-27/45)