~hwfwill/sahana-eden/test

« back to all changes in this revision

Viewing changes to static/scripts/gis/openlayers/lib/OpenLayers/Tile/Image/IFrame.js

  • Committer: William
  • Date: 2010-08-01 10:21:30 UTC
  • Revision ID: william@william-tp-20100801102130-cuw199enh3ot2vbg
changed

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (c) 2006-2008 MetaCarta, Inc., published under the Clear BSD
 
2
 * license.  See http://svn.openlayers.org/trunk/openlayers/license.txt for the
 
3
 * full text of the license. */
 
4
 
 
5
 
 
6
/**
 
7
 * @requires OpenLayers/Tile/Image.js
 
8
 */
 
9
 
 
10
/**
 
11
 * Class: OpenLayers.Tile.Image.IFrame
 
12
 * Instances of OpenLayers.Tile.Image.IFrame are used to manage the image tiles
 
13
 * used by Layer.WMS.Post loaded via HTTP-POST-protocol. Create a new image
 
14
 * tile with the <OpenLayers.Tile.Image.IFrame> constructor.
 
15
 *
 
16
 * Inherits from:
 
17
 *  - <OpenLayers.Tile.Image>
 
18
 */
 
19
OpenLayers.Tile.Image.IFrame = OpenLayers.Class(OpenLayers.Tile.Image, {
 
20
    
 
21
    /**
 
22
     * Property: layerAlphaHack
 
23
     * {Boolean} Always false for an instance.
 
24
     */
 
25
 
 
26
    /**
 
27
     * Constructor: OpenLayers.Tile.Image.IFrame
 
28
     * Constructor for a new <OpenLayers.Tile.Image.IFrame> instance.
 
29
     * 
 
30
     * Parameters:
 
31
     * layer - {<OpenLayers.Layer>} layer that the tile will go in.
 
32
     * position - {<OpenLayers.Pixel>}
 
33
     * bounds - {<OpenLayers.Bounds>}
 
34
     * size - {<OpenLayers.Size>}
 
35
     */   
 
36
    initialize: function(layer, position, bounds, url, size) {
 
37
        OpenLayers.Tile.Image.prototype.initialize.apply(this, arguments);
 
38
        this.layerAlphaHack = false;
 
39
    },
 
40
 
 
41
    /** 
 
42
     * Method: destroy
 
43
     * nullify references to prevent circular references and memory leaks
 
44
     */
 
45
    destroy: function() {
 
46
        if(this.imgDiv != null) {
 
47
            // unregister the "load" handler
 
48
            OpenLayers.Event.stopObservingElement(this.imgDiv.firstChild);
 
49
        }
 
50
        OpenLayers.Tile.Image.prototype.destroy.apply(this, arguments);
 
51
    },
 
52
 
 
53
    /**
 
54
     * Method: clear
 
55
     * Removes the iframe from DOM (avoids back-button problems).
 
56
     */
 
57
    clear: function() {
 
58
        if(this.imgDiv) {
 
59
            var iFrame = this.imgDiv.firstChild;
 
60
            OpenLayers.Event.stopObservingElement(iFrame);
 
61
            this.imgDiv.removeChild(iFrame);
 
62
        }
 
63
    },
 
64
 
 
65
    /**
 
66
     * Method: clone
 
67
     *
 
68
     * Parameters:
 
69
     * obj - {<OpenLayers.Tile.Image.IFrame>} The tile to be cloned
 
70
     *
 
71
     * Returns:
 
72
     * {<OpenLayers.Tile.Image.IFrame>} An exact clone of this 
 
73
     * <OpenLayers.Tile.Image.IFrame>
 
74
     */
 
75
    clone: function (obj) {
 
76
        if (obj == null) {
 
77
            obj = new OpenLayers.Tile.Image.IFrame(
 
78
                this.layer, this.position, this.bounds, this.url, this.size);
 
79
        } 
 
80
        
 
81
        //pick up properties from superclass
 
82
        obj = OpenLayers.Tile.Image.prototype.clone.apply(this, [obj]);
 
83
        
 
84
        return obj;
 
85
    },
 
86
 
 
87
    /**
 
88
     * Method: renderTile
 
89
     */
 
90
     renderTile: function() {
 
91
        if(OpenLayers.Tile.Image.prototype.renderTile.apply(this, arguments)) {
 
92
            // create a html form and add it temporary to the layer div
 
93
            var form = this.createRequestForm();
 
94
            this.imgDiv.appendChild(form);
 
95
 
 
96
            // submit the form (means fetching the image)
 
97
            form.submit();
 
98
            this.imgDiv.removeChild(form);
 
99
        }
 
100
    },
 
101
 
 
102
    /**
 
103
     * Method: initImgDiv
 
104
     * Creates the imgDiv property on the tile.
 
105
     */
 
106
    initImgDiv: function() {
 
107
        this.imgDiv = this.createImgDiv()
 
108
 
 
109
        OpenLayers.Util.modifyDOMElement(this.imgDiv, this.id, null,
 
110
            this.layer.getImageSize(), "relative");
 
111
        this.imgDiv.className = 'olTileImage';
 
112
 
 
113
        this.frame.appendChild(this.imgDiv); 
 
114
        this.layer.div.appendChild(this.frame); 
 
115
 
 
116
        if(this.layer.opacity != null) {
 
117
            
 
118
            OpenLayers.Util.modifyDOMElement(this.imgDiv, null, null, null,
 
119
                                             null, null, null, 
 
120
                                             this.layer.opacity);
 
121
        }
 
122
 
 
123
        // we need this reference to check back the viewRequestID
 
124
        this.imgDiv.map = this.layer.map;
 
125
    },
 
126
 
 
127
    /**
 
128
     * Method: createImgDiv
 
129
     * Creates a div with iframe.and eventPane
 
130
     *
 
131
     * Returns:
 
132
     * {DOMElement}
 
133
     */
 
134
    createImgDiv: function() {
 
135
        var eventPane = document.createElement("div");
 
136
 
 
137
        if(OpenLayers.Util.getBrowserName() == "msie") {
 
138
            // IE cannot handle events on elements without backgroundcolor. So we
 
139
            // use this little hack to make elements transparent
 
140
            eventPane.style.backgroundColor = '#FFFFFF';
 
141
            eventPane.style.filter          = 'chroma(color=#FFFFFF)';
 
142
        }
 
143
 
 
144
        OpenLayers.Util.modifyDOMElement(eventPane, null,
 
145
            new OpenLayers.Pixel(0,0), this.layer.getImageSize(), "absolute");
 
146
 
 
147
        var imgDiv = document.createElement("div");
 
148
        imgDiv.appendChild(eventPane);
 
149
        return imgDiv;
 
150
    },
 
151
 
 
152
    /**
 
153
     * Method: createIFrame
 
154
     * Create the IFrame which shows the image.
 
155
     *
 
156
     * Returns:
 
157
     * {DOMElement} Iframe
 
158
     */
 
159
    createIFrame: function() {
 
160
        var id = this.id+'_iFrame';
 
161
        var iframe;
 
162
        if(OpenLayers.Util.getBrowserName() == "msie") {
 
163
            // InternetExplorer does not set the name attribute of an iFrame 
 
164
            // properly via DOM manipulation, so we need to do it on our own with
 
165
            // this hack.
 
166
            iframe = document.createElement('<iframe name="'+id+'">');
 
167
 
 
168
            // IFrames in InternetExplorer are not transparent, if you set the
 
169
            // backgroundColor transparent. This is a workarround to get 
 
170
            // transparent iframes.
 
171
            iframe.style.backgroundColor = '#FFFFFF';
 
172
            iframe.style.filter          = 'chroma(color=#FFFFFF)';
 
173
        }
 
174
        else {
 
175
            iframe = document.createElement('iframe');
 
176
            iframe.style.backgroundColor = 'transparent';
 
177
        
 
178
            // iframe.name needs to be an unique id, otherwise it 
 
179
            // could happen that other iframes are overwritten.
 
180
            iframe.name = id;
 
181
        }
 
182
        iframe.id = id;
 
183
 
 
184
        // some special properties to avoid scaling the images and scrollbars 
 
185
        // in the iframe
 
186
        iframe.scrolling             = 'no';
 
187
        iframe.marginWidth           = '0px';
 
188
        iframe.marginHeight          = '0px';
 
189
        iframe.frameBorder           = '0';
 
190
 
 
191
        OpenLayers.Util.modifyDOMElement(iframe, id, 
 
192
            new OpenLayers.Pixel(0,0), this.layer.getImageSize(), "absolute");
 
193
 
 
194
        //bind a listener to the onload of the iframe so that we
 
195
        // can register when a tile has finished loading.
 
196
        var onload = function() {
 
197
            this.show();
 
198
            //normally isLoading should always be true here but there are some
 
199
            // right funky conditions where loading and then reloading a tile
 
200
            // with the same url *really*fast*. this check prevents sending
 
201
            // a 'loadend' if the msg has already been sent
 
202
            //
 
203
            if (this.isLoading) {
 
204
                this.isLoading = false;
 
205
                this.events.triggerEvent("loadend");
 
206
            }
 
207
        };
 
208
        OpenLayers.Event.observe(iframe, 'load',
 
209
            OpenLayers.Function.bind(onload, this));
 
210
 
 
211
        return iframe;
 
212
    },
 
213
    
 
214
    /**
 
215
     * Method: createRequestForm
 
216
     * Create the html <form> element with width, height, bbox and all 
 
217
     * parameters specified in the layer params.
 
218
     *
 
219
     * Returns: 
 
220
     * {DOMElement} The form element which sends the HTTP-POST request to the
 
221
     *              WMS. 
 
222
     */
 
223
    createRequestForm: function() {
 
224
        // creation of the form element
 
225
        var form = document.createElement('form');
 
226
        form.method = 'POST';
 
227
        var cacheId = this.layer.params["_OLSALT"];
 
228
        cacheId = (cacheId ? cacheId + "_" : "") + this.bounds.toBBOX();
 
229
        form.action = OpenLayers.Util.urlAppend(this.layer.url, cacheId);
 
230
 
 
231
        // insert the iframe, which has been removed to avoid back-button
 
232
        // problems
 
233
        this.imgDiv.insertBefore(this.createIFrame(), this.imgDiv.firstChild);
 
234
 
 
235
        form.target = this.id+'_iFrame';
 
236
 
 
237
        // adding all parameters in layer params as hidden fields to the html
 
238
        // form element
 
239
        var imageSize = this.layer.getImageSize();
 
240
        var params = OpenLayers.Util.extend(
 
241
            {
 
242
                "BBOX": this.encodeBBOX ? this.bounds.toBBOX() :
 
243
                        this.bounds.toArray(),
 
244
                "WIDTH": imageSize.w,
 
245
                "HEIGHT": imageSize.h
 
246
            }, this.layer.params);
 
247
            
 
248
        for(var par in params) {
 
249
            var field = document.createElement('input');
 
250
            field.type  = 'hidden';
 
251
            field.name  = par;
 
252
            field.value = params[par];
 
253
            form.appendChild(field);
 
254
        }   
 
255
 
 
256
        return form;
 
257
    },
 
258
    
 
259
    CLASS_NAME: "OpenLayers.Tile.Image.IFrame"
 
260
  }
 
261
);