~ubuntu-branches/ubuntu/quantal/python-django/quantal

« back to all changes in this revision

Viewing changes to django/contrib/gis/templates/gis/admin/openlayers.js

  • Committer: Bazaar Package Importer
  • Author(s): Scott James Remnant
  • Date: 2008-11-15 19:15:33 UTC
  • mfrom: (1.1.6 upstream)
  • Revision ID: james.westby@ubuntu.com-20081115191533-xbt1ut2xf4fvwtvc
Tags: 1.0.1-0ubuntu1
* New upstream release:
  - Bug fixes.

* The tests/ sub-directory appaers to have been dropped upstream, so pull
  our patch to workaround the tests and modify the rules.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
{# Author: Justin Bronn, Travis Pinney & Dane Springmeyer #}
2
 
{% block vars %}var {{ module }} = {};
3
 
{{ module }}.map = null; {{ module }}.controls = null; {{ module }}.panel = null; {{ module }}.re = new RegExp("^SRID=\d+;(.+)", "i"); {{ module }}.layers = {}; 
4
 
{{ module }}.wkt_f = new OpenLayers.Format.WKT();
5
 
{{ module }}.is_collection = {% if is_collection %}true{% else %}false{% endif %};
6
 
{{ module }}.collection_type = '{{ collection_type }}';
7
 
{{ module }}.is_linestring = {% if is_linestring %}true{% else %}false{% endif %};
8
 
{{ module }}.is_polygon = {% if is_polygon %}true{% else %}false{% endif %};
9
 
{{ module }}.is_point = {% if is_point %}true{% else %}false{% endif %};
10
 
{% endblock %}
11
 
{{ module }}.get_ewkt = function(feat){return 'SRID={{ srid }};' + {{ module }}.wkt_f.write(feat);}
12
 
{{ module }}.read_wkt = function(wkt){
13
 
  // OpenLayers cannot handle EWKT -- we make sure to strip it out.
14
 
  // EWKT is only exposed to OL if there's a validation error in the admin.
15
 
  var match = {{ module }}.re.exec(wkt);
16
 
  if (match){wkt = match[1];}
17
 
  return {{ module }}.wkt_f.read(wkt);
18
 
}
19
 
{{ module }}.write_wkt = function(feat){
20
 
  if ({{ module }}.is_collection){ {{ module }}.num_geom = feat.geometry.components.length;}
21
 
  else { {{ module }}.num_geom = 1;}
22
 
  document.getElementById('{{ id }}').value = {{ module }}.get_ewkt(feat);
23
 
}
24
 
{{ module }}.add_wkt = function(event){
25
 
  // This function will sync the contents of the `vector` layer with the
26
 
  // WKT in the text field.
27
 
  if ({{ module }}.is_collection){
28
 
    var feat = new OpenLayers.Feature.Vector(new OpenLayers.Geometry.{{ geom_type }}());
29
 
    for (var i = 0; i < {{ module }}.layers.vector.features.length; i++){
30
 
      feat.geometry.addComponents([{{ module }}.layers.vector.features[i].geometry]);
31
 
    }
32
 
    {{ module }}.write_wkt(feat);
33
 
  } else {
34
 
    // Make sure to remove any previously added features.
35
 
    if ({{ module }}.layers.vector.features.length > 1){
36
 
      old_feats = [{{ module }}.layers.vector.features[0]];
37
 
      {{ module }}.layers.vector.removeFeatures(old_feats);
38
 
      {{ module }}.layers.vector.destroyFeatures(old_feats);
39
 
    }
40
 
    {{ module }}.write_wkt(event.feature);
41
 
  }
42
 
}
43
 
{{ module }}.modify_wkt = function(event){
44
 
  if ({{ module }}.is_collection){
45
 
    if ({{ module }}.is_point){
46
 
      {{ module }}.add_wkt(event); 
47
 
      return;
48
 
    } else {
49
 
      // When modifying the selected components are added to the 
50
 
      // vector layer so we only increment to the `num_geom` value.
51
 
      var feat = new OpenLayers.Feature.Vector(new OpenLayers.Geometry.{{ geom_type }}());
52
 
      for (var i = 0; i < {{ module }}.num_geom; i++){
53
 
        feat.geometry.addComponents([{{ module }}.layers.vector.features[i].geometry]);
54
 
      }
55
 
      {{ module }}.write_wkt(feat);
56
 
    }
57
 
  } else {
58
 
    {{ module }}.write_wkt(event.feature);
59
 
  }
60
 
}
61
 
// Function to clear vector features and purge wkt from div
62
 
{{ module }}.deleteFeatures = function(){
63
 
  {{ module }}.layers.vector.removeFeatures({{ module }}.layers.vector.features);
64
 
  {{ module }}.layers.vector.destroyFeatures();
65
 
}
66
 
{{ module }}.clearFeatures = function (){
67
 
  {{ module }}.deleteFeatures();
68
 
  document.getElementById('{{ id }}').value = '';
69
 
  {{ module }}.map.setCenter(new OpenLayers.LonLat({{ default_lon }}, {{ default_lat }}), {{ default_zoom }});
70
 
}
71
 
// Add Select control
72
 
{{ module }}.addSelectControl = function(){   
73
 
  var select = new OpenLayers.Control.SelectFeature({{ module }}.layers.vector, {'toggle' : true, 'clickout' : true});
74
 
  {{ module }}.map.addControl(select);
75
 
  select.activate();
76
 
}
77
 
{{ module }}.enableDrawing = function(){ {{ module }}.map.getControlsByClass('OpenLayers.Control.DrawFeature')[0].activate();}
78
 
{{ module }}.enableEditing = function(){ {{ module }}.map.getControlsByClass('OpenLayers.Control.ModifyFeature')[0].activate();}
79
 
// Create an array of controls based on geometry type
80
 
{{ module }}.getControls = function(lyr){
81
 
  {{ module }}.panel = new OpenLayers.Control.Panel({'displayClass': 'olControlEditingToolbar'});
82
 
  var nav = new OpenLayers.Control.Navigation();
83
 
  var draw_ctl;
84
 
  if ({{ module }}.is_linestring){
85
 
    draw_ctl = new OpenLayers.Control.DrawFeature(lyr, OpenLayers.Handler.Path, {'displayClass': 'olControlDrawFeaturePath'});
86
 
  } else if ({{ module }}.is_polygon){
87
 
    draw_ctl = new OpenLayers.Control.DrawFeature(lyr, OpenLayers.Handler.Polygon, {'displayClass': 'olControlDrawFeaturePolygon'});
88
 
  } else if ({{ module }}.is_point){
89
 
    draw_ctl = new OpenLayers.Control.DrawFeature(lyr, OpenLayers.Handler.Point, {'displayClass': 'olControlDrawFeaturePoint'});
90
 
  }
91
 
  {% if modifiable %}
92
 
  var mod = new OpenLayers.Control.ModifyFeature(lyr, {'displayClass': 'olControlModifyFeature'});
93
 
  {{ module }}.controls = [nav, draw_ctl, mod];
94
 
  {% else %}
95
 
  {{ module }}.controls = [nav, darw_ctl];
96
 
  {% endif %}  
97
 
}
98
 
{{ module }}.init = function(){
99
 
    {% block map_options %}// The options hash, w/ zoom, resolution, and projection settings.
100
 
    var options = { 
101
 
{% autoescape off %}{% for item in map_options.items %}      '{{ item.0 }}' : {{ item.1 }}{% if not forloop.last %},{% endif %}
102
 
{% endfor %}{% endautoescape %}    };{% endblock %}
103
 
    // The admin map for this geometry field.
104
 
    {{ module }}.map = new OpenLayers.Map('{{ id }}_map', options);
105
 
    // Base Layer
106
 
    {{ module }}.layers.base = {% block base_layer %}new OpenLayers.Layer.WMS( "{{ wms_name }}", "{{ wms_url }}", {layers: '{{ wms_layer }}'} );{% endblock %}
107
 
    {{ module }}.map.addLayer({{ module }}.layers.base);
108
 
    {% block extra_layers %}{% endblock %}
109
 
    {% if is_linestring %}OpenLayers.Feature.Vector.style["default"]["strokeWidth"] = 3; // Default too thin for linestrings. {% endif %}
110
 
    {{ module }}.layers.vector = new OpenLayers.Layer.Vector(" {{ field_name }}");
111
 
    {{ module }}.map.addLayer({{ module }}.layers.vector);
112
 
    // Read WKT from the text field.
113
 
    var wkt = document.getElementById('{{ id }}').value;
114
 
    if (wkt){
115
 
      // After reading into geometry, immediately write back to 
116
 
      // WKT <textarea> as EWKT (so that SRID is included).
117
 
      var admin_geom = {{ module }}.read_wkt(wkt);
118
 
      {{ module }}.write_wkt(admin_geom);
119
 
      if ({{ module }}.is_collection){
120
 
        // If geometry collection, add each component individually so they may be
121
 
        // edited individually.
122
 
        for (var i = 0; i < {{ module }}.num_geom; i++){
123
 
          {{ module }}.layers.vector.addFeatures([new OpenLayers.Feature.Vector(admin_geom.geometry.components[i].clone())]);
124
 
        }
125
 
      } else {
126
 
        {{ module }}.layers.vector.addFeatures([admin_geom]);
127
 
      }
128
 
      // Zooming to the bounds.
129
 
      {{ module }}.map.zoomToExtent(admin_geom.geometry.getBounds());
130
 
    } else {
131
 
      {{ module }}.map.setCenter(new OpenLayers.LonLat({{ default_lon }}, {{ default_lat }}), {{ default_zoom }});
132
 
    }
133
 
    // This allows editing of the geographic fields -- the modified WKT is
134
 
    // written back to the content field (as EWKT, so that the ORM will know
135
 
    // to transform back to original SRID). 
136
 
    {{ module }}.layers.vector.events.on({"featuremodified" : {{ module }}.modify_wkt});
137
 
    {{ module }}.layers.vector.events.on({"featureadded" : {{ module }}.add_wkt});
138
 
    {% block controls %}
139
 
    // Map controls:
140
 
    // Add geometry specific panel of toolbar controls
141
 
    {{ module }}.getControls({{ module }}.layers.vector);
142
 
    {{ module }}.panel.addControls({{ module }}.controls);
143
 
    {{ module }}.map.addControl({{ module }}.panel);
144
 
    {{ module }}.addSelectControl();
145
 
    // Then add optional visual controls
146
 
    {% if mouse_position %}{{ module }}.map.addControl(new OpenLayers.Control.MousePosition());{% endif %}
147
 
    {% if scale_text %}{{ module }}.map.addControl(new OpenLayers.Control.Scale());{% endif %}
148
 
    {% if layerswitcher %}{{ module }}.map.addControl(new OpenLayers.Control.LayerSwitcher());{% endif %}
149
 
    // Then add optional behavior controls
150
 
    {% if scrollable %}{% else %}{{ module }}.map.getControlsByClass('OpenLayers.Control.Navigation')[0].disableZoomWheel();{% endif %}
151
 
    {% endblock %}
152
 
    if (wkt){
153
 
      {{ module }}.enableEditing();
154
 
    } else {
155
 
      {{ module }}.enableDrawing();
156
 
    }
157
 
}