Hello site
[auf_framonde.git] / eggs / Django-1.4.5-py2.7.egg / django / contrib / admin / static / admin / js / admin / RelatedObjectLookups.js
CommitLineData
01b54c21
MN
1// Handles related-objects functionality: lookup link for raw_id_fields
2// and Add Another links.
3
4function html_unescape(text) {
5 // Unescape a string that was escaped using django.utils.html.escape.
6 text = text.replace(/&lt;/g, '<');
7 text = text.replace(/&gt;/g, '>');
8 text = text.replace(/&quot;/g, '"');
9 text = text.replace(/&#39;/g, "'");
10 text = text.replace(/&amp;/g, '&');
11 return text;
12}
13
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.
18function id_to_windowname(text) {
19 text = text.replace(/\./g, '__dot__');
20 text = text.replace(/\-/g, '__dash__');
21 return text;
22}
23
24function windowname_to_id(text) {
25 text = text.replace(/__dot__/g, '.');
26 text = text.replace(/__dash__/g, '-');
27 return text;
28}
29
30function showRelatedObjectLookupPopup(triggeringLink) {
31 var name = triggeringLink.id.replace(/^lookup_/, '');
32 name = id_to_windowname(name);
33 var href;
34 if (triggeringLink.href.search(/\?/) >= 0) {
35 href = triggeringLink.href + '&pop=1';
36 } else {
37 href = triggeringLink.href + '?pop=1';
38 }
39 var win = window.open(href, name, 'height=500,width=800,resizable=yes,scrollbars=yes');
40 win.focus();
41 return false;
42}
43
44function 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;
49 } else {
50 document.getElementById(name).value = chosenId;
51 }
52 win.close();
53}
54
55function showAddAnotherPopup(triggeringLink) {
56 var name = triggeringLink.id.replace(/^add_/, '');
57 name = id_to_windowname(name);
58 href = triggeringLink.href
59 if (href.indexOf('?') == -1) {
60 href += '?_popup=1';
61 } else {
62 href += '&_popup=1';
63 }
64 var win = window.open(href, name, 'height=500,width=800,resizable=yes,scrollbars=yes');
65 win.focus();
66 return false;
67}
68
69function 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);
76 if (elem) {
77 var elemName = elem.nodeName.toUpperCase();
78 if (elemName == 'SELECT') {
79 var o = new Option(newRepr, newId);
80 elem.options[elem.options.length] = o;
81 o.selected = true;
82 } else if (elemName == 'INPUT') {
83 if (elem.className.indexOf('vManyToManyRawIdAdminField') != -1 && elem.value) {
84 elem.value += ',' + newId;
85 } else {
86 elem.value = newId;
87 }
88 }
89 } else {
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);
95 }
96 win.close();
97}