Merge pull request '[gitea] week 12 cherry-pick' (#2679) from algernon/forgejo:wcp/week-12 into forgejo

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/2679
Reviewed-by: Otto <otto@codeberg.org>
Reviewed-by: oliverpool <oliverpool@noreply.codeberg.org>
Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org>
This commit is contained in:
Earl Warren 2024-03-20 08:15:06 +00:00
commit 2f78daa3af
174 changed files with 3157 additions and 2907 deletions

View file

@ -384,28 +384,30 @@ export default sfc; // activate the IDE's Vue plugin
</div>
</div>
</div>
<div class="ui secondary tiny pointing borderless menu center grid repos-filter">
<a class="item" :class="{active: reposFilter === 'all'}" @click="changeReposFilter('all')">
{{ textAll }}
<div v-show="reposFilter === 'all'" class="ui circular mini grey label">{{ repoTypeCount }}</div>
</a>
<a class="item" :class="{active: reposFilter === 'sources'}" @click="changeReposFilter('sources')">
{{ textSources }}
<div v-show="reposFilter === 'sources'" class="ui circular mini grey label">{{ repoTypeCount }}</div>
</a>
<a class="item" :class="{active: reposFilter === 'forks'}" @click="changeReposFilter('forks')">
{{ textForks }}
<div v-show="reposFilter === 'forks'" class="ui circular mini grey label">{{ repoTypeCount }}</div>
</a>
<a class="item" :class="{active: reposFilter === 'mirrors'}" @click="changeReposFilter('mirrors')" v-if="isMirrorsEnabled">
{{ textMirrors }}
<div v-show="reposFilter === 'mirrors'" class="ui circular mini grey label">{{ repoTypeCount }}</div>
</a>
<a class="item" :class="{active: reposFilter === 'collaborative'}" @click="changeReposFilter('collaborative')">
{{ textCollaborative }}
<div v-show="reposFilter === 'collaborative'" class="ui circular mini grey label">{{ repoTypeCount }}</div>
</a>
</div>
<overflow-menu class="ui secondary pointing tabular borderless menu repos-filter">
<div class="overflow-menu-items tw-justify-center">
<a class="item" tabindex="0" :class="{active: reposFilter === 'all'}" @click="changeReposFilter('all')">
{{ textAll }}
<div v-show="reposFilter === 'all'" class="ui circular mini grey label">{{ repoTypeCount }}</div>
</a>
<a class="item" tabindex="0" :class="{active: reposFilter === 'sources'}" @click="changeReposFilter('sources')">
{{ textSources }}
<div v-show="reposFilter === 'sources'" class="ui circular mini grey label">{{ repoTypeCount }}</div>
</a>
<a class="item" tabindex="0" :class="{active: reposFilter === 'forks'}" @click="changeReposFilter('forks')">
{{ textForks }}
<div v-show="reposFilter === 'forks'" class="ui circular mini grey label">{{ repoTypeCount }}</div>
</a>
<a class="item" tabindex="0" :class="{active: reposFilter === 'mirrors'}" @click="changeReposFilter('mirrors')" v-if="isMirrorsEnabled">
{{ textMirrors }}
<div v-show="reposFilter === 'mirrors'" class="ui circular mini grey label">{{ repoTypeCount }}</div>
</a>
<a class="item" tabindex="0" :class="{active: reposFilter === 'collaborative'}" @click="changeReposFilter('collaborative')">
{{ textCollaborative }}
<div v-show="reposFilter === 'collaborative'" class="ui circular mini grey label">{{ repoTypeCount }}</div>
</a>
</div>
</overflow-menu>
</div>
<div v-if="repos.length" class="ui attached table segment gt-rounded-bottom">
<ul class="repo-owner-name-list">
@ -501,6 +503,22 @@ ul li:not(:last-child) {
border-bottom: 1px solid var(--color-secondary);
}
.repos-search {
padding-bottom: 0 !important;
}
.repos-filter {
padding-top: 0 !important;
margin-top: 0 !important;
border-bottom-width: 0 !important;
margin-bottom: 2px !important;
}
.repos-filter .item {
padding-left: 6px !important;
padding-right: 6px !important;
}
.repo-list-link {
min-width: 0; /* for text truncation */
display: flex;

View file

@ -248,7 +248,7 @@ export default {
{{ commit.committer_or_author_name }}
<span class="text right">
<!-- TODO: make this respect the PreferredTimestampTense setting -->
<relative-time class="time-since" prefix="" :datetime="commit.time" data-tooltip-content data-tooltip-interactive="true">{{ commit.time }}</relative-time>
<relative-time prefix="" :datetime="commit.time" data-tooltip-content data-tooltip-interactive="true">{{ commit.time }}</relative-time>
</span>
</div>
</div>

View file

@ -5,7 +5,7 @@ import {createApp} from 'vue';
import {toggleElem} from '../utils/dom.js';
import {formatDatetime} from '../utils/time.js';
import {renderAnsi} from '../render/ansi.js';
import {POST, DELETE} from '../modules/fetch.js';
import {GET, POST, DELETE} from '../modules/fetch.js';
const sfc = {
name: 'RepoActionView',
@ -199,7 +199,7 @@ const sfc = {
},
async fetchArtifacts() {
const resp = await POST(`${this.actionsURL}/runs/${this.runIndex}/artifacts`);
const resp = await GET(`${this.actionsURL}/runs/${this.runIndex}/artifacts`);
return await resp.json();
},
@ -466,7 +466,7 @@ export function initRepositoryActionView() {
</div>
</div>
</div>
<div class="job-step-container" ref="steps">
<div class="job-step-container" ref="steps" v-if="currentJob.steps.length">
<div class="job-step-section" v-for="(jobStep, i) in currentJob.steps" :key="i">
<div class="job-step-summary" @click.stop="toggleStepLogs(i)" :class="currentJobStepsStates[i].expanded ? 'selected' : ''">
<!-- If the job is done and the job step log is loaded for the first time, show the loading icon
@ -533,7 +533,7 @@ export function initRepositoryActionView() {
width: 30%;
max-width: 400px;
position: sticky;
top: 0;
top: 12px;
max-height: 100vh;
overflow-y: auto;
}
@ -695,11 +695,15 @@ export function initRepositoryActionView() {
background-color: var(--color-console-bg);
position: sticky;
top: 0;
border-radius: var(--border-radius) var(--border-radius) 0 0;
border-radius: var(--border-radius);
height: 60px;
z-index: 1;
}
.job-info-header:has(+ .job-step-container) {
border-radius: var(--border-radius) var(--border-radius) 0 0;
}
.job-info-header .job-info-header-title {
color: var(--color-console-fg);
font-size: 16px;

View file

@ -123,7 +123,7 @@ const sfc = {
return -1;
},
scrollToActive() {
let el = this.$refs[`listItem${this.active}`];
let el = this.$refs[`listItem${this.active}`]; // eslint-disable-line no-jquery/variable-pattern
if (!el || !el.length) return;
if (Array.isArray(el)) {
el = el[0];

View file

@ -49,7 +49,7 @@ export function initAdminCommon() {
}
function onUsePagedSearchChange() {
if ($('#use_paged_search').prop('checked')) {
if (document.getElementById('use_paged_search').checked) {
showElem('.search-page-size');
$('.search-page-size').find('input').attr('required', 'required');
} else {

View file

@ -12,7 +12,7 @@ export function initAdminUserListSearchForm() {
if (searchForm.StatusFilterMap) {
for (const [k, v] of Object.entries(searchForm.StatusFilterMap)) {
if (!v) continue;
$form.find(`input[name="status_filter[${k}]"][value=${v}]`).prop('checked', true);
$form.find(`input[name="status_filter[${k}]"][value=${v}]`).checked = true;
}
}
@ -25,7 +25,7 @@ export function initAdminUserListSearchForm() {
$form.find(`input[type=radio]`).each((_, e) => {
const $e = $(e);
if ($e.attr('name').startsWith('status_filter[')) {
$e.prop('checked', false);
$e.checked = false;
}
});
$form.trigger('submit');

View file

@ -40,28 +40,35 @@ export async function initCitationFileCopyContent() {
$citationCopyApa.toggleClass('primary', !isBibtex);
};
try {
await initInputCitationValue($citationCopyApa, $citationCopyBibtex);
} catch (e) {
console.error(`initCitationFileCopyContent error: ${e}`, e);
return;
}
updateUi();
$('#cite-repo-button').on('click', async (e) => {
const dropdownBtn = e.target.closest('.ui.dropdown.button');
dropdownBtn.classList.add('is-loading');
$citationCopyApa.on('click', () => {
localStorage.setItem('citation-copy-format', 'apa');
updateUi();
});
$citationCopyBibtex.on('click', () => {
localStorage.setItem('citation-copy-format', 'bibtex');
updateUi();
});
try {
try {
await initInputCitationValue($citationCopyApa, $citationCopyBibtex);
} catch (e) {
console.error(`initCitationFileCopyContent error: ${e}`, e);
return;
}
updateUi();
$inputContent.on('click', () => {
$inputContent.trigger('select');
});
$citationCopyApa.on('click', () => {
localStorage.setItem('citation-copy-format', 'apa');
updateUi();
});
$citationCopyBibtex.on('click', () => {
localStorage.setItem('citation-copy-format', 'bibtex');
updateUi();
});
$inputContent.on('click', () => {
$inputContent.trigger('select');
});
} finally {
dropdownBtn.classList.remove('is-loading');
}
$('#cite-repo-button').on('click', () => {
$('#cite-repo-modal').modal('show');
});
}

View file

@ -15,10 +15,8 @@ export function initGlobalCopyToClipboardListener() {
e.preventDefault();
let text;
if (target.hasAttribute('data-clipboard-text')) {
text = target.getAttribute('data-clipboard-text');
} else {
let text = target.getAttribute('data-clipboard-text');
if (!text) {
text = document.querySelector(target.getAttribute('data-clipboard-target'))?.value;
}

View file

@ -231,8 +231,8 @@ export function initDropzone(el) {
init() {
this.on('success', (file, data) => {
file.uuid = data.uuid;
const input = $(`<input id="${data.uuid}" name="files" type="hidden">`).val(data.uuid);
$dropzone.find('.files').append(input);
const $input = $(`<input id="${data.uuid}" name="files" type="hidden">`).val(data.uuid);
$dropzone.find('.files').append($input);
// Create a "Copy Link" element, to conveniently copy the image
// or file link as Markdown to the clipboard
const copyLinkElement = document.createElement('div');
@ -305,15 +305,15 @@ export function initGlobalLinkActions() {
filter += `#${$this.attr('data-modal-id')}`;
}
const dialog = $(`.delete.modal${filter}`);
dialog.find('.name').text($this.data('name'));
const $dialog = $(`.delete.modal${filter}`);
$dialog.find('.name').text($this.data('name'));
for (const [key, value] of Object.entries(dataArray)) {
if (key && key.startsWith('data')) {
dialog.find(`.${key}`).text(value);
$dialog.find(`.${key}`).text(value);
}
}
dialog.modal({
$dialog.modal({
closable: false,
onApprove: async () => {
if ($this.data('type') === 'form') {
@ -380,8 +380,8 @@ function initGlobalShowModal() {
$attrTarget.text(attrib.value); // FIXME: it should be more strict here, only handle div/span/p
}
}
const colorPickers = $modal.find('.color-picker');
if (colorPickers.length > 0) {
const $colorPickers = $modal.find('.color-picker');
if ($colorPickers.length > 0) {
initCompColorPicker(); // FIXME: this might cause duplicate init
}
$modal.modal('setting', {

View file

@ -6,23 +6,23 @@ function isExclusiveScopeName(name) {
}
function updateExclusiveLabelEdit(form) {
const nameInput = $(`${form} .label-name-input`);
const exclusiveField = $(`${form} .label-exclusive-input-field`);
const exclusiveCheckbox = $(`${form} .label-exclusive-input`);
const exclusiveWarning = $(`${form} .label-exclusive-warning`);
const $nameInput = $(`${form} .label-name-input`);
const $exclusiveField = $(`${form} .label-exclusive-input-field`);
const $exclusiveCheckbox = $(`${form} .label-exclusive-input`);
const $exclusiveWarning = $(`${form} .label-exclusive-warning`);
if (isExclusiveScopeName(nameInput.val())) {
exclusiveField.removeClass('muted');
exclusiveField.removeAttr('aria-disabled');
if (exclusiveCheckbox.prop('checked') && exclusiveCheckbox.data('exclusive-warn')) {
exclusiveWarning.removeClass('gt-hidden');
if (isExclusiveScopeName($nameInput.val())) {
$exclusiveField.removeClass('muted');
$exclusiveField.removeAttr('aria-disabled');
if ($exclusiveCheckbox[0].checked && $exclusiveCheckbox.data('exclusive-warn')) {
$exclusiveWarning.removeClass('gt-hidden');
} else {
exclusiveWarning.addClass('gt-hidden');
$exclusiveWarning.addClass('gt-hidden');
}
} else {
exclusiveField.addClass('muted');
exclusiveField.attr('aria-disabled', 'true');
exclusiveWarning.addClass('gt-hidden');
$exclusiveField.addClass('muted');
$exclusiveField.attr('aria-disabled', 'true');
$exclusiveWarning.addClass('gt-hidden');
}
}
@ -46,18 +46,18 @@ export function initCompLabelEdit(selector) {
$('.edit-label .color-picker').minicolors('value', $(this).data('color'));
$('#label-modal-id').val($(this).data('id'));
const nameInput = $('.edit-label .label-name-input');
nameInput.val($(this).data('title'));
const $nameInput = $('.edit-label .label-name-input');
$nameInput.val($(this).data('title'));
const isArchivedCheckbox = $('.edit-label .label-is-archived-input');
isArchivedCheckbox.prop('checked', this.hasAttribute('data-is-archived'));
const $isArchivedCheckbox = $('.edit-label .label-is-archived-input');
$isArchivedCheckbox[0].checked = this.hasAttribute('data-is-archived');
const exclusiveCheckbox = $('.edit-label .label-exclusive-input');
exclusiveCheckbox.prop('checked', this.hasAttribute('data-exclusive'));
const $exclusiveCheckbox = $('.edit-label .label-exclusive-input');
$exclusiveCheckbox[0].checked = this.hasAttribute('data-exclusive');
// Warn when label was previously not exclusive and used in issues
exclusiveCheckbox.data('exclusive-warn',
$exclusiveCheckbox.data('exclusive-warn',
$(this).data('num-issues') > 0 &&
(!this.hasAttribute('data-exclusive') || !isExclusiveScopeName(nameInput.val())));
(!this.hasAttribute('data-exclusive') || !isExclusiveScopeName($nameInput.val())));
updateExclusiveLabelEdit('.edit-label');
$('.edit-label .label-desc-input').val($(this).data('description'));

View file

@ -17,21 +17,21 @@ export function initCompReactionSelector($parent) {
const data = await res.json();
if (data && (data.html || data.empty)) {
const content = $(this).closest('.content');
let react = content.find('.segment.reactions');
if ((!data.empty || data.html === '') && react.length > 0) {
react.remove();
const $content = $(this).closest('.content');
let $react = $content.find('.segment.reactions');
if ((!data.empty || data.html === '') && $react.length > 0) {
$react.remove();
}
if (!data.empty) {
const attachments = content.find('.segment.bottom:first');
react = $(data.html);
if (attachments.length > 0) {
react.insertBefore(attachments);
const $attachments = $content.find('.segment.bottom:first');
$react = $(data.html);
if ($attachments.length > 0) {
$react.insertBefore($attachments);
} else {
react.appendTo(content);
$react.appendTo($content);
}
react.find('.dropdown').dropdown();
initCompReactionSelector(react);
$react.find('.dropdown').dropdown();
initCompReactionSelector($react);
}
}
});

View file

@ -1,6 +1,7 @@
import $ from 'jquery';
import {GET} from '../modules/fetch.js';
const {appSubUrl, csrfToken, notificationSettings, assetVersionEncoded} = window.config;
const {appSubUrl, notificationSettings, assetVersionEncoded} = window.config;
let notificationSequenceNumber = 0;
export function initNotificationsTable() {
@ -27,25 +28,6 @@ export function initNotificationsTable() {
e.target.closest('.notifications-item').setAttribute('data-remove', 'true');
});
}
$('#notification_table .button').on('click', function () {
(async () => {
const data = await updateNotification(
$(this).data('url'),
$(this).data('status'),
$(this).data('page'),
$(this).data('q'),
$(this).data('notification-id'),
);
if ($(data).data('sequence-number') === notificationSequenceNumber) {
$('#notification_div').replaceWith(data);
initNotificationsTable();
}
await updateNotificationCount();
})();
return false;
});
}
async function receiveUpdateCount(event) {
@ -63,9 +45,9 @@ async function receiveUpdateCount(event) {
}
export function initNotificationCount() {
const notificationCount = $('.notification_count');
const $notificationCount = $('.notification_count');
if (!notificationCount.length) {
if (!$notificationCount.length) {
return;
}
@ -73,7 +55,7 @@ export function initNotificationCount() {
const startPeriodicPoller = (timeout, lastCount) => {
if (timeout <= 0 || !Number.isFinite(timeout)) return;
usingPeriodicPoller = true;
lastCount = lastCount ?? notificationCount.text();
lastCount = lastCount ?? $notificationCount.text();
setTimeout(async () => {
await updateNotificationCountWithCallback(startPeriodicPoller, timeout, lastCount);
}, timeout);
@ -161,60 +143,52 @@ async function updateNotificationCountWithCallback(callback, timeout, lastCount)
}
async function updateNotificationTable() {
const notificationDiv = $('#notification_div');
if (notificationDiv.length > 0) {
const data = await $.ajax({
type: 'GET',
url: `${appSubUrl}/notifications${window.location.search}`,
data: {
'div-only': true,
'sequence-number': ++notificationSequenceNumber,
const notificationDiv = document.getElementById('notification_div');
if (notificationDiv) {
try {
const params = new URLSearchParams(window.location.search);
params.set('div-only', true);
params.set('sequence-number', ++notificationSequenceNumber);
const url = `${appSubUrl}/notifications?${params.toString()}`;
const response = await GET(url);
if (!response.ok) {
throw new Error('Failed to fetch notification table');
}
});
if ($(data).data('sequence-number') === notificationSequenceNumber) {
notificationDiv.replaceWith(data);
initNotificationsTable();
const data = await response.text();
if ($(data).data('sequence-number') === notificationSequenceNumber) {
notificationDiv.outerHTML = data;
initNotificationsTable();
}
} catch (error) {
console.error(error);
}
}
}
async function updateNotificationCount() {
const data = await $.ajax({
type: 'GET',
url: `${appSubUrl}/notifications/new`,
headers: {
'X-Csrf-Token': csrfToken,
},
});
try {
const response = await GET(`${appSubUrl}/notifications/new`);
const notificationCount = $('.notification_count');
if (data.new === 0) {
notificationCount.addClass('gt-hidden');
} else {
notificationCount.removeClass('gt-hidden');
if (!response.ok) {
throw new Error('Failed to fetch notification count');
}
const data = await response.json();
const $notificationCount = $('.notification_count');
if (data.new === 0) {
$notificationCount.addClass('gt-hidden');
} else {
$notificationCount.removeClass('gt-hidden');
}
$notificationCount.text(`${data.new}`);
return `${data.new}`;
} catch (error) {
console.error(error);
return '0';
}
notificationCount.text(`${data.new}`);
return `${data.new}`;
}
async function updateNotification(url, status, page, q, notificationID) {
if (status !== 'pinned') {
$(`#notification_${notificationID}`).remove();
}
return $.ajax({
type: 'POST',
url,
data: {
_csrf: csrfToken,
notification_id: notificationID,
status,
page,
q,
noredirect: true,
'sequence-number': ++notificationSequenceNumber,
},
});
}

View file

@ -8,8 +8,9 @@ import {initViewedCheckboxListenerFor, countAndUpdateViewedFiles, initExpandAndC
import {initImageDiff} from './imagediff.js';
import {showErrorToast} from '../modules/toast.js';
import {submitEventSubmitter} from '../utils/dom.js';
import {POST, GET} from '../modules/fetch.js';
const {csrfToken, pageData, i18n} = window.config;
const {pageData, i18n} = window.config;
function initRepoDiffReviewButton() {
const $reviewBox = $('#review-box');
@ -65,8 +66,9 @@ function initRepoDiffConversationForm() {
if (isSubmittedByButton && submitter.name) {
formData.append(submitter.name, submitter.value);
}
const formDataString = String(new URLSearchParams(formData));
const $newConversationHolder = $(await $.post($form.attr('action'), formDataString));
const response = await POST($form.attr('action'), {data: formData});
const $newConversationHolder = $(await response.text());
const {path, side, idx} = $newConversationHolder.data();
$form.closest('.conversation-holder').replaceWith($newConversationHolder);
@ -92,15 +94,20 @@ function initRepoDiffConversationForm() {
const action = $(this).data('action');
const url = $(this).data('update-url');
const data = await $.post(url, {_csrf: csrfToken, origin, action, comment_id});
try {
const response = await POST(url, {data: new URLSearchParams({origin, action, comment_id})});
const data = await response.text();
if ($(this).closest('.conversation-holder').length) {
const conversation = $(data);
$(this).closest('.conversation-holder').replaceWith(conversation);
conversation.find('.dropdown').dropdown();
initCompReactionSelector(conversation);
} else {
window.location.reload();
if ($(this).closest('.conversation-holder').length) {
const $conversation = $(data);
$(this).closest('.conversation-holder').replaceWith($conversation);
$conversation.find('.dropdown').dropdown();
initCompReactionSelector($conversation);
} else {
window.location.reload();
}
} catch (error) {
console.error('Error:', error);
}
});
}
@ -135,7 +142,7 @@ function onShowMoreFiles() {
initImageDiff();
}
export function loadMoreFiles(url) {
export async function loadMoreFiles(url) {
const $target = $('a#diff-show-more-files');
if ($target.hasClass('disabled') || pageData.diffFileInfo.isLoadingNewData) {
return;
@ -143,10 +150,10 @@ export function loadMoreFiles(url) {
pageData.diffFileInfo.isLoadingNewData = true;
$target.addClass('disabled');
$.ajax({
type: 'GET',
url,
}).done((resp) => {
try {
const response = await GET(url);
const resp = await response.text();
const $resp = $(resp);
// the response is a full HTML page, we need to extract the relevant contents:
// 1. append the newly loaded file list items to the existing list
@ -155,10 +162,13 @@ export function loadMoreFiles(url) {
$('body').append($resp.find('script#diff-data-script'));
onShowMoreFiles();
}).always(() => {
} catch (error) {
console.error('Error:', error);
showErrorToast('An error occurred while loading more files.');
} finally {
$target.removeClass('disabled');
pageData.diffFileInfo.isLoadingNewData = false;
});
}
}
function initRepoDiffShowMore() {
@ -170,7 +180,7 @@ function initRepoDiffShowMore() {
loadMoreFiles(linkLoadMore);
});
$(document).on('click', 'a.diff-load-button', (e) => {
$(document).on('click', 'a.diff-load-button', async (e) => {
e.preventDefault();
const $target = $(e.target);
@ -181,26 +191,28 @@ function initRepoDiffShowMore() {
$target.addClass('disabled');
const url = $target.data('href');
$.ajax({
type: 'GET',
url,
}).done((resp) => {
try {
const response = await GET(url);
const resp = await response.text();
if (!resp) {
$target.removeClass('disabled');
return;
}
$target.parent().replaceWith($(resp).find('#diff-file-boxes .diff-file-body .file-body').children());
onShowMoreFiles();
}).fail(() => {
} catch (error) {
console.error('Error:', error);
} finally {
$target.removeClass('disabled');
});
}
});
}
export function initRepoDiffView() {
initRepoDiffConversationForm();
const diffFileList = $('#diff-file-list');
if (diffFileList.length === 0) return;
const $diffFileList = $('#diff-file-list');
if ($diffFileList.length === 0) return;
initDiffFileTree();
initDiffCommitSelect();
initRepoDiffShowMore();

View file

@ -15,9 +15,9 @@ function initEditPreviewTab($form) {
const $this = $(this);
let context = `${$this.data('context')}/`;
const mode = $this.data('markup-mode') || 'comment';
const treePathEl = $form.find('input#tree_path');
if (treePathEl.length > 0) {
context += treePathEl.val();
const $treePathEl = $form.find('input#tree_path');
if ($treePathEl.length > 0) {
context += $treePathEl.val();
}
context = context.substring(0, context.lastIndexOf('/'));
@ -25,7 +25,7 @@ function initEditPreviewTab($form) {
formData.append('mode', mode);
formData.append('context', context);
formData.append('text', $form.find(`.tab[data-tab="${$tabMenu.data('write')}"] textarea`).val());
formData.append('file_path', treePathEl.val());
formData.append('file_path', $treePathEl.val());
try {
const response = await POST($this.data('url'), {data: formData});
const data = await response.text();
@ -67,10 +67,10 @@ export function initRepoEditor() {
$('.js-quick-pull-choice-option').on('change', function () {
if ($(this).val() === 'commit-to-new-branch') {
showElem($('.quick-pull-branch-name'));
$('.quick-pull-branch-name input').prop('required', true);
document.querySelector('.quick-pull-branch-name input').required = true;
} else {
hideElem($('.quick-pull-branch-name'));
$('.quick-pull-branch-name input').prop('required', false);
document.querySelector('.quick-pull-branch-name input').required = false;
}
$('#commit-button').text($(this).attr('button_text'));
});
@ -78,11 +78,11 @@ export function initRepoEditor() {
const joinTreePath = ($fileNameEl) => {
const parts = [];
$('.breadcrumb span.section').each(function () {
const element = $(this);
if (element.find('a').length) {
parts.push(element.find('a').text());
const $element = $(this);
if ($element.find('a').length) {
parts.push($element.find('a').text());
} else {
parts.push(element.text());
parts.push($element.text());
}
});
if ($fileNameEl.val()) parts.push($fileNameEl.val());
@ -135,13 +135,13 @@ export function initRepoEditor() {
// Using events from https://github.com/codedance/jquery.AreYouSure#advanced-usage
// to enable or disable the commit button
const $commitButton = $('#commit-button');
const commitButton = document.getElementById('commit-button');
const $editForm = $('.ui.edit.form');
const dirtyFileClass = 'dirty-file';
// Disabling the button at the start
if ($('input[name="page_has_posted"]').val() !== 'true') {
$commitButton.prop('disabled', true);
commitButton.disabled = true;
}
// Registering a custom listener for the file path and the file content
@ -151,7 +151,7 @@ export function initRepoEditor() {
fieldSelector: ':input:not(.commit-form-wrapper :input)',
change() {
const dirty = $(this).hasClass(dirtyFileClass);
$commitButton.prop('disabled', !dirty);
commitButton.disabled = !dirty;
},
});
@ -163,7 +163,7 @@ export function initRepoEditor() {
editor.setValue(value);
}
$commitButton.on('click', (event) => {
commitButton?.addEventListener('click', (e) => {
// A modal which asks if an empty file should be committed
if ($editArea.val().length === 0) {
$('#edit-empty-content-modal').modal({
@ -171,7 +171,7 @@ export function initRepoEditor() {
$('.edit.form').trigger('submit');
},
}).modal('show');
event.preventDefault();
e.preventDefault();
}
});
})();
@ -181,6 +181,6 @@ export function renderPreviewPanelContent($panelPreviewer, data) {
$panelPreviewer.html(data);
initMarkupContent();
const refIssues = $panelPreviewer.find('p .ref-issue');
attachRefIssueContextPopup(refIssues);
const $refIssues = $panelPreviewer.find('p .ref-issue');
attachRefIssueContextPopup($refIssues);
}

View file

@ -63,10 +63,10 @@ export function initRepoGraphGit() {
(async () => {
const response = await GET(String(ajaxUrl));
const html = await response.text();
const div = $(html);
$('#pagination').html(div.find('#pagination').html());
$('#rel-container').html(div.find('#rel-container').html());
$('#rev-container').html(div.find('#rev-container').html());
const $div = $(html);
$('#pagination').html($div.find('#pagination').html());
$('#rel-container').html($div.find('#rel-container').html());
$('#rev-container').html($div.find('#rev-container').html());
$('#loading-indicator').addClass('gt-hidden');
$('#rel-container').removeClass('gt-hidden');
$('#rev-container').removeClass('gt-hidden');

View file

@ -6,55 +6,55 @@ 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 $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'),
countPrompt: $topicDropdown.attr('data-text-count-prompt'),
formatPrompt: $topicDropdown.attr('data-text-format-prompt'),
};
mgrBtn.on('click', () => {
hideElem(viewDiv);
showElem(editDiv);
topicDropdownSearch.focus();
$mgrBtn.on('click', () => {
hideElem($viewDiv);
showElem($editDiv);
$topicDropdownSearch.trigger('focus');
});
$('#cancel_topic_edit').on('click', () => {
hideElem(editDiv);
showElem(viewDiv);
mgrBtn.focus();
hideElem($editDiv);
showElem($viewDiv);
$mgrBtn.trigger('focus');
});
saveBtn.on('click', async () => {
$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});
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();
$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 gt-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 = $('<a class="ui repo-topic large label topic gt-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);
hideElem($editDiv);
showElem($viewDiv);
}
} else if (response.status === 422) {
const responseData = await response.json();
@ -62,10 +62,10 @@ export function initRepoTopicBar() {
topicPrompts.formatPrompt = responseData.message;
const {invalidTopics} = responseData;
const topicLabels = topicDropdown.children('a.ui.label');
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');
$topicLabels.eq(index).removeClass('green').addClass('red');
}
}
} else {
@ -74,10 +74,10 @@ export function initRepoTopicBar() {
}
// Always validate the form
topicForm.form('validate form');
$topicForm.form('validate form');
});
topicDropdown.dropdown({
$topicDropdown.dropdown({
allowAdditions: true,
forceSelection: false,
fullTextSearch: 'exact',
@ -100,7 +100,7 @@ export function initRepoTopicBar() {
const query = stripTags(this.urlData.query.trim());
let found_query = false;
const current_topics = [];
topicDropdown.find('a.label.visible').each((_, el) => {
$topicDropdown.find('a.label.visible').each((_, el) => {
current_topics.push(el.getAttribute('data-value'));
});
@ -150,15 +150,15 @@ export function initRepoTopicBar() {
});
$.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 $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');
$topics.last().removeClass('green').addClass('red');
}
return status && topicDropdown.children('a.ui.label.red').length === 0;
return status && $topicDropdown.children('a.ui.label.red').length === 0;
};
topicForm.form({
$topicForm.form({
on: 'change',
inline: true,
fields: {

View file

@ -1,8 +1,9 @@
import $ from 'jquery';
import {svg} from '../svg.js';
import {showErrorToast} from '../modules/toast.js';
import {GET, POST} from '../modules/fetch.js';
const {appSubUrl, csrfToken} = window.config;
const {appSubUrl} = window.config;
let i18nTextEdited;
let i18nTextOptions;
let i18nTextDeleteFromHistory;
@ -31,19 +32,27 @@ function showContentHistoryDetail(issueBaseUrl, commentId, historyId, itemTitleH
$dialog.find('.dialog-header-options').dropdown({
showOnFocus: false,
allowReselection: true,
onChange(_value, _text, $item) {
async onChange(_value, _text, $item) {
const optionItem = $item.data('option-item');
if (optionItem === 'delete') {
if (window.confirm(i18nTextDeleteFromHistoryConfirm)) {
$.post(`${issueBaseUrl}/content-history/soft-delete?comment_id=${commentId}&history_id=${historyId}`, {
_csrf: csrfToken,
}).done((resp) => {
try {
const params = new URLSearchParams();
params.append('comment_id', commentId);
params.append('history_id', historyId);
const response = await POST(`${issueBaseUrl}/content-history/soft-delete?${params.toString()}`);
const resp = await response.json();
if (resp.ok) {
$dialog.modal('hide');
} else {
showErrorToast(resp.message);
}
});
} catch (error) {
console.error('Error:', error);
showErrorToast('An error occurred while deleting the history.');
}
}
} else { // required by eslint
showErrorToast(`unknown option item: ${optionItem}`);
@ -54,19 +63,24 @@ function showContentHistoryDetail(issueBaseUrl, commentId, historyId, itemTitleH
}
});
$dialog.modal({
onShow() {
$.ajax({
url: `${issueBaseUrl}/content-history/detail?comment_id=${commentId}&history_id=${historyId}`,
data: {
_csrf: csrfToken,
},
}).done((resp) => {
async onShow() {
try {
const params = new URLSearchParams();
params.append('comment_id', commentId);
params.append('history_id', historyId);
const url = `${issueBaseUrl}/content-history/detail?${params.toString()}`;
const response = await GET(url);
const resp = await response.json();
$dialog.find('.comment-diff-data').removeClass('is-loading').html(resp.diffHtml);
// there is only one option "item[data-option-item=delete]", so the dropdown can be entirely shown/hidden.
if (resp.canSoftDelete) {
$dialog.find('.dialog-header-options').removeClass('gt-hidden');
}
});
} catch (error) {
console.error('Error:', error);
}
},
onHidden() {
$dialog.remove();
@ -103,7 +117,7 @@ function showContentHistoryMenu(issueBaseUrl, $item, commentId) {
});
}
export function initRepoIssueContentHistory() {
export async function initRepoIssueContentHistory() {
const issueIndex = $('#issueIndex').val();
if (!issueIndex) return;
@ -114,12 +128,10 @@ export function initRepoIssueContentHistory() {
const repoLink = $('#repolink').val();
const issueBaseUrl = `${appSubUrl}/${repoLink}/issues/${issueIndex}`;
$.ajax({
url: `${issueBaseUrl}/content-history/overview`,
data: {
_csrf: csrfToken,
},
}).done((resp) => {
try {
const response = await GET(`${issueBaseUrl}/content-history/overview`);
const resp = await response.json();
i18nTextEdited = resp.i18n.textEdited;
i18nTextDeleteFromHistory = resp.i18n.textDeleteFromHistory;
i18nTextDeleteFromHistoryConfirm = resp.i18n.textDeleteFromHistoryConfirm;
@ -133,5 +145,7 @@ export function initRepoIssueContentHistory() {
const $itemComment = $(`#issuecomment-${commentId}`);
showContentHistoryMenu(issueBaseUrl, $itemComment, commentId);
}
});
} catch (error) {
console.error('Error:', error);
}
}

View file

@ -1,6 +1,6 @@
import $ from 'jquery';
import {updateIssuesMeta} from './repo-issue.js';
import {toggleElem, hideElem} from '../utils/dom.js';
import {toggleElem, hideElem, isElemHidden} from '../utils/dom.js';
import {htmlEscape} from 'escape-goat';
import {confirmModal} from './comp/ConfirmModal.js';
import {showErrorToast} from '../modules/toast.js';
@ -8,32 +8,42 @@ import {createSortable} from '../modules/sortable.js';
import {DELETE, POST} from '../modules/fetch.js';
function initRepoIssueListCheckboxes() {
const $issueSelectAll = $('.issue-checkbox-all');
const $issueCheckboxes = $('.issue-checkbox');
const issueSelectAll = document.querySelector('.issue-checkbox-all');
const issueCheckboxes = document.querySelectorAll('.issue-checkbox');
const syncIssueSelectionState = () => {
const $checked = $issueCheckboxes.filter(':checked');
const anyChecked = $checked.length !== 0;
const allChecked = anyChecked && $checked.length === $issueCheckboxes.length;
const checkedCheckboxes = Array.from(issueCheckboxes).filter((el) => el.checked);
const anyChecked = Boolean(checkedCheckboxes.length);
const allChecked = anyChecked && checkedCheckboxes.length === issueCheckboxes.length;
if (allChecked) {
$issueSelectAll.prop({'checked': true, 'indeterminate': false});
issueSelectAll.checked = true;
issueSelectAll.indeterminate = false;
} else if (anyChecked) {
$issueSelectAll.prop({'checked': false, 'indeterminate': true});
issueSelectAll.checked = false;
issueSelectAll.indeterminate = true;
} else {
$issueSelectAll.prop({'checked': false, 'indeterminate': false});
issueSelectAll.checked = false;
issueSelectAll.indeterminate = false;
}
// if any issue is selected, show the action panel, otherwise show the filter panel
toggleElem($('#issue-filters'), !anyChecked);
toggleElem($('#issue-actions'), anyChecked);
// there are two panels but only one select-all checkbox, so move the checkbox to the visible panel
$('#issue-filters, #issue-actions').filter(':visible').find('.issue-list-toolbar-left').prepend($issueSelectAll);
const panels = document.querySelectorAll('#issue-filters, #issue-actions');
const visiblePanel = Array.from(panels).find((el) => !isElemHidden(el));
const toolbarLeft = visiblePanel.querySelector('.issue-list-toolbar-left');
toolbarLeft.prepend(issueSelectAll);
};
$issueCheckboxes.on('change', syncIssueSelectionState);
for (const el of issueCheckboxes) {
el.addEventListener('change', syncIssueSelectionState);
}
$issueSelectAll.on('change', () => {
$issueCheckboxes.prop('checked', $issueSelectAll.is(':checked'));
issueSelectAll.addEventListener('change', () => {
for (const el of issueCheckboxes) {
el.checked = issueSelectAll.checked;
}
syncIssueSelectionState();
});
@ -125,7 +135,9 @@ function initRepoIssueListAuthorDropdown() {
if (newMenuHtml) {
const $newMenuItems = $(newMenuHtml);
$newMenuItems.addClass('dynamic-item');
$menu.append('<div class="divider dynamic-item"></div>', ...$newMenuItems);
const div = document.createElement('div');
div.classList.add('divider', 'dynamic-item');
$menu[0].append(div, ...$newMenuItems);
}
$searchDropdown.dropdown('refresh');
// defer our selection to the next tick, because dropdown will set the selection item after this `menu` function

View file

@ -6,8 +6,9 @@ import {setFileFolding} from './file-fold.js';
import {getComboMarkdownEditor, initComboMarkdownEditor} from './comp/ComboMarkdownEditor.js';
import {toAbsoluteUrl} from '../utils.js';
import {initDropzone} from './common-global.js';
import {POST, GET} from '../modules/fetch.js';
const {appSubUrl, csrfToken} = window.config;
const {appSubUrl} = window.config;
export function initRepoIssueTimeTracking() {
$(document).on('click', '.issue-add-time', () => {
@ -40,7 +41,7 @@ export function initRepoIssueTimeTracking() {
});
}
function updateDeadline(deadlineString) {
async function updateDeadline(deadlineString) {
hideElem($('#deadline-err-invalid-date'));
$('#deadline-loader').addClass('loading');
@ -56,23 +57,21 @@ function updateDeadline(deadlineString) {
realDeadline = new Date(newDate);
}
$.ajax(`${$('#update-issue-deadline-form').attr('action')}`, {
data: JSON.stringify({
due_date: realDeadline,
}),
headers: {
'X-Csrf-Token': csrfToken,
},
contentType: 'application/json',
type: 'POST',
success() {
try {
const response = await POST($('#update-issue-deadline-form').attr('action'), {
data: {due_date: realDeadline}
});
if (response.ok) {
window.location.reload();
},
error() {
$('#deadline-loader').removeClass('loading');
showElem($('#deadline-err-invalid-date'));
},
});
} else {
throw new Error('Invalid response');
}
} catch (error) {
console.error(error);
$('#deadline-loader').removeClass('loading');
showElem($('#deadline-err-invalid-date'));
}
}
export function initRepoIssueDue() {
@ -145,9 +144,9 @@ export function initRepoIssueSidebarList() {
$('.menu .ui.dropdown.label-filter').on('keydown', (e) => {
if (e.altKey && e.keyCode === 13) {
const selectedItems = $('.menu .ui.dropdown.label-filter .menu .item.selected');
if (selectedItems.length > 0) {
excludeLabel($(selectedItems[0]));
const $selectedItems = $('.menu .ui.dropdown.label-filter .menu .item.selected');
if ($selectedItems.length > 0) {
excludeLabel($($selectedItems[0]));
}
}
});
@ -156,12 +155,12 @@ export function initRepoIssueSidebarList() {
export function initRepoIssueCommentDelete() {
// Delete comment
$(document).on('click', '.delete-comment', function () {
$(document).on('click', '.delete-comment', async function () {
const $this = $(this);
if (window.confirm($this.data('locale'))) {
$.post($this.data('url'), {
_csrf: csrfToken,
}).done(() => {
try {
const response = await POST($this.data('url'));
if (!response.ok) throw new Error('Failed to delete comment');
const $conversationHolder = $this.closest('.conversation-holder');
// Check if this was a pending comment.
@ -186,7 +185,9 @@ export function initRepoIssueCommentDelete() {
}
$conversationHolder.remove();
}
});
} catch (error) {
console.error(error);
}
}
return false;
});
@ -213,12 +214,12 @@ export function initRepoIssueDependencyDelete() {
export function initRepoIssueCodeCommentCancel() {
// Cancel inline code comment
$(document).on('click', '.cancel-code-comment', (e) => {
const form = $(e.currentTarget).closest('form');
if (form.length > 0 && form.hasClass('comment-form')) {
form.addClass('gt-hidden');
showElem(form.closest('.comment-code-cloud').find('button.comment-form-reply'));
const $form = $(e.currentTarget).closest('form');
if ($form.length > 0 && $form.hasClass('comment-form')) {
$form.addClass('gt-hidden');
showElem($form.closest('.comment-code-cloud').find('button.comment-form-reply'));
} else {
form.closest('.comment-code-cloud').remove();
$form.closest('.comment-code-cloud').remove();
}
});
}
@ -226,22 +227,32 @@ export function initRepoIssueCodeCommentCancel() {
export function initRepoPullRequestUpdate() {
// Pull Request update button
const $pullUpdateButton = $('.update-button > button');
$pullUpdateButton.on('click', function (e) {
$pullUpdateButton.on('click', async function (e) {
e.preventDefault();
const $this = $(this);
const redirect = $this.data('redirect');
$this.addClass('loading');
$.post($this.data('do'), {
_csrf: csrfToken
}).done((data) => {
if (data.redirect) {
window.location.href = data.redirect;
} else if (redirect) {
window.location.href = redirect;
} else {
window.location.reload();
}
});
let response;
try {
response = await POST($this.data('do'));
} catch (error) {
console.error(error);
} finally {
$this.removeClass('loading');
}
let data;
try {
data = await response?.json(); // the response is probably not a JSON
} catch (error) {
console.error(error);
}
if (data?.redirect) {
window.location.href = data.redirect;
} else if (redirect) {
window.location.href = redirect;
} else {
window.location.reload();
}
});
$('.update-button > .dropdown').dropdown({
@ -267,20 +278,24 @@ export function initRepoPullRequestAllowMaintainerEdit() {
const promptError = $checkbox.attr('data-prompt-error');
$checkbox.checkbox({
'onChange': () => {
'onChange': async () => {
const checked = $checkbox.checkbox('is checked');
let url = $checkbox.attr('data-url');
url += '/set_allow_maintainer_edit';
$checkbox.checkbox('set disabled');
$.ajax({url, type: 'POST',
data: {_csrf: csrfToken, allow_maintainer_edit: checked},
error: () => {
showTemporaryTooltip($checkbox[0], promptError);
},
complete: () => {
$checkbox.checkbox('set enabled');
},
});
try {
const response = await POST(url, {
data: {allow_maintainer_edit: checked},
});
if (!response.ok) {
throw new Error('Failed to update maintainer edit permission');
}
} catch (error) {
console.error(error);
showTemporaryTooltip($checkbox[0], promptError);
} finally {
$checkbox.checkbox('set enabled');
}
},
});
}
@ -329,17 +344,15 @@ export function initRepoIssueWipTitle() {
});
}
export async function updateIssuesMeta(url, action, issueIds, elementId) {
return $.ajax({
type: 'POST',
url,
data: {
_csrf: csrfToken,
action,
issue_ids: issueIds,
id: elementId,
},
});
export async function updateIssuesMeta(url, action, issue_ids, id) {
try {
const response = await POST(url, {data: new URLSearchParams({action, issue_ids, id})});
if (!response.ok) {
throw new Error('Failed to update issues meta');
}
} catch (error) {
console.error(error);
}
}
export function initRepoIssueComments() {
@ -357,10 +370,10 @@ export function initRepoIssueComments() {
});
$(document).on('click', (event) => {
const urlTarget = $(':target');
if (urlTarget.length === 0) return;
const $urlTarget = $(':target');
if ($urlTarget.length === 0) return;
const urlTargetId = urlTarget.attr('id');
const urlTargetId = $urlTarget.attr('id');
if (!urlTargetId) return;
if (!/^(issue|pull)(comment)?-\d+$/.test(urlTargetId)) return;
@ -377,18 +390,18 @@ export function initRepoIssueComments() {
export async function handleReply($el) {
hideElem($el);
const form = $el.closest('.comment-code-cloud').find('.comment-form');
form.removeClass('gt-hidden');
const $form = $el.closest('.comment-code-cloud').find('.comment-form');
$form.removeClass('gt-hidden');
const $textarea = form.find('textarea');
const $textarea = $form.find('textarea');
let editor = getComboMarkdownEditor($textarea);
if (!editor) {
// FIXME: the initialization of the dropzone is not consistent.
// When the page is loaded, the dropzone is initialized by initGlobalDropzone, but the editor is not initialized.
// When the form is submitted and partially reload, none of them is initialized.
const dropzone = form.find('.dropzone')[0];
const dropzone = $form.find('.dropzone')[0];
if (!dropzone.dropzone) initDropzone(dropzone);
editor = await initComboMarkdownEditor(form.find('.combo-markdown-editor'));
editor = await initComboMarkdownEditor($form.find('.combo-markdown-editor'));
}
editor.focus();
return editor;
@ -400,30 +413,30 @@ export function initRepoPullRequestReview() {
if (window.history.scrollRestoration !== 'manual') {
window.history.scrollRestoration = 'manual';
}
const commentDiv = $(window.location.hash);
if (commentDiv) {
const $commentDiv = $(window.location.hash);
if ($commentDiv) {
// get the name of the parent id
const groupID = commentDiv.closest('div[id^="code-comments-"]').attr('id');
const groupID = $commentDiv.closest('div[id^="code-comments-"]').attr('id');
if (groupID && groupID.startsWith('code-comments-')) {
const id = groupID.slice(14);
const ancestorDiffBox = commentDiv.closest('.diff-file-box');
const $ancestorDiffBox = $commentDiv.closest('.diff-file-box');
// on pages like conversation, there is no diff header
const diffHeader = ancestorDiffBox.find('.diff-file-header');
const $diffHeader = $ancestorDiffBox.find('.diff-file-header');
// offset is for scrolling
let offset = 30;
if (diffHeader[0]) {
offset += $('.diff-detail-box').outerHeight() + diffHeader.outerHeight();
if ($diffHeader[0]) {
offset += $('.diff-detail-box').outerHeight() + $diffHeader.outerHeight();
}
$(`#show-outdated-${id}`).addClass('gt-hidden');
$(`#code-comments-${id}`).removeClass('gt-hidden');
$(`#code-preview-${id}`).removeClass('gt-hidden');
$(`#hide-outdated-${id}`).removeClass('gt-hidden');
// if the comment box is folded, expand it
if (ancestorDiffBox.attr('data-folded') && ancestorDiffBox.attr('data-folded') === 'true') {
setFileFolding(ancestorDiffBox[0], ancestorDiffBox.find('.fold-file')[0], false);
if ($ancestorDiffBox.attr('data-folded') && $ancestorDiffBox.attr('data-folded') === 'true') {
setFileFolding($ancestorDiffBox[0], $ancestorDiffBox.find('.fold-file')[0], false);
}
window.scrollTo({
top: commentDiv.offset().top - offset,
top: $commentDiv.offset().top - offset,
behavior: 'instant'
});
}
@ -491,12 +504,12 @@ export function initRepoPullRequestReview() {
const side = $(this).data('side');
const idx = $(this).data('idx');
const path = $(this).closest('[data-path]').data('path');
const tr = $(this).closest('tr');
const lineType = tr.data('line-type');
const $tr = $(this).closest('tr');
const lineType = $tr.data('line-type');
let ntr = tr.next();
if (!ntr.hasClass('add-comment')) {
ntr = $(`
let $ntr = $tr.next();
if (!$ntr.hasClass('add-comment')) {
$ntr = $(`
<tr class="add-comment" data-line-type="${lineType}">
${isSplit ? `
<td class="add-comment-left" colspan="4"></td>
@ -505,21 +518,26 @@ export function initRepoPullRequestReview() {
<td class="add-comment-left add-comment-right" colspan="5"></td>
`}
</tr>`);
tr.after(ntr);
$tr.after($ntr);
}
const td = ntr.find(`.add-comment-${side}`);
const commentCloud = td.find('.comment-code-cloud');
if (commentCloud.length === 0 && !ntr.find('button[name="pending_review"]').length) {
const html = await $.get($(this).closest('[data-new-comment-url]').attr('data-new-comment-url'));
td.html(html);
td.find("input[name='line']").val(idx);
td.find("input[name='side']").val(side === 'left' ? 'previous' : 'proposed');
td.find("input[name='path']").val(path);
const $td = $ntr.find(`.add-comment-${side}`);
const $commentCloud = $td.find('.comment-code-cloud');
if ($commentCloud.length === 0 && !$ntr.find('button[name="pending_review"]').length) {
try {
const response = await GET($(this).closest('[data-new-comment-url]').attr('data-new-comment-url'));
const html = await response.text();
$td.html(html);
$td.find("input[name='line']").val(idx);
$td.find("input[name='side']").val(side === 'left' ? 'previous' : 'proposed');
$td.find("input[name='path']").val(path);
initDropzone(td.find('.dropzone')[0]);
const editor = await initComboMarkdownEditor(td.find('.combo-markdown-editor'));
editor.focus();
initDropzone($td.find('.dropzone')[0]);
const editor = await initComboMarkdownEditor($td.find('.combo-markdown-editor'));
editor.focus();
} catch (error) {
console.error(error);
}
}
});
}
@ -547,11 +565,19 @@ export function initRepoIssueWipToggle() {
const title = toggleWip.getAttribute('data-title');
const wipPrefix = toggleWip.getAttribute('data-wip-prefix');
const updateUrl = toggleWip.getAttribute('data-update-url');
await $.post(updateUrl, {
_csrf: csrfToken,
title: title?.startsWith(wipPrefix) ? title.slice(wipPrefix.length).trim() : `${wipPrefix.trim()} ${title}`,
});
window.location.reload();
try {
const params = new URLSearchParams();
params.append('title', title?.startsWith(wipPrefix) ? title.slice(wipPrefix.length).trim() : `${wipPrefix.trim()} ${title}`);
const response = await POST(updateUrl, {data: params});
if (!response.ok) {
throw new Error('Failed to toggle WIP status');
}
window.location.reload();
} catch (error) {
console.error(error);
}
});
}
@ -576,39 +602,43 @@ export function initRepoIssueTitleEdit() {
$('#edit-title').on('click', editTitleToggle);
$('#cancel-edit-title').on('click', editTitleToggle);
$('#save-edit-title').on('click', editTitleToggle).on('click', function () {
const pullrequest_targetbranch_change = function (update_url) {
$('#save-edit-title').on('click', editTitleToggle).on('click', async function () {
const pullrequest_targetbranch_change = async function (update_url) {
const targetBranch = $('#pull-target-branch').data('branch');
const $branchTarget = $('#branch_target');
if (targetBranch === $branchTarget.text()) {
window.location.reload();
return false;
}
$.post(update_url, {
_csrf: csrfToken,
target_branch: targetBranch
}).always(() => {
try {
await POST(update_url, {data: new URLSearchParams({target_branch: targetBranch})});
} catch (error) {
console.error(error);
} finally {
window.location.reload();
});
}
};
const pullrequest_target_update_url = $(this).attr('data-target-update-url');
if ($editInput.val().length === 0 || $editInput.val() === $issueTitle.text()) {
$editInput.val($issueTitle.text());
pullrequest_targetbranch_change(pullrequest_target_update_url);
await pullrequest_targetbranch_change(pullrequest_target_update_url);
} else {
$.post($(this).attr('data-update-url'), {
_csrf: csrfToken,
title: $editInput.val()
}, (data) => {
try {
const params = new URLSearchParams();
params.append('title', $editInput.val());
const response = await POST($(this).attr('data-update-url'), {data: params});
const data = await response.json();
$editInput.val(data.title);
$issueTitle.text(data.title);
if (pullrequest_target_update_url) {
pullrequest_targetbranch_change(pullrequest_target_update_url); // it will reload the window
await pullrequest_targetbranch_change(pullrequest_target_update_url); // it will reload the window
} else {
window.location.reload();
}
});
} catch (error) {
console.error(error);
}
}
return false;
});
@ -616,18 +646,18 @@ export function initRepoIssueTitleEdit() {
export function initRepoIssueBranchSelect() {
const changeBranchSelect = function () {
const selectionTextField = $('#pull-target-branch');
const $selectionTextField = $('#pull-target-branch');
const baseName = selectionTextField.data('basename');
const baseName = $selectionTextField.data('basename');
const branchNameNew = $(this).data('branch');
const branchNameOld = selectionTextField.data('branch');
const branchNameOld = $selectionTextField.data('branch');
// Replace branch name to keep translation from HTML template
selectionTextField.html(selectionTextField.html().replace(
$selectionTextField.html($selectionTextField.html().replace(
`${baseName}:${branchNameOld}`,
`${baseName}:${branchNameNew}`
));
selectionTextField.data('branch', branchNameNew); // update branch name in setting
$selectionTextField.data('branch', branchNameNew); // update branch name in setting
};
$('#branch-select > .item').on('click', changeBranchSelect);
}

View file

@ -24,6 +24,7 @@ import {initRepoPullRequestCommitStatus} from './repo-issue-pr-status.js';
import {hideElem, showElem} from '../utils/dom.js';
import {getComboMarkdownEditor, initComboMarkdownEditor} from './comp/ComboMarkdownEditor.js';
import {attachRefIssueContextPopup} from './contextpopup.js';
import {POST, GET} from '../modules/fetch.js';
const {csrfToken} = window.config;
@ -65,7 +66,7 @@ export function initRepoCommentForm() {
const $selectBranch = $('.ui.select-branch');
const $branchMenu = $selectBranch.find('.reference-list-menu');
const $isNewIssue = $branchMenu.hasClass('new-issue');
$branchMenu.find('.item:not(.no-select)').on('click', function () {
$branchMenu.find('.item:not(.no-select)').on('click', async function () {
const selectedValue = $(this).data('id');
const editMode = $('#editing_mode').val();
$($(this).data('id-selector')).val(selectedValue);
@ -75,8 +76,15 @@ export function initRepoCommentForm() {
}
if (editMode === 'true') {
const form = $('#update_issueref_form');
$.post(form.attr('action'), {_csrf: csrfToken, ref: selectedValue}, () => window.location.reload());
const $form = $('#update_issueref_form');
const params = new URLSearchParams();
params.append('ref', selectedValue);
try {
await POST($form.attr('action'), {data: params});
window.location.reload();
} catch (error) {
console.error(error);
}
} else if (editMode === '') {
$selectBranch.find('.ui .branch-name').text(selectedValue);
}
@ -131,7 +139,7 @@ export function initRepoCommentForm() {
hasUpdateAction = $listMenu.data('action') === 'update'; // Update the var
const clickedItem = $(this);
const $clickedItem = $(this);
const scope = $(this).attr('data-scope');
$(this).parent().find('.item').each(function () {
@ -140,10 +148,10 @@ export function initRepoCommentForm() {
if ($(this).attr('data-scope') !== scope) {
return true;
}
if (!$(this).is(clickedItem) && !$(this).hasClass('checked')) {
if (!$(this).is($clickedItem) && !$(this).hasClass('checked')) {
return true;
}
} else if (!$(this).is(clickedItem)) {
} else if (!$(this).is($clickedItem)) {
// Toggle for other labels
return true;
}
@ -344,17 +352,18 @@ async function onEditContent(event) {
this.on('success', (file, data) => {
file.uuid = data.uuid;
fileUuidDict[file.uuid] = {submitted: false};
const input = $(`<input id="${data.uuid}" name="files" type="hidden">`).val(data.uuid);
$dropzone.find('.files').append(input);
const $input = $(`<input id="${data.uuid}" name="files" type="hidden">`).val(data.uuid);
$dropzone.find('.files').append($input);
});
this.on('removedfile', (file) => {
this.on('removedfile', async (file) => {
if (disableRemovedfileEvent) return;
$(`#${file.uuid}`).remove();
if ($dropzone.attr('data-remove-url') && !fileUuidDict[file.uuid].submitted) {
$.post($dropzone.attr('data-remove-url'), {
file: file.uuid,
_csrf: csrfToken,
});
try {
await POST($dropzone.attr('data-remove-url'), {data: new URLSearchParams({file: file.uuid})});
} catch (error) {
console.error(error);
}
}
});
this.on('submit', () => {
@ -362,8 +371,10 @@ async function onEditContent(event) {
fileUuidDict[fileUuid].submitted = true;
});
});
this.on('reload', () => {
$.getJSON($editContentZone.attr('data-attachment-url'), (data) => {
this.on('reload', async () => {
try {
const response = await GET($editContentZone.attr('data-attachment-url'));
const data = await response.json();
// do not trigger the "removedfile" event, otherwise the attachments would be deleted from server
disableRemovedfileEvent = true;
dz.removeAllFiles(true);
@ -379,10 +390,12 @@ async function onEditContent(event) {
dz.files.push(attachment);
fileUuidDict[attachment.uuid] = {submitted: true};
$dropzone.find(`img[src='${imgSrc}']`).css('max-width', '100%');
const input = $(`<input id="${attachment.uuid}" name="files" type="hidden">`).val(attachment.uuid);
$dropzone.find('.files').append(input);
const $input = $(`<input id="${attachment.uuid}" name="files" type="hidden">`).val(attachment.uuid);
$dropzone.find('.files').append($input);
}
});
} catch (error) {
console.error(error);
}
});
},
});
@ -398,35 +411,37 @@ async function onEditContent(event) {
}
};
const saveAndRefresh = (dz) => {
const saveAndRefresh = async (dz) => {
showElem($renderContent);
hideElem($editContentZone);
$.post($editContentZone.attr('data-update-url'), {
_csrf: csrfToken,
content: comboMarkdownEditor.value(),
context: $editContentZone.attr('data-context'),
files: dz.files.map((file) => file.uuid),
}, (data) => {
try {
const params = new URLSearchParams({
content: comboMarkdownEditor.value(),
context: $editContentZone.attr('data-context'),
});
for (const file of dz.files) params.append('files[]', file.uuid);
const response = await POST($editContentZone.attr('data-update-url'), {data: params});
const data = await response.json();
if (!data.content) {
$renderContent.html($('#no-content').html());
$rawContent.text('');
} else {
$renderContent.html(data.content);
$rawContent.text(comboMarkdownEditor.value());
const refIssues = $renderContent.find('p .ref-issue');
attachRefIssueContextPopup(refIssues);
const $refIssues = $renderContent.find('p .ref-issue');
attachRefIssueContextPopup($refIssues);
}
const $content = $segment;
if (!$content.find('.dropzone-attachments').length) {
if (data.attachments !== '') {
$content.append(`<div class="dropzone-attachments"></div>`);
$content.find('.dropzone-attachments').replaceWith(data.attachments);
$content[0].append(data.attachments);
}
} else if (data.attachments === '') {
$content.find('.dropzone-attachments').remove();
} else {
$content.find('.dropzone-attachments').replaceWith(data.attachments);
$content.find('.dropzone-attachments')[0].outerHTML = data.attachments;
}
if (dz) {
dz.emit('submit');
@ -434,7 +449,9 @@ async function onEditContent(event) {
}
initMarkupContent();
initCommentContent();
});
} catch (error) {
console.error(error);
}
};
if (!$editContentZone.html()) {
@ -516,7 +533,7 @@ export function initRepository() {
const gitignores = $('input[name="gitignores"]').val();
const license = $('input[name="license"]').val();
if (gitignores || license) {
$('input[name="auto_init"]').prop('checked', true);
document.querySelector('input[name="auto_init"]').checked = true;
}
});
}

View file

@ -2,8 +2,7 @@ import $ from 'jquery';
import {useLightTextOnBackground} from '../utils/color.js';
import tinycolor from 'tinycolor2';
import {createSortable} from '../modules/sortable.js';
const {csrfToken} = window.config;
import {POST, DELETE, PUT} from '../modules/fetch.js';
function updateIssueCount(cards) {
const parent = cards.parentElement;
@ -11,22 +10,23 @@ function updateIssueCount(cards) {
parent.getElementsByClassName('project-column-issue-count')[0].textContent = cnt;
}
function createNewColumn(url, columnTitle, projectColorInput) {
$.ajax({
url,
data: JSON.stringify({title: columnTitle.val(), color: projectColorInput.val()}),
headers: {
'X-Csrf-Token': csrfToken,
},
contentType: 'application/json',
method: 'POST',
}).done(() => {
async function createNewColumn(url, columnTitle, projectColorInput) {
try {
await POST(url, {
data: {
title: columnTitle.val(),
color: projectColorInput.val(),
},
});
} catch (error) {
console.error(error);
} finally {
columnTitle.closest('form').removeClass('dirty');
window.location.reload();
});
}
}
function moveIssue({item, from, to, oldIndex}) {
async function moveIssue({item, from, to, oldIndex}) {
const columnCards = to.getElementsByClassName('issue-card');
updateIssueCount(from);
updateIssueCount(to);
@ -38,18 +38,14 @@ function moveIssue({item, from, to, oldIndex}) {
})),
};
$.ajax({
url: `${to.getAttribute('data-url')}/move`,
data: JSON.stringify(columnSorting),
headers: {
'X-Csrf-Token': csrfToken,
},
contentType: 'application/json',
type: 'POST',
error: () => {
from.insertBefore(item, from.children[oldIndex]);
},
});
try {
await POST(`${to.getAttribute('data-url')}/move`, {
data: columnSorting,
});
} catch (error) {
console.error(error);
from.insertBefore(item, from.children[oldIndex]);
}
}
async function initRepoProjectSortable() {
@ -67,20 +63,21 @@ async function initRepoProjectSortable() {
ghostClass: 'card-ghost',
delayOnTouchOnly: true,
delay: 500,
onSort: () => {
onSort: async () => {
boardColumns = mainBoard.getElementsByClassName('project-column');
for (let i = 0; i < boardColumns.length; i++) {
const column = boardColumns[i];
if (parseInt($(column).data('sorting')) !== i) {
$.ajax({
url: $(column).data('url'),
data: JSON.stringify({sorting: i, color: rgbToHex($(column).css('backgroundColor'))}),
headers: {
'X-Csrf-Token': csrfToken,
},
contentType: 'application/json',
method: 'PUT',
});
try {
await PUT($(column).data('url'), {
data: {
sorting: i,
color: rgbToHex($(column).css('backgroundColor')),
},
});
} catch (error) {
console.error(error);
}
}
}
},
@ -108,90 +105,85 @@ export function initRepoProject() {
const _promise = initRepoProjectSortable();
$('.edit-project-column-modal').each(function () {
const projectHeader = $(this).closest('.project-column-header');
const projectTitleLabel = projectHeader.find('.project-column-title');
const projectTitleInput = $(this).find('.project-column-title-input');
const projectColorInput = $(this).find('#new_project_column_color');
const boardColumn = $(this).closest('.project-column');
const $projectHeader = $(this).closest('.project-column-header');
const $projectTitleLabel = $projectHeader.find('.project-column-title');
const $projectTitleInput = $(this).find('.project-column-title-input');
const $projectColorInput = $(this).find('#new_project_column_color');
const $boardColumn = $(this).closest('.project-column');
if (boardColumn.css('backgroundColor')) {
setLabelColor(projectHeader, rgbToHex(boardColumn.css('backgroundColor')));
if ($boardColumn.css('backgroundColor')) {
setLabelColor($projectHeader, rgbToHex($boardColumn.css('backgroundColor')));
}
$(this).find('.edit-project-column-button').on('click', function (e) {
$(this).find('.edit-project-column-button').on('click', async function (e) {
e.preventDefault();
$.ajax({
url: $(this).data('url'),
data: JSON.stringify({title: projectTitleInput.val(), color: projectColorInput.val()}),
headers: {
'X-Csrf-Token': csrfToken,
},
contentType: 'application/json',
method: 'PUT',
}).done(() => {
projectTitleLabel.text(projectTitleInput.val());
projectTitleInput.closest('form').removeClass('dirty');
if (projectColorInput.val()) {
setLabelColor(projectHeader, projectColorInput.val());
try {
await PUT($(this).data('url'), {
data: {
title: $projectTitleInput.val(),
color: $projectColorInput.val(),
},
});
} catch (error) {
console.error(error);
} finally {
$projectTitleLabel.text($projectTitleInput.val());
$projectTitleInput.closest('form').removeClass('dirty');
if ($projectColorInput.val()) {
setLabelColor($projectHeader, $projectColorInput.val());
}
boardColumn.attr('style', `background: ${projectColorInput.val()}!important`);
$boardColumn.attr('style', `background: ${$projectColorInput.val()}!important`);
$('.ui.modal').modal('hide');
});
}
});
});
$('.default-project-column-modal').each(function () {
const boardColumn = $(this).closest('.project-column');
const showButton = $(boardColumn).find('.default-project-column-show');
const commitButton = $(this).find('.actions > .ok.button');
const $boardColumn = $(this).closest('.project-column');
const $showButton = $($boardColumn).find('.default-project-column-show');
const $commitButton = $(this).find('.actions > .ok.button');
$(commitButton).on('click', (e) => {
$($commitButton).on('click', async (e) => {
e.preventDefault();
$.ajax({
method: 'POST',
url: $(showButton).data('url'),
headers: {
'X-Csrf-Token': csrfToken,
},
contentType: 'application/json',
}).done(() => {
try {
await POST($($showButton).data('url'));
} catch (error) {
console.error(error);
} finally {
window.location.reload();
});
}
});
});
$('.show-delete-project-column-modal').each(function () {
const deleteColumnModal = $(`${$(this).attr('data-modal')}`);
const deleteColumnButton = deleteColumnModal.find('.actions > .ok.button');
const $deleteColumnModal = $(`${$(this).attr('data-modal')}`);
const $deleteColumnButton = $deleteColumnModal.find('.actions > .ok.button');
const deleteUrl = $(this).attr('data-url');
deleteColumnButton.on('click', (e) => {
$deleteColumnButton.on('click', async (e) => {
e.preventDefault();
$.ajax({
url: deleteUrl,
headers: {
'X-Csrf-Token': csrfToken,
},
contentType: 'application/json',
method: 'DELETE',
}).done(() => {
try {
await DELETE(deleteUrl);
} catch (error) {
console.error(error);
} finally {
window.location.reload();
});
}
});
});
$('#new_project_column_submit').on('click', (e) => {
e.preventDefault();
const columnTitle = $('#new_project_column');
const projectColorInput = $('#new_project_column_color_picker');
if (!columnTitle.val()) {
const $columnTitle = $('#new_project_column');
const $projectColorInput = $('#new_project_column_color_picker');
if (!$columnTitle.val()) {
return;
}
const url = $(this).data('url');
createNewColumn(url, columnTitle, projectColorInput);
const url = e.target.getAttribute('data-url');
createNewColumn(url, $columnTitle, $projectColorInput);
});
}

View file

@ -1,3 +1,3 @@
import $ from 'jquery';
window.$ = window.jQuery = $;
window.$ = window.jQuery = $; // eslint-disable-line no-jquery/variable-pattern

View file

@ -73,7 +73,9 @@ function delegateOne($dropdown) {
dropdownTemplates.menu = function(response, fields, preserveHTML, className) {
// when the dropdown menu items are loaded from AJAX requests, the items are created dynamically
const menuItems = dropdownTemplatesMenuOld(response, fields, preserveHTML, className);
const $wrapper = $('<div>').append(menuItems);
const div = document.createElement('div');
div.innerHTML = menuItems;
const $wrapper = $(div);
const $items = $wrapper.find('> .item');
$items.each((_, item) => updateMenuItem($dropdown[0], item));
$dropdown[0][ariaPatchKey].deferredRefreshAriaActiveItem();

View file

@ -7,7 +7,8 @@ const visibleInstances = new Set();
export function createTippy(target, opts = {}) {
// the callback functions should be destructured from opts,
// because we should use our own wrapper functions to handle them, do not let the user override them
const {onHide, onShow, onDestroy, ...other} = opts;
const {onHide, onShow, onDestroy, role, theme, ...other} = opts;
const instance = tippy(target, {
appendTo: document.body,
animation: false,
@ -35,17 +36,14 @@ export function createTippy(target, opts = {}) {
return onShow?.(instance);
},
arrow: `<svg width="16" height="7"><path d="m0 7 8-7 8 7Z" class="tippy-svg-arrow-outer"/><path d="m0 8 8-7 8 7Z" class="tippy-svg-arrow-inner"/></svg>`,
role: 'menu', // HTML role attribute, only tooltips should use "tooltip"
theme: other.role || 'menu', // CSS theme, either "tooltip", "menu" or "box-with-header"
role: role || 'menu', // HTML role attribute
theme: theme || role || 'menu', // CSS theme, either "tooltip", "menu" or "box-with-header"
plugins: [followCursor],
...other,
});
// for popups where content refers to a DOM element, we use the 'tippy-target' class
// to initially hide the content, now we can remove it as the content has been removed
// from the DOM by tippy
if (other.content instanceof Element) {
other.content.classList.remove('tippy-target');
if (role === 'menu') {
target.setAttribute('aria-haspopup', 'true');
}
return instance;

View file

@ -6,7 +6,6 @@ https://developer.mozilla.org/en-US/docs/Web/Web_Components
# Guidelines
* These components are loaded in `<head>` (before DOM body),
so they should have their own dependencies and should be very light,
then they won't affect the page loading time too much.
* If the component is not a public one, it's suggested to have its own `Gitea` or `gitea-` prefix to avoid conflicts.
* These components are loaded in `<head>` (before DOM body) in a separate entry point, they need to be lightweight to not affect the page loading time too much.
* Do not import `svg.js` into a web component because that file is currently not tree-shakeable, import svg files individually insteat.
* All our components must be added to `webpack.config.js` so they work correctly in Vue.

View file

@ -0,0 +1,40 @@
import {Temporal} from 'temporal-polyfill';
export function toAbsoluteLocaleDate(dateStr, lang, opts) {
return Temporal.PlainDate.from(dateStr).toLocaleString(lang ?? [], opts);
}
window.customElements.define('absolute-date', class extends HTMLElement {
static observedAttributes = ['date', 'year', 'month', 'weekday', 'day'];
update = () => {
const year = this.getAttribute('year') ?? '';
const month = this.getAttribute('month') ?? '';
const weekday = this.getAttribute('weekday') ?? '';
const day = this.getAttribute('day') ?? '';
const lang = this.closest('[lang]')?.getAttribute('lang') ||
this.ownerDocument.documentElement.getAttribute('lang') || '';
// only use the first 10 characters, e.g. the `yyyy-mm-dd` part
const dateStr = this.getAttribute('date').substring(0, 10);
if (!this.shadowRoot) this.attachShadow({mode: 'open'});
this.shadowRoot.textContent = toAbsoluteLocaleDate(dateStr, lang, {
...(year && {year}),
...(month && {month}),
...(weekday && {weekday}),
...(day && {day}),
});
};
attributeChangedCallback(_name, oldValue, newValue) {
if (!this.initialized || oldValue === newValue) return;
this.update();
}
connectedCallback() {
this.initialized = false;
this.update();
this.initialized = true;
}
});

View file

@ -0,0 +1,15 @@
import {toAbsoluteLocaleDate} from './absolute-date.js';
test('toAbsoluteLocaleDate', () => {
expect(toAbsoluteLocaleDate('2024-03-15', 'en-US', {
year: 'numeric',
month: 'long',
day: 'numeric',
})).toEqual('March 15, 2024');
expect(toAbsoluteLocaleDate('2024-03-15', 'de-DE', {
year: 'numeric',
month: 'long',
day: 'numeric',
})).toEqual('15. März 2024');
});

View file

@ -0,0 +1,5 @@
import './polyfills.js';
import '@github/relative-time-element';
import './origin-url.js';
import './overflow-menu.js';
import './absolute-date.js';

View file

@ -15,7 +15,7 @@ export function toOriginUrl(urlStr) {
return urlStr;
}
window.customElements.define('gitea-origin-url', class extends HTMLElement {
window.customElements.define('origin-url', class extends HTMLElement {
connectedCallback() {
this.textContent = toOriginUrl(this.getAttribute('data-url'));
}

View file

@ -1,4 +1,4 @@
import {toOriginUrl} from './GiteaOriginUrl.js';
import {toOriginUrl} from './origin-url.js';
test('toOriginUrl', () => {
const oldLocation = window.location;

View file

@ -0,0 +1,179 @@
import {throttle} from 'throttle-debounce';
import {createTippy} from '../modules/tippy.js';
import {isDocumentFragmentOrElementNode} from '../utils/dom.js';
import octiconKebabHorizontal from '../../../public/assets/img/svg/octicon-kebab-horizontal.svg';
window.customElements.define('overflow-menu', class extends HTMLElement {
updateItems = throttle(100, () => {
if (!this.tippyContent) {
const div = document.createElement('div');
div.classList.add('tippy-target');
div.tabIndex = '-1'; // for initial focus, programmatic focus only
div.addEventListener('keydown', (e) => {
if (e.key === 'Tab') {
const items = this.tippyContent.querySelectorAll('[role="menuitem"]');
if (e.shiftKey) {
if (document.activeElement === items[0]) {
e.preventDefault();
items[items.length - 1].focus();
}
} else {
if (document.activeElement === items[items.length - 1]) {
e.preventDefault();
items[0].focus();
}
}
} else if (e.key === 'Escape') {
e.preventDefault();
e.stopPropagation();
this.button._tippy.hide();
this.button.focus();
} else if (e.key === ' ' || e.code === 'Enter') {
if (document.activeElement?.matches('[role="menuitem"]')) {
e.preventDefault();
e.stopPropagation();
document.activeElement.click();
}
} else if (e.key === 'ArrowDown') {
if (document.activeElement?.matches('.tippy-target')) {
e.preventDefault();
e.stopPropagation();
document.activeElement.querySelector('[role="menuitem"]:first-of-type').focus();
} else if (document.activeElement?.matches('[role="menuitem"]')) {
e.preventDefault();
e.stopPropagation();
document.activeElement.nextElementSibling?.focus();
}
} else if (e.key === 'ArrowUp') {
if (document.activeElement?.matches('.tippy-target')) {
e.preventDefault();
e.stopPropagation();
document.activeElement.querySelector('[role="menuitem"]:last-of-type').focus();
} else if (document.activeElement?.matches('[role="menuitem"]')) {
e.preventDefault();
e.stopPropagation();
document.activeElement.previousElementSibling?.focus();
}
}
});
this.append(div);
this.tippyContent = div;
}
// move items in tippy back into the menu items for subsequent measurement
for (const item of this.tippyItems || []) {
this.menuItemsEl.append(item);
}
// measure which items are partially outside the element and move them into the button menu
this.tippyItems = [];
const menuRight = this.offsetLeft + this.offsetWidth;
const menuItems = this.menuItemsEl.querySelectorAll('.item');
for (const item of menuItems) {
const itemRight = item.offsetLeft + item.offsetWidth;
if (menuRight - itemRight < 38) { // roughly the width of .overflow-menu-button
this.tippyItems.push(item);
}
}
// if there are no overflown items, remove any previously created button
if (!this.tippyItems?.length) {
const btn = this.querySelector('.overflow-menu-button');
btn?._tippy?.destroy();
btn?.remove();
return;
}
// remove aria role from items that moved from tippy to menu
for (const item of menuItems) {
if (!this.tippyItems.includes(item)) {
item.removeAttribute('role');
}
}
// move all items that overflow into tippy
for (const item of this.tippyItems) {
item.setAttribute('role', 'menuitem');
this.tippyContent.append(item);
}
// update existing tippy
if (this.button?._tippy) {
this.button._tippy.setContent(this.tippyContent);
return;
}
// create button initially
const btn = document.createElement('button');
btn.classList.add('overflow-menu-button', 'btn', 'tw-px-2', 'hover:tw-text-text-dark');
btn.setAttribute('aria-label', window.config.i18n.more_items);
btn.innerHTML = octiconKebabHorizontal;
this.append(btn);
this.button = btn;
createTippy(btn, {
trigger: 'click',
hideOnClick: true,
interactive: true,
placement: 'bottom-end',
role: 'menu',
content: this.tippyContent,
onShow: () => { // FIXME: onShown doesn't work (never be called)
setTimeout(() => {
this.tippyContent.focus();
}, 0);
},
});
});
init() {
// ResizeObserver triggers on initial render, so we don't manually call `updateItems` here which
// also avoids a full-page FOUC in Firefox that happens when `updateItems` is called too soon.
this.resizeObserver = new ResizeObserver((entries) => {
for (const entry of entries) {
const newWidth = entry.contentBoxSize[0].inlineSize;
if (newWidth !== this.lastWidth) {
requestAnimationFrame(() => {
this.updateItems();
});
this.lastWidth = newWidth;
}
}
});
this.resizeObserver.observe(this);
}
connectedCallback() {
this.setAttribute('role', 'navigation');
// check whether the mandatory `.overflow-menu-items` element is present initially which happens
// with Vue which renders differently than browsers. If it's not there, like in the case of browser
// template rendering, wait for its addition.
// The eslint rule is not sophisticated enough or aware of this problem, see
// https://github.com/43081j/eslint-plugin-wc/pull/130
const menuItemsEl = this.querySelector('.overflow-menu-items'); // eslint-disable-line wc/no-child-traversal-in-connectedcallback
if (menuItemsEl) {
this.menuItemsEl = menuItemsEl;
this.init();
} else {
this.mutationObserver = new MutationObserver((mutations) => {
for (const mutation of mutations) {
for (const node of mutation.addedNodes) {
if (!isDocumentFragmentOrElementNode(node)) continue;
if (node.classList.contains('overflow-menu-items')) {
this.menuItemsEl = node;
this.mutationObserver?.disconnect();
this.init();
}
}
}
});
this.mutationObserver.observe(this, {childList: true});
}
}
disconnectedCallback() {
this.mutationObserver?.disconnect();
this.resizeObserver?.disconnect();
}
});

View file

@ -1,5 +0,0 @@
import '@webcomponents/custom-elements'; // polyfill for some browsers like PaleMoon
import './polyfill.js';
import '@github/relative-time-element';
import './GiteaOriginUrl.js';