Skip to content

Commit aeb15d9

Browse files
committed
Replace two cases of jQuery ajax with fetch
1 parent 7047df3 commit aeb15d9

File tree

2 files changed

+29
-28
lines changed

2 files changed

+29
-28
lines changed

web_src/js/features/common-global.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {htmlEscape} from 'escape-goat';
1111
import {showTemporaryTooltip} from '../modules/tippy.js';
1212
import {confirmModal} from './comp/ConfirmModal.js';
1313
import {showErrorToast} from '../modules/toast.js';
14-
import {request} from '../modules/fetch.js';
14+
import {request, POST} from '../modules/fetch.js';
1515

1616
const {appUrl, appSubUrl, csrfToken, i18n} = window.config;
1717

@@ -243,9 +243,8 @@ export function initGlobalDropzone() {
243243
this.on('removedfile', (file) => {
244244
$(`#${file.uuid}`).remove();
245245
if ($dropzone.data('remove-url')) {
246-
$.post($dropzone.data('remove-url'), {
247-
file: file.uuid,
248-
_csrf: csrfToken,
246+
POST($dropzone.data('remove-url'), {
247+
data: new URLSearchParams({file: file.uuid}),
249248
});
250249
}
251250
});

web_src/js/features/imagediff.js

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
import $ from 'jquery';
2+
import {GET} from '../modules/fetch.js';
23
import {hideElem} from '../utils/dom.js';
4+
import {parseUrl} from '../utils.js';
35

4-
function getDefaultSvgBoundsIfUndefined(svgXml, src) {
6+
function getDefaultSvgBoundsIfUndefined(text, src) {
57
const DefaultSize = 300;
68
const MaxSize = 99999;
79

8-
const svg = svgXml.documentElement;
10+
const parser = new DOMParser();
11+
const svgDoc = parser.parseFromString(text, 'image/svg+xml');
12+
const svg = svgDoc.documentElement;
913
const width = svg?.width?.baseVal;
1014
const height = svg?.height?.baseVal;
1115
if (width === undefined || height === undefined) {
@@ -65,7 +69,7 @@ export function initImageDiff() {
6569
};
6670
}
6771

68-
$('.image-diff:not([data-image-diff-loaded])').each(function() {
72+
$('.image-diff:not([data-image-diff-loaded])').each(async function() {
6973
const $container = $(this);
7074
$container.attr('data-image-diff-loaded', 'true');
7175

@@ -88,29 +92,27 @@ export function initImageDiff() {
8892

8993
for (const info of imageInfos) {
9094
if (info.$image.length > 0) {
91-
$.ajax({
92-
url: info.path,
93-
success: (data, _, jqXHR) => {
94-
info.$image.on('load', () => {
95-
info.loaded = true;
96-
setReadyIfLoaded();
97-
}).on('error', () => {
98-
info.loaded = true;
99-
setReadyIfLoaded();
100-
info.$boundsInfo.text('(image error)');
101-
});
102-
info.$image.attr('src', info.path);
95+
info.$image.on('load', () => {
96+
info.loaded = true;
97+
setReadyIfLoaded();
98+
}).on('error', () => {
99+
info.loaded = true;
100+
setReadyIfLoaded();
101+
info.$boundsInfo.text('(image error)');
102+
});
103+
info.$image.attr('src', info.path);
103104

104-
if (jqXHR.getResponseHeader('Content-Type') === 'image/svg+xml') {
105-
const bounds = getDefaultSvgBoundsIfUndefined(data, info.path);
106-
if (bounds) {
107-
info.$image.attr('width', bounds.width);
108-
info.$image.attr('height', bounds.height);
109-
hideElem(info.$boundsInfo);
110-
}
111-
}
105+
// this may be dead code as we currently do not render SVGs images in image diffs
106+
if (parseUrl(info.path).pathname.toLowerCase().endsWith('.svg')) {
107+
const resp = await GET(info.path);
108+
const text = await resp.text();
109+
const bounds = getDefaultSvgBoundsIfUndefined(text, info.path);
110+
if (bounds) {
111+
info.$image.attr('width', bounds.width);
112+
info.$image.attr('height', bounds.height);
113+
hideElem(info.$boundsInfo);
112114
}
113-
});
115+
}
114116
} else {
115117
info.loaded = true;
116118
setReadyIfLoaded();

0 commit comments

Comments
 (0)