Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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

…s present.
  • Loading branch information...
commit 267b1fea2518b2a39c35708b21b39ebeb069b0ee 1 parent 499bcd8
Mart Somermaa authored
View
36 django/contrib/admin/media/js/admin/RelatedObjectLookups.js
@@ -36,6 +36,34 @@ function windowname_to_id(text) {
return text;
}
+function getAdminMediaPrefix() {
+ // Deduce admin_media_prefix by looking at the <script>s in the
+ // current document and finding the URL of *this* module.
+ // Copy-paste from DateTimeShortcuts.js, makes sense as a
+ // separate function in core.js.
+ var scripts = document.getElementsByTagName('script');
+
+ for (var i=0; i < scripts.length; i++) {
+ if (scripts[i].src.match(/RelatedObjectLookups/)) {
+ var idx = scripts[i].src.indexOf('js/admin/RelatedObjectLookups');
+ return scripts[i].src.substring(0, idx);
+ }
+ }
+ // poor man's assert
+ alert('This line is unreachable. Please file a bug if you see this message.');
+}
+
+var CLEAR_RAW_ID = '<a href="#" onclick="return clearRawId(this);">' +
+'<img src="' + getAdminMediaPrefix() + 'img/admin/icon_deletelink.gif" ' +
+'width="10" height="10" alt="Clear" title="Clear" /></a>';
+
+// FIXME: the following produce 'gettext is not defined' errors in FireBug.
+// Needs to be tracked down.
+// (jsi18n is generally included before this in admin templates)
+//
+// 'width="10" height="10" alt="' + gettext('Clear') + '" title="' +
+// gettext('Clear') + '" /></a>';
+
function showRelatedObjectPopup(triggeringLink) {
var name = triggeringLink.parentNode.id.replace(/^view_lookup_/, '');
name = id_to_windowname(name);
@@ -62,7 +90,7 @@ function dismissRelatedLookupPopup(win, chosenId, chosenIdHref, chosenName) {
if (nameElem) {
nameElem.innerHTML = '<a href="' + chosenIdHref + '" ' +
'onclick="return showRelatedObjectPopup(this);">' +
- html_escape(chosenName) + '</a>';
+ html_escape(chosenName) + '</a> ' + CLEAR_RAW_ID;
}
win.close();
@@ -100,7 +128,7 @@ function dismissAddAnotherPopup(win, newId, newRepr) {
'/' + newId + '/');
nameElem.innerHTML = '<a href="' + chosenIdHref + '" ' +
'onclick="return showRelatedObjectPopup(this);">' +
- newRepr_escaped + '</a>';
+ newRepr_escaped + '</a> ' + CLEAR_RAW_ID;
}
}
} else {
@@ -114,8 +142,8 @@ function dismissAddAnotherPopup(win, newId, newRepr) {
}
function clearRawId(triggeringLink) {
- triggeringLink.previousSibling.previousSibling.value = '';
- triggeringLink.nextSibling.nextSibling.nextSibling.nextSibling.innerHTML = '';
+ triggeringLink.parentNode.previousSibling.previousSibling.previousSibling.previousSibling.value = '';
+ triggeringLink.parentNode.innerHTML = '';
return false;
}
View
16 django/contrib/admin/widgets.py
@@ -108,8 +108,6 @@ def __init__(self, rel, attrs=None, using=None):
def render(self, name, value, attrs=None):
if attrs is None:
attrs = {}
- # it would make sense to bind admin_site to ForeignKeyRawIdWidget as
- # well (see RelatedFieldWidgetWrapper.__init__) for proper URL reversing
related_url = get_related_url(self.rel.to)
params = self.url_parameters()
if params:
@@ -121,7 +119,6 @@ def render(self, name, value, attrs=None):
output = [super(ForeignKeyRawIdWidget, self).render(name, value, attrs)]
# TODO: "id_" is hard-coded here. This should instead use the correct
# API to determine the ID dynamically.
- 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')})
output.append(' <a href="%s%s" class="related-lookup" id="lookup_id_%s" onclick="return showRelatedObjectLookupPopup(this);"> ' % \
(related_url, url, name))
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):
obj = self.rel.to._default_manager.using(
self.db).get(**{key: value})
related_url = get_related_url(obj, obj.pk)
- return (' <strong id="%s"><a href="%s" '
- 'onclick="return showRelatedObjectPopup(this);">%s</a>'
- '</strong>' % (name, related_url, obj_label(obj)))
+ return (' <strong id="%(name)s"><a href="%(url)s" '
+ 'onclick="return showRelatedObjectPopup(this);">%(label)s</a> '
+ '<a href="#" onclick="return clearRawId(this);">'
+ '<img src="%(prefix)simg/admin/icon_deletelink.gif" '
+ 'width="10" height="10" alt="%(clear)s" title="%(clear)s" />'
+ '</a></strong>' % {'name': name, 'url': related_url,
+ 'label': obj_label(obj),
+ 'prefix': settings.ADMIN_MEDIA_PREFIX,
+ 'clear': _("Clear"),}
+ )
else:
# a placeholder that will be filled in
# JavaScript dismissRelatedLookupPopup()
Please sign in to comment.
Something went wrong with that request. Please try again.