Fixed django 1.7 and django 1.9 compatibility

pull/27/head
Fabio Caccamo 2017-07-13 18:00:56 +02:00
parent 71ad991ba7
commit ed669a88be
2 changed files with 43 additions and 12 deletions

View File

@ -22,13 +22,23 @@
var initData = JSON.parse(document.getElementById('django-admin-popup-response-constants').dataset.popupResponse); var initData = JSON.parse(document.getElementById('django-admin-popup-response-constants').dataset.popupResponse);
switch(initData.action){ switch(initData.action){
case 'change': case 'change':
openerRef.dismissChangeRelatedObjectPopup(modalRef, initData.value, initData.obj, initData.new_value); if( typeof(openerRef.dismissChangeRelatedObjectPopup) == 'function' ){
openerRef.dismissChangeRelatedObjectPopup(modalRef, initData.value, initData.obj, initData.new_value);
}
break; break;
case 'delete': case 'delete':
openerRef.dismissDeleteRelatedObjectPopup(modalRef, initData.value); if( typeof(openerRef.dismissDeleteRelatedObjectPopup) == 'function' ){
openerRef.dismissDeleteRelatedObjectPopup(modalRef, initData.value);
}
break; break;
default: default:
openerRef.dismissAddRelatedObjectPopup(modalRef, initData.value, initData.obj); if( typeof(openerRef.dismissAddRelatedObjectPopup) == 'function' ){
openerRef.dismissAddRelatedObjectPopup(modalRef, initData.value, initData.obj);
}
else if( typeof(openerRef.dismissAddAnotherPopup) == 'function' ){
// django 1.7 compatibility
openerRef.dismissAddAnotherPopup(modalRef, initData.value, initData.obj);
}
break; break;
} }
})(); })();

View File

@ -7,18 +7,23 @@ if(typeof django !== 'undefined' && typeof django.jQuery !== 'undefined' )
// create the function that will close the modal // create the function that will close the modal
function dismissRelatedObjectModal() function dismissRelatedObjectModal()
{ {
// close the popup // close the popup as modal
$.magnificPopup.close(); $.magnificPopup.close();
} }
// assign the function to a global variable // assign the function to a global variable
window.dismissRelatedObjectModal = dismissRelatedObjectModal; window.dismissRelatedObjectModal = dismissRelatedObjectModal;
// listen click events on related links function presentRelatedObjectModal(e)
// (:link prevents to listen click event if href is not defined) {
$('a.related-widget-wrapper-link:link').click(function(e){ var href = ($(this).attr('href') || '');
if( href == '' ){
return;
}
// open the popup as modal
e.preventDefault(); e.preventDefault();
e.stopImmediatePropagation();
// remove focus from clicked link // remove focus from clicked link
$(this).blur(); $(this).blur();
@ -26,16 +31,23 @@ if(typeof django !== 'undefined' && typeof django.jQuery !== 'undefined' )
// use the clicked link id as iframe name // use the clicked link id as iframe name
// it will be available as window.name in the loaded iframe // it will be available as window.name in the loaded iframe
var iframeName = $(this).attr('id'); var iframeName = $(this).attr('id');
//var iframeName = String(window.name + '____' + $(this).attr('id')); // var iframeName = String(window.name + '____' + $(this).attr('id'));
//console.log('open modal with name: "' + iframeName + '"'); // console.log('open modal with name: "' + iframeName + '"');
// browsers stop loading nested iframes having the same src url // browsers stop loading nested iframes having the same src url
// create a random parameter and append it to the src url to prevent it // create a random parameter and append it to the src url to prevent it
var iframeSrcRandom = String(Math.round(Math.random() * 999999)); var iframeSrcRandom = String(Math.round(Math.random() * 999999));
var iframeSrc = $(this).attr('href') + '&_modal=' + iframeSrcRandom; var iframeSrc = href;
// fix for django 1.7
if( iframeSrc.indexOf('_popup=1') == -1 ){
iframeSrc += '&_popup=1';
}
iframeSrc += '&_modal=' + iframeSrcRandom;
// build the iframe html // build the iframe html
//var iframeHTML = '<iframe id="related-modal" name="' + iframeName + '" src="' + iframeSrc + '"></iframe>'; // var iframeHTML = '<iframe id="related-modal" name="' + iframeName + '" src="' + iframeSrc + '"></iframe>';
var iframeHTML = '<iframe id="related-modal-iframe" name="' + iframeName + '" src="' + iframeSrc + '"></iframe>'; var iframeHTML = '<iframe id="related-modal-iframe" name="' + iframeName + '" src="' + iframeSrc + '"></iframe>';
// create the iframe jquery element // create the iframe jquery element
@ -61,7 +73,16 @@ if(typeof django !== 'undefined' && typeof django.jQuery !== 'undefined' )
}); });
return false; return false;
}); }
// listen click events on related links
// django 1.7 compatibility
$('a.add-another').removeAttr('onclick');
$('a.add-another').click( presentRelatedObjectModal );
// django 1.8 and above
$('a.related-widget-wrapper-link').click( presentRelatedObjectModal );
}); });
})(django.jQuery); })(django.jQuery);