Ticket #13068: prepopulate.3.diff
File prepopulate.3.diff, 12.4 KB (added by , 15 years ago) |
---|
-
django/contrib/admin/media/js/admin/DateTimeShortcuts.js
120 120 }, 121 121 handleClockQuicklink: function(num, val) { 122 122 DateTimeShortcuts.clockInputs[num].value = val; 123 DateTimeShortcuts.clockInputs[num].focus(); 123 124 DateTimeShortcuts.dismissClock(num); 124 125 }, 125 126 // Add calendar widget to a given field. … … 247 248 format = format.replace('\n', '\\n'); 248 249 format = format.replace('\t', '\\t'); 249 250 format = format.replace("'", "\\'"); 250 return "function(y, m, d) { DateTimeShortcuts.calendarInputs["+num+"].value = new Date(y, m-1, d).strftime('"+format+"');document.getElementById(DateTimeShortcuts.calendarDivName1+"+num+").style.display='none';}"; 251 return ["function(y, m, d) { DateTimeShortcuts.calendarInputs[", 252 num, 253 "].value = new Date(y, m-1, d).strftime('", 254 format, 255 "');DateTimeShortcuts.calendarInputs[", 256 num, 257 "].focus();document.getElementById(DateTimeShortcuts.calendarDivName1+", 258 num, 259 ").style.display='none';}"].join(''); 251 260 }, 252 261 handleCalendarQuickLink: function(num, offset) { 253 262 var d = new Date(); 254 263 d.setDate(d.getDate() + offset) 255 264 DateTimeShortcuts.calendarInputs[num].value = d.strftime(get_format('DATE_INPUT_FORMATS')[0]); 265 DateTimeShortcuts.calendarInputs[num].focus(); 256 266 DateTimeShortcuts.dismissCalendar(num); 257 267 }, 258 268 cancelEventPropagation: function(e) { -
django/contrib/admin/media/js/prepopulate.js
1 (function($) { 2 $.fn.prepopulate = function(dependencies, maxLength) { 3 /* 4 Depends on urlify.js 5 Populates a selected field with the values of the dependent fields, 6 URLifies and shortens the string. 7 dependencies - selected jQuery object of dependent fields 8 maxLength - maximum length of the URLify'd string 9 */ 10 return this.each(function() { 11 var field = $(this); 12 13 field.data('_changed', false); 14 field.change(function() { 15 field.data('_changed', true); 16 }); 17 18 var populate = function () { 19 // Bail if the fields value has changed 20 if (field.data('_changed') == true) return; 21 22 var values = []; 23 dependencies.each(function() { 24 if ($(this).val().length > 0) { 25 values.push($(this).val()); 26 } 27 }); 28 field.val(URLify(values.join(' '), maxLength)); 29 }; 30 31 dependencies.keyup(populate).change(populate).focus(populate); 32 }); 33 }; 34 })(jQuery.noConflict()); -
django/contrib/admin/media/js/inlines.js
54 54 var totalForms = $("#id_" + options.prefix + "-TOTAL_FORMS"); 55 55 var nextIndex = parseInt(totalForms.val()) + 1; 56 56 var template = $("#" + options.prefix + "-empty"); 57 var row = template.clone(true).get(0); 58 $(row).removeClass(options.emptyCssClass).removeAttr("id").insertBefore($(template)); 59 $(row).html($(row).html().replace(/__prefix__/g, nextIndex)); 60 $(row).addClass(options.formCssClass).attr("id", options.prefix + nextIndex); 61 if ($(row).is("TR")) { 57 var row = template.clone(true); 58 row.removeClass(options.emptyCssClass) 59 .addClass(options.formCssClass) 60 .attr("id", options.prefix + nextIndex) 61 .insertBefore($(template)); 62 row.find("*") 63 .filter(function() { 64 var el = $(this); 65 return el.attr("id") && el.attr("id").search(/__prefix__/) >= 0; 66 }).each(function() { 67 var el = $(this); 68 el.attr("id", el.attr("id").replace(/__prefix__/g, nextIndex)); 69 }) 70 .end() 71 .filter(function() { 72 var el = $(this); 73 return el.attr("name") && el.attr("name").search(/__prefix__/) >= 0; 74 }).each(function() { 75 var el = $(this); 76 el.attr("name", el.attr("name").replace(/__prefix__/g, nextIndex)); 77 }); 78 if (row.is("tr")) { 62 79 // If the forms are laid out in table rows, insert 63 80 // the remove button into the last table cell: 64 $(row).children(":last").append('<div><a class="' + options.deleteCssClass +'" href="javascript:void(0)">' + options.deleteText + "</a></div>");65 } else if ( $(row).is("UL") || $(row).is("OL")) {81 row.children(":last").append('<div><a class="' + options.deleteCssClass +'" href="javascript:void(0)">' + options.deleteText + "</a></div>"); 82 } else if (row.is("ul") || row.is("ol")) { 66 83 // If they're laid out as an ordered/unordered list, 67 84 // insert an <li> after the last list item: 68 $(row).append('<li><a class="' + options.deleteCssClass +'" href="javascript:void(0)">' + options.deleteText + "</a></li>");85 row.append('<li><a class="' + options.deleteCssClass +'" href="javascript:void(0)">' + options.deleteText + "</a></li>"); 69 86 } else { 70 87 // Otherwise, just insert the remove button as the 71 88 // last child element of the form's container: 72 $(row).children(":first").append('<span><a class="' + options.deleteCssClass + '" href="javascript:void(0)">' + options.deleteText + "</a></span>");89 row.children(":first").append('<span><a class="' + options.deleteCssClass + '" href="javascript:void(0)">' + options.deleteText + "</a></span>"); 73 90 } 74 $(row).find("input,select,textarea,label,a").each(function() {91 row.find("input,select,textarea,label,a").each(function() { 75 92 updateElementIndex(this, options.prefix, totalForms.val()); 76 93 }); 77 94 // Update number of total forms … … 81 98 addButton.parent().hide(); 82 99 } 83 100 // The delete button of each row triggers a bunch of other things 84 $(row).find("a." + options.deleteCssClass).click(function() {101 row.find("a." + options.deleteCssClass).click(function() { 85 102 // Remove the parent form containing this button: 86 103 var row = $(this).parents("." + options.formCssClass); 87 104 row.remove(); … … 108 125 }); 109 126 // If a post-add callback was supplied, call it with the added form: 110 127 if (options.added) { 111 options.added( $(row));128 options.added(row); 112 129 } 113 130 return false; 114 131 }); -
django/contrib/admin/options.py
273 273 js.extend(['js/jquery.min.js', 'js/actions.min.js']) 274 274 if self.prepopulated_fields: 275 275 js.append('js/urlify.js') 276 js.append('js/prepopulate.js') 276 277 if self.opts.get_ordered_objects(): 277 278 js.extend(['js/getElementsBySelector.js', 'js/dom-drag.js' , 'js/admin/ordering.js']) 278 279 … … 1201 1202 1202 1203 def _media(self): 1203 1204 from django.conf import settings 1204 js = ['js/jquery.min.js', 'js/inlines. min.js']1205 js = ['js/jquery.min.js', 'js/inlines.js'] 1205 1206 if self.prepopulated_fields: 1206 1207 js.append('js/urlify.js') 1208 js.append('js/prepopulate.js') 1207 1209 if self.filter_vertical or self.filter_horizontal: 1208 1210 js.extend(['js/SelectBox.js' , 'js/SelectFilter2.js']) 1209 1211 return forms.Media(js=['%s%s' % (settings.ADMIN_MEDIA_PREFIX, url) for url in js]) -
django/contrib/admin/templates/admin/edit_inline/stacked.html
48 48 }) 49 49 } 50 50 } 51 var initPrepopulatedFields = function(row) { 52 row.find('.prepopulated_field').each(function() { 53 var field = $(this); 54 var input = field.find('input'); 55 var dependency_list = input.data('dependency_list') || []; 56 var dependencies = row.find(dependency_list.join(',')); 57 if (dependencies.length) { 58 input.prepopulate(dependencies, input.attr('maxlength')); 59 } 60 }); 61 } 51 62 $(rows).formset({ 52 63 prefix: "{{ inline_admin_formset.formset.prefix }}", 53 64 addText: "{% blocktrans with inline_admin_formset.opts.verbose_name|title as verbose_name %}Add another {{ verbose_name }}{% endblocktrans %}", … … 57 68 emptyCssClass: "empty-form", 58 69 removed: updateInlineLabel, 59 70 added: (function(row) { 71 initPrepopulatedFields(row); 60 72 reinitDateTimeShortCuts(); 61 73 updateSelectFilter(); 62 74 updateInlineLabel(row); -
django/contrib/admin/templates/admin/edit_inline/tabular.html
94 94 }) 95 95 } 96 96 } 97 var initPrepopulatedFields = function(row) { 98 row.find('.prepopulated_field').each(function() { 99 var field = $(this); 100 var input = field.find('input'); 101 var dependency_list = input.data('dependency_list') || []; 102 var dependencies = row.find(dependency_list.join(',')); 103 if (dependencies.length) { 104 input.prepopulate(dependencies, input.attr('maxlength')); 105 } 106 }); 107 } 97 108 $(rows).formset({ 98 109 prefix: "{{ inline_admin_formset.formset.prefix }}", 99 110 addText: "{% blocktrans with inline_admin_formset.opts.verbose_name|title as verbose_name %}Add another {{ verbose_name }}{% endblocktrans %}", … … 103 114 emptyCssClass: "empty-form", 104 115 removed: alternatingRows, 105 116 added: (function(row) { 117 initPrepopulatedFields(row); 106 118 reinitDateTimeShortCuts(); 107 119 updateSelectFilter(); 108 120 alternatingRows(row); -
django/contrib/admin/templates/admin/prepopulated_fields_js.html
1 1 <script type="text/javascript"> 2 (function($) { 3 var field = null; 4 2 5 {% for field in prepopulated_fields %} 3 document.getElementById("{{ field.field.auto_id }}").onchange = function() { this._changed = true; }; 6 field = { 7 id: '#{{ field.field.auto_id }}', 8 dependency_ids: [], 9 dependency_list: [], 10 maxLength: {{ field.field.field.max_length|default_if_none:"50" }} 11 }; 12 4 13 {% for dependency in field.dependencies %} 5 document.getElementById("{{ dependency.auto_id }}").onkeyup = function() { 6 var e = document.getElementById("{{ field.field.auto_id }}"); 7 if (!e._changed) { e.value = URLify({% for innerdep in field.dependencies %}document.getElementById("{{ innerdep.auto_id }}").value{% if not forloop.last %} + ' ' + {% endif %}{% endfor %}, {{ field.field.field.max_length|default_if_none:"50" }}); } 8 } 14 field['dependency_ids'].push('#{{ dependency.auto_id }}'); 15 field['dependency_list'].push('.{{ dependency.name }} input'); 9 16 {% endfor %} 17 18 console.log() 19 $('.empty-form .{{ field.field.name }}').addClass('prepopulated_field'); 20 $(field.id).data('dependency_list', field['dependency_list']) 21 .prepopulate($(field['dependency_ids'].join(',')), field.maxLength); 10 22 {% endfor %} 23 })(jQuery.noConflict()); 11 24 </script>