Ticket #11397: add_edit_icon_r15922.diff

File add_edit_icon_r15922.diff, 9.8 KB (added by Dario Ocles, 13 years ago)
  • 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

     
    2525from django.utils.translation import ugettext as _
    2626from django.utils.translation import ungettext
    2727from django.utils.encoding import force_unicode
     28from django.core.urlresolvers import reverse, NoReverseMatch
    2829
    2930HORIZONTAL, VERTICAL = 1, 2
    3031# returns the <ul> class for a given radio_admin field
     
    724725            return HttpResponseRedirect(post_url_continue % pk_value)
    725726
    726727        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>' % \
    728736                # escape() calls force_unicode.
    729                 (escape(pk_value), escapejs(obj)))
     737                (escape(pk_value), escapejs(obj), change_url))
    730738        elif "_addanother" in request.POST:
    731739            self.message_user(request, msg + ' ' + (_("You may add another %s below.") % force_unicode(opts.verbose_name)))
    732740            return HttpResponseRedirect(request.path)
     
    770778        elif "_addanother" in request.POST:
    771779            self.message_user(request, msg + ' ' + (_("You may add another %s below.") % force_unicode(verbose_name)))
    772780            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)))
    773786        else:
    774787            self.message_user(request, msg)
    775788            # Figure out where to redirect. If the user has change permission,
  • django/contrib/admin/widgets.py

     
    232232        rel_to = self.rel.to
    233233        info = (rel_to._meta.app_label, rel_to._meta.object_name.lower())
    234234        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)
    236236        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
    239242        self.widget.choices = self.choices
    240         output = [self.widget.render(name, value, *args, **kwargs)]
     243
     244        output = []
    241245        if self.can_add_related:
    242246            # TODO: "id_" is hard-coded here. This should instead use the correct
    243247            # API to determine the ID dynamically.
    244248            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
    247276        return mark_safe(u''.join(output))
    248277
    249278    def build_attrs(self, extra_attrs=None, **kwargs):
Back to Top