PageRenderTime 204ms CodeModel.GetById 100ms app.highlight 8ms RepoModel.GetById 93ms app.codeStats 0ms

/django/contrib/admin/media/js/admin/RelatedObjectLookups.js

https://code.google.com/p/mango-py/
JavaScript | 96 lines | 92 code | 1 blank | 3 comment | 0 complexity | 653864d199d928d2b70fe239980edacf MD5 | raw file
 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        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            if (elem.className.indexOf('vManyToManyRawIdAdminField') != -1 && elem.value) {
83                elem.value += ',' + newId;
84            } else {
85                elem.value = newId;
86            }
87        }
88    } else {
89        var toId = name + "_to";
90        elem = document.getElementById(toId);
91        var o = new Option(newRepr, newId);
92        SelectBox.add_to_cache(toId, o);
93        SelectBox.redisplay(toId);
94    }
95    win.close();
96}