Move tooltips JS helpers to dedicated class "CombodoGlobalToolbox => CombodoTooltip"

This commit is contained in:
Molkobain
2021-02-20 15:27:32 +01:00
parent 5bde218076
commit 9bbec47976
7 changed files with 47 additions and 45 deletions

View File

@@ -489,7 +489,7 @@ abstract class Dashboard
<<<EOF
// Note: the title gets deleted by the validation mechanism
$("#attr_auto_reload_sec").attr('data-tooltip-content', '$sRateTitle');
CombodoGlobalToolbox.InitTooltipFromMarkup($("#attr_auto_reload_sec"));
CombodoTooltip.InitTooltipFromMarkup($("#attr_auto_reload_sec"));
$("#attr_auto_reload_sec").prop('disabled', !$('#attr_auto_reload').is(':checked'));
$('#attr_auto_reload').change( function(ev) {

View File

@@ -266,7 +266,7 @@ class DesignerForm
$this->AddReadyScript(
<<<EOF
$('#row_$sFieldId').$sWidgetClass({parent_selector: $sNotifyParentSelectorJS, field_id: '$sFieldId', equals: $sHandlerEquals, get_field_value: $sHandlerGetValue, auto_apply: $sAutoApply, value: '', submit_to: '$sActionUrl', submit_parameters: $sJSSubmitParams $sJSExtraParams });
CombodoGlobalToolbox.InitTooltipFromMarkup($('#$sFormId [data-tooltip-content]'));
CombodoTooltip.InitTooltipFromMarkup($('#$sFormId [data-tooltip-content]'));
EOF
);
}

View File

@@ -450,14 +450,12 @@ class TableDetailsAttachmentsRenderer extends AbstractAttachmentsRenderer
$oAttachmentTableBlock = DataTableUIBlockFactory::MakeForStaticData('', $aAttribs, $aData);
$oAttachmentTableBlock->AddCSSClass('ibo-attachment--datatable');
$this->oPage->AddUiBlock($oAttachmentTableBlock);
$sTableId = $oAttachmentTableBlock->GetId();
$this->oPage->add_script(
$this->oPage->add_script(
<<<JS
$('#$sTableId').on('inited drawn', function(){
$('#$sTableId [data-tooltip-content]').each(function(){
CombodoGlobalToolbox.InitTooltipFromMarkup($(this), true);
});
CombodoTooltip.InitAllNonInstantiatedTooltips($(this), true);
});
JS
);

View File

@@ -502,30 +502,32 @@
{
ShowErrorDialog();
}
}
else
} else
{
ShowErrorDialog();
}
});
});
// Enable tooltips based on existing HTML markup, won't work on markup added dynamically after DOM ready (AJAX, ...) ...
CombodoGlobalToolbox.InitAllNonInstantiatedTooltips();
// ... except for object form which are handled with the following ...
$('body').on('form_built', function(oEvent){
CombodoGlobalToolbox.InitAllNonInstantiatedTooltips($(oEvent.target));
});
// ... and BS modals which are handle with the following
$('body').on('loaded.bs.modal', function (oEvent){
// Little timeout for stuff that need a moment to get ready but don't have a proper event.
setTimeout(function(){
//CombodoGlobalToolbox.InitAllNonInstantiatedTooltips($(oEvent.target));
}, 500);
});
// Initialize confirmation message handler when a form with touched fields is closed
$('body').portal_leave_handler({'message': '{{ 'Portal:Form:Close:Warning'|dict_s }}'});
{% endblock %}
// Enable tooltips based on existing HTML markup, won't work on markup added dynamically after DOM ready (AJAX, ...) ...
CombodoTooltip.InitAllNonInstantiatedTooltips();
// ... except for object form which are handled with the following ...
$('body').on('form_built', function (oEvent)
{
CombodoTooltip.InitAllNonInstantiatedTooltips($(oEvent.target));
});
// ... and BS modals which are handle with the following
$('body').on('loaded.bs.modal', function (oEvent)
{
// Little timeout for stuff that need a moment to get ready but don't have a proper event.
setTimeout(function ()
{
//CombodoTooltip.InitAllNonInstantiatedTooltips($(oEvent.target));
}, 500);
});
// Initialize confirmation message handler when a form with touched fields is closed
$('body').portal_leave_handler({'message': '{{ 'Portal:Form:Close:Warning'|dict_s }}'});
{% endblock %}
});
</script>
{% endblock %}

View File

@@ -88,7 +88,7 @@ $(function()
this.element.append(sMarkup);
CombodoGlobalToolbox.InitAllNonInstantiatedTooltips(this.element);
CombodoTooltip.InitAllNonInstantiatedTooltips(this.element);
},
_bindEvents: function () {
const me = this;
@@ -129,7 +129,7 @@ $(function()
oRemoveBtn.prop('disabled', false);
}
});
$('#remove_'+me.options.input_name).click(function () {
if ($(this).prop('disabled')) {
return;

View File

@@ -110,7 +110,7 @@ CKEDITOR.plugins.add( 'disabler',
$(document).ready(function(){
// Enable tooltips based on existing HTML markup, won't work on markup added dynamically after DOM ready (AJAX, ...)
$('[data-tooltip-content]:not([data-tooltip-instanciated="true"])').each(function(){
CombodoGlobalToolbox.InitTooltipFromMarkup($(this));
CombodoTooltip.InitTooltipFromMarkup($(this));
});
// Enable fullscreen togglers based on existing HTML markup, won't work on markup added dynamically after DOM ready (AJAX, ...)

View File

@@ -713,7 +713,6 @@ Dict.Format = function () {
}
// TODO 3.0.0: Move functions above either in CombodoGlobalToolbox or CombodoBackofficeToolbox and deprecate them
/**
* A toolbox for common JS operations accross the app no matter the GUI. Meant to be used by Combodo developers and the community.
*
@@ -722,8 +721,17 @@ Dict.Format = function () {
* @api
* @since 3.0.0
*/
const CombodoGlobalToolbox = {
// Instanciate tooltips (abstraction layer between iTop markup and tooltip plugin to ease its replacement in the future)
const CombodoGlobalToolbox = {};
/**
* Helper for tooltip instantiation (abstraction layer between iTop markup and tooltip plugin to ease its replacement in the future)
*
* Note: Content SHOULD be HTML entity encoded to avoid markup breaks (eg. when using a double quote in a sentence)
*
* @api
* @since 3.0.0
*/
const CombodoTooltip = {
/**
* Instanciate a tooltip on oElem from its data attributes
*
@@ -733,15 +741,13 @@ const CombodoGlobalToolbox = {
* @param {boolean} bForce When set to true, tooltip will be instanciate even if one already exists, overwritting it.
* @constructor
*/
InitTooltipFromMarkup: function(oElem, bForce = false)
{
InitTooltipFromMarkup: function (oElem, bForce = false) {
const oOptions = {
allowHTML: true, // Always true so line breaks can work. Don't worry content will be sanitized.
};
// First, check if the tooltip isn't already instanciated
if((oElem.attr('data-tooltip-instanciated') === 'true') && (bForce === false))
{
if ((oElem.attr('data-tooltip-instanciated') === 'true') && (bForce === false)) {
return false;
}
@@ -758,16 +764,12 @@ const CombodoGlobalToolbox = {
// - Sanitize content and make sure line breaks are kept
const oTmpContentElem = $('<div />').html(oElem.attr('data-tooltip-content'));
let sContent = '';
if(bEnableHTML)
{
if (bEnableHTML) {
sContent = oTmpContentElem.html();
if(bSanitizeContent)
{
if (bSanitizeContent) {
sContent = sContent.replace(/<script/g, '&lt;script WARNING: scripts are not allowed in tooltips');
}
}
else
{
} else {
sContent = oTmpContentElem.text();
sContent = sContent.replace(/(\r\n|\n\r|\r|\n)/g, '<br/>');
}
@@ -811,7 +813,7 @@ const CombodoGlobalToolbox = {
}
oContainerElem.find('[data-tooltip-content]:not([data-tooltip-instanciated="true"])').each(function () {
CombodoGlobalToolbox.InitTooltipFromMarkup($(this), bForce);
CombodoTooltip.InitTooltipFromMarkup($(this), bForce);
});
}
};