bgneal@1: // Handles related-objects functionality: lookup link for raw_id_fields bgneal@1: // and Add Another links. bgneal@1: bgneal@1: function html_unescape(text) { bgneal@1: // Unescape a string that was escaped using django.utils.html.escape. bgneal@1: text = text.replace(/</g, '<'); bgneal@1: text = text.replace(/>/g, '>'); bgneal@1: text = text.replace(/"/g, '"'); bgneal@1: text = text.replace(/'/g, "'"); bgneal@1: text = text.replace(/&/g, '&'); bgneal@1: return text; bgneal@1: } bgneal@1: bgneal@1: // IE doesn't accept periods or dashes in the window name, but the element IDs bgneal@1: // we use to generate popup window names may contain them, therefore we map them bgneal@1: // to allowed characters in a reversible way so that we can locate the correct bgneal@1: // element when the popup window is dismissed. bgneal@1: function id_to_windowname(text) { bgneal@1: text = text.replace(/\./g, '__dot__'); bgneal@1: text = text.replace(/\-/g, '__dash__'); bgneal@1: return text; bgneal@1: } bgneal@1: bgneal@1: function windowname_to_id(text) { bgneal@1: text = text.replace(/__dot__/g, '.'); bgneal@1: text = text.replace(/__dash__/g, '-'); bgneal@1: return text; bgneal@1: } bgneal@1: bgneal@1: function showRelatedObjectLookupPopup(triggeringLink) { bgneal@1: var name = triggeringLink.id.replace(/^lookup_/, ''); bgneal@1: name = id_to_windowname(name); bgneal@1: var href; bgneal@1: if (triggeringLink.href.search(/\?/) >= 0) { bgneal@1: href = triggeringLink.href + '&pop=1'; bgneal@1: } else { bgneal@1: href = triggeringLink.href + '?pop=1'; bgneal@1: } bgneal@1: var win = window.open(href, name, 'height=500,width=800,resizable=yes,scrollbars=yes'); bgneal@1: win.focus(); bgneal@1: return false; bgneal@1: } bgneal@1: bgneal@1: function dismissRelatedLookupPopup(win, chosenId) { bgneal@1: var name = windowname_to_id(win.name); bgneal@1: var elem = document.getElementById(name); bgneal@1: if (elem.className.indexOf('vManyToManyRawIdAdminField') != -1 && elem.value) { bgneal@1: elem.value += ',' + chosenId; bgneal@1: } else { bgneal@1: document.getElementById(name).value = chosenId; bgneal@1: } bgneal@1: win.close(); bgneal@1: } bgneal@1: bgneal@1: function showAddAnotherPopup(triggeringLink) { bgneal@1: var name = triggeringLink.id.replace(/^add_/, ''); bgneal@1: name = id_to_windowname(name); bgneal@1: href = triggeringLink.href bgneal@1: if (href.indexOf('?') == -1) { bgneal@1: href += '?_popup=1'; bgneal@1: } else { bgneal@1: href += '&_popup=1'; bgneal@1: } bgneal@1: var win = window.open(href, name, 'height=500,width=800,resizable=yes,scrollbars=yes'); bgneal@1: win.focus(); bgneal@1: return false; bgneal@1: } bgneal@1: bgneal@1: function dismissAddAnotherPopup(win, newId, newRepr) { bgneal@1: // newId and newRepr are expected to have previously been escaped by bgneal@1: // django.utils.html.escape. bgneal@1: newId = html_unescape(newId); bgneal@1: newRepr = html_unescape(newRepr); bgneal@1: var name = windowname_to_id(win.name); bgneal@1: var elem = document.getElementById(name); bgneal@1: if (elem) { bgneal@1: if (elem.nodeName == 'SELECT') { bgneal@1: var o = new Option(newRepr, newId); bgneal@1: elem.options[elem.options.length] = o; bgneal@1: o.selected = true; bgneal@1: } else if (elem.nodeName == 'INPUT') { bgneal@1: elem.value = newId; bgneal@1: } bgneal@1: } else { bgneal@1: var toId = name + "_to"; bgneal@1: elem = document.getElementById(toId); bgneal@1: var o = new Option(newRepr, newId); bgneal@1: SelectBox.add_to_cache(toId, o); bgneal@1: SelectBox.redisplay(toId); bgneal@1: } bgneal@1: win.close(); bgneal@1: }