| /* |
| * Licensed to the Apache Software Foundation (ASF) under one or more |
| * contributor license agreements. See the NOTICE file distributed with |
| * this work for additional information regarding copyright ownership. |
| * The ASF licenses this file to You under the Apache License, Version 2.0 |
| * (the "License"); you may not use this file except in compliance with |
| * the License. You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| /*global SelectBox, interpolate*/ |
| // Handles related-objects functionality: lookup link for raw_id_fields |
| // and Add Another links. |
| |
| (function($) { |
| 'use strict'; |
| |
| function html_unescape(text) { |
| // Unescape a string that was escaped using django.utils.html.escape. |
| text = text.replace(/</g, '<'); |
| text = text.replace(/>/g, '>'); |
| text = text.replace(/"/g, '"'); |
| text = text.replace(/'/g, "'"); |
| text = text.replace(/&/g, '&'); |
| return text; |
| } |
| |
| // IE doesn't accept periods or dashes in the window name, but the element IDs |
| // we use to generate popup window names may contain them, therefore we map them |
| // to allowed characters in a reversible way so that we can locate the correct |
| // element when the popup window is dismissed. |
| function id_to_windowname(text) { |
| text = text.replace(/\./g, '__dot__'); |
| text = text.replace(/\-/g, '__dash__'); |
| return text; |
| } |
| |
| function windowname_to_id(text) { |
| text = text.replace(/__dot__/g, '.'); |
| text = text.replace(/__dash__/g, '-'); |
| return text; |
| } |
| |
| function showAdminPopup(triggeringLink, name_regexp, add_popup) { |
| var name = triggeringLink.id.replace(name_regexp, ''); |
| name = id_to_windowname(name); |
| var href = triggeringLink.href; |
| if (add_popup) { |
| if (href.indexOf('?') === -1) { |
| href += '?_popup=1'; |
| } else { |
| href += '&_popup=1'; |
| } |
| } |
| // GRAPPELLI CUSTOM: changed width |
| var win = window.open(href, name, 'height=500,width=1000,resizable=yes,scrollbars=yes'); |
| win.focus(); |
| return false; |
| } |
| |
| function showRelatedObjectLookupPopup(triggeringLink) { |
| return showAdminPopup(triggeringLink, /^lookup_/, true); |
| } |
| |
| function dismissRelatedLookupPopup(win, chosenId) { |
| var name = windowname_to_id(win.name); |
| var elem = document.getElementById(name); |
| if (elem.className.indexOf('vManyToManyRawIdAdminField') !== -1 && elem.value) { |
| elem.value += ',' + chosenId; |
| } else { |
| document.getElementById(name).value = chosenId; |
| } |
| // GRAPPELLI CUSTOM: element focus |
| elem.focus(); |
| win.close(); |
| } |
| |
| function showRelatedObjectPopup(triggeringLink) { |
| return showAdminPopup(triggeringLink, /^(change|add|delete)_/, false); |
| } |
| |
| function updateRelatedObjectLinks(triggeringLink) { |
| var $this = grp.jQuery(triggeringLink); |
| var siblings = $this.nextAll('.change-related, .delete-related'); |
| if (!siblings.length) { |
| return; |
| } |
| var value = $this.val(); |
| if (value) { |
| siblings.each(function() { |
| var elm = grp.jQuery(this); |
| elm.attr('href', elm.attr('data-href-template').replace('__fk__', value)); |
| }); |
| } else { |
| siblings.removeAttr('href'); |
| } |
| } |
| |
| function dismissAddRelatedObjectPopup(win, newId, newRepr) { |
| // newId and newRepr are expected to have previously been escaped by |
| // django.utils.html.escape. |
| newId = html_unescape(newId); |
| newRepr = html_unescape(newRepr); |
| var name = windowname_to_id(win.name); |
| var elem = document.getElementById(name); |
| if (elem) { |
| var elemName = elem.nodeName.toUpperCase(); |
| if (elemName === 'SELECT') { |
| elem.options[elem.options.length] = new Option(newRepr, newId, true, true); |
| } else if (elemName === 'INPUT') { |
| if (elem.className.indexOf('vManyToManyRawIdAdminField') !== -1 && elem.value) { |
| elem.value += ',' + newId; |
| } else { |
| elem.value = newId; |
| } |
| // GRAPPELLI CUSTOM: element focus |
| elem.focus(); |
| } |
| // Trigger a change event to update related links if required. |
| grp.jQuery(elem).trigger('change'); |
| } else { |
| var toId = name + "_to"; |
| var o = new Option(newRepr, newId); |
| SelectBox.add_to_cache(toId, o); |
| SelectBox.redisplay(toId); |
| } |
| win.close(); |
| } |
| |
| function dismissChangeRelatedObjectPopup(win, objId, newRepr, newId) { |
| objId = html_unescape(objId); |
| newRepr = html_unescape(newRepr); |
| var id = windowname_to_id(win.name).replace(/^edit_/, ''); |
| var selectsSelector = interpolate('#%s, #%s_from, #%s_to', [id, id, id]); |
| var selects = grp.jQuery(selectsSelector); |
| selects.find('option').each(function() { |
| if (this.value === objId) { |
| this.innerHTML = newRepr; |
| this.value = newId; |
| } |
| }); |
| // GRAPPELLI CUSTOM: element focus |
| elem.focus(); |
| win.close(); |
| } |
| |
| function dismissDeleteRelatedObjectPopup(win, objId) { |
| objId = html_unescape(objId); |
| var id = windowname_to_id(win.name).replace(/^delete_/, ''); |
| var selectsSelector = interpolate('#%s, #%s_from, #%s_to', [id, id, id]); |
| var selects = grp.jQuery(selectsSelector); |
| selects.find('option').each(function() { |
| if (this.value === objId) { |
| grp.jQuery(this).remove(); |
| } |
| }).trigger('change'); |
| // GRAPPELLI CUSTOM: element focus |
| elem.focus(); |
| win.close(); |
| } |
| |
| // GRAPPELLI CUSTOM |
| function removeRelatedObject(triggeringLink) { |
| var id = triggeringLink.id.replace(/^remove_/, ''); |
| var elem = document.getElementById(id); |
| elem.value = ""; |
| elem.focus(); |
| } |
| window.removeRelatedObject = removeRelatedObject; |
| |
| // Global for testing purposes |
| window.html_unescape = html_unescape; |
| window.id_to_windowname = id_to_windowname; |
| window.windowname_to_id = windowname_to_id; |
| |
| window.showRelatedObjectLookupPopup = showRelatedObjectLookupPopup; |
| window.dismissRelatedLookupPopup = dismissRelatedLookupPopup; |
| window.showRelatedObjectPopup = showRelatedObjectPopup; |
| window.updateRelatedObjectLinks = updateRelatedObjectLinks; |
| window.dismissAddRelatedObjectPopup = dismissAddRelatedObjectPopup; |
| window.dismissChangeRelatedObjectPopup = dismissChangeRelatedObjectPopup; |
| window.dismissDeleteRelatedObjectPopup = dismissDeleteRelatedObjectPopup; |
| |
| // Kept for backward compatibility |
| window.showAddAnotherPopup = showRelatedObjectPopup; |
| window.dismissAddAnotherPopup = dismissAddRelatedObjectPopup; |
| |
| $(document).ready(function() { |
| $('body').on('click', '.related-widget-wrapper-link', function(e) { |
| e.preventDefault(); |
| if (this.href) { |
| var event = $.Event('django:show-related', {href: this.href}); |
| $(this).trigger(event); |
| if (!event.isDefaultPrevented()) { |
| showRelatedObjectPopup(this); |
| } |
| } |
| }); |
| $('body').on('change', '.related-widget-wrapper select', function(e) { |
| var event = $.Event('django:update-related'); |
| $(this).trigger(event); |
| if (!event.isDefaultPrevented()) { |
| updateRelatedObjectLinks(this); |
| } |
| }); |
| // GRAPPELLI CUSTOM |
| /* triggering select means that update_lookup is triggered with |
| generic autocompleted (which would empty the field) */ |
| // $('.related-widget-wrapper select').trigger('change'); |
| }); |
| |
| })(grp.jQuery); |