Skip to content

Commit 85a4d47

Browse files
committed
[PRAC/cont] Correct moving/fix view row/area logic
Change to "universal" row/client highligh logic, when "add/edit" data. Worth noting: - excluding code duplication. - improving the UX. core: B-3 / JS-BL
1 parent 72a42ba commit 85a4d47

File tree

1 file changed

+28
-10
lines changed
  • core-courses/3-js-basic-level/practicum-js-basic-level/sb-crm-client/js

1 file changed

+28
-10
lines changed

core-courses/3-js-basic-level/practicum-js-basic-level/sb-crm-client/js/index.js

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2695,10 +2695,14 @@
26952695
bootstrapModal.hide();
26962696
}
26972697

2698+
// выделение/показ только что добавленного/от редактируемого клиента/строки (исходя из типа)
26982699
if (type === 'add') {
2699-
// и напоследок.. выделение/показ только что добавленного клиента/строки
27002700
setTimeout(() => {
2701-
movingToLastNewTableRow(); // перемещение фокуса
2701+
moveToAndHighlightClientRow('add'); // перемещение фокуса на только, что добавленного клиента
2702+
}, 300); // временная задержка, больше.. чтобы модальное окно успело закрыться
2703+
} else if (type === 'edit' && clientData.id) {
2704+
setTimeout(() => {
2705+
moveToAndHighlightClientRow('edit', clientData.id); // перемещение фокуса на только, что от редактируемого клиента
27022706
}, 300); // временная задержка, больше.. чтобы модальное окно успело закрыться
27032707
}
27042708
}, 200);
@@ -2879,17 +2883,31 @@
28792883
saveButton.style.cursor = saveButton.disabled ? 'help' : 'pointer';
28802884
}
28812885

2882-
// ** перемещение/фиксация области просмотра на только что добавленном клиенте/на последней строке (выделение цветом)
2883-
function movingToLastNewTableRow() {
2886+
// ** перемещение/фиксация области просмотра на только что добавленном/от редактируемом клиенте, соответствующей строке (выделение цветом)
2887+
function moveToAndHighlightClientRow(type, clientId = null) {
28842888
if (!outputTable) {
28852889
console.error('Таблица НЕ обнаружена!');
28862890
return;
28872891
}
28882892

2889-
const lastNewTableRow = outTableBody.lastElementChild; // фиксация последней строки
2890-
if (!lastNewTableRow) return; // нет строки.. возврат
2893+
let clientRow;
2894+
2895+
if (type === 'add') {
2896+
clientRow = outTableBody.lastElementChild; // фиксация последней строки
2897+
} else if (type === 'edit' && clientId) {
2898+
clientRow = outTableBody.querySelector(`[data-server-id="${clientId}"]`); // фиксация строки по ID
2899+
}
2900+
2901+
if (!clientRow) {
2902+
console.warn(
2903+
`Не удалось найти строку клиента (${
2904+
type === 'add' ? 'последнюю' : `ID: ${clientId}`
2905+
})`
2906+
);
2907+
return; // нет строк.. возврат
2908+
}
28912909

2892-
// кого предстоит перекрасить/выделить
2910+
// кого предстоит перекрасить/выделить (какие ячейки?)
28932911
const highlightClasses = [
28942912
'crm__output-table-body-cell_fio',
28952913
'crm__output-table-body-cell_crt-d-time',
@@ -2898,14 +2916,14 @@
28982916

28992917
const defaultColorsMap = new Map(); // хранение исходных цветов/красок
29002918

2901-
// перемещение к "новому" клиенту/к последней строке таблицы
2902-
lastNewTableRow.scrollIntoView({
2919+
// перемещение к соответствующей строке таблицы
2920+
clientRow.scrollIntoView({
29032921
behavior: 'smooth',
29042922
block: 'center',
29052923
});
29062924

29072925
// изменение цвета/выделение строки
2908-
lastNewTableRow.querySelectorAll('td').forEach((td) => {
2926+
clientRow.querySelectorAll('td').forEach((td) => {
29092927
if (highlightClasses.some((cls) => td.classList.contains(cls))) {
29102928
defaultColorsMap.set(td, td.style.color); // сохранение default цвета
29112929
td.style.fontWeight = 'bold';

0 commit comments

Comments
 (0)