annotate media/django/js/admin/RelatedObjectLookups.js @ 1:0dcfcdf50c62

Initial import of Madeira project from the private repository.
author Brian Neal <bgneal@gmail.com>
date Mon, 06 Apr 2009 03:10:59 +0000
parents
children
rev   line source
bgneal@1 1 // Handles related-objects functionality: lookup link for raw_id_fields
bgneal@1 2 // and Add Another links.
bgneal@1 3
bgneal@1 4 function html_unescape(text) {
bgneal@1 5 // Unescape a string that was escaped using django.utils.html.escape.
bgneal@1 6 text = text.replace(/&lt;/g, '<');
bgneal@1 7 text = text.replace(/&gt;/g, '>');
bgneal@1 8 text = text.replace(/&quot;/g, '"');
bgneal@1 9 text = text.replace(/&#39;/g, "'");
bgneal@1 10 text = text.replace(/&amp;/g, '&');
bgneal@1 11 return text;
bgneal@1 12 }
bgneal@1 13
bgneal@1 14 // IE doesn't accept periods or dashes in the window name, but the element IDs
bgneal@1 15 // we use to generate popup window names may contain them, therefore we map them
bgneal@1 16 // to allowed characters in a reversible way so that we can locate the correct
bgneal@1 17 // element when the popup window is dismissed.
bgneal@1 18 function id_to_windowname(text) {
bgneal@1 19 text = text.replace(/\./g, '__dot__');
bgneal@1 20 text = text.replace(/\-/g, '__dash__');
bgneal@1 21 return text;
bgneal@1 22 }
bgneal@1 23
bgneal@1 24 function windowname_to_id(text) {
bgneal@1 25 text = text.replace(/__dot__/g, '.');
bgneal@1 26 text = text.replace(/__dash__/g, '-');
bgneal@1 27 return text;
bgneal@1 28 }
bgneal@1 29
bgneal@1 30 function showRelatedObjectLookupPopup(triggeringLink) {
bgneal@1 31 var name = triggeringLink.id.replace(/^lookup_/, '');
bgneal@1 32 name = id_to_windowname(name);
bgneal@1 33 var href;
bgneal@1 34 if (triggeringLink.href.search(/\?/) >= 0) {
bgneal@1 35 href = triggeringLink.href + '&pop=1';
bgneal@1 36 } else {
bgneal@1 37 href = triggeringLink.href + '?pop=1';
bgneal@1 38 }
bgneal@1 39 var win = window.open(href, name, 'height=500,width=800,resizable=yes,scrollbars=yes');
bgneal@1 40 win.focus();
bgneal@1 41 return false;
bgneal@1 42 }
bgneal@1 43
bgneal@1 44 function dismissRelatedLookupPopup(win, chosenId) {
bgneal@1 45 var name = windowname_to_id(win.name);
bgneal@1 46 var elem = document.getElementById(name);
bgneal@1 47 if (elem.className.indexOf('vManyToManyRawIdAdminField') != -1 && elem.value) {
bgneal@1 48 elem.value += ',' + chosenId;
bgneal@1 49 } else {
bgneal@1 50 document.getElementById(name).value = chosenId;
bgneal@1 51 }
bgneal@1 52 win.close();
bgneal@1 53 }
bgneal@1 54
bgneal@1 55 function showAddAnotherPopup(triggeringLink) {
bgneal@1 56 var name = triggeringLink.id.replace(/^add_/, '');
bgneal@1 57 name = id_to_windowname(name);
bgneal@1 58 href = triggeringLink.href
bgneal@1 59 if (href.indexOf('?') == -1) {
bgneal@1 60 href += '?_popup=1';
bgneal@1 61 } else {
bgneal@1 62 href += '&_popup=1';
bgneal@1 63 }
bgneal@1 64 var win = window.open(href, name, 'height=500,width=800,resizable=yes,scrollbars=yes');
bgneal@1 65 win.focus();
bgneal@1 66 return false;
bgneal@1 67 }
bgneal@1 68
bgneal@1 69 function dismissAddAnotherPopup(win, newId, newRepr) {
bgneal@1 70 // newId and newRepr are expected to have previously been escaped by
bgneal@1 71 // django.utils.html.escape.
bgneal@1 72 newId = html_unescape(newId);
bgneal@1 73 newRepr = html_unescape(newRepr);
bgneal@1 74 var name = windowname_to_id(win.name);
bgneal@1 75 var elem = document.getElementById(name);
bgneal@1 76 if (elem) {
bgneal@1 77 if (elem.nodeName == 'SELECT') {
bgneal@1 78 var o = new Option(newRepr, newId);
bgneal@1 79 elem.options[elem.options.length] = o;
bgneal@1 80 o.selected = true;
bgneal@1 81 } else if (elem.nodeName == 'INPUT') {
bgneal@1 82 elem.value = newId;
bgneal@1 83 }
bgneal@1 84 } else {
bgneal@1 85 var toId = name + "_to";
bgneal@1 86 elem = document.getElementById(toId);
bgneal@1 87 var o = new Option(newRepr, newId);
bgneal@1 88 SelectBox.add_to_cache(toId, o);
bgneal@1 89 SelectBox.redisplay(toId);
bgneal@1 90 }
bgneal@1 91 win.close();
bgneal@1 92 }