Commits

Mark Lavin  committed cd58870

Started combobox implementations.

  • Participants
  • Parent commits 68498d3

Comments (0)

Files changed (2)

File selectable/forms/widgets.py

 __all__ = (
     'AutoCompleteWidget',
     'AutoCompleteSelectWidget',
+    'AutoComboboxWidget',
+    'AutoComboboxSelectWidget'
 )
 
 
             return [item_value, value]
         return [None, None]
 
+
+class AutoComboboxWidget(AutoCompleteWidget):
+    
+    def __init__(self, lookup_class, *args, **kwargs):
+        super(AutoComboboxWidget, self).__init__(lookup_class, *args, **kwargs)
+        self.attrs[u'data-selectable-type'] = 'combobox'
+
+
+class AutoComboboxSelectWidget(forms.MultiWidget):
+
+    def __init__(self, lookup_class, *args, **kwargs):
+        self.lookup_class = lookup_class
+        self.allow_new = kwargs.pop('allow_new', False)
+        widgets = [
+            AutoComboboxWidget(lookup_class, allow_new=self.allow_new),
+            forms.HiddenInput(attrs={u'data-selectable-is-hidden': 'true'})
+        ]
+        super(AutoComboboxSelectWidget, self).__init__(widgets, *args, **kwargs)
+
+    def decompress(self, value):
+        if value:
+            lookup = self.lookup_class()
+            item = lookup.get_item(value)
+            item_value = lookup.get_item_value(item)
+            return [item_value, value]
+        return [None, None]
+
+

File selectable/static/js/jquery.dj.selectable.js

                 }
             }
         });
+        if ($(elem).attr('data-selectable-type') === 'combobox') {
+            // Change auto-complete options
+            $(elem).autocomplete("option", {
+                delay: 0,
+                minLength: 0
+            });
+
+            $("<button>&nbsp;</button>").attr("tabIndex", -1).attr("title", "Show All Items")
+            .insertAfter($(elem))
+            .button({
+                icons: {
+                    primary: "ui-icon-triangle-1-s"
+                },
+                text: false
+            })
+            .click(function() {
+                console.log('Click!');
+                // close if already visible
+                if ($(elem).autocomplete("widget").is(":visible")) {
+                    $(elem).autocomplete("close");
+                    return false;
+                }
+
+                // pass empty string as value to search for, displaying all results
+                $(elem).autocomplete("search", "");
+                $(elem).focus();
+                return false;
+            });
+        }
     });
     $(":input[data-selectable-is-hidden]").each(function(i, elem) {
         var hiddenName = $(elem).attr('name');