Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove jQuery .attr from the repository topic bar #30050

Merged
merged 2 commits into from
Mar 24, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 24 additions & 22 deletions web_src/js/features/repo-home.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
silverwind marked this conversation as resolved.
Show resolved Hide resolved
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();
Expand All @@ -47,13 +48,14 @@ export function initRepoTopicBar() {
const topicArray = topics.split(',');
topicArray.sort();
for (const topic of topicArray) {
const $link = $('<a class="ui repo-topic large label topic tw-m-0"></a>');
$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) {
Expand Down Expand Up @@ -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');
}
Expand Down