Ticket #11397: add_edit_icon.diff
File add_edit_icon.diff, 10.4 KB (added by , 15 years ago) |
---|
-
django/db/models/fields/related.py
814 814 Field.__init__(self, **kwargs) 815 815 816 816 msg = ugettext_lazy('Hold down "Control", or "Command" on a Mac, to select more than one.') 817 self.help_text = string_concat(self.help_text, ' ', msg) 817 # To put help message to edit selected items in many2many and one2many selection field. 818 change_msg = ugettext_lazy('To edit select only one.') 819 self.help_text = string_concat(self.help_text, ' ', msg, '<br/>', change_msg) 818 820 819 821 def get_choices_default(self): 820 822 return Field.get_choices(self, include_blank=False) -
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
20 20 from django.utils.translation import ugettext as _ 21 21 from django.utils.translation import ungettext, ugettext_lazy 22 22 from django.utils.encoding import force_unicode 23 from django.core.urlresolvers import reverse, NoReverseMatch 23 24 try: 24 25 set 25 26 except NameError: … … 605 606 return HttpResponseRedirect(post_url_continue % pk_value) 606 607 607 608 if request.POST.has_key("_popup"): 608 return HttpResponse('<script type="text/javascript">opener.dismissAddAnotherPopup(window, "%s", "%s");</script>' % \ 609 info = (opts.app_label, opts.object_name.lower()) 610 # To add URL for Change existing record. 611 try: 612 related_info = (self.admin_site.name,) + info 613 change_url = reverse('%sadmin_%s_%s_' % related_info) 614 except NoReverseMatch: 615 change_url = '../../../%s/%s/' % info 616 617 return HttpResponse('<script type="text/javascript">opener.dismissAddAnotherPopup(window, "%s", "%s", "%s");</script>' % \ 609 618 # escape() calls force_unicode. 610 (escape(pk_value), escape(obj) ))619 (escape(pk_value), escape(obj), change_url)) 611 620 elif request.POST.has_key("_addanother"): 612 621 self.message_user(request, msg + ' ' + (_("You may add another %s below.") % force_unicode(opts.verbose_name))) 613 622 return HttpResponseRedirect(request.path) … … 644 653 elif request.POST.has_key("_addanother"): 645 654 self.message_user(request, msg + ' ' + (_("You may add another %s below.") % force_unicode(opts.verbose_name))) 646 655 return HttpResponseRedirect("../add/") 656 # To close popup edit window. 657 elif request.POST.has_key("_popup"): 658 return HttpResponse('<script type="text/javascript">opener.dismissChangeCurrentPopup(window, "%s", "%s");</script>' % \ 659 # escape() calls force_unicode. 660 (escape(pk_value), escape(obj))) 647 661 else: 648 662 self.message_user(request, msg) 649 663 return HttpResponseRedirect("../") -
django/contrib/admin/widgets.py
3 3 """ 4 4 5 5 import copy 6 import types 6 7 7 8 from django import forms 8 9 from django.forms.widgets import RadioFieldRenderer … … 221 222 def render(self, name, value, *args, **kwargs): 222 223 rel_to = self.rel.to 223 224 info = (rel_to._meta.app_label, rel_to._meta.object_name.lower()) 225 # To edit URL Pattern for Add new records and for Change existing record. 224 226 try: 225 227 related_info = (self.admin_site.name,) + info 226 related_url = reverse('%sadmin_%s_%s_add' % related_info)228 add_url = reverse('%sadmin_%s_%s_add' % related_info) 227 229 except NoReverseMatch: 228 related_url = '../../../%s/%s/add/' % info 230 add_url = '../../../%s/%s/add/' % info 231 try: 232 change_url = reverse('%sadmin_%s_%s_' % related_info) 233 except NoReverseMatch: 234 change_url = '../../../%s/%s/' % info 229 235 self.widget.choices = self.choices 236 # To put a onChange event in selection tag. 237 if kwargs.get('attrs', False): 238 kwargs['attrs'].update({"onchange": "onChangeValue(this, '%s', 'change_id_%s', 'change_icon_id_%s')" % (change_url, name, name)}) 230 239 output = [self.widget.render(name, value, *args, **kwargs)] 231 240 if rel_to in self.admin_site._registry: # If the related object has an admin interface: 232 241 # TODO: "id_" is hard-coded here. This should instead use the correct 233 242 # API to determine the ID dynamically. 234 243 output.append(u'<a href="%s" class="add-another" id="add_id_%s" onclick="return showAddAnotherPopup(this);"> ' % \ 235 ( related_url, name))244 (add_url, name)) 236 245 output.append(u'<img src="%simg/admin/icon_addlink.gif" width="10" height="10" alt="%s"/></a>' % (settings.ADMIN_MEDIA_PREFIX, _('Add Another'))) 246 # To put a changeLink, changeLinkIcon in selecion widget and to add id of selected record in ChangeLink. 247 display = None 248 id = '' 249 if value and (type(value) == types.ListType and len(value) == 1) or (type(value) == types.IntType): 250 display = '' 251 if type(value) == types.ListType: 252 id = '%s/'% (value[0],) 253 else: 254 id = '%s/'% (value,) 255 output.append(u'''<a href="%s%s" class="change-current" id="change_id_%s" onclick="return showChangeCurrentPopup(this, 'id_%s');"> ''' % \ 256 (change_url, id, name, name)) 257 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>' % (settings.ADMIN_MEDIA_PREFIX, _('Edit'), name, display)) 237 258 return mark_safe(u''.join(output)) 238 259 239 260 def build_attrs(self, extra_attrs=None, **kwargs):