Commits

tyrion  committed b5264af

Added jquery_autocomplete.js (thanks Patrick Guido Armino), not yet tested.

  • Participants
  • Parent commits db8bbb6

Comments (0)

Files changed (4)

File autocomplete/media/js/autocomplete.js

-
-function yui_autocomplete(name, ac_url, force_selection) {
-    YAHOO.util.Event.onDOMReady(function () {
-        var datasource = new YAHOO.util.XHRDataSource(ac_url);
-        datasource.responseType = YAHOO.util.XHRDataSource.TYPE_JSON;
-        datasource.responseSchema = {
-            resultsList: "result",
-            fields: ["label", "id"]
-        };
-
-        datasource.doBeforeParseData = function (request, original, callback) {
-            var parsed = {"result": []};
-            for (var i in original)
-                parsed.result.push({"id": original[i][0], "label": original[i][1]});
-            return parsed;
-        }
-        datasource.resultTypeList = false;
-
-        var input = document.getElementById("id_"+name);
-        var container = document.createElement("div");
-        YAHOO.util.Dom.insertAfter(container, input);
-        if (!YAHOO.util.Dom.hasClass(document.body, "yui-skin-sam"))
-            YAHOO.util.Dom.addClass(document.body, "yui-skin-sam");
-
-        var autocomplete = new YAHOO.widget.AutoComplete(input, container, datasource);
-        autocomplete.resultTypeList = false;
-        autocomplete.queryDelay = .5;
-        autocomplete.forceSelection = force_selection;
-
-        var selected_item = {label: null, id: null};
-        var hidden = document.getElementById("id_hidden_"+name)
-        autocomplete.itemSelectEvent.subscribe(function (type, args) {
-            selected_item = args[2];
-            hidden.value = selected_item.id;
-        });
-        form = document.getElementsByTagName("form")[0];
-        YAHOO.util.Event.addListener(form, "submit", function (event, form) {
-            if (selected_item.label != input.value && !force_selection)
-                hidden.value = input.value;
-        });
-    });
-}
-
-autocomplete = yui_autocomplete;

File autocomplete/media/js/jquery_autocomplete.js

+function jquery_autocomplete(name, ac_url, force_selection) {
+    $(document).ready(function () {
+        var input = $('#id_' + name);
+        var hidden_input = $('#id_hidden_' + name);
+        input.autocomplete(ac_url, {
+            limit: 10,
+            dataType: 'json',
+            parse: function(data) {
+                var parsed = [];
+                for (var i in data) {
+                    row = {
+                        data: data[i][1]+'|'+data[i][0],
+                        value: data[i][0],
+                        result: data[i][1]
+                    };
+                    parsed[parsed.length] = row;
+                }
+                return parsed;
+            },    
+            formatItem: function(data, i, total) {
+                return data.split('|')[0];
+            }
+        });
+        input.result(function(event, data, formatted) {
+            hidden_input.val(data.split('|')[1]);
+        });
+        form = $("form:first");
+        form.submit(function() {
+            if (hidden_input.val() != input.val() && !force_selection) {
+                hidden_input.val(input.val());
+            }
+        });
+    });
+}
+
+autocomplete = jquery_autocomplete;

File autocomplete/media/js/yui_autocomplete.js

+
+function yui_autocomplete(name, ac_url, force_selection) {
+    YAHOO.util.Event.onDOMReady(function () {
+        var datasource = new YAHOO.util.XHRDataSource(ac_url);
+        datasource.responseType = YAHOO.util.XHRDataSource.TYPE_JSON;
+        datasource.responseSchema = {
+            resultsList: "result",
+            fields: ["label", "id"]
+        };
+
+        datasource.doBeforeParseData = function (request, original, callback) {
+            var parsed = {"result": []};
+            for (var i in original)
+                parsed.result.push({"id": original[i][0], "label": original[i][1]});
+            return parsed;
+        }
+        datasource.resultTypeList = false;
+
+        var input = document.getElementById("id_"+name);
+        var container = document.createElement("div");
+        YAHOO.util.Dom.insertAfter(container, input);
+        if (!YAHOO.util.Dom.hasClass(document.body, "yui-skin-sam"))
+            YAHOO.util.Dom.addClass(document.body, "yui-skin-sam");
+
+        var autocomplete = new YAHOO.widget.AutoComplete(input, container, datasource);
+        autocomplete.resultTypeList = false;
+        autocomplete.queryDelay = .5;
+        autocomplete.forceSelection = force_selection;
+
+        var selected_item = {label: null, id: null};
+        var hidden = document.getElementById("id_hidden_"+name)
+        autocomplete.itemSelectEvent.subscribe(function (type, args) {
+            selected_item = args[2];
+            hidden.value = selected_item.id;
+        });
+        form = document.getElementsByTagName("form")[0];
+        YAHOO.util.Event.addListener(form, "submit", function (event, form) {
+            if (selected_item.label != input.value && !force_selection)
+                hidden.value = input.value;
+        });
+    });
+}
+
+autocomplete = yui_autocomplete;

File autocomplete/widgets.py

               '&2.6.0/build/connection/connection-min.js'
               '&2.6.0/build/datasource/datasource-min.js'
               '&2.6.0/build/autocomplete/autocomplete-min.js',
-              'js/autocomplete.js')
+              'js/yui_autocomplete.js')
 
     def __init__(self, ac_name, force_selection=True, reverse_label=True,
                  view_name='autocomplete', attrs=None):