Mark Lavin avatar Mark Lavin committed 45cd0e9

Reorganize JS.

Comments (0)

Files changed (2)

selectable/forms/widgets.py

         # New attrs
         url = lookup_class.url()
         self.attrs[u'data-selectable-url'] = url
+        self.attrs[u'data-selectable-type'] = 'text'
         self.attrs[u'data-selectable-allow-new'] = str(self.allow_new).lower()
 
 
         self.allow_new = kwargs.pop('allow_new', False)
         widgets = [
             AutoCompleteWidget(lookup_class, allow_new=self.allow_new),
-            forms.HiddenInput(attrs={u'data-selectable-is-hidden': 'true'})
+            forms.HiddenInput(attrs={u'data-selectable-type': 'hidden'})
         ]
         super(AutoCompleteSelectWidget, self).__init__(widgets, *args, **kwargs)
 
         self.allow_new = kwargs.pop('allow_new', False)
         widgets = [
             AutoComboboxWidget(lookup_class, allow_new=self.allow_new),
-            forms.HiddenInput(attrs={u'data-selectable-is-hidden': 'true'})
+            forms.HiddenInput(attrs={u'data-selectable-type': 'hidden'})
         ]
         super(AutoComboboxSelectWidget, self).__init__(widgets, *args, **kwargs)
 

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

+(function($) {
+	$.widget("ui.djselectable", {
+        _create: function() {
+            var self = $(this),
+            input = this.element;
+            type = $(input).attr('data-selectable-type');
+            url = $(input).attr('data-selectable-url');
+            
+            var allowAttr = $(input).attr('data-selectable-allow-new');
+            var allowNew = false;
+            if (typeof allowAttr !== 'undefined' && allowAttr === 'true') {
+                allowNew = true;
+            }
+            if (type === 'combobox' || type === 'text') {
+                $(input).autocomplete({
+                    source: url,
+                    change: function(event, ui) {
+                        if (!ui.item) {
+                            if (!allowNew) {
+                                $(input).val("");
+				                $(input).data("autocomplete").term = "";
+				                return false;
+                            }
+                        }
+                    }
+                });
+                if (type === 'combobox') {
+                    // Change auto-complete options
+                    $(input).autocomplete("option", {
+                        delay: 0,
+                        minLength: 0
+                    });
+
+                    $("<button>&nbsp;</button>").attr("tabIndex", -1).attr("title", "Show All Items")
+                    .insertAfter($(input))
+                    .button({
+                        icons: {
+                            primary: "ui-icon-triangle-1-s"
+                        },
+                        text: false
+                    })
+                    .click(function() {
+                        // close if already visible
+                        if ($(input).autocomplete("widget").is(":visible")) {
+                            $(input).autocomplete("close");
+                            return false;
+                        }
+
+                        // pass empty string as value to search for, displaying all results
+                        $(input).autocomplete("search", "");
+                        $(input).focus();
+                        return false;
+                    });
+                }
+            } else if (type === 'hidden') {
+                var hiddenName = $(input).attr('name');
+                var textName = hiddenName.replace('_1', '_0');
+                $(":input[name=" + textName + "][data-selectable-url]").bind("autocompletechange", function(event, ui) {
+                    if (ui.item && ui.item.id) {
+                        $(input).val(ui.item.id);
+                    } else {
+                        $(input).val("");
+                    }
+                });
+            }
+        }
+	});
+
+})(jQuery);
+
 $(document).ready(function() {
-    $(":input[data-selectable-url]").each(function(i, elem) {
-        var url = $(elem).attr('data-selectable-url');
-        $(elem).autocomplete({
-            source: url,
-            change: function(event, ui) {
-                var allowAttr = $(this).attr('data-selectable-allow-new');
-                var allowNew = false;
-                if (typeof allowAttr !== 'undefined' && allowAttr === 'true') {
-                    allowNew = true;
-                }
-                if (!ui.item) {
-                    if (!allowNew) {
-                        $(this).val("");
-					    $(this).data("autocomplete").term = "";
-					    return false;
-                    }
-                }
-            }
-        });
-        if ($(elem).attr('data-selectable-type') === 'combobox') {
-            // Change auto-complete options
-            $(elem).autocomplete("option", {
-                delay: 0,
-                minLength: 0
-            });
-
-            $("<button>&nbsp;</button>").attr("tabIndex", -1).attr("title", "Show All Items")
-            .insertAfter($(elem))
-            .button({
-                icons: {
-                    primary: "ui-icon-triangle-1-s"
-                },
-                text: false
-            })
-            .click(function() {
-                // close if already visible
-                if ($(elem).autocomplete("widget").is(":visible")) {
-                    $(elem).autocomplete("close");
-                    return false;
-                }
-
-                // pass empty string as value to search for, displaying all results
-                $(elem).autocomplete("search", "");
-                $(elem).focus();
-                return false;
-            });
-        }
-    });
-    $(":input[data-selectable-is-hidden]").each(function(i, elem) {
-        var hiddenName = $(elem).attr('name');
-        var textName = hiddenName.replace('_1', '_0');
-        $(":input[name=" + textName + "][data-selectable-url]").bind("autocompletechange", function(event, ui) {
-            if (ui.item && ui.item.id) {
-                $(elem).val(ui.item.id);
-            } else {
-                $(elem).val("");
-            }
-        });
+    $(":input[data-selectable-type]").each(function(i, elem) {
+        $(elem).djselectable();
     });
 });
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.