Ticket #11397: add_edit_icon.diff

File add_edit_icon.diff, 10.4 KB (added by Dharmesh Patel, 15 years ago)

SVN Diff File for add an Edit Icon in selection widget of Relationship fields like ForeignKey and ManyToMany Fields.

  • django/db/models/fields/related.py

     
    814814        Field.__init__(self, **kwargs)
    815815
    816816        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)
    818820
    819821    def get_choices_default(self):
    820822        return Field.get_choices(self, include_blank=False)
  • django/contrib/admin/media/js/admin/RelatedObjectLookups.js

     
    5555function showAddAnotherPopup(triggeringLink) {
    5656    var name = triggeringLink.id.replace(/^add_/, '');
    5757    name = id_to_windowname(name);
    58     href = triggeringLink.href
     58    href = triggeringLink.href;
    5959    if (href.indexOf('?') == -1) {
    6060        href += '?_popup=1';
    6161    } else {
     
    6666    return false;
    6767}
    6868
    69 function dismissAddAnotherPopup(win, newId, newRepr) {
     69// To show edit popup window of selected item.
     70function 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
     86function dismissAddAnotherPopup(win, newId, newRepr, triggeringLink) {
    7087    // newId and newRepr are expected to have previously been escaped by
    7188    // django.utils.html.escape.
    7289    newId = html_unescape(newId);
    7390    newRepr = html_unescape(newRepr);
    7491    var name = windowname_to_id(win.name);
    7592    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);
    7696    if (elem) {
    7797        if (elem.nodeName == 'SELECT') {
    7898            var o = new Option(newRepr, newId);
    7999            elem.options[elem.options.length] = o;
    80100            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
    81120        } else if (elem.nodeName == 'INPUT') {
    82121            if (elem.className.indexOf('vManyToManyRawIdAdminField') != -1 && elem.value) {
    83122                elem.value += ',' + newId;
     
    94133    }
    95134    win.close();
    96135}
     136
     137// To close popup edit page.
     138function 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.
     173function 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

     
    2020from django.utils.translation import ugettext as _
    2121from django.utils.translation import ungettext, ugettext_lazy
    2222from django.utils.encoding import force_unicode
     23from django.core.urlresolvers import reverse, NoReverseMatch
    2324try:
    2425    set
    2526except NameError:
     
    605606            return HttpResponseRedirect(post_url_continue % pk_value)
    606607
    607608        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>' % \
    609618                # escape() calls force_unicode.
    610                 (escape(pk_value), escape(obj)))
     619                (escape(pk_value), escape(obj), change_url))
    611620        elif request.POST.has_key("_addanother"):
    612621            self.message_user(request, msg + ' ' + (_("You may add another %s below.") % force_unicode(opts.verbose_name)))
    613622            return HttpResponseRedirect(request.path)
     
    644653        elif request.POST.has_key("_addanother"):
    645654            self.message_user(request, msg + ' ' + (_("You may add another %s below.") % force_unicode(opts.verbose_name)))
    646655            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)))
    647661        else:
    648662            self.message_user(request, msg)
    649663            return HttpResponseRedirect("../")
  • django/contrib/admin/widgets.py

     
    33"""
    44
    55import copy
     6import types
    67
    78from django import forms
    89from django.forms.widgets import RadioFieldRenderer
     
    221222    def render(self, name, value, *args, **kwargs):
    222223        rel_to = self.rel.to
    223224        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.
    224226        try:
    225227            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)
    227229        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
    229235        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)})
    230239        output = [self.widget.render(name, value, *args, **kwargs)]
    231240        if rel_to in self.admin_site._registry: # If the related object has an admin interface:
    232241            # TODO: "id_" is hard-coded here. This should instead use the correct
    233242            # API to determine the ID dynamically.
    234243            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))
    236245            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))
    237258        return mark_safe(u''.join(output))
    238259
    239260    def build_attrs(self, extra_attrs=None, **kwargs):
Back to Top