Commits

Mark Lavin committed 53c3c8e

Backport fixes for double event and incorrect pagination.

Comments (0)

Files changed (2)

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

  * Docs: http://django-selectable.readthedocs.org/
  *
  * Depends:
- *   - jQuery 1.4.3+
+ *   - jQuery 1.4.4+
  *   - jQuery UI 1.8 widget factory
  *
  * Copyright 2010-2012, Mark Lavin
             );
         },
 
-        select: function(item) {
+        select: function(item, event) {
             /* Trigger selection of a given item.
-            Item should contain two properties: id and value */
+            Item should contain two properties: id and value 
+            Event is the original select event if there is one.
+            Event should not be passed if trigger manually.
+            */
             var self = this,
             input = this.element;
             $(input).removeClass('ui-state-error');
                 } else {
                     $(input).val(item.value);
                     var ui = {item: item};
-                    $(input).trigger('autocompleteselect', [ui ]);
+                    if (typeof(event) == 'undefined' || event.type != "autocompleteselect") {
+                        $(input).trigger('autocompleteselect', [ui ]);
+                    }
                 }
             }
         },
                     $(input).removeClass('ui-state-error');
                     if (ui.item && ui.item.page) {
                         // Set current page value
-                        $(input).data("page", ui.tem.page);
+                        $(input).data("page", ui.item.page);
                         $('.selectable-paginator', self.menu).remove();
                         // Search for next page of results
                         $(input).autocomplete("search");
                         return false;
                     }
-                    self.select(ui.item);
+                    self.select(ui.item, event);
                 }
             }).addClass("ui-widget ui-widget-content ui-corner-all");
             // Override the default auto-complete render.

selectable/tests/qunit/index.html

 <head>
     <title>Django Selectable Qunit Test Suite</title>
     <link rel="stylesheet" href="qunit.css" type="text/css" media="screen" />
-    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
-    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.13/jquery-ui.min.js"></script>
+    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
+    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/jquery-ui.min.js"></script>
     <script type="text/javascript" src="qunit.js"></script>
     <script>
         // Disable auto-binding of djselectable widgets to avoid test race conditions
             equal(hidden.val(), item.id, 'hidden input should get item id');
             equal(hidden.attr('title'), item.value, 'hidden input title should be item value');
         });
+
+        module("Events");
+        test("manual select", function() {
+            bindSelectables('#manual-event');
+            var input = $('#id_manualevent');
+            var count = 0;
+            input.bind('autocompleteselect', function(e, item) {
+                count = count + 1;
+            });
+            var item = {id: "1", value: 'foo'};
+            input.djselectable('select', item);
+            equal(count, 1, "autocompleteselect should fire once when manually selected.");
+        });
+        asyncTest("autocompleteselect", function() {
+            expect(1);
+            bindSelectables('#select-event');
+            var input = $('#id_selectevent');
+            var count = 0;
+            input.bind('autocompleteselect', function(e, item) {
+                count = count + 1;
+            });
+            // Change source to prevent remote calls
+            input.autocomplete("option", "source", ["javascript", "python"]);
+            // Simulate typing in and selecting the first item.
+            input.val("ja").keydown();
+            setTimeout(function() {
+                var down = jQuery.Event("keydown");
+                down.keyCode = $.ui.keyCode.DOWN;
+                input.trigger(down);
+                var enter = jQuery.Event("keydown");
+                enter.keyCode = $.ui.keyCode.ENTER;
+                input.trigger(enter);
+		        equal(count, 1, "autocompleteselect should only fire once.");
+                start();
+	        }, 500);
+        });
     });
     </script>
 </head>
             <input data-selectable-multiple="true" name="multicombo-initial_0" data-selectable-type="combobox" data-selectable-position="bottom" data-selectable-allow-new="false" data-selectable-url="/_/core-fruitlookup/" type="text" id="id_multicombo-initial_0" />
             <input name="multicombo-initial_1" title="Initial" value="0" data-selectable-type="hidden-multiple" type="hidden" id="id_multicombo-initial_1_0">
         </div>
+        <div id="manual-event">
+            <input name="manualevent" data-selectable-type="text" data-selectable-allow-new="false" data-selectable-url="/_/core-fruitlookup/" type="text" id="id_manualevent" />
+        </div>
+        <div id="select-event">
+            <input name="selectevent" data-selectable-type="text" data-selectable-allow-new="false" data-selectable-url="/_/core-fruitlookup/" type="text" id="id_selectevent" />
+        </div>
     </div>
 </body>
 </html>
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.