Commits

Anonymous committed 553ab7a

Fixed keyboard behaviour of the Javascript ui.
Now the input gets cleared correctly on select.

Comments (0)

Files changed (1)

django/contrib/admin/media/js/admin/autocomplete.js

         var self = this;
         this.hidden_input = this.element.prev( "input[type=hidden]" );
         this.name = this.hidden_input.attr( "name" );
-        if ( this.options.m2m ) {
-            this.values = this.hidden_input.val() ?
-                this.hidden_input.val().split( "," ) :
-                [];
-            this.values_ul = this.element.next( "ul" );
-            this.lastSelected = { id: null, value: null };
-            if ( this.values.length && this.values_ul[0] ) {
-                this.values_ul.children().each(function(i) {
-                    $(this)
-                        .addClass( "ui-autocomplete-value" )
-                        .data( "value.autocomplete", self.values[i] )
-                        .append( '<a href="#">x</a>' );
-                });
-            } else {
-                this.values_ul = $( "<ul />" ).insertAfter( this.element );
-            }
-            this.values_ul.addClass( "ui-autocomplete-values" );
-            $( ".ui-autocomplete-value a", this.values_ul[0] ).live( "click", function() {
-                var span = $(this).parent();
-                var id = span.data( "value.autocomplete" );
-                $.each( self.values, function (i, v) {
-                    if (v === id) self.values.splice(i, 1);
-                });
-                span.remove();
-            });
-        } else {
-            this.lastSelected = {
-                id: this.hidden_input.val(),
-                value: this.element.val(),
-            };
-        }
         this.element.autocomplete({
             select: function( event, ui ) {
                 self.lastSelected = ui.item;
             },
         });
         this._initSource();
+        if ( this.options.m2m ) {
+            this._initManyToMany()
+        } else {
+            this.lastSelected = {
+                id: this.hidden_input.val(),
+                value: this.element.val(),
+            };
+        }
         // Implements "force selection".
         this.element.focusout(function() {
-            if ( self.element.val() != self.lastSelected.value ) {
+            if ( self.element.val() !== self.lastSelected.value ) {
                 self.element.val( "" );
             }
         });
             this.options.source;
         this.element.autocomplete( "option", "source", source );
     },
+
+    _initManyToMany: function() {
+        var self = this;
+        this.element.bind( "autocompleteclose", function( event, ui ) {
+            self.element.val( "" );
+        });
+        this.values = this.hidden_input.val() ?
+            this.hidden_input.val().split( "," ) :
+            [];
+        this.values_ul = this.element.next( "ul" );
+        this.lastSelected = { id: null, value: null };
+        if ( this.values.length && this.values_ul[0] ) {
+            this.values_ul.children().each(function(i) {
+                $(this)
+                    .addClass( "ui-autocomplete-value" )
+                    .data( "value.autocomplete", self.values[i] )
+                    .append( '<a href="#">x</a>' );
+            });
+        } else {
+            this.values_ul = $( "<ul />" ).insertAfter( this.element );
+        }
+        this.values_ul.addClass( "ui-autocomplete-values" );
+        $( ".ui-autocomplete-value a", this.values_ul[0] ).live( "click", function() {
+            var span = $(this).parent();
+            var id = span.data( "value.autocomplete" );
+            $.each( self.values, function (i, v) {
+                if (v === id) self.values.splice(i, 1);
+            });
+            span.remove();
+        });
+    },
 });
 
 $(function() {