Changeset 76

Show
Ignore:
Timestamp:
06/30/07 10:48:47 (19 months ago)
Author:
steadicat
Message:

Fixed script messing up with non-list pages.
Added validation.
Added possibility of editing linked fields as well.
Fixed bug with empty fields.

Location:
unicoders/trunk/unicoders
Files:
4 modified

Legend:

Unmodified
Added
Removed
  • unicoders/trunk/unicoders/adminedit/media/scripts/adminedit.js

    r75 r76  
    5050} 
    5151 
     52function setupForm(target, single) { 
     53    var form = $(target).find('input, textarea, select'); 
     54    if (form.length == 0) return; 
     55 
     56    // do not start the edit again if we click on a form element 
     57    form.click(function(event) { event.stopPropagation(); event.preventDefault(); }); 
     58    // on blur: save 
     59    form.blur(function() { saveField.call(this, single); }); 
     60    // on enter: save 
     61    form.keypress(function(event) { 
     62        if (event.keyCode == 13) saveField.call(this, single); 
     63    }); 
     64 
     65 
     66    function changeField() { 
     67        function innerChangeField() { 
     68            var cell = $(target); 
     69            if (target.tagName == 'A') cell = $(target).parent(); 
     70            getCellsInColumn(cell).each(function() { 
     71                if (isRowSelected(this)) { 
     72                    var f = $(this).find('input, textarea, select')[0]; 
     73                    if (f != form[0]) f.value = form[0].value; 
     74                } 
     75            }); 
     76        }; 
     77        setTimeout(innerChangeField, 10); 
     78    } 
     79    form.change(changeField); 
     80    form.keypress(changeField); 
     81 
     82    // focus on the new form widget 
     83    if (single) form[0].focus(); 
     84} 
    5285 
    5386function editSingleField(single) { 
     87    var target = this; 
     88    // keep the link if present 
     89    if ($(target).find('a').length > 0) { 
     90        target = $(this).find('a')[0]; 
     91    } 
     92 
    5493    // throbber 
    55     $(this).append('<img src="/media/images/throbber.gif"/>'); 
     94    $(target).append('<img src="/media/images/throbber.gif"/>'); 
    5695 
    57  
    58     var target = this; 
    5996    // ajax request for the form widget 
    6097    $.get(getUrl(this), function(data) { 
     
    6299        $(target).html(data); 
    63100 
    64         var form = $(target).find('input, textarea, select'); 
    65         // do not start the edit again if we click on a form element 
    66         form.click(function(event) { event.stopPropagation(); }); 
    67         // focus on the new form widget 
    68         if (single) form[0].focus(); 
    69  
    70         // on blur: save 
    71         form.blur(function() { saveField.call(this, single); }); 
    72         // on enter: save 
    73         form.keypress(function(event) { 
    74             if (event.keyCode == 13) saveField.call(this, single); 
    75         }); 
    76  
    77  
    78         function changeField() { 
    79             function innerChangeField() { 
    80                 getCellsInColumn(form.parent()).each(function() { 
    81                     if (isRowSelected(this)) { 
    82                         var f = $(this).find('input, textarea, select')[0]; 
    83                         if (f != form[0]) f.value = form[0].value; 
    84                     } 
    85                 }); 
    86             }; 
    87             setTimeout(innerChangeField, 10); 
    88         } 
    89         form.change(changeField); 
    90         form.keypress(changeField); 
     101        setupForm(target, single); 
    91102    }); 
    92103} 
     
    94105function saveField(single) { 
    95106    var cell = $(this).parent(); 
     107//    var target = cell; 
     108//    if (cell[0].tagName == 'SPAN') cell = cell.parent(); 
    96109 
    97110    if (!single) { 
     
    108121        // just one argument to the post: the field we are editing with its new value 
    109122        var args = {}; 
    110         args[$(this).attr('name')] = $(this).attr('value'); 
     123        if ($(this).attr('value')) { 
     124            args[$(this).attr('name')] = $(this).attr('value'); 
     125        } else { 
     126            args[$(this).attr('name')] = ''; 
     127        } 
    111128 
    112129        // ajax post to edit the object 
     
    114131            // display the new value in the page 
    115132            $(cell).html(data); 
     133 
     134            // in case of validation error 
     135            setupForm(cell, single); 
    116136        }); 
    117137    } 
     
    119139 
    120140$(document).ready(function() { 
    121     $('#content h1').append('<h3>Click any field to edit it in place</h2>'); 
     141    if ($('body').attr('class') != 'change-list') return; 
     142 
     143    $('#content h1').append('<ul class="messagelist"><li>Click any field to edit it in place</li></ul>'); 
    122144 
    123145    // on click: edit 
    124     $('td').click(editField); 
     146    $('td, th').click(editField); 
     147    // when clicking on a link, do not edit 
     148    $('td a, th a').click(function(event) { event.stopPropagation(); }); 
    125149 
    126150    // add checkboxes to each row 
  • unicoders/trunk/unicoders/adminedit/templates/adminedit/field_edit.html

    r75 r76  
    1 {{ field }} 
     1{% if error %}{{ error.messages }}{% endif %}{{ field }} 
  • unicoders/trunk/unicoders/adminedit/views.py

    r75 r76  
    1818        o = c.objects.get(id=id) 
    1919        try: 
    20             setattr(o, field, form[field].field.clean(request.POST[field])) 
     20            setattr(o, field, form[field].field.clean(request.POST.get(field, None))) 
    2121            o.save() 
    2222        except forms.ValidationError, e: 
    23             print e 
     23            return render_to_response("adminedit/field_edit.html", dict(field=form[field], error=e)) 
    2424 
    2525        return field_view(o, field) 
  • unicoders/trunk/unicoders/media/scripts/adminedit.js

    r75 r76  
    5050} 
    5151 
     52function setupForm(target, single) { 
     53    var form = $(target).find('input, textarea, select'); 
     54    if (form.length == 0) return; 
     55 
     56    // do not start the edit again if we click on a form element 
     57    form.click(function(event) { event.stopPropagation(); event.preventDefault(); }); 
     58    // on blur: save 
     59    form.blur(function() { saveField.call(this, single); }); 
     60    // on enter: save 
     61    form.keypress(function(event) { 
     62        if (event.keyCode == 13) saveField.call(this, single); 
     63    }); 
     64 
     65 
     66    function changeField() { 
     67        function innerChangeField() { 
     68            var cell = $(target); 
     69            if (target.tagName == 'A') cell = $(target).parent(); 
     70            getCellsInColumn(cell).each(function() { 
     71                if (isRowSelected(this)) { 
     72                    var f = $(this).find('input, textarea, select')[0]; 
     73                    if (f != form[0]) f.value = form[0].value; 
     74                } 
     75            }); 
     76        }; 
     77        setTimeout(innerChangeField, 10); 
     78    } 
     79    form.change(changeField); 
     80    form.keypress(changeField); 
     81 
     82    // focus on the new form widget 
     83    if (single) form[0].focus(); 
     84} 
    5285 
    5386function editSingleField(single) { 
     87    var target = this; 
     88    // keep the link if present 
     89    if ($(target).find('a').length > 0) { 
     90        target = $(this).find('a')[0]; 
     91    } 
     92 
    5493    // throbber 
    55     $(this).append('<img src="/media/images/throbber.gif"/>'); 
     94    $(target).append('<img src="/media/images/throbber.gif"/>'); 
    5695 
    57  
    58     var target = this; 
    5996    // ajax request for the form widget 
    6097    $.get(getUrl(this), function(data) { 
     
    6299        $(target).html(data); 
    63100 
    64         var form = $(target).find('input, textarea, select'); 
    65         // do not start the edit again if we click on a form element 
    66         form.click(function(event) { event.stopPropagation(); }); 
    67         // focus on the new form widget 
    68         if (single) form[0].focus(); 
    69  
    70         // on blur: save 
    71         form.blur(function() { saveField.call(this, single); }); 
    72         // on enter: save 
    73         form.keypress(function(event) { 
    74             if (event.keyCode == 13) saveField.call(this, single); 
    75         }); 
    76  
    77  
    78         function changeField() { 
    79             function innerChangeField() { 
    80                 getCellsInColumn(form.parent()).each(function() { 
    81                     if (isRowSelected(this)) { 
    82                         var f = $(this).find('input, textarea, select')[0]; 
    83                         if (f != form[0]) f.value = form[0].value; 
    84                     } 
    85                 }); 
    86             }; 
    87             setTimeout(innerChangeField, 10); 
    88         } 
    89         form.change(changeField); 
    90         form.keypress(changeField); 
     101        setupForm(target, single); 
    91102    }); 
    92103} 
     
    94105function saveField(single) { 
    95106    var cell = $(this).parent(); 
     107//    var target = cell; 
     108//    if (cell[0].tagName == 'SPAN') cell = cell.parent(); 
    96109 
    97110    if (!single) { 
     
    108121        // just one argument to the post: the field we are editing with its new value 
    109122        var args = {}; 
    110         args[$(this).attr('name')] = $(this).attr('value'); 
     123        if ($(this).attr('value')) { 
     124            args[$(this).attr('name')] = $(this).attr('value'); 
     125        } else { 
     126            args[$(this).attr('name')] = ''; 
     127        } 
    111128 
    112129        // ajax post to edit the object 
     
    114131            // display the new value in the page 
    115132            $(cell).html(data); 
     133 
     134            // in case of validation error 
     135            setupForm(cell, single); 
    116136        }); 
    117137    } 
     
    119139 
    120140$(document).ready(function() { 
    121     $('#content h1').append('<h3>Click any field to edit it in place</h2>'); 
     141    if ($('body').attr('class') != 'change-list') return; 
     142 
     143    $('#content h1').append('<ul class="messagelist"><li>Click any field to edit it in place</li></ul>'); 
    122144 
    123145    // on click: edit 
    124     $('td').click(editField); 
     146    $('td, th').click(editField); 
     147    // when clicking on a link, do not edit 
     148    $('td a, th a').click(function(event) { event.stopPropagation(); }); 
    125149 
    126150    // add checkboxes to each row