11/** global: django */
22
3- if ( typeof ( django ) !== 'undefined' && typeof ( django . jQuery ) !== 'undefined' )
4- {
3+ if ( typeof ( django ) !== 'undefined' && typeof ( django . jQuery ) !== 'undefined' ) {
54 ( function ( $ ) {
65
7- $ ( document ) . ready ( function ( ) {
6+ $ ( document ) . ready ( function ( ) {
87
9- function dismissModal ( )
10- {
8+ function dismissModal ( ) {
119 $ . magnificPopup . close ( ) ;
1210 }
1311
1412 // create the function that will close the modal
15- function dismissRelatedObjectModal ( )
16- {
13+ function dismissRelatedObjectModal ( ) {
1714 dismissModal ( ) ;
1815 }
1916
20- function dismissRelatedLookupModal ( win , chosenId )
21- {
22- var windowRef = win ;
23- var windowName = windowRef . name ;
24- var widgetName = windowName . replace ( / ^ ( c h a n g e | a d d | d e l e t e | l o o k u p ) _ / , '' ) ;
25- var widgetEl = $ ( '#' + widgetName ) ;
26- var widgetVal = widgetEl . val ( ) ;
17+ function dismissRelatedLookupModal ( win , chosenId ) {
18+ const windowRef = win ;
19+ const windowName = windowRef . name ;
20+ const widgetName = windowName . replace ( / ^ ( c h a n g e | a d d | d e l e t e | l o o k u p ) _ / , '' ) ;
21+ const widgetEl = $ ( '#' + widgetName ) ;
22+ const widgetVal = widgetEl . val ( ) ;
2723 if ( widgetEl . hasClass ( 'vManyToManyRawIdAdminField' ) && Boolean ( widgetVal ) ) {
2824 widgetEl . val ( widgetVal + ', ' + chosenId ) ;
2925 } else {
@@ -36,11 +32,20 @@ if (typeof(django) !== 'undefined' && typeof(django.jQuery) !== 'undefined')
3632 window . dismissRelatedObjectModal = dismissRelatedObjectModal ;
3733 window . dismissRelatedLookupPopup = dismissRelatedLookupModal ;
3834
39- function presentRelatedObjectModal ( e )
40- {
41- var linkEl = $ ( this ) ;
35+ function updateURLParameter ( url , key , value ) {
36+ const urlPattern = / ^ h t t p s ? : \/ \/ / i;
37+ if ( ! urlPattern . test ( url ) ) {
38+ url = window . location . origin + url ;
39+ }
40+ const urlObject = new URL ( url ) ;
41+ urlObject . searchParams . set ( key , value ) ;
42+ return urlObject . toString ( ) ;
43+ }
44+
45+ function presentRelatedObjectModal ( e ) {
46+ const linkEl = $ ( this ) ;
4247
43- var href = ( linkEl . attr ( 'href' ) || '' ) ;
48+ const href = ( linkEl . attr ( 'href' ) || '' ) ;
4449 if ( href === '' ) {
4550 return ;
4651 }
@@ -54,50 +59,38 @@ if (typeof(django) !== 'undefined' && typeof(django.jQuery) !== 'undefined')
5459
5560 // use the clicked link id as iframe name
5661 // it will be available as window.name in the loaded iframe
57- var iframeName = linkEl . attr ( 'id' ) ;
58- var iframeSrc = href ;
62+ const iframeName = linkEl . attr ( 'id' ) ;
63+ let iframeSrc = href ;
5964
60- if ( e . data . lookup !== true )
61- {
65+ if ( e . data . lookup !== true ) {
6266 // browsers stop loading nested iframes having the same src url
6367 // create a random parameter and append it to the src url to prevent it
6468 // this workaround doesn't work with related lookup url
65- var iframeSrcRandom = String ( Math . round ( Math . random ( ) * 999999 ) ) ;
66- if ( iframeSrc . indexOf ( '?' ) === - 1 ) {
67- iframeSrc += '?_modal=' + iframeSrcRandom ;
68- } else {
69- iframeSrc += '&_modal=' + iframeSrcRandom ;
70- }
69+ const iframeSrcRandom = String ( Math . round ( Math . random ( ) * 999999 ) ) ;
70+ iframeSrc = updateURLParameter ( iframeSrc , '_modal' , iframeSrcRandom ) ;
7171 }
7272
73- // fix for django 1.7 TODO remove
74- if ( iframeSrc . indexOf ( '_popup=1' ) === - 1 ) {
75- if ( iframeSrc . indexOf ( '?' ) === - 1 ) {
76- iframeSrc += '?_popup=1' ;
77- } else {
78- iframeSrc += '&_popup=1' ;
79- }
80- }
73+ // fix for django 1.7 TODO remove
74+ iframeSrc = updateURLParameter ( iframeSrc , '_popup' , '1' ) ;
8175
8276 // build the iframe html
83- var iframeHTML = '<iframe id="related-modal-iframe" name="' + iframeName + '" src="' + iframeSrc + '"></iframe>' ;
84- var modalHTML = '<div class="related-modal-iframe-container">' + iframeHTML + '</div>' ;
85- var modalEl = $ ( modalHTML ) ;
86- var iframeEl = modalEl . find ( '#related-modal-iframe' ) ;
77+ const iframeHTML = '<iframe id="related-modal-iframe" name="' + iframeName + '" src="' + iframeSrc + '"></iframe>' ;
78+ const modalHTML = '<div class="related-modal-iframe-container">' + iframeHTML + '</div>' ;
79+ const modalEl = $ ( modalHTML ) ;
80+ const iframeEl = modalEl . find ( '#related-modal-iframe' ) ;
8781
88- if ( e . data . lookup === true )
89- {
82+ if ( e . data . lookup === true ) {
9083 // set current window as iframe opener because
9184 // the callback is called on the opener window
9285 iframeEl . on ( 'load' , function ( ) {
93- var iframeObj = $ ( this ) . get ( 0 ) ;
94- var iframeWindow = iframeObj . contentWindow ;
86+ const iframeObj = $ ( this ) . get ( 0 ) ;
87+ const iframeWindow = iframeObj . contentWindow ;
9588 iframeWindow . opener = window ;
9689 } ) ;
9790 }
9891
9992 // the modal css class
100- var iframeInternalModalClass = 'related-modal' ;
93+ let iframeInternalModalClass = 'related-modal' ;
10194
10295 // if the current window is inside an iframe, it means that it is already in a modal,
10396 // append an additional css class to the modal to offer more customization
@@ -122,11 +115,11 @@ if (typeof(django) !== 'undefined' && typeof(django.jQuery) !== 'undefined')
122115
123116 // listen click events on related links
124117 function presentRelatedObjectModalOnClickOn ( selector , lookup ) {
125- var data = {
126- lookup :( lookup === true ? true : false )
118+ const data = {
119+ lookup : ( lookup === true ? true : false )
127120 } ;
128121 // remove potential existing click event listener
129- var el = $ ( selector ) ;
122+ const el = $ ( selector ) ;
130123 el . removeAttr ( 'onclick' ) ;
131124 el . unbind ( 'click' ) ;
132125 el . click ( data , presentRelatedObjectModal ) ;
0 commit comments