N°4051 Make sure js dependencies are met before resuming ajax calls js code

This commit is contained in:
Stephen Abello
2021-06-10 15:05:39 +02:00
parent 679542eaaa
commit bcfdf76b71
3 changed files with 132 additions and 111 deletions

View File

@@ -21,119 +21,132 @@
{% endfor %}
{% endblock %}
{% set sPromiseId = aPage.sPromiseId %}
{% if aPage.aJsFiles is not empty %}
{% set sId = oLayout.GetId() | sanitize(constant('utils::ENUM_SANITIZATION_FILTER_VARIABLE_NAME')) %}
{% block iboPageJsFiles %}
<script type="text/javascript">
var aFilesToLoad{{ sId }} = [];
{# manage Dictionary file if exist #}
var sUrlDictFile = '';
{% if aPage.sDictScript is not empty %}
if ($.inArray('{{ aPage.sDictScript|raw }}', aListJsFiles) == -1)
{
sUrlDictFile = "{{ aPage.sDictScript|raw }}";
aListJsFiles.push(sUrlDictFile);
}
{% endif %}
{# manage the other Js File #}
{% for sJsFile in aPage.aJsFiles %}
if ($.inArray('{{ sJsFile|raw }}', aListJsFiles) == -1)
{
aFilesToLoad{{ sId }}.push('{{ sJsFile|raw|add_itop_version }}');
aListJsFiles.push("{{ sJsFile|raw }}");
}
{% endfor %}
var iCurrentIdx{{ sId }} = 0;
var iFilesToLoadCount{{ sId }} = aFilesToLoad{{ sId }}.length;
if (iFilesToLoadCount{{ sId }}> 0)
{
var fLoadScript{{ sId }} = function () {
$.when(
$.ajax({
url: aFilesToLoad{{ sId }}[iCurrentIdx{{ sId }}],
dataType: 'script',
cache: true
})
)
.then(function () {
iCurrentIdx{{ sId }}++;
if (iCurrentIdx{{ sId }} !== iFilesToLoadCount{{ sId }})
{
fLoadScript{{ sId }}();
}
else
{
{% for sJsInlineOnDomReady in aPage.aJsInlineOnDomReady %}
{{ sJsInlineOnDomReady|raw }}
{% endfor %}
}
});
};
{# first load Dictionary if exist #}
if (sUrlDictFile != '')
{
$.ajax({
url: '{{ aPage.sDictScript|raw }}',
dataType: 'script',
cache: true
}).done(function (data) {
if (Dict != undefined && data.startsWith("var aDictEntries = {\""))
{
eval(data);
$.extend(Dict._entries, aDictEntries);
}
}).then(function () {
fLoadScript{{ sId }}();
});
}
else
{
fLoadScript{{ sId }}();
}
}
else
{
{% for sJsInlineOnDomReady in aPage.aJsInlineOnDomReady %}
{{ sJsInlineOnDomReady|raw }}
{% endfor %}
}
</script>
{% endblock %}
{% else %}
{% block iboPageJsInlineOnDomReady %}
{# first load Dictionnary if exist #}
{% if aPage.sDictScript is not empty %}
<script type="text/javascript">
window['{{ sPromiseId }}'] = new Promise(function(resolve, reject){
var aFilesToLoad{{ sId }} = [];
{# manage Dictionary file if exist #}
var sUrlDictFile = '';
{% if aPage.sDictScript is not empty %}
if ($.inArray('{{ aPage.sDictScript|raw }}', aListJsFiles) == -1)
{
aListJsFiles.push("{{ aPage.sDictScript|raw }}");
$.ajax({
url: '{{ aPage.sDictScript|raw }}',
dataType: 'script',
cache: true
}).done(function (data) {
if (Dict != undefined && data.startsWith("var aDictEntries = {\""))
{
eval(data);
$.extend(Dict._entries, aDictEntries);
}
}).then(function () {
{% for sJsInlineOnDomReady in aPage.aJsInlineOnDomReady %}
{{ sJsInlineOnDomReady|raw }}
{% endfor %}
});
sUrlDictFile = "{{ aPage.sDictScript|raw }}";
aListJsFiles.push(sUrlDictFile);
}
{% endif %}
{# manage the other Js File #}
{% for sJsFile in aPage.aJsFiles %}
if ($.inArray('{{ sJsFile|raw }}', aListJsFiles) == -1)
{
aFilesToLoad{{ sId }}.push('{{ sJsFile|raw|add_itop_version }}');
aListJsFiles.push("{{ sJsFile|raw }}");
}
{% endfor %}
var iCurrentIdx{{ sId }} = 0;
var iFilesToLoadCount{{ sId }} = aFilesToLoad{{ sId }}.length;
if (iFilesToLoadCount{{ sId }}> 0)
{
var fLoadScript{{ sId }} = function () {
$.when(
$.ajax({
url: aFilesToLoad{{ sId }}[iCurrentIdx{{ sId }}],
dataType: 'script',
cache: true
})
)
.then(function () {
iCurrentIdx{{ sId }}++;
if (iCurrentIdx{{ sId }} !== iFilesToLoadCount{{ sId }})
{
fLoadScript{{ sId }}();
}
else
{
{% for sJsInlineOnDomReady in aPage.aJsInlineOnDomReady %}
{{ sJsInlineOnDomReady|raw }}
{% endfor %}
resolve();
}
});
};
{# first load Dictionary if exist #}
if (sUrlDictFile != '')
{
$.ajax({
url: '{{ aPage.sDictScript|raw }}',
dataType: 'script',
cache: true
}).done(function (data) {
if (Dict != undefined && data.startsWith("var aDictEntries = {\""))
{
eval(data);
$.extend(Dict._entries, aDictEntries);
}
}).then(function () {
fLoadScript{{ sId }}();
});
}
else
{
fLoadScript{{ sId }}();
}
}
else
{
{% for sJsInlineOnDomReady in aPage.aJsInlineOnDomReady %}
{{ sJsInlineOnDomReady|raw }}
{% endfor %}
resolve();
}
});
</script>
{% endblock %}
{% else %}
{% block iboPageJsInlineOnDomReady %}
{# first load Dictionnary if exist #}
{% if aPage.sDictScript is not empty %}
<script type="text/javascript">
window['{{ sPromiseId }}'] = new Promise(function(resolve, reject){
if ($.inArray('{{ aPage.sDictScript|raw }}', aListJsFiles) == -1)
{
aListJsFiles.push("{{ aPage.sDictScript|raw }}");
$.ajax({
url: '{{ aPage.sDictScript|raw }}',
dataType: 'script',
cache: true
}).done(function (data) {
if (Dict != undefined && data.startsWith("var aDictEntries = {\""))
{
eval(data);
$.extend(Dict._entries, aDictEntries);
}
}).then(function () {
{% for sJsInlineOnDomReady in aPage.aJsInlineOnDomReady %}
{{ sJsInlineOnDomReady|raw }}
{% endfor %}
resolve();
});
}
else
{
{% for sJsInlineOnDomReady in aPage.aJsInlineOnDomReady %}
{{ sJsInlineOnDomReady|raw }}
{% endfor %}
resolve();
}
});
</script>
{% else %}
{% for sJsInlineOnDomReady in aPage.aJsInlineOnDomReady %}
<script type="text/javascript">
{{ sJsInlineOnDomReady|raw }}
window['{{ sPromiseId }}'] = new Promise(function(resolve, reject){
{{ sJsInlineOnDomReady|raw }}
resolve();
});
</script>
{% endfor %}
{% endif %}