Portal: Refactor creation of modal dialog through a common helper (CreatePortalModal(oOptions))

This commit is contained in:
Molkobain
2019-07-31 15:50:09 +02:00
parent 661ecc57c5
commit 66287757b3
7 changed files with 134 additions and 153 deletions

View File

@@ -34,30 +34,57 @@ var CreatePortalModal = function (oOptions)
true,
{
id: null, // ID of the created modal
attributes: {}, // HTML attributes
base_modal: {
usage: 'clone', // Either 'clone' or 'replace'
selector: '#modal-for-all' // Selector of the modal element used to base this one on
usage: 'clone', // Either 'clone' or 'replace'
selector: '#modal-for-all' // Either a selector of the modal element used to base this one on or the modal element itself
},
content: '', // Either a string or an object containing the endpoint / data
content: undefined, // Either a string, an object containing the endpoint / data or undefined to keep base modal content as-is
size: 'lg', // Either 'xs' / 'sm' / 'md' / 'lg'
},
oOptions
);
// Compute modal selector
var oSelectorElem = null;
switch(typeof oOptions.base_modal.selector)
{
case 'string':
oSelectorElem = $(oOptions.base_modal.selector);
break;
case 'object':
oSelectorElem = oOptions.base_modal.selector;
break;
default:
if (window.console && window.console.warn)
{
console.warn('Could not open modal dialog as the select option was malformed: ', oOptions.content);
}
break;
}
// Get modal element by either
var oModalElem = null;
// - Create a new modal from template
// Note : This could be better if we check for an existing modal first instead of always creating a new one
if (oOptions.base_modal.usage === 'clone')
{
oModalElem = $(oOptions.base_modal.selector).clone();
oModalElem = oSelectorElem.clone();
oModalElem.attr('id', oOptions.id)
.appendTo('body');
}
// - Get an existing modal in the DOM
else
{
oModalElem = $(oOptions.base_modal.selector);
oModalElem = oSelectorElem;
}
// Set attributes
for(var sProp in oOptions.attributes)
{
oModalElem.attr(sProp, oOptions.attributes[sProp]);
}
// Resize to small modal
@@ -90,15 +117,21 @@ var CreatePortalModal = function (oOptions)
);
break;
case 'undefined':
// Do nothing, we keep the content as-is
break;
default:
if (window.console)
if (window.console && window.console.warn)
{
console.log('Could not open modal dialog as the content option was malformed: ', oOptions.content);
console.warn('Could not open modal dialog as the content option was malformed: ', oOptions.content);
}
}
// Show modal
oModalElem.modal('show');
return oModalElem;
};
$(document).ready(function()

View File

@@ -167,26 +167,16 @@ $(function()
if(bRedirectionAjax)
{
// Creating a new modal
var oModalElem = $('#modal-for-all').clone();
oModalElem.attr('id', '').appendTo('body');
// Loading content
oModalElem.find('.modal-content').html($('#page_overlay .overlay_content').html());
oModalElem.find('.modal-content').load(sUrl, {
// Passing formmanager data to the next page, just in case it needs it (eg. when applying stimulus)
formmanager_class: me.options.formmanager_class,
formmanager_data: JSON.stringify(me.options.formmanager_data)
CreatePortalModal({
content: {
endpoint: sUrl,
data: {
// Passing form manager data to the next page, just in case it needs it (eg. when applying stimulus)
formmanager_class: me.options.formmanager_class,
formmanager_data: JSON.stringify(me.options.formmanager_data)
},
},
function(oData, sStatus, oXHR)
{
if(sStatus === 'error')
{
// Hiding modal in case of error as the general AJAX error handler will display a message
oModalElem.modal('hide');
}
}
);
oModalElem.modal('show');
});
}
else
{

View File

@@ -833,7 +833,7 @@ class ObjectFormManager extends FormManager
if ($oField instanceof SelectObjectField)
{
// - Set if remote object can be accessed
if ($this->oContainer !== null && !$oAttDef->IsNull($oField->GetCurrentValue()))
if ($this->oContainer !== null && !$oAttDef->IsNull($oField->GetCurrentValue()) && !is_null($oField->GetSearch()))
{
$sRemoteObjectFieldClass = $oField->GetSearch()->GetClass();
$sRemoteObjectFieldId = $oField->GetCurrentValue();

View File

@@ -19,21 +19,21 @@
$('#{{ sLeafClassesListId }} a').off('click').on('click', function(oEvent){
oEvent.preventDefault();
// Preparing target class url
var sUrl = '{{ app['url_generator'].generate('p_object_create', {sObjectClass : '-sObjectClass-'})|raw }}';
var oModalElem = $(this).closest('.modal');
// Showing loader
oModalElem.find('.modal-content').html($('#page_overlay .overlay_content').html());
// Preparing target class url
sUrl = sUrl.replace(/-sObjectClass-/, $(this).attr('data-target-class') );
sUrl = AddParameterToUrl(sUrl, 'ar_token', '{{ ar_token }}');
// Loading form
oModalElem.find('.modal-content').load(sUrl, function(oData, sStatus, oXHR){
if(sStatus === 'error')
{
// Hiding modal in case of error as the general AJAX error handler will display a message
oModalElem.modal('hide');
}
});
sUrl = sUrl.replace(/-sObjectClass-/, $(this).attr('data-target-class') );
sUrl = AddParameterToUrl(sUrl, 'ar_token', '{{ ar_token }}');
// Creating a new modal
CreatePortalModal({
base_modal: {
usage: 'replace',
selector: $(this).closest('.modal'),
},
content: {
endpoint: sUrl,
},
});
});
});
</script>

View File

@@ -142,23 +142,12 @@
// Prevents row selection
oEvent.stopPropagation();
// Note : This could be better if we check for an existing modal first instead of always creating a new one
var oModalElem = $('#modal-for-all').clone();
oModalElem.attr('id', '').appendTo('body');
// Loading content
oModalElem.find('.modal-content').html($('#page_overlay .overlay_content').html());
oModalElem.find('.modal-content').load(
$(this).attr('href'),
{},
function(sResponseText, sStatus, oXHR){
// Hiding modal in case of error as the general AJAX error handler will display a message
if(sStatus === 'error')
{
oModalElem.modal('hide');
}
}
);
oModalElem.modal('show');
// Creating a new modal
CreatePortalModal({
content: {
endpoint: $(this).attr('href'),
},
});
});
},
"drawCallback": function(settings){