1
// Handles related-objects functionality: lookup link for raw_id_fields
2
// and Add Another links.
4
function html_unescape(text) {
5
// Unescape a string that was escaped using django.utils.html.escape.
6
text = text.replace(/</g, '<');
7
text = text.replace(/>/g, '>');
8
text = text.replace(/"/g, '"');
9
text = text.replace(/'/g, "'");
10
text = text.replace(/&/g, '&');
14
// IE doesn't accept periods or dashes in the window name, but the element IDs
15
// we use to generate popup window names may contain them, therefore we map them
16
// to allowed characters in a reversible way so that we can locate the correct
17
// element when the popup window is dismissed.
18
function id_to_windowname(text) {
19
text = text.replace(/\./g, '__dot__');
20
text = text.replace(/\-/g, '__dash__');
24
function windowname_to_id(text) {
25
text = text.replace(/__dot__/g, '.');
26
text = text.replace(/__dash__/g, '-');
30
function showRelatedObjectLookupPopup(triggeringLink) {
31
var name = triggeringLink.id.replace(/^lookup_/, '');
32
name = id_to_windowname(name);
34
if (triggeringLink.href.search(/\?/) >= 0) {
35
href = triggeringLink.href + '&pop=1';
37
href = triggeringLink.href + '?pop=1';
39
var win = window.open(href, name, 'height=500,width=800,resizable=yes,scrollbars=yes');
44
function dismissRelatedLookupPopup(win, chosenId) {
45
var name = windowname_to_id(win.name);
46
var elem = document.getElementById(name);
47
if (elem.className.indexOf('vManyToManyRawIdAdminField') != -1 && elem.value) {
48
elem.value += ',' + chosenId;
50
document.getElementById(name).value = chosenId;
55
function showAddAnotherPopup(triggeringLink) {
56
var name = triggeringLink.id.replace(/^add_/, '');
57
name = id_to_windowname(name);
58
href = triggeringLink.href
59
if (href.indexOf('?') == -1) {
64
var win = window.open(href, name, 'height=500,width=800,resizable=yes,scrollbars=yes');
69
function dismissAddAnotherPopup(win, newId, newRepr) {
70
// newId and newRepr are expected to have previously been escaped by
71
// django.utils.html.escape.
72
newId = html_unescape(newId);
73
newRepr = html_unescape(newRepr);
74
var name = windowname_to_id(win.name);
75
var elem = document.getElementById(name);
77
var elemName = elem.nodeName.toUpperCase();
78
if (elemName == 'SELECT') {
79
var o = new Option(newRepr, newId);
80
elem.options[elem.options.length] = o;
82
} else if (elemName == 'INPUT') {
83
if (elem.className.indexOf('vManyToManyRawIdAdminField') != -1 && elem.value) {
84
elem.value += ',' + newId;
90
var toId = name + "_to";
91
elem = document.getElementById(toId);
92
var o = new Option(newRepr, newId);
93
SelectBox.add_to_cache(toId, o);
94
SelectBox.redisplay(toId);