Move code for showEyeForField into a dedicated function.

This commit is contained in:
ldestailleur 2025-04-15 13:38:59 +02:00
parent 708a6fa527
commit e26744d625
2 changed files with 37 additions and 21 deletions

View file

@ -589,3 +589,36 @@ function dolJSToSetRandomPassword($htmlname, $htmlnameofbutton = 'generate_token
return $out;
}
/**
* Output the eye picto to show/hide a password HTML field.
*
* @param string $htmlname HTML name of element to insert key into
* @param string $htmlnameofinput HTML id of input field
* @return string HTML javascript code to set a password
*/
function showEyeForField($htmlname, $htmlnameofinput)
{
return '<!-- code to manage the eye hide/show -->
<span id="'.$htmlname.'" tabindex="-1"><span class="fa fa-eye"></span></span>
<script nonce="<?php echo getNonce(); ?>">
$(document).ready(function () {
$(\'#'.$htmlname.'\').on(\'click\', function (e) {
e.preventDefault();
if (event.detail === 0) return false; // Ignore keyboard "clicks"
console.log("We click on '.$htmlname.'");
const $passwordInput = $(\'#'.$htmlnameofinput.'\');
if ($passwordInput.is(\'[type=password]\')) {
$passwordInput.attr(\'type\', \'text\');
jQuery(\'#'.$htmlname.' .fa-eye\').attr(\'class\', \'fa fa-eye-slash\');
} else {
$passwordInput.attr(\'type\', \'password\');
jQuery(\'#'.$htmlname.' .fa-eye-slash\').attr(\'class\', \'fa fa-eye\');
}
return false; // This prevents the click from reloading the page
});
});
</script>';
}

View file

@ -306,28 +306,11 @@ if ($disablenofollow) {
<!--<span class="span-icon-password">-->
<span class="fa fa-key"></span>
<input type="password" id="password" maxlength="128" placeholder="<?php echo $langs->trans("Password"); ?>" name="password" class="flat input-icon-password minwidth150" value="<?php echo dol_escape_htmltag($password); ?>" tabindex="2" autocomplete="<?php echo !getDolGlobalString('MAIN_LOGIN_ENABLE_PASSWORD_AUTOCOMPLETE') ? 'off' : 'on'; ?>" />
<span id="togglepassword" tabindex="-1"><span class="fa fa-eye"></span></span>
<?php
include_once DOL_DOCUMENT_ROOT.'/core/lib/security2.lib.php';
print showEyeForField('togglepassword', 'password');
?>
</div></div>
<script nonce="<?php echo getNonce(); ?>">
$(document).ready(function () {
$('#togglepassword').on('click', function (e) {
e.preventDefault();
if (event.detail === 0) return false; // Ignore keyboard "clicks"
console.log("We click on togglepassword");
const $passwordInput = $('#password');
if ($passwordInput.is('[type=password]')) {
$passwordInput.attr('type', 'text');
jQuery('#togglepassword .fa-eye').attr('class', 'fa fa-eye-slash');
} else {
$passwordInput.attr('type', 'password');
jQuery('#togglepassword .fa-eye-slash').attr('class', 'fa fa-eye');
}
return false; // This prevents the click from reloading the page
});
});
</script>
<?php } ?>