0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo.git synced 2024-12-27 09:54:15 -05:00
forgejo/web_src/js/features/repo-home.js
silverwind c82bef515e
Migrate margin and padding helpers to tailwind (#30043)
This will conclude the refactor of 1:1 class replacements to tailwind,
except `gt-hidden`. Commands ran:

```bash
perl -p -i -e 's#gt-(p|m)([lrtbxy])?-0#tw-$1$2-0#g'   {web_src/js,templates,routers,services}/**/*
perl -p -i -e 's#gt-(p|m)([lrtbxy])?-1#tw-$1$2-0.5#g' {web_src/js,templates,routers,services}/**/*
perl -p -i -e 's#gt-(p|m)([lrtbxy])?-2#tw-$1$2-1#g'   {web_src/js,templates,routers,services}/**/*
perl -p -i -e 's#gt-(p|m)([lrtbxy])?-3#tw-$1$2-2#g'   {web_src/js,templates,routers,services}/**/*
perl -p -i -e 's#gt-(p|m)([lrtbxy])?-4#tw-$1$2-4#g'   {web_src/js,templates,routers,services}/**/*
perl -p -i -e 's#gt-(p|m)([lrtbxy])?-5#tw-$1$2-8#g'   {web_src/js,templates,routers,services}/**/*
```

(cherry picked from commit 68ec9b48592fe88765bcc3a73093d43c98b315de)

Conflicts:
	routers/web/repo/view.go
	templates/base/head_navbar.tmpl
	templates/repo/code/recently_pushed_new_branches.tmpl
	templates/repo/diff/box.tmpl
	templates/repo/diff/compare.tmpl
	templates/repo/diff/conversation.tmpl
	templates/repo/header.tmpl
	templates/repo/issue/filter_list.tmpl
	templates/repo/issue/view_content/conversation.tmpl
	templates/repo/issue/view_content/sidebar.tmpl
	templates/repo/settings/options.tmpl
	templates/repo/view_file.tmpl
	templates/shared/user/blocked_users.tmpl
	templates/status/500.tmpl
	web_src/js/components/DashboardRepoList.vue
	resolved by prefering Forgejo version and applying the
	commands to all files
2024-03-30 07:17:29 +01:00

181 lines
5.8 KiB
JavaScript

import $ from 'jquery';
import {stripTags} from '../utils.js';
import {hideElem, showElem} from '../utils/dom.js';
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 $viewDiv = $('#repo-topics');
const $saveBtn = $('#save_topic');
const $topicDropdown = $('#topic_edit .dropdown');
const $topicForm = $editDiv; // the old logic, $editDiv is topicForm
const $topicDropdownSearch = $topicDropdown.find('input.search');
const topicPrompts = {
countPrompt: $topicDropdown.attr('data-text-count-prompt'),
formatPrompt: $topicDropdown.attr('data-text-format-prompt'),
};
$mgrBtn.on('click', () => {
hideElem($viewDiv);
showElem($editDiv);
$topicDropdownSearch.trigger('focus');
});
$('#cancel_topic_edit').on('click', () => {
hideElem($editDiv);
showElem($viewDiv);
$mgrBtn.trigger('focus');
});
$saveBtn.on('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});
if (response.ok) {
const responseData = await response.json();
if (responseData.status === 'ok') {
$viewDiv.children('.topic').remove();
if (topics.length) {
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
}
}
hideElem($editDiv);
showElem($viewDiv);
}
} else if (response.status === 422) {
const responseData = await response.json();
if (responseData.invalidTopics.length > 0) {
topicPrompts.formatPrompt = responseData.message;
const {invalidTopics} = responseData;
const $topicLabels = $topicDropdown.children('a.ui.label');
for (const [index, value] of topics.split(',').entries()) {
if (invalidTopics.includes(value)) {
$topicLabels.eq(index).removeClass('green').addClass('red');
}
}
} else {
topicPrompts.countPrompt = responseData.message;
}
}
// Always validate the form
$topicForm.form('validate form');
});
$topicDropdown.dropdown({
allowAdditions: true,
forceSelection: false,
fullTextSearch: 'exact',
fields: {name: 'description', value: 'data-value'},
saveRemoteData: false,
label: {
transition: 'horizontal flip',
duration: 200,
variation: false,
},
apiSettings: {
url: `${appSubUrl}/explore/topics/search?q={query}`,
throttle: 500,
cache: false,
onResponse(res) {
const formattedResponse = {
success: false,
results: [],
};
const query = stripTags(this.urlData.query.trim());
let found_query = false;
const current_topics = [];
$topicDropdown.find('a.label.visible').each((_, el) => {
current_topics.push(el.getAttribute('data-value'));
});
if (res.topics) {
let found = false;
for (let i = 0; i < res.topics.length; i++) {
// skip currently added tags
if (current_topics.includes(res.topics[i].topic_name)) {
continue;
}
if (res.topics[i].topic_name.toLowerCase() === query.toLowerCase()) {
found_query = true;
}
formattedResponse.results.push({description: res.topics[i].topic_name, 'data-value': res.topics[i].topic_name});
found = true;
}
formattedResponse.success = found;
}
if (query.length > 0 && !found_query) {
formattedResponse.success = true;
formattedResponse.results.unshift({description: query, 'data-value': query});
} else if (query.length > 0 && found_query) {
formattedResponse.results.sort((a, b) => {
if (a.description.toLowerCase() === query.toLowerCase()) return -1;
if (b.description.toLowerCase() === query.toLowerCase()) return 1;
if (a.description > b.description) return -1;
if (a.description < b.description) return 1;
return 0;
});
}
return formattedResponse;
},
},
onLabelCreate(value) {
value = value.toLowerCase().trim();
this.attr('data-value', value).contents().first().replaceWith(value);
return $(this);
},
onAdd(addedValue, _addedText, $addedChoice) {
addedValue = addedValue.toLowerCase().trim();
$($addedChoice).attr('data-value', addedValue);
$($addedChoice).attr('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);
if (!status) {
$topics.last().removeClass('green').addClass('red');
}
return status && $topicDropdown.children('a.ui.label.red').length === 0;
};
$topicForm.form({
on: 'change',
inline: true,
fields: {
topics: {
identifier: 'topics',
rules: [
{
type: 'validateTopic',
value: /^\s*[a-z0-9][-.a-z0-9]{0,35}\s*$/,
prompt: topicPrompts.formatPrompt,
},
{
type: 'maxCount[25]',
prompt: topicPrompts.countPrompt,
},
],
},
},
});
}