Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Update clearing - the red clearing button only appears if an object i…

…s present.
  • Loading branch information...
commit 267b1fea2518b2a39c35708b21b39ebeb069b0ee 1 parent 499bcd8
authored January 18, 2010
36  django/contrib/admin/media/js/admin/RelatedObjectLookups.js
@@ -36,6 +36,34 @@ function windowname_to_id(text) {
36 36
     return text;
37 37
 }
38 38
 
  39
+function getAdminMediaPrefix() {
  40
+    // Deduce admin_media_prefix by looking at the <script>s in the
  41
+    // current document and finding the URL of *this* module.
  42
+    // Copy-paste from DateTimeShortcuts.js, makes sense as a
  43
+    // separate function in core.js.
  44
+    var scripts = document.getElementsByTagName('script');
  45
+
  46
+    for (var i=0; i < scripts.length; i++) {
  47
+        if (scripts[i].src.match(/RelatedObjectLookups/)) {
  48
+            var idx = scripts[i].src.indexOf('js/admin/RelatedObjectLookups');
  49
+            return scripts[i].src.substring(0, idx);
  50
+        }
  51
+    }
  52
+    // poor man's assert
  53
+    alert('This line is unreachable. Please file a bug if you see this message.');
  54
+}
  55
+
  56
+var CLEAR_RAW_ID = '<a href="#" onclick="return clearRawId(this);">' +
  57
+'<img src="' + getAdminMediaPrefix() + 'img/admin/icon_deletelink.gif" ' +
  58
+'width="10" height="10" alt="Clear" title="Clear" /></a>';
  59
+
  60
+// FIXME: the following produce 'gettext is not defined' errors in FireBug.
  61
+// Needs to be tracked down.
  62
+// (jsi18n is generally included before this in admin templates)
  63
+//
  64
+// 'width="10" height="10" alt="' + gettext('Clear') + '" title="' +
  65
+// gettext('Clear') + '" /></a>';
  66
+
39 67
 function showRelatedObjectPopup(triggeringLink) {
40 68
     var name = triggeringLink.parentNode.id.replace(/^view_lookup_/, '');
41 69
     name = id_to_windowname(name);
@@ -62,7 +90,7 @@ function dismissRelatedLookupPopup(win, chosenId, chosenIdHref, chosenName) {
62 90
     if (nameElem) {
63 91
       nameElem.innerHTML = '<a href="' + chosenIdHref + '" ' +
64 92
        'onclick="return showRelatedObjectPopup(this);">' +
65  
-        html_escape(chosenName) + '</a>';
  93
+        html_escape(chosenName) + '</a> ' + CLEAR_RAW_ID;
66 94
     }
67 95
 
68 96
     win.close();
@@ -100,7 +128,7 @@ function dismissAddAnotherPopup(win, newId, newRepr) {
100 128
                     '/' + newId + '/');
101 129
                 nameElem.innerHTML = '<a href="' + chosenIdHref + '" ' +
102 130
                   'onclick="return showRelatedObjectPopup(this);">' +
103  
-                  newRepr_escaped + '</a>';
  131
+                  newRepr_escaped + '</a> ' + CLEAR_RAW_ID;
104 132
             }
105 133
         }
106 134
     } else {
@@ -114,8 +142,8 @@ function dismissAddAnotherPopup(win, newId, newRepr) {
114 142
 }
115 143
 
116 144
 function clearRawId(triggeringLink) {
117  
-    triggeringLink.previousSibling.previousSibling.value = '';
118  
-    triggeringLink.nextSibling.nextSibling.nextSibling.nextSibling.innerHTML = '';
  145
+    triggeringLink.parentNode.previousSibling.previousSibling.previousSibling.previousSibling.value = '';
  146
+    triggeringLink.parentNode.innerHTML = '';
119 147
     return false;
120 148
 }
121 149
 
16  django/contrib/admin/widgets.py
@@ -108,8 +108,6 @@ def __init__(self, rel, attrs=None, using=None):
108 108
     def render(self, name, value, attrs=None):
109 109
         if attrs is None:
110 110
             attrs = {}
111  
-        # it would make sense to bind admin_site to ForeignKeyRawIdWidget as
112  
-        # well (see RelatedFieldWidgetWrapper.__init__) for proper URL reversing
113 111
         related_url = get_related_url(self.rel.to)
114 112
         params = self.url_parameters()
115 113
         if params:
@@ -121,7 +119,6 @@ def render(self, name, value, attrs=None):
121 119
         output = [super(ForeignKeyRawIdWidget, self).render(name, value, attrs)]
122 120
         # TODO: "id_" is hard-coded here. This should instead use the correct
123 121
         # API to determine the ID dynamically.
124  
-        output.append(' <a href="#" onclick="return clearRawId(this);"><img src="%(prefix)simg/admin/icon_deletelink.gif" width="10" height="10" alt="%(title)s" title="%(title)s" /></a>' % {'prefix': settings.ADMIN_MEDIA_PREFIX, 'title': _('Clear')})
125 122
         output.append(' <a href="%s%s" class="related-lookup" id="lookup_id_%s" onclick="return showRelatedObjectLookupPopup(this);"> ' % \
126 123
             (related_url, url, name))
127 124
         output.append('<img src="%(prefix)simg/admin/selector-search.gif" width="16" height="16" alt="%(title)s" title="%(title)s" /></a>' % {'prefix': settings.ADMIN_MEDIA_PREFIX, 'title': _('Lookup')})
@@ -153,9 +150,16 @@ def label_for_value(self, value, name):
153 150
             obj = self.rel.to._default_manager.using(
154 151
                     self.db).get(**{key: value})
155 152
             related_url = get_related_url(obj, obj.pk)
156  
-            return (' <strong id="%s"><a href="%s" '
157  
-                    'onclick="return showRelatedObjectPopup(this);">%s</a>'
158  
-                    '</strong>' % (name, related_url, obj_label(obj)))
  153
+            return (' <strong id="%(name)s"><a href="%(url)s" '
  154
+                    'onclick="return showRelatedObjectPopup(this);">%(label)s</a> '
  155
+                    '<a href="#" onclick="return clearRawId(this);">'
  156
+                    '<img src="%(prefix)simg/admin/icon_deletelink.gif" '
  157
+                    'width="10" height="10" alt="%(clear)s" title="%(clear)s" />'
  158
+                    '</a></strong>' % {'name': name, 'url': related_url,
  159
+                        'label': obj_label(obj),
  160
+                        'prefix': settings.ADMIN_MEDIA_PREFIX,
  161
+                        'clear': _("Clear"),}
  162
+                    )
159 163
         else:
160 164
             # a placeholder that will be filled in
161 165
             # JavaScript dismissRelatedLookupPopup()

0 notes on commit 267b1fe

Please sign in to comment.
Something went wrong with that request. Please try again.