]> dev.renevier.net Git - syp.git/blob - openlayers/lib/OpenLayers/Strategy/Fixed.js
fixes notices
[syp.git] / openlayers / lib / OpenLayers / Strategy / Fixed.js
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  * @requires OpenLayers/Strategy.js
7  */
8
9 /**
10  * Class: OpenLayers.Strategy.Fixed
11  * A simple strategy that requests features once and never requests new data.
12  *
13  * Inherits from:
14  *  - <OpenLayers.Strategy>
15  */
16 OpenLayers.Strategy.Fixed = OpenLayers.Class(OpenLayers.Strategy, {
17     
18     /**
19      * APIProperty: preload
20      * {Boolean} Load data before layer made visible. Enabling this may result
21      *   in considerable overhead if your application loads many data layers
22      *   that are not visible by default. Default is true.
23      */
24     preload: false,
25
26     /**
27      * Constructor: OpenLayers.Strategy.Fixed
28      * Create a new Fixed strategy.
29      *
30      * Parameters:
31      * options - {Object} Optional object whose properties will be set on the
32      *     instance.
33      */
34     initialize: function(options) {
35         OpenLayers.Strategy.prototype.initialize.apply(this, [options]);
36     },
37
38     /**
39      * APIMethod: destroy
40      * Clean up the strategy.
41      */
42     destroy: function() {
43         OpenLayers.Strategy.prototype.destroy.apply(this, arguments);
44     },
45
46     /**
47      * Method: activate
48      * Activate the strategy: load data or add listener to load when visible
49      *
50      * Returns:
51      * {Boolean} True if the strategy was successfully activated or false if
52      *      the strategy was already active.
53      */
54     activate: function() {
55         if(OpenLayers.Strategy.prototype.activate.apply(this, arguments)) {
56             this.layer.events.on({
57                 "refresh": this.load,
58                 scope: this
59             });
60             if(this.layer.visibility == true || this.preload) {
61                 this.load();
62             } else {
63                 this.layer.events.on({
64                     "visibilitychanged": this.load,
65                     scope: this
66                 });
67             }
68             return true;
69         }
70         return false;
71     },
72     
73     /**
74      * Method: deactivate
75      * Deactivate the strategy.  Undo what is done in <activate>.
76      * 
77      * Returns:
78      * {Boolean} The strategy was successfully deactivated.
79      */
80     deactivate: function() {
81         var deactivated = OpenLayers.Strategy.prototype.deactivate.call(this);
82         if(deactivated) {
83             this.layer.events.un({
84                 "refresh": this.load,
85                 "visibilitychanged": this.load,
86                 scope: this
87             });
88         }
89         return deactivated;
90     },
91
92     /**
93      * Method: load
94      * Tells protocol to load data and unhooks the visibilitychanged event
95      *
96      * Parameters:
97      * options - {Object} options to pass to protocol read.
98      */
99     load: function(options) {
100         this.layer.events.triggerEvent("loadstart");
101         this.layer.protocol.read(OpenLayers.Util.applyDefaults({
102             callback: this.merge,
103             scope: this
104         }, options));
105         this.layer.events.un({
106             "visibilitychanged": this.load,
107             scope: this
108         });
109     },
110
111     /**
112      * Method: merge
113      * Add all features to the layer.
114      */
115     merge: function(resp) {
116         this.layer.destroyFeatures();
117         var features = resp.features;
118         if (features && features.length > 0) {
119             var remote = this.layer.projection;
120             var local = this.layer.map.getProjectionObject();
121             if(!local.equals(remote)) {
122                 var geom;
123                 for(var i=0, len=features.length; i<len; ++i) {
124                     geom = features[i].geometry;
125                     if(geom) {
126                         geom.transform(remote, local);
127                     }
128                 }
129             }
130             this.layer.addFeatures(features);
131         }
132         this.layer.events.triggerEvent("loadend");
133     },
134
135     CLASS_NAME: "OpenLayers.Strategy.Fixed"
136 });