Anonymous avatar Anonymous committed d7bcea5

Added support for jQuery UI Autocomplete.
The old jQuery Autocomplete Plugin is still usable with the
"bassistance_autocomplete" function in the jquery_autocomplete.js.

Comments (0)

Files changed (1)

autocomplete/media/js/jquery_autocomplete.js

-function jquery_autocomplete(name, ac_url, force_selection) {
+function bassistance_autocomplete(name, ac_url, force_selection) {
     $(document).ready(function () {
         var input = $('#id_' + name);
         var hidden_input = $('#id_hidden_' + name);
     });
 }
 
+
+function jquery_autocomplete(name, ac_url, force_selection) {
+
+    this.name = name;
+    this.ac_url = ac_url;
+    this.force_selection = force_selection;
+
+    this.source = function (request, response) {
+        function success(data) {
+            var parsed = [];
+            for (var i in data) {
+                parsed[parsed.length] = {
+                    id: data[i][0],
+                    value: data[i][1],
+                    label: data[i][1],
+                };
+            }
+            response(parsed);
+        };
+        $.ajax({
+            url: this.ac_url,
+            dataType: "json",
+            data: {q: request.term},
+            success: success
+        });
+    };
+
+    this.select = function (event, ui) {
+        // set the hidden input field.
+        this.last_item = ui.item;
+        this.hidden_input.val(ui.item.id);
+    };
+
+    this.close = function (event, ui) {
+        alert(ui.toSource());  
+    };
+
+    this.setup = function () {
+        this.input = $("#id_" + this.name);
+        this.hidden_input = $("#id_hidden_" + this.name);
+        this.last_item = {};
+        this.input.autocomplete({
+            // minLength: 2,
+            source: jQuery.proxy(this.source, this),
+            select: jQuery.proxy(this.select, this),
+        });
+        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;
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.