Package madrona :: Package analysistools :: Module widgets
[hide private]

Source Code for Module madrona.analysistools.widgets

  1  from django import forms 
  2  from django.utils.safestring import mark_safe 
  3  from django.forms.util import flatatt 
  4  from django.conf import settings 
  5  from django.contrib.gis.geos import fromstr 
  6   
7 -class SliderWidget(forms.TextInput):
8 """ 9 http://pastebin.com/f34f0c71d 10 """ 11
12 - def __init__(self, min=0, max=100, step=None, image=None, attrs=None, show_number=True):
13 super(SliderWidget, self).__init__(attrs) 14 15 self.max = max 16 self.min = min 17 self.step = step 18 self.image = image 19 self.show_number = show_number
20
21 - def get_step(self):
22 if self.step: 23 return "step : %s," % self.step 24 else: 25 return ''
26
27 - def render(self, name, value, attrs=None):
28 attrs['class'] = 'slidervalue' 29 final_attrs = self.build_attrs(attrs, name=name) 30 slider_id = 'slider-' + name 31 32 field = super(SliderWidget, self).render(name, value, attrs) 33 hide_js = "" 34 if not self.show_number: 35 hide_js = "field.hide();" 36 37 image_html = "" 38 if self.image: 39 url = self.image 40 if not url.startswith("/") and not self.image.startswith("http://"): 41 url = settings.MEDIA_URL + url 42 image_html = """<span class="form-image"><img src="%s" /></span>""" % url 43 slider = """ 44 <div class="slider" id="%(slider_id)s"></div> 45 <script type="text/javascript"> 46 madrona.onShow( function() { 47 var field = $('#%(field_id)s'); 48 %(hide_js)s // hide field? 49 var slidy = $('#%(slider_id)s'); 50 // Create the sliderbar 51 slidy.slider({ 52 range: 'min', 53 min : %(min)s, 54 max : %(max)s, 55 %(step)s 56 change : function(event, ui) { 57 // When the slider changes, set the value of the field 58 field.val(slidy.slider('value')); 59 }, 60 slide : function(event, ui) { 61 // When the slider slides, set the value of the field 62 field.val(slidy.slider('value')); 63 } 64 }); 65 66 // Initialize the slider bar to the current value 67 slidy.slider("value", field.val() ); 68 69 // If the field changes, change the slider bar 70 field.change( function (){ 71 slidy.slider("value", field.val()) 72 }); 73 }); 74 </script> 75 """ % {'slider_id': slider_id, 76 'field_id': "id_%s" % name, 77 'min': self.min, 78 'max': self.max, 79 'hide_js': hide_js, 80 'step': self.get_step()} 81 82 return mark_safe(image_html + field + slider)
83 84
85 -class SimplePoint(forms.TextInput):
86
87 - def __init__(self, title='Point', attrs=None):
88 super(SimplePoint, self).__init__(attrs) 89 self.title = title
90
91 - def render(self, name, value, attrs=None):
92 output = super(SimplePoint, self).render(name, value, attrs) 93 set_text = "Set" 94 new_text = "New" 95 if value: 96 geo = fromstr(value) 97 set_text = "Reset" 98 new_text = "Reset" 99 return mark_safe(""" 100 <div> 101 <a id="do_grabpoint" class="button" href="#"> 102 <span>Click to %s Starting Point</span> 103 </a> 104 <span style="display:none"> 105 %s 106 </span> 107 </div> 108 <br/><br/> 109 <script type="text/javascript"> 110 var shape; 111 112 madrona.beforeDestroy( function() { 113 if(shape && shape.getParentNode()){ 114 gex.dom.removeObject(shape); 115 } 116 }); 117 118 madrona.onShow( function() { 119 function shape_to_wkt(shape) { 120 var lat = shape.getGeometry().getLatitude(); 121 var lon = shape.getGeometry().getLongitude(); 122 var wkt = "SRID=4326;POINT(" + lon + " " + lat + ")"; 123 return wkt; 124 } 125 126 $('#do_grabpoint').click( function () { 127 if(!$(this).hasClass('disabled')){ 128 if(shape && shape.getParentNode()){ 129 gex.dom.removeObject(shape); 130 } 131 $(this).addClass('disabled'); 132 var button = $(this); 133 button.html('<span>Click map to set placemark</span>'); 134 135 var popts = { 136 visibility: true, 137 name: '%s %s', 138 style: { icon: { color: '#FF0' } } 139 } 140 popts['point'] = [0,0]; 141 shape = gex.dom.addPlacemark(popts); 142 gex.edit.place(shape, { 143 bounce: false, 144 dropCallback: function(){ 145 $('#id_%s').val(shape_to_wkt(shape)); 146 button.html('<span>Drag Placemark to Reset</span>'); 147 gex.edit.makeDraggable(shape, { 148 bounce: false, 149 dropCallback: function () { 150 $('#id_%s').val(shape_to_wkt(shape)); 151 } 152 }); 153 } 154 }); 155 } 156 }); 157 }); 158 </script> 159 """ % (set_text,output,new_text,self.title,name,name))
160
161 -class DualSliderWidget(forms.TextInput):
162
163 - def __init__(self, min_field, max_field, min=0, max=100, step=None, image=None, attrs=None):
164 super(DualSliderWidget, self).__init__(attrs) 165 166 self.max = max 167 self.min = min 168 self.step = step 169 self.image = image 170 self.min_field = min_field 171 self.max_field = max_field
172
173 - def get_step(self):
174 if self.step: 175 return "step : %s," % self.step 176 else: 177 return ''
178
179 - def render(self, name, value, attrs=None):
180 attrs['class'] = 'slidervalue' 181 final_attrs = self.build_attrs(attrs, name=name) 182 slider_id = 'slider-' + name 183 184 #field = super(DualSliderWidget, self).render(name, value, attrs) 185 186 field = """<input style="display:none;" type="text" name="%(name)s" value="0" id="id_%(name)s" />""" % {'name':name} 187 188 image_html = "" 189 if self.image: 190 url = self.image 191 if not url.startswith("/") and not self.image.startswith("http://"): 192 url = settings.MEDIA_URL + url 193 image_html = """<span class="form-image"><img src="%s" /></span>""" % url 194 slider = """ 195 <div class="slider" id="%(slider_id)s"></div> 196 <script type="text/javascript"> 197 madrona.onShow( function() { 198 var low_field = $('#%(low_field_id)s'); 199 var high_field = $('#%(high_field_id)s'); 200 var slidy = $('#%(slider_id)s'); 201 // Create the sliderbar 202 slidy.slider({ 203 range: true, 204 min : %(min)s, 205 max : %(max)s, 206 values: [%(min)s, %(max)s], 207 %(step)s 208 change : function(event, ui) { 209 // When the slider changes, set the value of the field 210 low_field.val(ui.values[0]); 211 high_field.val(ui.values[1]); 212 }, 213 slide : function(event, ui) { 214 // When the slider slides, set the value of the field 215 low_field.val(ui.values[0]); 216 high_field.val(ui.values[1]); 217 } 218 }); 219 220 // initialize 221 slidy.slider("values", [low_field.val(),high_field.val()]) 222 223 // If the field changes, change the slider bar 224 low_field.change( function (){ 225 slidy.slider("values", [low_field.val(),high_field.val()]) 226 }); 227 high_field.change( function (){ 228 slidy.slider("values", [low_field.val(),high_field.val()]) 229 }); 230 }); 231 </script> 232 """ % {'slider_id': slider_id, 233 'low_field_id': "id_%s" % self.min_field, 234 'high_field_id': "id_%s" % self.max_field, 235 'min': self.min, 236 'max': self.max, 237 'step': self.get_step()} 238 239 return mark_safe(image_html + field + slider)
240