File: Fixed.html

package info (click to toggle)
openlayers 2.11%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 60,144 kB
  • ctags: 10,906
  • sloc: xml: 7,435; python: 778; sh: 68; makefile: 30
file content (243 lines) | stat: -rw-r--r-- 7,947 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
<html>
<head>
  <script src="../OLLoader.js"></script>
  <script type="text/javascript">

    function test_activate(t) {
        t.plan(5);

        var featureList = ['foo', 'bar'];
        // a fake protocol
        var protocol = {
            read: function(options) {
                options.callback.call(options.scope, {features: featureList});
            }
        };
        
        // Create a dummy layer that can act as the map base layer.
        // This will be unnecessary if #1920 is addressed or if base layer
        // handling is changed.
        var dummy = new OpenLayers.Layer(null, {isBaseLayer: true});

        var layer = new OpenLayers.Layer.Vector("Vector Layer", {
            strategies: [new OpenLayers.Strategy.Fixed()],
            protocol: protocol,
            addFeatures: function(features) {
                t.eq(features, featureList, "Features added to the layer");
            }
        });
        
        var layerp = new OpenLayers.Layer.Vector("Hidden preload Layer", {
            strategies: [new OpenLayers.Strategy.Fixed({preload:true})],
            protocol: protocol,
            visibility: false,
            addFeatures: function(features) {
                t.ok(!this.visibility, "Features preloaded before visible");
            }
        });

        var s = new OpenLayers.Strategy.Fixed();
        var layer2 = new OpenLayers.Layer.Vector("Hidden lazyload Layer", {
            strategies: [s],
            protocol: protocol,
            visibility: false,
            addFeatures: function(features) {
                t.ok(this.visibility, "Layer visible when features added");
            }
        });

        var map = new OpenLayers.Map('map');
        map.addLayers([dummy, layer, layerp, layer2]);

        t.ok(layer2.events.listeners["visibilitychanged"][0].obj == s &&
                layer2.events.listeners["visibilitychanged"][0].func == s.load,
                "activate registers visibilitychanged listener if layer hidden"+
                " and is lazyloading");

        layer2.setVisibility(true);
        
        t.ok(layer2.events.listeners["visibilitychanged"] == false,
                "visibilitychanged listener unregistered");
    }
    
    function test_events(t) {
        
        t.plan(3);
        
        var log = {
            loadstart: 0,
            loadend: 0
        };
        
        var map = new OpenLayers.Map("map");
        var layer = new OpenLayers.Layer.Vector(null, {
            strategies: [new OpenLayers.Strategy.Fixed()],
            protocol: new OpenLayers.Protocol({
                read: function(config) {
                    config.callback.call(config.scope, {});
                }
            }),
            isBaseLayer: true,
            eventListeners: {
                loadstart: function() {
                    ++log.loadstart;
                },
                loadend: function() {
                    ++log.loadend;
                }
            }
        });

        map.addLayer(layer);
        map.zoomToMaxExtent();
        
        t.eq(log.loadstart, 1, "loadstart triggered");
        t.eq(log.loadend, 1, "loadend triggered");
        var log = {};
        layer.protocol.read = function(obj) {
            log.obj = obj;
        }
        layer.refresh({whee: 'chicken'});

        t.eq(log.obj && log.obj.whee, "chicken", "properties passed to read on refresh correctly.");
        
        map.destroy();
        
    }
    
    
    function test_merge(t) {
        
        t.plan(6);
        
        var strategy = new OpenLayers.Strategy.Fixed();
        
        // create map with default projection
        var map = new OpenLayers.Map("map");
        
        var log = {
            loadend: 0
        };
        
        // create layer with custom projection
        var layer = new OpenLayers.Layer.Vector(null, {
            isBaseLayer: true,
            strategies: [strategy],
            protocol: new OpenLayers.Protocol(),
            projection: new OpenLayers.Projection("EPSG:900913"),
            eventListeners: {
                loadend: function() {
                    ++log.loadend;
                }
            }
        });
        
        // give the layer some existing features (one)
        layer.addFeatures([
            new OpenLayers.Feature.Vector(
                new OpenLayers.Geometry.Point(0, 0)
            )
        ]);
        
        map.addLayer(layer);
        map.zoomToMaxExtent();
        
        // create some features
        var geometries = [
            new OpenLayers.Geometry.Point(100, 200),
            new OpenLayers.Geometry.Point(1000, 2000)
        ];
        var features = [
            new OpenLayers.Feature.Vector(geometries[0].clone()),
            new OpenLayers.Feature.Vector(geometries[1].clone())
        ];

        // call merge with a mocked up response
        strategy.merge(new OpenLayers.Projection("EPSG:900913"), {features: features});
        
        // confirm that the original features were destroyed
        t.eq(layer.features.length, 2, "old features destroyed");
        
        // confirm that loadend was called
        t.eq(log.loadend, 1, "merge triggers loadend");
        
        // test that feature geometries have been transformed to map projection
        var from = layer.projection;
        var to = map.getProjectionObject();
        t.geom_eq(layer.features[0].geometry, features[0].geometry.transform(from, to), "[different proj] feature 0 geometry transformed");
        t.geom_eq(layer.features[1].geometry, features[1].geometry.transform(from, to), "[different proj] feature 1 geometry transformed");
        
        // same as above but with same map/layer projection
        layer.destroyFeatures();
        layer.projection = map.getProjectionObject();
        
        features = [
            new OpenLayers.Feature.Vector(geometries[0].clone()),
            new OpenLayers.Feature.Vector(geometries[1].clone())
        ];
        
        // call merge again with mocked up response
        strategy.merge(new OpenLayers.Projection("EPSG:900913"), {features: features});

        // test that feature geometries have not been transformed
        t.geom_eq(layer.features[0].geometry, features[0].geometry, "[same proj] feature 0 geometry not transformed");
        t.geom_eq(layer.features[1].geometry, features[1].geometry, "[same proj] feature 1 geometry not transformed");
        
    }

    function test_load(t) {
        t.plan(4);

        // set up

        var log;

        var map = new OpenLayers.Map({
            div: "map",
            projection: new OpenLayers.Projection("EPSG:900913"),
            layers: [new OpenLayers.Layer("", {isBaseLayer: true})]
        });

        var response = new OpenLayers.Protocol.Response();

        var strategy = new OpenLayers.Strategy.Fixed({
            merge: function(p, r) {
                log = {scope: this, projection: p, response: r};
            }
        });

        var layer = new OpenLayers.Layer.Vector("vector", {
            strategies: [strategy],
            protocol: {
                read: function(o) {
                    o.callback.call(o.scope, response);
                }
            }
        });

        map.addLayer(layer);

        // test

        strategy.load();

        // verify that the callback is correctly bound
        t.ok(log !== undefined,
             "merge was called");
        t.ok(log.scope == strategy,
             "merge called with expected scope");
        t.eq(log.projection.getCode(), map.getProjectionObject().getCode(),
             "merge called the map projection as the first arg");
        t.ok(log.response == response,
             "merge called with response as the first arg");

        // tear down

        map.destroy();
    }
  </script>
</head>
<body>
    <div id="map" style="width: 400px; height: 200px" />
</body>
</html>