Skip to content

Commit 31f104b

Browse files
committed
fix
1 parent 88f5d33 commit 31f104b

File tree

9 files changed

+124
-151
lines changed

9 files changed

+124
-151
lines changed

routers/web/admin/emails.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ func ActivateEmail(ctx *context.Context) {
154154

155155
// DeleteEmail serves a POST request for delete a user's email
156156
func DeleteEmail(ctx *context.Context) {
157-
u, err := user_model.GetUserByID(ctx, ctx.FormInt64("Uid"))
157+
u, err := user_model.GetUserByID(ctx, ctx.FormInt64("uid"))
158158
if err != nil || u == nil {
159159
ctx.ServerError("GetUserByID", err)
160160
return

templates/admin/emails/list.tmpl

Lines changed: 20 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -50,20 +50,21 @@
5050
<td>{{svg (Iif .IsPrimary "octicon-check" "octicon-x")}}</td>
5151
<td>
5252
{{if .CanChange}}
53-
<a class="link-email-action" href data-uid="{{.UID}}"
54-
data-email="{{.Email}}"
55-
data-primary="{{if .IsPrimary}}1{{else}}0{{end}}"
56-
data-activate="{{if .IsActivated}}0{{else}}1{{end}}">
53+
<a class="show-modal" href data-modal="#change-email-modal" data-modal-uid="{{.UID}}"
54+
data-modal-email="{{.Email}}"
55+
data-modal-primary="{{if .IsPrimary}}1{{else}}0{{end}}"
56+
data-modal-activate="{{if .IsActivated}}0{{else}}1{{end}}">
5757
{{svg (Iif .IsActivated "octicon-check" "octicon-x")}}
5858
</a>
5959
{{else}}
6060
{{svg (Iif .IsActivated "octicon-check" "octicon-x")}}
6161
{{end}}
6262
</td>
6363
<td>
64-
<div class="tw-flex tw-gap-2">
65-
<a class="delete-button" href="" data-url="{{$.Link}}/delete" data-id="{{.ID}}" data-data-uid="{{.UID}}">{{svg "octicon-trash"}}</a>
66-
</div>
64+
<a class="link-action negative" href data-url="{{$.Link}}/delete?id={{.ID}}&uid={{.UID}}"
65+
data-modal-confirm-header="{{ctx.Locale.Tr "admin.emails.delete"}}"
66+
data-modal-confirm-content="{{ctx.Locale.Tr "admin.emails.delete_desc"}}"
67+
>{{svg "octicon-trash"}}</a>
6768
</td>
6869
</tr>
6970
{{end}}
@@ -77,40 +78,24 @@
7778
<div class="header">
7879
{{ctx.Locale.Tr "admin.emails.change_email_header"}}
7980
</div>
80-
<div class="content">
81+
<form class="content ui form" action="{{AppSubUrl}}/-/admin/emails/activate" method="post">
8182
<p class="center">{{ctx.Locale.Tr "admin.emails.change_email_text"}}</p>
8283

83-
<form class="ui form" id="email-action-form" action="{{AppSubUrl}}/-/admin/emails/activate" method="post">
84-
{{$.CsrfTokenHtml}}
84+
{{$.CsrfTokenHtml}}
8585

86-
<input type="hidden" id="query-sort" name="sort" value="{{.SortType}}">
87-
<input type="hidden" id="query-keyword" name="q" value="{{.Keyword}}">
88-
<input type="hidden" id="query-primary" name="is_primary" value="{{.IsPrimary}}" required>
89-
<input type="hidden" id="query-activated" name="is_activated" value="{{.IsActivated}}" required>
86+
<input type="hidden" name="sort" value="{{.SortType}}">
87+
<input type="hidden" name="q" value="{{.Keyword}}">
88+
<input type="hidden" name="is_primary" value="{{.IsPrimary}}">
89+
<input type="hidden" name="is_activated" value="{{.IsActivated}}">
9090

91-
<input type="hidden" id="form-uid" name="uid" value="" required>
92-
<input type="hidden" id="form-email" name="email" value="" required>
93-
<input type="hidden" id="form-primary" name="primary" value="" required>
94-
<input type="hidden" id="form-activate" name="activate" value="" required>
91+
<input type="hidden" name="uid">
92+
<input type="hidden" name="email">
93+
<input type="hidden" name="primary">
94+
<input type="hidden" name="activate">
9595

96-
<div class="center">
97-
{{template "base/modal_actions_confirm" .}}
98-
</div>
99-
</form>
100-
</div>
96+
{{template "base/modal_actions_confirm" .}}
97+
</form>
10198
</div>
102-
103-
</div>
104-
105-
<div class="ui g-modal-confirm delete modal">
106-
<div class="header">
107-
{{svg "octicon-trash"}}
108-
{{ctx.Locale.Tr "admin.emails.delete"}}
109-
</div>
110-
<div class="content">
111-
{{ctx.Locale.Tr "admin.emails.delete_desc"}}
11299
</div>
113-
{{template "base/modal_actions_confirm" .}}
114-
</div>
115100

116101
{{template "admin/layout_footer" .}}

web_src/js/features/admin/emails.ts

Lines changed: 0 additions & 13 deletions
This file was deleted.

web_src/js/features/common-button.ts

Lines changed: 80 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
import $ from 'jquery';
21
import {POST} from '../modules/fetch.ts';
3-
import {hideElem, showElem, toggleElem} from '../utils/dom.ts';
4-
import {showErrorToast} from '../modules/toast.ts';
2+
import {addDelegatedEventListener, hideElem, queryElems, showElem, toggleElem} from '../utils/dom.ts';
3+
import {fomanticQuery} from '../modules/fomantic/base.ts';
54

65
export function initGlobalButtonClickOnEnter(): void {
7-
$(document).on('keypress', 'div.ui.button,span.ui.button', (e) => {
6+
addDelegatedEventListener(document, 'keypress', 'div.ui.button, span.ui.button', (el, e: KeyboardEvent) => {
87
if (e.code === ' ' || e.code === 'Enter') {
9-
$(e.target).trigger('click');
108
e.preventDefault();
9+
el.click();
1110
}
1211
});
1312
}
@@ -40,7 +39,7 @@ export function initGlobalDeleteButton(): void {
4039
}
4140
}
4241

43-
$(modal).modal({
42+
fomanticQuery(modal).modal({
4443
closable: false,
4544
onApprove: async () => {
4645
// if `data-type="form"` exists, then submit the form by the selector provided by `data-form="..."`
@@ -73,87 +72,92 @@ export function initGlobalDeleteButton(): void {
7372
}
7473
}
7574

76-
export function initGlobalButtons(): void {
77-
// There are many "cancel button" elements in modal dialogs, Fomantic UI expects they are button-like elements but never submit a form.
78-
// However, Gitea misuses the modal dialog and put the cancel buttons inside forms, so we must prevent the form submission.
79-
// There are a few cancel buttons in non-modal forms, and there are some dynamically created forms (eg: the "Edit Issue Content")
80-
$(document).on('click', 'form button.ui.cancel.button', (e) => {
81-
e.preventDefault();
82-
});
83-
84-
$('.show-panel').on('click', function (e) {
85-
// a '.show-panel' element can show a panel, by `data-panel="selector"`
86-
// if it has "toggle" class, it toggles the panel
87-
e.preventDefault();
88-
const sel = this.getAttribute('data-panel');
89-
if (this.classList.contains('toggle')) {
90-
toggleElem(sel);
91-
} else {
92-
showElem(sel);
93-
}
94-
});
75+
function onShowPanelClick(e) {
76+
// a '.show-panel' element can show a panel, by `data-panel="selector"`
77+
// if it has "toggle" class, it toggles the panel
78+
const el = e.currentTarget;
79+
e.preventDefault();
80+
const sel = el.getAttribute('data-panel');
81+
if (el.classList.contains('toggle')) {
82+
toggleElem(sel);
83+
} else {
84+
showElem(sel);
85+
}
86+
}
9587

96-
$('.hide-panel').on('click', function (e) {
97-
// a `.hide-panel` element can hide a panel, by `data-panel="selector"` or `data-panel-closest="selector"`
98-
e.preventDefault();
99-
let sel = this.getAttribute('data-panel');
100-
if (sel) {
101-
hideElem($(sel));
102-
return;
103-
}
104-
sel = this.getAttribute('data-panel-closest');
105-
if (sel) {
106-
hideElem($(this).closest(sel));
107-
return;
108-
}
109-
// should never happen, otherwise there is a bug in code
110-
showErrorToast('Nothing to hide');
111-
});
88+
function onHidePanelClick(e) {
89+
// a `.hide-panel` element can hide a panel, by `data-panel="selector"` or `data-panel-closest="selector"`
90+
const el = e.currentTarget;
91+
e.preventDefault();
92+
let sel = el.getAttribute('data-panel');
93+
if (sel) {
94+
hideElem(sel);
95+
return;
96+
}
97+
sel = el.getAttribute('data-panel-closest');
98+
if (sel) {
99+
hideElem(el.closest(sel));
100+
return;
101+
}
102+
throw new Error('no panel to hide'); // should never happen, otherwise there is a bug in code
112103
}
113104

114-
export function initGlobalShowModal() {
105+
function onShowModalClick(e) {
115106
// A ".show-modal" button will show a modal dialog defined by its "data-modal" attribute.
116107
// Each "data-modal-{target}" attribute will be filled to target element's value or text-content.
117108
// * First, try to query '#target'
118109
// * Then, try to query '.target'
119110
// * Then, try to query 'target' as HTML tag
120111
// If there is a ".{attr}" part like "data-modal-form.action", then the form's "action" attribute will be set.
121-
$('.show-modal').on('click', function (e) {
122-
e.preventDefault();
123-
const modalSelector = this.getAttribute('data-modal');
124-
const $modal = $(modalSelector);
125-
if (!$modal.length) {
126-
throw new Error('no modal for this action');
112+
const el = e.currentTarget;
113+
e.preventDefault();
114+
const modalSelector = el.getAttribute('data-modal');
115+
const elModal = document.querySelector(modalSelector);
116+
if (!elModal) throw new Error('no modal for this action');
117+
118+
const modalAttrPrefix = 'data-modal-';
119+
for (const attrib of el.attributes) {
120+
if (!attrib.name.startsWith(modalAttrPrefix)) {
121+
continue;
127122
}
128-
const modalAttrPrefix = 'data-modal-';
129-
for (const attrib of this.attributes) {
130-
if (!attrib.name.startsWith(modalAttrPrefix)) {
131-
continue;
132-
}
133123

134-
const attrTargetCombo = attrib.name.substring(modalAttrPrefix.length);
135-
const [attrTargetName, attrTargetAttr] = attrTargetCombo.split('.');
136-
// try to find target by: "#target" -> ".target" -> "target tag"
137-
let $attrTarget = $modal.find(`#${attrTargetName}`);
138-
if (!$attrTarget.length) $attrTarget = $modal.find(`.${attrTargetName}`);
139-
if (!$attrTarget.length) $attrTarget = $modal.find(`${attrTargetName}`);
140-
if (!$attrTarget.length) continue; // TODO: show errors in dev mode to remind developers that there is a bug
141-
142-
if (attrTargetAttr) {
143-
$attrTarget[0][attrTargetAttr] = attrib.value;
144-
} else if ($attrTarget[0].matches('input, textarea')) {
145-
$attrTarget.val(attrib.value); // FIXME: add more supports like checkbox
146-
} else {
147-
$attrTarget[0].textContent = attrib.value; // FIXME: it should be more strict here, only handle div/span/p
148-
}
124+
const attrTargetCombo = attrib.name.substring(modalAttrPrefix.length);
125+
const [attrTargetName, attrTargetAttr] = attrTargetCombo.split('.');
126+
// try to find target by: "#target" -> "[name=target]" -> ".target" -> "<target> tag"
127+
const attrTarget = elModal.querySelector(`#${attrTargetName}`) ||
128+
elModal.querySelector(`[name=${attrTargetName}]`) ||
129+
elModal.querySelector(`.${attrTargetName}`) ||
130+
elModal.querySelector(`${attrTargetName}`);
131+
if (!attrTarget) {
132+
if (!window.config.runModeIsProd) throw new Error(`attr target "${attrTargetCombo}" not found for modal`);
133+
continue;
149134
}
150135

151-
$modal.modal('setting', {
152-
onApprove: () => {
153-
// "form-fetch-action" can handle network errors gracefully,
154-
// so keep the modal dialog to make users can re-submit the form if anything wrong happens.
155-
if ($modal.find('.form-fetch-action').length) return false;
156-
},
157-
}).modal('show');
158-
});
136+
if (attrTargetAttr) {
137+
attrTarget[attrTargetAttr] = attrib.value;
138+
} else if (attrTarget.matches('input, textarea')) {
139+
attrTarget.value = attrib.value; // FIXME: add more supports like checkbox
140+
} else {
141+
attrTarget.textContent = attrib.value; // FIXME: it should be more strict here, only handle div/span/p
142+
}
143+
}
144+
145+
fomanticQuery(elModal).modal('setting', {
146+
onApprove: () => {
147+
// "form-fetch-action" can handle network errors gracefully,
148+
// so keep the modal dialog to make users can re-submit the form if anything wrong happens.
149+
if (elModal.querySelector('.form-fetch-action')) return false;
150+
},
151+
}).modal('show');
152+
}
153+
154+
export function initGlobalButtons(): void {
155+
// There are many "cancel button" elements in modal dialogs, Fomantic UI expects they are button-like elements but never submit a form.
156+
// However, Gitea misuses the modal dialog and put the cancel buttons inside forms, so we must prevent the form submission.
157+
// There are a few cancel buttons in non-modal forms, and there are some dynamically created forms (eg: the "Edit Issue Content")
158+
addDelegatedEventListener(document, 'click', 'form button.ui.cancel.button', (_ /* el */, e) => e.preventDefault());
159+
160+
queryElems(document, '.show-panel', (el) => el.addEventListener('click', onShowPanelClick));
161+
queryElems(document, '.hide-panel', (el) => el.addEventListener('click', onHidePanelClick));
162+
queryElems(document, '.show-modal', (el) => el.addEventListener('click', onShowModalClick));
159163
}

web_src/js/features/common-fetch-action.ts

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import {request} from '../modules/fetch.ts';
22
import {showErrorToast} from '../modules/toast.ts';
3-
import {submitEventSubmitter} from '../utils/dom.ts';
4-
import {htmlEscape} from 'escape-goat';
3+
import {addDelegatedEventListener, submitEventSubmitter} from '../utils/dom.ts';
54
import {confirmModal} from './comp/ConfirmModal.ts';
5+
import type {RequestOpts} from '../types.ts';
66

77
const {appSubUrl, i18n} = window.config;
88

99
// fetchActionDoRedirect does real redirection to bypass the browser's limitations of "location"
1010
// more details are in the backend's fetch-redirect handler
11-
function fetchActionDoRedirect(redirect) {
11+
function fetchActionDoRedirect(redirect: string) {
1212
const form = document.createElement('form');
1313
const input = document.createElement('input');
1414
form.method = 'post';
@@ -21,7 +21,7 @@ function fetchActionDoRedirect(redirect) {
2121
form.submit();
2222
}
2323

24-
async function fetchActionDoRequest(actionElem, url, opt) {
24+
async function fetchActionDoRequest(actionElem: HTMLElement, url: string, opt: RequestOpts) {
2525
try {
2626
const resp = await request(url, opt);
2727
if (resp.status === 200) {
@@ -55,11 +55,8 @@ async function fetchActionDoRequest(actionElem, url, opt) {
5555
actionElem.classList.remove('is-loading', 'loading-icon-2px');
5656
}
5757

58-
async function formFetchAction(e) {
59-
if (!e.target.classList.contains('form-fetch-action')) return;
60-
58+
async function formFetchAction(formEl: HTMLFormElement, e: SubmitEvent) {
6159
e.preventDefault();
62-
const formEl = e.target;
6360
if (formEl.classList.contains('is-loading')) return;
6461

6562
formEl.classList.add('is-loading');
@@ -77,7 +74,7 @@ async function formFetchAction(e) {
7774
}
7875

7976
let reqUrl = formActionUrl;
80-
const reqOpt = {method: formMethod.toUpperCase()};
77+
const reqOpt = {method: formMethod.toUpperCase(), body: null};
8178
if (formMethod.toLowerCase() === 'get') {
8279
const params = new URLSearchParams();
8380
for (const [key, value] of formData) {
@@ -95,34 +92,36 @@ async function formFetchAction(e) {
9592
await fetchActionDoRequest(formEl, reqUrl, reqOpt);
9693
}
9794

98-
async function linkAction(e) {
95+
async function linkAction(el: HTMLElement, e: Event) {
9996
// A "link-action" can post AJAX request to its "data-url"
10097
// Then the browser is redirected to: the "redirect" in response, or "data-redirect" attribute, or current URL by reloading.
10198
// If the "link-action" has "data-modal-confirm" attribute, a confirm modal dialog will be shown before taking action.
102-
const el = e.target.closest('.link-action');
103-
if (!el) return;
104-
10599
e.preventDefault();
106100
const url = el.getAttribute('data-url');
107101
const doRequest = async () => {
108-
el.disabled = true;
102+
if ('disabled' in el) el.disabled = true; // el could be A or BUTTON, but A doesn't have disabled attribute
109103
await fetchActionDoRequest(el, url, {method: 'POST'});
110-
el.disabled = false;
104+
if ('disabled' in el) el.disabled = false;
111105
};
112106

113-
const modalConfirmContent = htmlEscape(el.getAttribute('data-modal-confirm') || '');
107+
const modalConfirmContent = el.getAttribute('data-modal-confirm') ||
108+
el.getAttribute('data-modal-confirm-content') || '';
114109
if (!modalConfirmContent) {
115110
await doRequest();
116111
return;
117112
}
118113

119114
const isRisky = el.classList.contains('red') || el.classList.contains('negative');
120-
if (await confirmModal(modalConfirmContent, {confirmButtonColor: isRisky ? 'red' : 'primary'})) {
115+
if (await confirmModal({
116+
header: el.getAttribute('data-modal-confirm-header') || '',
117+
content: modalConfirmContent,
118+
confirmButtonColor: isRisky ? 'red' : 'primary',
119+
})) {
121120
await doRequest();
122121
}
123122
}
124123

125124
export function initGlobalFetchAction() {
126-
document.addEventListener('submit', formFetchAction);
127-
document.addEventListener('click', linkAction);
125+
addDelegatedEventListener(document, 'click', '.form-fetch-action', formFetchAction);
126+
addDelegatedEventListener(document, 'click', '.link-action', linkAction);
128127
}

0 commit comments

Comments
 (0)