comparison 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
comparison
equal deleted inserted replaced
0:df0370bfe3f0 1:0dcfcdf50c62
1 // Handles related-objects functionality: lookup link for raw_id_fields
2 // and Add Another links.
3
4 function 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.
18 function id_to_windowname(text) {
19 text = text.replace(/\./g, '__dot__');
20 text = text.replace(/\-/g, '__dash__');
21 return text;
22 }
23
24 function windowname_to_id(text) {
25 text = text.replace(/__dot__/g, '.');
26 text = text.replace(/__dash__/g, '-');
27 return text;
28 }
29
30 function 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
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;
49 } else {
50 document.getElementById(name).value = chosenId;
51 }
52 win.close();
53 }
54
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) {
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
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);
76 if (elem) {
77 if (elem.nodeName == 'SELECT') {
78 var o = new Option(newRepr, newId);
79 elem.options[elem.options.length] = o;
80 o.selected = true;
81 } else if (elem.nodeName == 'INPUT') {
82 elem.value = newId;
83 }
84 } else {
85 var toId = name + "_to";
86 elem = document.getElementById(toId);
87 var o = new Option(newRepr, newId);
88 SelectBox.add_to_cache(toId, o);
89 SelectBox.redisplay(toId);
90 }
91 win.close();
92 }