Commits

Alex Ulianytskyi  committed 97a7cae

Enhanced rendering of FancyRangeWidget

  • Participants
  • Parent commits 8f1eee1
  • Tags 0.2

Comments (0)

Files changed (3)

File django_utils/forms/widgets.py

 from django.forms.widgets import Select
 from django.forms.widgets import TextInput
 from django.utils.safestring import mark_safe
+from django.template.loader import render_to_string
 
 __all__ = ['SplitDateWidget', 'RangeWidget', 'FancyRangeWidget']
 
 class FancyRangeWidget(RangeWidget):
     widgets = [HiddenInput, HiddenInput]
     delimator = u''
-    
-    def __init__(self, attrs={}, min=None, max=None):
+
+    def __init__(self, attrs=None, min=None, max=None, amount='before', slider_js_template_name='django_utils/slider.js'):
+        ''' ``amount`` - "after" or "before"'''
         self.min = min
         self.max = max
+        self.amount = amount
+        self.slider_js_template_name = slider_js_template_name
         super(FancyRangeWidget, self).__init__(attrs=attrs)
 
     def render(self, name, value, attrs=None):
-        #import pdb; pdb.set_trace()
         if not isinstance(value, list):
             value = self.decompress(value)
         final_attrs = self.build_attrs(attrs)
         id_ = final_attrs.get('id', None)
+        slider_id = '%s_slider' % id_
         amount_id = '%s_amount' % id_
         value_dict = dict(start=value[0], end=value[1])
         opts = {
-        'slider_id': id_,
-        'amount_id': amount_id,
-        'start_value': value[0] or 0,
-        'end_value': value[1] or 0,
+            'slider_id': slider_id,
+            'amount_id': amount_id,
+            'start_value': value[0] or 0,
+            'end_value': value[1] or 0,
+            'min': self.min,
+            'max': self.max,
         }
-        output = [u'<div%s></div>' % flatatt(final_attrs)]
-        output.append(u'<input%s />' % flatatt(dict(final_attrs, id=amount_id, type='text', disabled='disabled')))
+        output=[]
+        amount_html = u'<input%s />\n' % flatatt(dict(final_attrs, id=amount_id, type='text', disabled='disabled'))
+        output.append(u'<div%s>\n' % flatatt(final_attrs))
+        if self.amount == 'before':
+            output.append(amount_html)
+        output.append(u'<div%s></div>\n' % flatatt(dict(final_attrs, id=slider_id)))
+        if self.amount == 'after':
+            output.append(amount_html)
         for i, widget in zip(self.suffix_list, self.widgets):
             try:
                 widget_value = value_dict[i]
                 widget_value = None
             if id_:
                 w_id = '%s_%s' % (id_, i)
-                final_attrs = dict(final_attrs, id=w_id)
+                w_attrs = dict(final_attrs, id=w_id)
                 opts['%s_id' % i] = w_id
-            output.append(widget.render(name + '_%s' % i, widget_value, final_attrs))
+            output.append(u'%s\n' % widget.render(name + '_%s' % i, widget_value, w_attrs))
         output.insert(1, self.delimator)
-        output.append(u'''<script type="text/javascript">
-	jQuery(function() {
-		jQuery("#%s").slider({
-			range: true,''' % id_)
-	if self.min:
-            output.append(u'min: %s,' % self.min)
-        if self.max:
-            output.append(u'max: %s,' % self.max)
-	output.append(u'''values: [%(start_value)s, %(end_value)s],
-			slide: function(event, ui) {
-				jQuery("#%(start_id)s").val(ui.values[0]);
-                                jQuery("#%(end_id)s").val(ui.values[1]);
-				jQuery("#%(amount_id)s").val(ui.values[0] + ' - ' + ui.values[1]);
-			}
-		});
-                jQuery("#%(start_id)s").val(jQuery("#%(slider_id)s").slider("values", 0));
-                jQuery("#%(end_id)s").val(jQuery("#%(slider_id)s").slider("values", 1));
-                jQuery("#%(amount_id)s").val(jQuery("#%(slider_id)s").slider("values", 0) + ' - ' + jQuery("#%(slider_id)s").slider("values", 1));
-	});
-	</script>''' % opts)
+        slider_js = render_to_string(self.slider_js_template_name, opts)
+        output.append(u'<script%s>\n%s\n</script>\n' % (flatatt(dict(type='text/javascript')), slider_js))
+        output.append(u'</div>\n')
         return mark_safe(self.format_output(output))

File django_utils/templates/django_utils/slider.js

+jQuery(function() {
+        jQuery("#{{ slider_id }}").slider({
+                range: true,
+                {% if min %}min: {{ min }},{% endif %}
+                {% if max %}max: {{ max }},{% endif %}
+                values: [{{start_value }}, {{ end_value }}],
+                slide: function(event, ui) {
+                        jQuery("#{{ start_id }}").val(ui.values[0]);
+                        jQuery("#{{ end_id }}").val(ui.values[1]);
+                        jQuery("#{{ amount_id }}").val(ui.values[0] + ' - ' + ui.values[1]);
+                }
+        });
+        jQuery("#{{ start_id }}").val(jQuery("#{{ slider_id }}").slider("values", 0));
+        jQuery("#{{ end_id }}").val(jQuery("#{{ slider_id }}").slider("values", 1));
+        jQuery("#{{ amount_id }}").val(jQuery("#{{ slider_id }}").slider("values", 0) + ' - ' + jQuery("#{{ slider_id }}").slider("values", 1));
+});
 
 setup (
        name='django-utils',
-       version='0.1',
+       version='0.2',
        packages=find_packages(),
+       package_data = {
+        '': ['templates/*/*'],
+       },
        install_requires=['django>=1.1', 'django-annoying'],
        author='Alexander Ulyanitsky',
        author_email='a.ulyanitsky@gmail.com',
-       summary='My functions and decorators',
+       description='My functions and decorators',
        url='http://bitbucket.org/asux/django_utils/',
        license='BSD',
-       long_description='My functions and decorators',
-       keywords = ('django',)
+       keywords = ('django',),
 )