Source

django-selectable-akaihola / issue-72

# HG changeset patch
# Parent b7217d6a88f7846d0bfcb391d52b92116e1c5b3d

diff -r b7217d6a88f7 selectable/static/selectable/js/jquery.dj.selectable.js
--- a/selectable/static/selectable/js/jquery.dj.selectable.js	Thu Sep 20 20:57:57 2012 -0400
+++ b/selectable/static/selectable/js/jquery.dj.selectable.js	Tue Sep 25 11:19:58 2012 +0300
@@ -69,7 +69,7 @@
 
         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.
             */
@@ -145,7 +145,7 @@
                         });
                     }
                     return response(results);
-                }   
+                }
 				$.getJSON(url, query, unwrapResponse);
             }
             // Create base auto-complete lookup
@@ -255,86 +255,87 @@
             }
         }
 	});
+
+        window.bindSelectables = function(context) {
+            /* Bind all selectable widgets in a given context.
+            Automatically called on document.ready.
+            Additional calls can be made for dynamically added widgets.
+            */
+            $(":input[data-selectable-type=text]", context).djselectable();
+            $(":input[data-selectable-type=combobox]", context).djselectable();
+            $(":input[data-selectable-type=hidden]", context).each(function(i, elem) {
+                var hiddenName = $(elem).attr('name');
+                var textName = hiddenName.replace('_1', '_0');
+                $(":input[name=" + textName + "][data-selectable-url]").bind(
+                    "autocompletechange autocompleteselect",
+                    function(event, ui) {
+                        if (ui.item && ui.item.id) {
+                            $(elem).val(ui.item.id);
+                        } else {
+                            $(elem).val("");
+                        }
+                    }
+                );
+            });
+        };
+
+        /* Monkey-patch Django's dynamic formset, if defined */
+        if (typeof(django) !== "undefined" && typeof(django.jQuery) !== "undefined") {
+            if (django.jQuery.fn.formset) {
+                var oldformset = django.jQuery.fn.formset;
+                django.jQuery.fn.formset = function(opts) {
+                    var options = $.extend({}, opts);
+                    var addedevent = function(row) {
+                        bindSelectables($(row));
+                    };
+                    var added = null;
+                    if (options.added) {
+                        // Wrap previous added function and include call to bindSelectables
+                        var oldadded = options.added;
+                        added = function(row) { oldadded(row); addedevent(row); };
+                    }
+                    options.added = added || addedevent;
+                    return oldformset.call(this, options);
+                };
+            }
+        }
+
+        /* Monkey-patch Django's dismissAddAnotherPopup(), if defined */
+        if (typeof(dismissAddAnotherPopup) !== "undefined" && typeof(windowname_to_id) !== "undefined" && typeof(html_unescape) !== "undefined") {
+            var django_dismissAddAnotherPopup = dismissAddAnotherPopup;
+            dismissAddAnotherPopup = function(win, newId, newRepr) {
+                /* See if the popup came from a selectable field.
+                   If not, pass control to Django's code.
+                   If so, handle it. */
+                var fieldName = windowname_to_id(win.name); /* e.g. "id_fieldname" */
+                var field = $('#' + fieldName);
+                var multiField = $('#' + fieldName + '_0');
+                /* Check for bound selectable */
+                var singleWidget = field.data('djselectable');
+                var multiWidget = multiField.data('djselectable');
+                if (singleWidget || multiWidget) {
+                    // newId and newRepr are expected to have previously been escaped by
+                    // django.utils.html.escape.
+                    var item =  {
+                        id: html_unescape(newId),
+                        value: html_unescape(newRepr)
+                    };
+                    if (singleWidget) {
+                        field.djselectable('select', item);
+                    }
+                    if (multiWidget) {
+                        multiField.djselectable('select', item);
+                    }
+                    win.close();
+                } else {
+                    /* Not ours, pass on to original function. */
+                    return django_dismissAddAnotherPopup(win, newId, newRepr);
+                }
+            };
+        }
+
 })(jQuery);
 
-function bindSelectables(context) {
-    /* Bind all selectable widgets in a given context.
-    Automatically called on document.ready.
-    Additional calls can be made for dynamically added widgets.
-    */
-    $(":input[data-selectable-type=text]", context).djselectable();
-    $(":input[data-selectable-type=combobox]", context).djselectable();
-    $(":input[data-selectable-type=hidden]", context).each(function(i, elem) {
-        var hiddenName = $(elem).attr('name');
-        var textName = hiddenName.replace('_1', '_0');
-        $(":input[name=" + textName + "][data-selectable-url]").bind(
-            "autocompletechange autocompleteselect",
-            function(event, ui) {
-                if (ui.item && ui.item.id) {
-                    $(elem).val(ui.item.id);
-                } else {
-                    $(elem).val("");
-                }
-            }
-        );
-    });
-}
-
-/* Monkey-patch Django's dynamic formset, if defined */
-if (typeof(django) !== "undefined" && typeof(django.jQuery) !== "undefined") {
-    if (django.jQuery.fn.formset) {
-        var oldformset = django.jQuery.fn.formset;
-        django.jQuery.fn.formset = function(opts) {
-            var options = $.extend({}, opts);
-            var addedevent = function(row) {
-                bindSelectables($(row));
-            };
-            var added = null;
-            if (options.added) {
-                // Wrap previous added function and include call to bindSelectables
-                var oldadded = options.added;
-                added = function(row) { oldadded(row); addedevent(row); };
-            }
-            options.added = added || addedevent;
-            return oldformset.call(this, options);
-        };
-    }
-}
-
-/* Monkey-patch Django's dismissAddAnotherPopup(), if defined */
-if (typeof(dismissAddAnotherPopup) !== "undefined" && typeof(windowname_to_id) !== "undefined" && typeof(html_unescape) !== "undefined") {
-    var django_dismissAddAnotherPopup = dismissAddAnotherPopup;
-    dismissAddAnotherPopup = function(win, newId, newRepr) {
-        /* See if the popup came from a selectable field.
-           If not, pass control to Django's code.
-           If so, handle it. */
-        var fieldName = windowname_to_id(win.name); /* e.g. "id_fieldname" */
-        var field = $('#' + fieldName);
-        var multiField = $('#' + fieldName + '_0');
-        /* Check for bound selectable */
-        var singleWidget = field.data('djselectable');
-        var multiWidget = multiField.data('djselectable');
-        if (singleWidget || multiWidget) {
-            // newId and newRepr are expected to have previously been escaped by
-            // django.utils.html.escape.
-            var item =  {
-                id: html_unescape(newId),
-                value: html_unescape(newRepr)
-            };
-            if (singleWidget) {
-                field.djselectable('select', item);
-            }
-            if (multiWidget) {
-                multiField.djselectable('select', item);
-            }
-            win.close();
-        } else {
-            /* Not ours, pass on to original function. */
-            return django_dismissAddAnotherPopup(win, newId, newRepr);
-        }
-    };
-}
-
 $(document).ready(function() {
     // Bind existing widgets on document ready
     if (typeof(djselectableAutoLoad) === "undefined" || djselectableAutoLoad) {