Ticket #11397: add_edit_icon_r15922.diff
File add_edit_icon_r15922.diff, 9.8 KB (added by , 14 years ago) |
---|
-
django/contrib/admin/media/js/admin/RelatedObjectLookups.js
55 55 function showAddAnotherPopup(triggeringLink) { 56 56 var name = triggeringLink.id.replace(/^add_/, ''); 57 57 name = id_to_windowname(name); 58 href = triggeringLink.href 58 href = triggeringLink.href; 59 59 if (href.indexOf('?') == -1) { 60 60 href += '?_popup=1'; 61 61 } else { … … 66 66 return false; 67 67 } 68 68 69 function dismissAddAnotherPopup(win, newId, newRepr) { 69 // To show edit popup window of selected item. 70 function showChangeCurrentPopup(triggeringLink, element_id) { 71 element = document.getElementById(element_id); 72 var name = triggeringLink.id.replace(/^change_/, ''); 73 name = id_to_windowname(name); 74 href = triggeringLink.href; 75 76 if (href.indexOf('?') == -1) { 77 href += '?_popup=1'; 78 } else { 79 href += '&_popup=1'; 80 } 81 var win = window.open(href, name, 'height=500,width=800,resizable=yes,scrollbars=yes'); 82 win.focus(); 83 return false; 84 } 85 86 function dismissAddAnotherPopup(win, newId, newRepr, triggeringLink) { 70 87 // newId and newRepr are expected to have previously been escaped by 71 88 // django.utils.html.escape. 72 89 newId = html_unescape(newId); 73 90 newRepr = html_unescape(newRepr); 74 91 var name = windowname_to_id(win.name); 75 92 var elem = document.getElementById(name); 93 // create a object of changeIcon and ChangeLink. 94 var icon_elem = document.getElementById('change_icon_' + name); 95 var acr_elem = document.getElementById('change_' + name); 76 96 if (elem) { 77 97 if (elem.nodeName == 'SELECT') { 78 98 var o = new Option(newRepr, newId); 79 99 elem.options[elem.options.length] = o; 80 100 o.selected = true; 101 // To hide and visible change_icon image according to number of selection item. 102 var count = 0; 103 for (i=0; i<elem.options.length; i++) { 104 if (elem.options[i].selected) { 105 count++; 106 } 107 if (count > 1){ 108 break; 109 } 110 } 111 if (count == 1){ 112 icon_elem.style.display = ''; 113 acr_elem.href = triggeringLink + newId + '/'; 114 } 115 else{ 116 icon_elem.style.display = 'none'; 117 acr_elem.href = triggeringLink; 118 } 119 81 120 } else if (elem.nodeName == 'INPUT') { 82 121 if (elem.className.indexOf('vManyToManyRawIdAdminField') != -1 && elem.value) { 83 122 elem.value += ',' + newId; … … 94 133 } 95 134 win.close(); 96 135 } 136 137 // To close popup edit page. 138 function dismissChangeCurrentPopup(win, Id, newRepr) { 139 // Id and newRepr are expected to have previously been escaped by 140 // django.utils.html.escape. 141 142 Id = html_unescape(Id); 143 newRepr = html_unescape(newRepr); 144 145 var name = windowname_to_id(win.name); 146 var elem = document.getElementById(name); 147 148 if (elem) { 149 if (elem.nodeName == 'SELECT') { 150 for(i=0; i < elem.options.length; i++) 151 { 152 if(elem.options[i].value == Id) 153 { 154 elem.options[i].text = newRepr; 155 elem.options[i].selected = true; 156 } 157 } 158 159 } else if (elem.nodeName == 'INPUT') { 160 elem.value = Id; 161 } 162 } else { 163 var toId = name + "_to"; 164 elem = document.getElementById(toId); 165 var o = new Option(newRepr, Id); 166 SelectBox.add_to_cache(toId, o); 167 SelectBox.redisplay(toId); 168 } 169 win.close(); 170 } 171 172 // To hide and visible change_icon image according to number of selection item. 173 function onChangeValue(element, triggeringLink, anchor_id, icon_id) 174 { 175 var i; 176 var count = 0; 177 var icon_elem = document.getElementById(icon_id); 178 var acr_elem = document.getElementById(anchor_id); 179 180 for (i=0; i<element.options.length; i++) { 181 if (element.options[i].selected) { 182 count++; 183 } 184 if (count > 1){ 185 icon_elem.style.display = 'none'; 186 acr_elem.href = triggeringLink; 187 return; 188 } 189 } 190 191 if (!element.value){ 192 icon_elem.style.display = 'none'; 193 acr_elem.href = triggeringLink; 194 } 195 else{ 196 icon_elem.style.display = ''; 197 acr_elem.href = triggeringLink + element.value + '/'; 198 } 199 } 200 No newline at end of file -
django/contrib/admin/options.py
25 25 from django.utils.translation import ugettext as _ 26 26 from django.utils.translation import ungettext 27 27 from django.utils.encoding import force_unicode 28 from django.core.urlresolvers import reverse, NoReverseMatch 28 29 29 30 HORIZONTAL, VERTICAL = 1, 2 30 31 # returns the <ul> class for a given radio_admin field … … 724 725 return HttpResponseRedirect(post_url_continue % pk_value) 725 726 726 727 if "_popup" in request.POST: 727 return HttpResponse('<script type="text/javascript">opener.dismissAddAnotherPopup(window, "%s", "%s");</script>' % \ 728 info = (opts.app_label, opts.object_name.lower()) 729 # To add URL for Change existings record. 730 try: 731 related_info = (self.admin_site.name,) + info 732 change_url = reverse('%sadmin_%s_%s_' % related_info) 733 except NoReverseMatch: 734 change_url = '../../../%s/%s/' % info 735 return HttpResponse('<script type="text/javascript">opener.dismissAddAnotherPopup(window, "%s", "%s", "%s");</script>' % \ 728 736 # escape() calls force_unicode. 729 (escape(pk_value), escapejs(obj) ))737 (escape(pk_value), escapejs(obj), change_url)) 730 738 elif "_addanother" in request.POST: 731 739 self.message_user(request, msg + ' ' + (_("You may add another %s below.") % force_unicode(opts.verbose_name))) 732 740 return HttpResponseRedirect(request.path) … … 770 778 elif "_addanother" in request.POST: 771 779 self.message_user(request, msg + ' ' + (_("You may add another %s below.") % force_unicode(verbose_name))) 772 780 return HttpResponseRedirect("../add/") 781 # To close popup edit window. 782 elif "_popup" in request.POST: 783 return HttpResponse('<script type="text/javascript">opener.dismissChangeCurrentPopup(window, "%s", "%s");</script>' % \ 784 # escape() calls force_unicode. 785 (escape(pk_value), escape(obj))) 773 786 else: 774 787 self.message_user(request, msg) 775 788 # Figure out where to redirect. If the user has change permission, -
django/contrib/admin/widgets.py
232 232 rel_to = self.rel.to 233 233 info = (rel_to._meta.app_label, rel_to._meta.object_name.lower()) 234 234 try: 235 related_url = reverse('admin:%s_%s_add' % info, current_app=self.admin_site.name)235 add_url = reverse('admin:%s_%s_add' % info, current_app=self.admin_site.name) 236 236 except NoReverseMatch: 237 info = (self.admin_site.root_path, rel_to._meta.app_label, rel_to._meta.object_name.lower()) 238 related_url = '%s%s/%s/add/' % info 237 add_url = '%s%s/%s/add/' % ((self.admin_site.root_path,) + info) 238 try: 239 change_url = reverse('admin:%s_%s' % info) 240 except NoReverseMatch: 241 change_url = '../../../%s/%s/' % info 239 242 self.widget.choices = self.choices 240 output = [self.widget.render(name, value, *args, **kwargs)] 243 244 output = [] 241 245 if self.can_add_related: 242 246 # TODO: "id_" is hard-coded here. This should instead use the correct 243 247 # API to determine the ID dynamically. 244 248 output.append(u'<a href="%s" class="add-another" id="add_id_%s" onclick="return showAddAnotherPopup(this);"> ' % \ 245 (related_url, name)) 246 output.append(u'<img src="%simg/admin/icon_addlink.gif" width="10" height="10" alt="%s"/></a>' % (settings.ADMIN_MEDIA_PREFIX, _('Add Another'))) 249 (add_url, name)) 250 output.append(u'<img src="%simg/admin/icon_addlink.gif" width="10" height="10" alt="%s"/></a>' % \ 251 (settings.ADMIN_MEDIA_PREFIX, _('Add Another'))) 252 253 # To put a changeLink, changeLinkIcon in selecion widget and to add id of selected record in ChangeLink. 254 display = 'None' 255 id = '' 256 if value and (isinstance(value, list) and len(value) == 1) or (isinstance(value, int)): 257 display = '' 258 if isinstance(value, list): 259 id = '%s/' % (value[0],) 260 else: 261 id = '%s/' % value 262 263 if self.can_add_related: 264 if kwargs.get('attrs', False): 265 kwargs['attrs'].update({"onchange": "onChangeValue(this, '%s', 'change_id_%s', 'change_icon_id_%s')" % (change_url, name, name)}) 266 267 output.append(u'''<a href="%s%s" class="change-current" id="change_id_%s" onclick="return showChangeCurrentPopup(this, 'id_%s');"> ''' % \ 268 (change_url, id, name, name)) 269 output.append(u'<img src="%simg/admin/icon_changelink.gif" width="10" height="10" alt="%s" id="change_icon_id_%s" style="display:%s;"/></a>' % \ 270 (settings.ADMIN_MEDIA_PREFIX, _('Edit'), name, display)) 271 272 # The widget rendering is here because we can add extra attrs 273 # at any time, so the rendering has to be last. 274 output.insert(0, self.widget.render(name, value, *args, **kwargs)) 275 247 276 return mark_safe(u''.join(output)) 248 277 249 278 def build_attrs(self, extra_attrs=None, **kwargs):