Commits

Mark Lavin committed c5bde82

Allow passing a serialized set of options for the JS plugin. See #76

  • Participants
  • Parent commits e80d641
  • Branches plugin-refactor

Comments (0)

Files changed (2)

selectable/forms/widgets.py

+import json
+
 from django import forms
 from django.conf import settings
 from django.forms.util import flatatt
             self.qs['limit'] = self.limit
         if self.qs:
             url = '%s?%s' % (url, urlencode(self.qs))
+        if u'data-selectable-options' in attrs:
+            attrs[u'data-selectable-options'] = json.dumps(attrs[u'data-selectable-options'])
         attrs[u'data-selectable-url'] = url
         attrs[u'data-selectable-type'] = 'text'
         attrs[u'data-selectable-allow-new'] = str(self.allow_new).lower()
         widgets = [
             AutoCompleteWidget(
                 lookup_class, allow_new=self.allow_new,
-                limit=self.limit, query_params=query_params
+                limit=self.limit, query_params=query_params,
+                attrs=kwargs.get('attrs'),
             ),
             forms.HiddenInput(attrs={u'data-selectable-type': 'hidden'})
         ]
         widgets = [
             AutoComboboxWidget(
                 lookup_class, allow_new=self.allow_new,
-                limit=self.limit, query_params=query_params
+                limit=self.limit, query_params=query_params,
+                attrs=kwargs.get('attrs'),
             ),
             forms.HiddenInput(attrs={u'data-selectable-type': 'hidden'})
         ]
             u'data-selectable-multiple': 'true',
             u'data-selectable-position': position
         }
+        attrs.update(kwargs.get('attrs', {}))
         query_params = kwargs.pop('query_params', {})
         widgets = [
             AutoCompleteWidget(
             u'data-selectable-multiple': 'true',
             u'data-selectable-position': position
         }
+        attrs.update(kwargs.get('attrs', {}))
         query_params = kwargs.pop('query_params', {})
         widgets = [
             AutoComboboxWidget(

selectable/tests/widgets.py

+import json
 from urlparse import urlparse
 
 from django import forms
         with self.assertRaises(TypeError):
             self.__class__.widget_cls(dotted_path)
 
+
 class AutoCompleteWidgetTestCase(BaseWidgetTestCase):
     widget_cls = widgets.AutoCompleteWidget
     lookup_cls = ThingLookup
         query = parse.query
         self.assertEqual(query, urlencode(params))
 
+    def test_build_selectable_options(self):
+        "Serialize selectable options as json in data attribute."
+        options = {'autoFocus': True}
+        widget = self.get_widget_instance(attrs={'data-selectable-options': options})
+        attrs = widget.build_attrs()
+        self.assertTrue('data-selectable-options' in attrs)
+        self.assertEqual(attrs['data-selectable-options'], json.dumps(options))
+
 
 class AutoCompleteSelectWidgetTestCase(BaseWidgetTestCase):
     widget_cls = widgets.AutoCompleteSelectWidget
         query = parse.query
         self.assertEqual(query, urlencode(params))
 
+    def test_build_selectable_options(self):
+        "Serialize selectable options as json in data attribute."
+        options = {'autoFocus': True}
+        widget = self.get_widget_instance(attrs={'data-selectable-options': options})
+        sub_widget = widget.widgets[0]
+        attrs = sub_widget.build_attrs()
+        self.assertTrue('data-selectable-options' in attrs)
+        self.assertEqual(attrs['data-selectable-options'], json.dumps(options))
+
 
 class AutoComboboxWidgetTestCase(BaseWidgetTestCase):
     widget_cls = widgets.AutoComboboxWidget
         query = parse.query
         self.assertEqual(query, urlencode(params))
 
+    def test_build_selectable_options(self):
+        "Serialize selectable options as json in data attribute."
+        options = {'autoFocus': True}
+        widget = self.get_widget_instance(attrs={'data-selectable-options': options})
+        attrs = widget.build_attrs()
+        self.assertTrue('data-selectable-options' in attrs)
+        self.assertEqual(attrs['data-selectable-options'], json.dumps(options))
+
 
 class AutoComboboxSelectWidgetTestCase(BaseWidgetTestCase):
     widget_cls = widgets.AutoComboboxSelectWidget
         query = parse.query
         self.assertEqual(query, urlencode(params))
 
+    def test_build_selectable_options(self):
+        "Serialize selectable options as json in data attribute."
+        options = {'autoFocus': True}
+        widget = self.get_widget_instance(attrs={'data-selectable-options': options})
+        sub_widget = widget.widgets[0]
+        attrs = sub_widget.build_attrs()
+        self.assertTrue('data-selectable-options' in attrs)
+        self.assertEqual(attrs['data-selectable-options'], json.dumps(options))
+
 
 class AutoCompleteSelectMultipleWidgetTestCase(BaseWidgetTestCase):
     widget_cls = widgets.AutoCompleteSelectMultipleWidget
         query = parse.query
         self.assertEqual(query, urlencode(params))
 
+    def test_build_selectable_options(self):
+        "Serialize selectable options as json in data attribute."
+        options = {'autoFocus': True}
+        widget = self.get_widget_instance(attrs={'data-selectable-options': options})
+        sub_widget = widget.widgets[0]
+        attrs = sub_widget.build_attrs()
+        self.assertTrue('data-selectable-options' in attrs)
+        self.assertEqual(attrs['data-selectable-options'], json.dumps(options))
+
 
 class AutoComboboxSelectMultipleWidgetTestCase(BaseWidgetTestCase):
     widget_cls = widgets.AutoComboboxSelectMultipleWidget
         query = parse.query
         self.assertEqual(query, urlencode(params))
 
+    def test_build_selectable_options(self):
+        "Serialize selectable options as json in data attribute."
+        options = {'autoFocus': True}
+        widget = self.get_widget_instance(attrs={'data-selectable-options': options})
+        sub_widget = widget.widgets[0]
+        attrs = sub_widget.build_attrs()
+        self.assertTrue('data-selectable-options' in attrs)
+        self.assertEqual(attrs['data-selectable-options'], json.dumps(options))