Commits

Anonymous committed 2e52cbf

uncapitalized autocompletes and better jquery_ui_autocomplete integ

Comments (0)

Files changed (2)

autocomplete/media/js/jquery_autocomplete.js

                 parsed[parsed.length] = {
                     id: data[i][0],
                     value: data[i][1],
-                    label: data[i][1],
+                    label: data[i][1]
                 };
             }
             response(parsed);
 
     this.select = function (event, ui) {
         // set the hidden input field.
-        this.last_item = ui.item;
+        // this.last_item = ui.item;
         this.hidden_input.val(ui.item.id);
     };
 
         alert(ui.toSource());  
     };
 
+    this.change = function (event, ui) {
+	if (this.force_selection){
+            if (!ui.item) {
+		// remove invalid value, as it didn't match anything
+		this.input.val("");
+		this.hidden_input.val("");
+		return false;
+            }
+	}	
+	return true;	
+
+    };
+
+    // adapted from the select first script, reduces runtime ++    
+    this.open = function(event, ui){
+	menu  = $(this.input).data("autocomplete").menu;
+	menu.activate( $.Event({ type: "mouseenter" }), menu.element.children().first() );
+    };
+
     this.setup = function () {
         this.input = $("#id_" + this.name);
         this.hidden_input = $("#id_hidden_" + this.name);
         this.last_item = {};
-        this.input.autocomplete({
+        this.input.autocomplete(
+	    {
             // minLength: 2,
-            source: jQuery.proxy(this.source, this),
-            select: jQuery.proxy(this.select, this),
+		source: jQuery.proxy(this.source, this),
+		select: jQuery.proxy(this.select, this),
+		change: jQuery.proxy(this.change, this),
+		open: jQuery.proxy(this.open, this),
+		// always select first
+		selectFirst: true
         });
-        this.input.closest("form").submit(jQuery.proxy(function () {
-            if ((!this.input.val()) || (this.hidden_input.val() != this.input.val()
-                && !this.force_selection)) {
-                this.hidden_input.val(this.input.val());
-            }
-        }, this));
-        if (this.force_selection) {
-            this.input.focusout(jQuery.proxy(function (event) {
-                if (this.input.val() != this.last_item.value)
-                    this.input.val("");
-            }, this));
-        }
+
     };
 
     $(document).ready(jQuery.proxy(this.setup, this));
 };
 
 autocomplete = jquery_autocomplete;
+

autocomplete/views.py

         if not ac_name in self.settings:
             return self.not_found(request, ac_name)
 
-        qs, fields, limit, key, label, auth = self.settings[ac_name]
+        qs, fields, limit, key, label, auth, case_sensitive = self.settings[ac_name]
         if auth and not request.user.is_authenticated():
             return self.forbidden(request, ac_name)
         query = request.GET.get(query_param, '')
         
+        if case_sensitive:
+            filter_string = '%s__startswith'
+        else:
+            filter_string = '%s__istartswith'
+
         filter = Q()
         for field in fields:
             if not '__' in field:
-                field = '%s__startswith' % field
+                field = filter_string % field
             filter |= Q(**{field: query})
         
         qs = qs.filter(filter)[:limit]
                 mimetype='application/json')
 
     def register(self, id, queryset, fields, limit=None, key='pk',
-                 label=lambda obj: smart_unicode(obj), auth=False):
-        self.settings[id] = (queryset, fields, limit, key, label, auth)
+                 label=lambda obj: smart_unicode(obj), auth=False, case_sensitive=False):
+        self.settings[id] = (queryset, fields, limit, key, label, auth, case_sensitive)
 
     def not_found(self, request, ac_name):
         return HttpResponse(status=404)