mirror of
https://github.com/Combodo/iTop.git
synced 2026-04-22 18:18:46 +02:00
Fix and simplify dynamic dictionaries load in ajax pages
This commit is contained in:
@@ -20,8 +20,6 @@ class AjaxPage extends WebPage implements iTabbedPage
|
|||||||
*/
|
*/
|
||||||
protected $m_oTabs;
|
protected $m_oTabs;
|
||||||
private $m_sMenu; // If set, then the menu will be updated
|
private $m_sMenu; // If set, then the menu will be updated
|
||||||
/** @var string Scripts to load entries of dictionary */
|
|
||||||
protected $s_dict_scripts;
|
|
||||||
|
|
||||||
const DEFAULT_PAGE_TEMPLATE_REL_PATH = 'pages/backoffice/ajaxpage/layout';
|
const DEFAULT_PAGE_TEMPLATE_REL_PATH = 'pages/backoffice/ajaxpage/layout';
|
||||||
/** @var string */
|
/** @var string */
|
||||||
@@ -161,22 +159,10 @@ class AjaxPage extends WebPage implements iTabbedPage
|
|||||||
header($s_header);
|
header($s_header);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Prepare internal parts (js files, css files, js snippets, css snippets, ...)
|
||||||
// - Generate necessary dict. files
|
// - Generate necessary dict. files
|
||||||
// Dict entries for JS
|
|
||||||
if ($this->bAddJSDict) {
|
if ($this->bAddJSDict) {
|
||||||
if ((count($this->a_dict_entries) > 0) || (count($this->a_dict_entries_prefixes) > 0)) {
|
$this->output_dict_entries();
|
||||||
if (class_exists('Dict')) {
|
|
||||||
// The dictionary may not be available for example during the setup...
|
|
||||||
// Create a specific dictionary file and load it as a JS script
|
|
||||||
$sSignature = $this->get_dict_signature();
|
|
||||||
$sJSFileName = utils::GetCachePath().$sSignature.'.js';
|
|
||||||
if (!file_exists($sJSFileName) && is_writable(utils::GetCachePath())) {
|
|
||||||
file_put_contents($sJSFileName, $this->get_dict_file_content());
|
|
||||||
}
|
|
||||||
// Load the dictionary as the first javascript file, so that other JS file benefit from the translations
|
|
||||||
$this->s_dict_scripts = utils::GetAbsoluteUrlAppRoot().'pages/ajax.document.php?operation=dict&s='.$sSignature;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ConsoleBlockRenderer::AddCssJsToPage($this, $this->oContentLayout);
|
ConsoleBlockRenderer::AddCssJsToPage($this, $this->oContentLayout);
|
||||||
@@ -200,7 +186,6 @@ class AjaxPage extends WebPage implements iTabbedPage
|
|||||||
'aCssInline' => $this->a_styles,
|
'aCssInline' => $this->a_styles,
|
||||||
'aJsFiles' => $this->a_linked_scripts,
|
'aJsFiles' => $this->a_linked_scripts,
|
||||||
'aJsInlineLive' => $this->a_scripts,
|
'aJsInlineLive' => $this->a_scripts,
|
||||||
'sDictScript' => $this->s_dict_scripts,
|
|
||||||
'aJsInlineOnDomReady' => $this->GetReadyScripts(),
|
'aJsInlineOnDomReady' => $this->GetReadyScripts(),
|
||||||
'aJsInlineOnInit' => $this->a_init_scripts,
|
'aJsInlineOnInit' => $this->a_init_scripts,
|
||||||
'bEscapeContent' => ($this->sContentType == 'text/html') && ($this->sContentDisposition == 'inline'),
|
'bEscapeContent' => ($this->sContentType == 'text/html') && ($this->sContentDisposition == 'inline'),
|
||||||
|
|||||||
@@ -702,7 +702,18 @@ class WebPage implements Page
|
|||||||
{
|
{
|
||||||
$aEntries = array_merge($aEntries, Dict::ExportEntries($sPrefix));
|
$aEntries = array_merge($aEntries, Dict::ExportEntries($sPrefix));
|
||||||
}
|
}
|
||||||
$sJSFile = 'var aDictEntries = '.json_encode($aEntries);
|
|
||||||
|
$sEntriesAsJson = json_encode($aEntries);
|
||||||
|
$sJSFile = <<<JS
|
||||||
|
// Create variable so it can be used by the Dict class on initialization
|
||||||
|
var aDictEntries = {$sEntriesAsJson};
|
||||||
|
|
||||||
|
// Check if Dict._entries already exists in order to complete, this is for async calls only.
|
||||||
|
// Note: We should not overload the WebPage::get_dict_file_content() in AjaxPage to put the part below as the same dict file can be consumed either by a regular page or an async page.
|
||||||
|
if (typeof Dict._entries != "undefined") {
|
||||||
|
$.extend(Dict._entries, aDictEntries);
|
||||||
|
}
|
||||||
|
JS;
|
||||||
|
|
||||||
return $sJSFile;
|
return $sJSFile;
|
||||||
}
|
}
|
||||||
@@ -1087,15 +1098,16 @@ class WebPage implements Page
|
|||||||
header($sHeader);
|
header($sHeader);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Compute and add dict entries for JS
|
|
||||||
if ($this->bAddJSDict) {
|
|
||||||
$this->output_dict_entries();
|
|
||||||
}
|
|
||||||
|
|
||||||
$s_captured_output = $this->ob_get_clean_safe();
|
$s_captured_output = $this->ob_get_clean_safe();
|
||||||
|
|
||||||
$aData = [];
|
$aData = [];
|
||||||
|
|
||||||
|
// Prepare internal parts (js files, css files, js snippets, css snippets, ...)
|
||||||
|
// - Generate necessary dict. files
|
||||||
|
if ($this->bAddJSDict) {
|
||||||
|
$this->output_dict_entries();
|
||||||
|
}
|
||||||
|
|
||||||
$aData['oLayout'] = $this->oContentLayout;
|
$aData['oLayout'] = $this->oContentLayout;
|
||||||
$aData['aDeferredBlocks'] = $this->GetDeferredBlocks($this->oContentLayout);
|
$aData['aDeferredBlocks'] = $this->GetDeferredBlocks($this->oContentLayout);
|
||||||
|
|
||||||
|
|||||||
@@ -28,23 +28,14 @@
|
|||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
window['{{ sPromiseId }}'] = new Promise(function(resolve, reject){
|
window['{{ sPromiseId }}'] = new Promise(function(resolve, reject){
|
||||||
var aFilesToLoad{{ sId }} = [];
|
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 %}
|
{% for sJsFile in aPage.aJsFiles %}
|
||||||
if ($.inArray('{{ sJsFile|raw }}', aListJsFiles) == -1)
|
if ($.inArray('{{ sJsFile|add_itop_version|raw }}', aListJsFiles) === -1)
|
||||||
{
|
{
|
||||||
aFilesToLoad{{ sId }}.push('{{ sJsFile|raw|add_itop_version }}');
|
aFilesToLoad{{ sId }}.push('{{ sJsFile|add_itop_version|raw }}');
|
||||||
aListJsFiles.push("{{ sJsFile|raw }}");
|
aListJsFiles.push("{{ sJsFile|add_itop_version|raw }}");
|
||||||
}
|
}
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
|
|
||||||
var iCurrentIdx{{ sId }} = 0;
|
var iCurrentIdx{{ sId }} = 0;
|
||||||
var iFilesToLoadCount{{ sId }} = aFilesToLoad{{ sId }}.length;
|
var iFilesToLoadCount{{ sId }} = aFilesToLoad{{ sId }}.length;
|
||||||
if (iFilesToLoadCount{{ sId }}> 0)
|
if (iFilesToLoadCount{{ sId }}> 0)
|
||||||
@@ -66,38 +57,18 @@
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
{% for sJsInlineOnDomReady in aPage.aJsInlineOnDomReady %}
|
{% for sJsInlineOnDomReady in aPage.aJsInlineOnDomReady %}
|
||||||
{{ sJsInlineOnDomReady|raw }}
|
{{ sJsInlineOnDomReady|raw }}
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
resolve();
|
resolve();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
{# first load Dictionary if exist #}
|
fLoadScript{{ sId }}();
|
||||||
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
|
else
|
||||||
{
|
{
|
||||||
{% for sJsInlineOnDomReady in aPage.aJsInlineOnDomReady %}
|
{% for sJsInlineOnDomReady in aPage.aJsInlineOnDomReady %}
|
||||||
{{ sJsInlineOnDomReady|raw }}
|
{{ sJsInlineOnDomReady|raw }}
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
resolve();
|
resolve();
|
||||||
}
|
}
|
||||||
@@ -106,50 +77,14 @@
|
|||||||
{% endblock %}
|
{% endblock %}
|
||||||
{% else %}
|
{% else %}
|
||||||
{% block iboPageJsInlineOnDomReady %}
|
{% block iboPageJsInlineOnDomReady %}
|
||||||
|
{% for sJsInlineOnDomReady in aPage.aJsInlineOnDomReady %}
|
||||||
{# first load Dictionnary if exist #}
|
|
||||||
{% if aPage.sDictScript is not empty %}
|
|
||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
window['{{ sPromiseId }}'] = new Promise(function(resolve, reject){
|
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 }}
|
{{ sJsInlineOnDomReady|raw }}
|
||||||
{% endfor %}
|
|
||||||
resolve();
|
resolve();
|
||||||
}
|
});
|
||||||
});
|
|
||||||
</script>
|
</script>
|
||||||
{% else %}
|
{% endfor %}
|
||||||
{% for sJsInlineOnDomReady in aPage.aJsInlineOnDomReady %}
|
|
||||||
<script type="text/javascript">
|
|
||||||
window['{{ sPromiseId }}'] = new Promise(function(resolve, reject){
|
|
||||||
{{ sJsInlineOnDomReady|raw }}
|
|
||||||
resolve();
|
|
||||||
});
|
|
||||||
</script>
|
|
||||||
{% endfor %}
|
|
||||||
{% endif %}
|
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user