0
0
Fork 0
mirror of https://github.com/go-gitea/gitea synced 2024-11-26 15:02:56 +01:00

Remove jQuery .attr from the repository topic bar (#30050)

- Switched from jQuery `.attr` to plain javascript `getAttribute` and
`setAttribute`
- Tested the repository topic bar. It works as before

---------

Signed-off-by: Yarden Shoham <git@yardenshoham.com>
This commit is contained in:
Yarden Shoham 2024-03-25 01:44:05 +02:00 committed by GitHub
parent 3f26fe2fa2
commit 314cd1ec98
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -6,55 +6,57 @@ import {POST} from '../modules/fetch.js';
const {appSubUrl} = window.config; const {appSubUrl} = window.config;
export function initRepoTopicBar() { export function initRepoTopicBar() {
const $mgrBtn = $('#manage_topic'); const mgrBtn = document.getElementById('manage_topic');
if (!$mgrBtn.length) return; if (!mgrBtn) return;
const $editDiv = $('#topic_edit'); const editDiv = document.getElementById('topic_edit');
const $viewDiv = $('#repo-topics'); const viewDiv = document.getElementById('repo-topics');
const $saveBtn = $('#save_topic'); const saveBtn = document.getElementById('save_topic');
const $topicDropdown = $('#topic_edit .dropdown'); const topicDropdown = editDiv.querySelector('.dropdown');
const $topicForm = $editDiv; // the old logic, $editDiv is topicForm const $topicDropdown = $(topicDropdown);
const $topicForm = $(editDiv);
const $topicDropdownSearch = $topicDropdown.find('input.search'); const $topicDropdownSearch = $topicDropdown.find('input.search');
const topicPrompts = { const topicPrompts = {
countPrompt: $topicDropdown.attr('data-text-count-prompt'), countPrompt: topicDropdown.getAttribute('data-text-count-prompt') ?? undefined,
formatPrompt: $topicDropdown.attr('data-text-format-prompt'), formatPrompt: topicDropdown.getAttribute('data-text-format-prompt') ?? undefined,
}; };
$mgrBtn.on('click', () => { mgrBtn.addEventListener('click', () => {
hideElem($viewDiv); hideElem(viewDiv);
showElem($editDiv); showElem(editDiv);
$topicDropdownSearch.trigger('focus'); $topicDropdownSearch.trigger('focus');
}); });
$('#cancel_topic_edit').on('click', () => { $('#cancel_topic_edit').on('click', () => {
hideElem($editDiv); hideElem(editDiv);
showElem($viewDiv); showElem(viewDiv);
$mgrBtn.trigger('focus'); mgrBtn.focus();
}); });
$saveBtn.on('click', async () => { saveBtn.addEventListener('click', async () => {
const topics = $('input[name=topics]').val(); const topics = $('input[name=topics]').val();
const data = new FormData(); const data = new FormData();
data.append('topics', topics); 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) { if (response.ok) {
const responseData = await response.json(); const responseData = await response.json();
if (responseData.status === 'ok') { if (responseData.status === 'ok') {
$viewDiv.children('.topic').remove(); $(viewDiv).children('.topic').remove();
if (topics.length) { if (topics.length) {
const topicArray = topics.split(','); const topicArray = topics.split(',');
topicArray.sort(); topicArray.sort();
for (const topic of topicArray) { for (const topic of topicArray) {
const $link = $('<a class="ui repo-topic large label topic tw-m-0"></a>'); const link = document.createElement('a');
$link.attr('href', `${appSubUrl}/explore/repos?q=${encodeURIComponent(topic)}&topic=1`); link.classList.add('ui', 'repo-topic', 'large', 'label', 'topic', 'tw-m-0');
$link.text(topic); link.href = `${appSubUrl}/explore/repos?q=${encodeURIComponent(topic)}&topic=1`;
$link.insertBefore($mgrBtn); // insert all new topics before manage button link.textContent = topic;
mgrBtn.parentNode.insertBefore(link, mgrBtn); // insert all new topics before manage button
} }
} }
hideElem($editDiv); hideElem(editDiv);
showElem($viewDiv); showElem(viewDiv);
} }
} else if (response.status === 422) { } else if (response.status === 422) {
const responseData = await response.json(); const responseData = await response.json();
@ -144,14 +146,14 @@ export function initRepoTopicBar() {
}, },
onAdd(addedValue, _addedText, $addedChoice) { onAdd(addedValue, _addedText, $addedChoice) {
addedValue = addedValue.toLowerCase().trim(); addedValue = addedValue.toLowerCase().trim();
$($addedChoice).attr('data-value', addedValue); $($addedChoice)[0].setAttribute('data-value', addedValue);
$($addedChoice).attr('data-text', addedValue); $($addedChoice)[0].setAttribute('data-text', addedValue);
}, },
}); });
$.fn.form.settings.rules.validateTopic = function (_values, regExp) { $.fn.form.settings.rules.validateTopic = function (_values, regExp) {
const $topics = $topicDropdown.children('a.ui.label'); 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) { if (!status) {
$topics.last().removeClass('green').addClass('red'); $topics.last().removeClass('green').addClass('red');
} }