root/sandbox/fvanderbiest/geoext.ux/ux/Measure/lib/GeoExt.ux/Measure.js

Revision 1932, 5.2 kB (checked in by fvanderbiest, 6 months ago)

Measure ux: we do not include OpenLayers classes for now

Line 
1 /**
2  * Copyright (c) 2008-2009 The Open Source Geospatial Foundation
3  *
4  * Published under the BSD license.
5  * See http://svn.geoext.org/core/trunk/geoext/license.txt for the full text
6  * of the license.
7  */
8
9 Ext.namespace("GeoExt.ux")
10
11 /*
12  * @requires GeoExt/widgets/Action.js
13  */
14
15 /** api: (define)
16  *  module = GeoExt.ux
17  *  class = Measure
18  */
19
20 /** api: constructor
21  *  .. class:: Measure
22  *
23  *  A GeoExt.Action configured with an OpenLayers.Control.Measure
24  *  JSBuild: need to include OpenLayers/Control/Measure.js, OpenLayers/StyleMap.js,
25  *  OpenLayers/Style.js, OpenLayers/Rule.js, OpenLayers/Handler.js
26  */
27 GeoExt.ux.Measure = Ext.extend(GeoExt.Action, {
28
29     /** api: config[styleMap]
30      *  ``OpenLayers.StyleMap`` Optional StyleMap for the sketches
31      */
32    
33     /** private: property[tip]
34      *  ``Ext.Tip`` The displayed tip.
35      */
36     tip: null,
37
38     /** api: config[template]
39      *  ``String`` | ``Ext.XTemplate`` HTML template, or Ext.XTemplate used to display the measure.
40      */
41     /** api: property[template]
42      *  ``Ext.XTemplate`` The template to display the measure.
43      */
44     template: null,
45      
46     /** api: config[decimals]
47      *  ``Integer`` The number of decimals for the displayed values.
48      *    Defaults to 2.
49      */
50
51     /** private: method[constructor]
52      */
53     constructor: function(handler, config) {
54         config.control = this.buildControl({
55             handler: handler,
56             styleMap: config.styleMap || this.styleMap()
57         });
58         delete config.styleMap;
59         if (typeof(config.template) == "string") {
60             this.template = new Ext.XTemplate(config.template, {
61                 decimals: config.decimals || 2,
62                 compiled: true
63             });
64         } else if (config.template instanceof Ext.XTemplate) {
65             this.template = config.template;
66         }
67         delete config.template;
68         delete config.decimals;
69         arguments.callee.superclass.constructor.call(this, config);
70     },
71
72     /** private: method[buildControl]
73      *  Private method to build the Measure control.
74      *
75      *  :param options ``Object`` A hash with keys:
76      *         - handler: a reference to the OpenLayers.Handler class.
77      *         - styleMap: an OpenLayers.StyleMap for sketches.
78      *
79      *  :return: ``OpenLayers.Control.Measure`` The configured control.
80      */
81     buildControl: function(options) {
82         var listeners = {
83             "measure": OpenLayers.Function.bind(this.display, this),
84             "deactivate": OpenLayers.Function.bind(this.cleanup, this),
85             "measurepartial": OpenLayers.Function.bind(this.cleanup, this)
86         };
87         return new OpenLayers.Control.Measure(
88             options.handler, {
89                 persist: true,
90                 eventListeners: listeners,
91                 handlerOptions: {
92                     layerOptions: {
93                         styleMap: options.styleMap
94                     }
95                 }
96             }
97         );
98     },
99    
100     /** private: method[cleanup]
101      *  Private method to destroy the tip.
102      */
103     cleanup: function() {
104         if (this.tip) {
105             this.tip.destroy();
106             this.tip = null;
107         }
108     },
109    
110     /** private: method[makeString]
111      *  Private method to build the HTML string.
112      *
113      *  :param event ``Object`` The event object.
114      *
115      *  :return: ``String`` The HTML string.
116      */
117     makeString: function(event) {
118         return this.template.apply(event);
119     },
120
121     /** private: method[display]
122      *  Private method to create and display the tip.
123      *
124      *  :param event ``Object`` The event object.
125      */
126     display: function(event) {
127         this.cleanup();
128         this.tip = new Ext.Tip({
129             html: this.makeString(event),
130             closable: true,
131             draggable: false,
132             listeners: {
133                 hide: function() {
134                     this.control.cancel();
135                     this.cleanup();
136                 },
137                 scope: this
138             }
139         });
140         Ext.getBody().on("mousemove", function(e) {
141             this.tip.showAt(e.getXY());
142         }, this, {
143             single: true
144         });
145     },
146
147     /** private: method[styleMap]
148      *  Private method to build the OpenLayers StyleMap.
149      *
150      *  :return: ``OpenLayers.StyleMap`` The StyleMap.
151      */
152     styleMap: function() {
153         var sketchSymbolizers = {
154             "Point": {
155                 pointRadius: 4,
156                 graphicName: "square",
157                 fillColor: "white",
158                 fillOpacity: 1,
159                 strokeWidth: 1,
160                 strokeOpacity: 1,
161                 strokeColor: "#333333"
162             },
163             "Line": {
164                 strokeWidth: 2,
165                 strokeOpacity: 1,
166                 strokeColor: "#666666",
167                 strokeDashstyle: "dash"
168             },
169             "Polygon": {
170                 strokeWidth: 2,
171                 strokeOpacity: 1,
172                 strokeColor: "#666666",
173                 fillColor: "white",
174                 fillOpacity: 0.3
175             }
176         };
177         return new OpenLayers.StyleMap({
178             "default": new OpenLayers.Style(null, {
179                 rules: [new OpenLayers.Rule({symbolizer: sketchSymbolizers})]
180             })
181         });
182     }
183 });
Note: See TracBrowser for help on using the browser.