Skip to content

Commit 3dc6aeb

Browse files
committed
fix counter
1 parent a0c70cc commit 3dc6aeb

2 files changed

Lines changed: 56 additions & 16 deletions

File tree

front/migration_status.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@
3838
/** @var DBmysql $DB */
3939
global $CFG_GLPI, $DB;
4040

41+
// Handle get_system_name AJAX action
42+
if ($_SERVER['REQUEST_METHOD'] === 'GET' && isset($_GET['action']) && $_GET['action'] === 'get_system_name') {
43+
header('Content-Type: application/json');
44+
echo json_encode(['system_name' => PluginGenericobjectType::getSystemName((string) ($_GET['name'] ?? ''))]);
45+
exit;
46+
}
47+
4148
// Handle rename POST action
4249
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['type_id'], $_POST['new_name'])) {
4350
$type_id = (int) $_POST['type_id'];

templates/migration_status.html.twig

Lines changed: 49 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -255,12 +255,32 @@
255255
title="{{ __('Only letters and numbers, must start with a letter.', 'genericobject') }}"
256256
data-counter-target="{{ modal_id }}-counter"
257257
data-warning-target="{{ modal_id }}-length-warning"
258+
data-result-target="{{ modal_id }}-result"
258259
oninput="glpiGenericobjectUpdateCounter(this)"
259260
required
260261
>
261262
<div class="d-flex justify-content-between align-items-center mt-1">
263+
264+
<div class="d-flex flex-grow-1 gap-2" style="min-width: 0;">
265+
<small class="text-muted">
266+
{{ __('Use system name:', 'genericobject') }}
267+
</small>
268+
269+
<small id="{{ modal_id }}-result"
270+
class="text-muted fw-bold text-truncate"
271+
style="min-width: 0; max-width: 50%;">
272+
{{ call('PluginGenericobjectType::getSystemName', [genericobject_type.name]) }}
273+
</small>
274+
</div>
275+
276+
<small id="{{ modal_id }}-counter"
277+
class="text-muted flex-shrink-0">
278+
{{ genericobject_type.name|length }}/{{ constant('PluginGenericobjectType::MAX_TYPE_NAME_LENGTH') }}
279+
</small>
280+
281+
</div>
282+
<div>
262283
<small class="text-muted">{{ __('Only letters and numbers, must start with a letter.', 'genericobject') }}</small>
263-
<small id="{{ modal_id }}-counter" class="text-muted">{{ genericobject_type.name|length }}/{{ constant('PluginGenericobjectType::MAX_TYPE_NAME_LENGTH') }}</small>
264284
</div>
265285
<div id="{{ modal_id }}-length-warning" class="text-danger small mt-1 d-none">
266286
<i class="ti ti-alert-circle me-1"></i>
@@ -417,29 +437,42 @@
417437
<script>
418438
function glpiGenericobjectUpdateCounter(input) {
419439
const maxLen = parseInt(input.getAttribute('maxlength'));
420-
const currentLen = input.value.length;
421440
const counterId = input.dataset.counterTarget;
422441
const warningId = input.dataset.warningTarget;
442+
const resultId = input.dataset.resultTarget;
423443
const counter = counterId ? document.getElementById(counterId) : null;
424444
const warning = warningId ? document.getElementById(warningId) : null;
445+
const result = resultId ? document.getElementById(resultId) : null;
425446
426-
if (counter) {
427-
counter.textContent = currentLen + '/' + maxLen;
428-
counter.className = 'small';
429-
if (currentLen >= maxLen) {
430-
counter.classList.add('text-danger', 'fw-bold');
431-
} else {
432-
counter.classList.add('text-muted');
447+
fetch(
448+
'{{ config('root_doc') }}/plugins/genericobject/front/migration_status.php?action=get_system_name&name=' + encodeURIComponent(input.value),
449+
)
450+
.then(r => r.json())
451+
.then(data => {
452+
const currentLen = data.system_name.length;
453+
if (counter) {
454+
counter.textContent = currentLen + '/' + maxLen;
455+
counter.className = 'small';
456+
if (currentLen >= maxLen) {
457+
counter.classList.add('text-danger', 'fw-bold');
458+
} else {
459+
counter.classList.add('text-muted');
460+
}
433461
}
434-
}
435462
436-
if (warning) {
437-
if (currentLen >= maxLen) {
438-
warning.classList.remove('d-none');
439-
} else {
440-
warning.classList.add('d-none');
463+
if (warning) {
464+
if (currentLen >= maxLen) {
465+
warning.classList.remove('d-none');
466+
} else {
467+
warning.classList.add('d-none');
468+
}
441469
}
442-
}
470+
471+
if (result) {
472+
result.textContent = data.system_name;
473+
}
474+
})
475+
.catch((e) => console.error(e.message));
443476
}
444477
445478
document.addEventListener('shown.bs.modal', function (event) {

0 commit comments

Comments
 (0)