Skip to content

Commit e4a74b4

Browse files
committed
[PRAC/cont] Add logic to change icons/direction
Organiz svg/arrow direction change in header cells (up/down). Worth noting: - realization of toggle() methods and a ternary operator. core: B-3 / JS-BL
1 parent 7640489 commit e4a74b4

File tree

2 files changed

+63
-20
lines changed

2 files changed

+63
-20
lines changed

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

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -69,17 +69,20 @@
6969

7070
.head-cell__icon {
7171
padding-right: 16px;
72+
}
73+
74+
.head-cell__icon-up {
75+
background: url(../images/cell-arrow-up.svg) no-repeat 0px 1px / 17px;
76+
}
77+
78+
.head-cell__icon-down {
7279
background: url(../images/cell-arrow-down.svg) no-repeat 0px 1px / 17px;
7380
}
7481

7582
.head-cell__text-id {
7683
margin-right: 1px;
7784
}
7885

79-
.head-cell__icon-id {
80-
background: url(../images/cell-arrow-up.svg) no-repeat 0px 1px / 17px;
81-
}
82-
8386
.head-cell__icon-fio {
8487
margin-right: 2px;
8588
}
@@ -88,11 +91,3 @@
8891
font-weight: var(--semi-bold);
8992
color: var(--main-purple);
9093
}
91-
92-
.icon-up {
93-
transform: rotate(0deg);
94-
}
95-
96-
.icon-down {
97-
transform: rotate(180deg);
98-
}

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

Lines changed: 56 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -86,32 +86,44 @@
8686
outTblHeadTr.classList.add('crm__o-table-head-row');
8787
outTblHeadThId.classList.add(
8888
'head-cell',
89-
'head-cell-icon',
89+
'head-cell-with-icon',
9090
'crm__o-table-head-cell'
9191
);
9292
outTblHeadThIdWrap.classList.add('head-cell__wrap', 'head-cell__wrap-id');
9393
outTblHeadThIdText.classList.add('head-cell__text', 'head-cell__text-id');
94-
outTblHeadThIdIcon.classList.add('head-cell__icon', 'head-cell__icon-id');
94+
outTblHeadThIdIcon.classList.add(
95+
'head-cell__icon',
96+
'head-cell__icon-id',
97+
'head-cell__icon-up'
98+
);
9599
outTblHeadThFIO.classList.add(
96100
'head-cell',
97-
'head-cell-icon',
101+
'head-cell-with-icon',
98102
'crm__o-table-head-cell'
99103
);
100104
outTblHeadThFIOWrap.classList.add('head-cell__wrap', 'head-cell__wrap-fio');
101105
outTblHeadThFIOText.classList.add('head-cell__text', 'head-cell__text-fio');
102-
outTblHeadThFIOIcon.classList.add('head-cell__icon', 'head-cell__icon-fio');
106+
outTblHeadThFIOIcon.classList.add(
107+
'head-cell__icon',
108+
'head-cell__icon-fio',
109+
'head-cell__icon-down'
110+
);
103111
outTblHeadThFIOSort.classList.add('head-cell__sort', 'head-cell__sort-fio');
104112
outTblHeadThCreationDT.classList.add(
105113
'head-cell',
106-
'head-cell-icon',
114+
'head-cell-with-icon',
107115
'crm__o-table-head-cell'
108116
);
109117
outTblHeadThDTWrap.classList.add('head-cell__wrap', 'head-cell__wrap-dt');
110118
outTblHeadThDTText.classList.add('head-cell__text', 'head-cell__text-dt');
111-
outTblHeadThDTIcon.classList.add('head-cell__icon', 'head-cell__icon-dt');
119+
outTblHeadThDTIcon.classList.add(
120+
'head-cell__icon',
121+
'head-cell__icon-dt',
122+
'head-cell__icon-down'
123+
);
112124
outTblHeadThChanges.classList.add(
113125
'head-cell',
114-
'head-cell-icon',
126+
'head-cell-with-icon',
115127
'crm__o-table-head-cell'
116128
);
117129
outTblHeadThChangeWrap.classList.add(
@@ -124,7 +136,8 @@
124136
);
125137
outTblHeadThChangeIcon.classList.add(
126138
'head-cell__icon',
127-
'head-cell__icon-change'
139+
'head-cell__icon-change',
140+
'head-cell__icon-down'
128141
);
129142
outTblHeadThContacts.classList.add('head-cell', 'crm__o-table-head-cell');
130143
outTblHeadThContactWrap.classList.add(
@@ -240,4 +253,39 @@
240253

241254
const searchFormInput = document.querySelector('.crm__search-form input'); // получение search-инпута
242255
searchFormInputValidation(searchFormInput);
256+
257+
// изменение направления стрелки/svg-icon, согласно прожатия по заглавной ячейке (при сортировке данных)
258+
const allHeaderRowCells = document.querySelectorAll(
259+
'.crm__o-table-head-cell'
260+
);
261+
262+
function changeIconDirection(event) {
263+
const headerRowCell = event.currentTarget; // фиксация всей/целиком "th" заглавной ячейки
264+
const cellIcon = headerRowCell.querySelector('.head-cell__icon'); // определение иконки внутри ячейки
265+
const cellSort = headerRowCell.querySelector('.head-cell__sort'); // определение доп. текста, типа "А-Я"
266+
267+
// проверка/подтверждение наличия иконки (переключение)
268+
if (cellIcon) {
269+
cellIcon.classList.toggle('head-cell__icon-up');
270+
cellIcon.classList.toggle('head-cell__icon-down');
271+
}
272+
273+
// проверка/подтверждение наличия доп. текста (замена)
274+
if (cellSort) {
275+
cellSort.textContent = cellSort.textContent === 'А-Я' ? 'Я-А' : 'А-Я';
276+
}
277+
}
278+
279+
// организация прослушек "для каждой" заглавной ячейки
280+
allHeaderRowCells.forEach((cell) => {
281+
cell.addEventListener('click', (event) => changeIconDirection(event)); // передача события
282+
283+
// отработка сортировки/сброса сортировки через TAB/Enter (изменение направления стелок)
284+
cell.addEventListener('keydown', (event) => {
285+
if (event.key === 'Enter') {
286+
event.preventDefault();
287+
changeIconDirection(event); // передача события
288+
}
289+
});
290+
});
243291
})();

0 commit comments

Comments
 (0)