From 77d17b4d3d812513a7a3e853e622058e45d03acb Mon Sep 17 00:00:00 2001 From: Yarden Shoham Date: Sun, 24 Mar 2024 18:59:31 +0000 Subject: [PATCH 1/2] Remove jQuery `.attr` from the repository topic bar - Switched from jQuery `.attr` to plain javascript `getAttribute` and `setAttribute` - Tested the repository topic bar. It works as before Signed-off-by: Yarden Shoham --- web_src/js/features/repo-home.js | 46 +++++++++++++++++--------------- 1 file changed, 24 insertions(+), 22 deletions(-) diff --git a/web_src/js/features/repo-home.js b/web_src/js/features/repo-home.js index 74304d7f4afb..467c634a5790 100644 --- a/web_src/js/features/repo-home.js +++ b/web_src/js/features/repo-home.js @@ -6,38 +6,39 @@ import {POST} from '../modules/fetch.js'; const {appSubUrl} = window.config; export function initRepoTopicBar() { - const $mgrBtn = $('#manage_topic'); - if (!$mgrBtn.length) return; - const $editDiv = $('#topic_edit'); + const mgrBtn = document.getElementById('manage_topic'); + if (!mgrBtn) return; + const editDiv = document.getElementById('topic_edit'); const $viewDiv = $('#repo-topics'); - const $saveBtn = $('#save_topic'); - const $topicDropdown = $('#topic_edit .dropdown'); - const $topicForm = $editDiv; // the old logic, $editDiv is topicForm + const saveBtn = document.getElementById('save_topic'); + const topicDropdown = editDiv.querySelector('.dropdown'); + const $topicDropdown = $(topicDropdown); + const $topicForm = $(editDiv); const $topicDropdownSearch = $topicDropdown.find('input.search'); const topicPrompts = { - countPrompt: $topicDropdown.attr('data-text-count-prompt'), - formatPrompt: $topicDropdown.attr('data-text-format-prompt'), + countPrompt: topicDropdown.getAttribute('data-text-count-prompt') ?? undefined, + formatPrompt: topicDropdown.getAttribute('data-text-format-prompt') ?? undefined, }; - $mgrBtn.on('click', () => { + mgrBtn.addEventListener('click', () => { hideElem($viewDiv); - showElem($editDiv); + showElem(editDiv); $topicDropdownSearch.trigger('focus'); }); $('#cancel_topic_edit').on('click', () => { - hideElem($editDiv); + hideElem(editDiv); showElem($viewDiv); - $mgrBtn.trigger('focus'); + mgrBtn.focus(); }); - $saveBtn.on('click', async () => { + saveBtn.addEventListener('click', async () => { const topics = $('input[name=topics]').val(); const data = new FormData(); data.append('topics', topics); - const response = await POST($saveBtn.attr('data-link'), {data}); + const response = await POST(saveBtn.getAttribute('data-link'), {data}); if (response.ok) { const responseData = await response.json(); @@ -47,13 +48,14 @@ export function initRepoTopicBar() { const topicArray = topics.split(','); topicArray.sort(); for (const topic of topicArray) { - const $link = $(''); - $link.attr('href', `${appSubUrl}/explore/repos?q=${encodeURIComponent(topic)}&topic=1`); - $link.text(topic); - $link.insertBefore($mgrBtn); // insert all new topics before manage button + const link = document.createElement('a'); + link.classList.add('ui', 'repo-topic', 'large', 'label', 'topic', 'tw-m-0'); + link.href = `${appSubUrl}/explore/repos?q=${encodeURIComponent(topic)}&topic=1`; + link.textContent = topic; + mgrBtn.parentNode.insertBefore(link, mgrBtn); // insert all new topics before manage button } } - hideElem($editDiv); + hideElem(editDiv); showElem($viewDiv); } } else if (response.status === 422) { @@ -144,14 +146,14 @@ export function initRepoTopicBar() { }, onAdd(addedValue, _addedText, $addedChoice) { addedValue = addedValue.toLowerCase().trim(); - $($addedChoice).attr('data-value', addedValue); - $($addedChoice).attr('data-text', addedValue); + $($addedChoice)[0].setAttribute('data-value', addedValue); + $($addedChoice)[0].setAttribute('data-text', addedValue); }, }); $.fn.form.settings.rules.validateTopic = function (_values, regExp) { const $topics = $topicDropdown.children('a.ui.label'); - const status = $topics.length === 0 || $topics.last().attr('data-value').match(regExp); + const status = $topics.length === 0 || $topics.last()[0].getAttribute('data-value').match(regExp); if (!status) { $topics.last().removeClass('green').addClass('red'); } From 4ec645ef1624dd9626fbda4286b08002259f1da3 Mon Sep 17 00:00:00 2001 From: Yarden Shoham Date: Sun, 24 Mar 2024 20:56:39 +0000 Subject: [PATCH 2/2] viewDiv with getElementById --- web_src/js/features/repo-home.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/web_src/js/features/repo-home.js b/web_src/js/features/repo-home.js index 467c634a5790..2b0e38f0874e 100644 --- a/web_src/js/features/repo-home.js +++ b/web_src/js/features/repo-home.js @@ -9,7 +9,7 @@ export function initRepoTopicBar() { const mgrBtn = document.getElementById('manage_topic'); if (!mgrBtn) return; const editDiv = document.getElementById('topic_edit'); - const $viewDiv = $('#repo-topics'); + const viewDiv = document.getElementById('repo-topics'); const saveBtn = document.getElementById('save_topic'); const topicDropdown = editDiv.querySelector('.dropdown'); const $topicDropdown = $(topicDropdown); @@ -21,14 +21,14 @@ export function initRepoTopicBar() { }; mgrBtn.addEventListener('click', () => { - hideElem($viewDiv); + hideElem(viewDiv); showElem(editDiv); $topicDropdownSearch.trigger('focus'); }); $('#cancel_topic_edit').on('click', () => { hideElem(editDiv); - showElem($viewDiv); + showElem(viewDiv); mgrBtn.focus(); }); @@ -43,7 +43,7 @@ export function initRepoTopicBar() { if (response.ok) { const responseData = await response.json(); if (responseData.status === 'ok') { - $viewDiv.children('.topic').remove(); + $(viewDiv).children('.topic').remove(); if (topics.length) { const topicArray = topics.split(','); topicArray.sort(); @@ -56,7 +56,7 @@ export function initRepoTopicBar() { } } hideElem(editDiv); - showElem($viewDiv); + showElem(viewDiv); } } else if (response.status === 422) { const responseData = await response.json();