Skip to content

Commit bb71cee

Browse files
authored
Improve async/await usage, and sort init calls in index.js (#17386)
* clean up async/await, and sort init calls in `index.js * use `const _promise` to indicate that we do not need await an async function
1 parent 3a693bd commit bb71cee

21 files changed

+214
-202
lines changed

web_src/js/features/common-global.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,11 @@ export function initGlobalCommon() {
135135
});
136136
}
137137

138-
export async function initGlobalDropzone() {
138+
export function initGlobalDropzone() {
139139
// Dropzone
140140
for (const el of document.querySelectorAll('.dropzone')) {
141141
const $dropzone = $(el);
142-
await createDropzone(el, {
142+
const _promise = createDropzone(el, {
143143
url: $dropzone.data('upload-url'),
144144
headers: {'X-Csrf-Token': csrfToken},
145145
maxFiles: $dropzone.data('max-file'),

web_src/js/features/comp/SearchUserBox.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {htmlEscape} from 'escape-goat';
22

33
const {appSubUrl} = window.config;
44

5-
export function initSearchUserBox() {
5+
export function initCompSearchUserBox() {
66
const $searchUserBox = $('#search-user-box');
77
$searchUserBox.search({
88
minCharacters: 2,

web_src/js/features/comp/WebHookEditor.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const {csrfToken} = window.config;
22

3-
export function initWebHookEditor() {
3+
export function initCompWebHookEditor() {
44
if ($('.new.webhook').length === 0) {
55
return;
66
}

web_src/js/features/diff.js

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

web_src/js/features/dropzone.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ export default async function createDropzone(el, opts) {
33
import(/* webpackChunkName: "dropzone" */'dropzone'),
44
import(/* webpackChunkName: "dropzone" */'dropzone/dist/dropzone.css'),
55
]);
6-
76
Dropzone.autoDiscover = false;
87
return new Dropzone(el, opts);
98
}

web_src/js/features/heatmap.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import Vue from 'vue';
22

33
import ActivityHeatmap from '../components/ActivityHeatmap.vue';
44

5-
export default async function initHeatmap() {
5+
export default function initHeatmap() {
66
const el = document.getElementById('user-heatmap');
77
if (!el) return;
88

@@ -24,7 +24,7 @@ export default async function initHeatmap() {
2424

2525
new View().$mount(el);
2626
} catch (err) {
27-
console.error(err);
27+
console.error('Heatmap failed to load', err);
2828
el.textContent = 'Heatmap failed to load';
2929
}
3030
}

web_src/js/features/imagediff.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ function getDefaultSvgBoundsIfUndefined(svgXml, src) {
2929
}
3030
}
3131

32-
export default async function initImageDiff() {
32+
export default function initImageDiff() {
3333
function createContext(image1, image2) {
3434
const size1 = {
3535
width: image1 && image1.width || 0,

web_src/js/features/lastcommitloader.js

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

web_src/js/features/notification.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ async function receiveUpdateCount(event) {
4040
}
4141
}
4242

43-
export async function initNotificationCount() {
43+
export function initNotificationCount() {
4444
const notificationCount = $('.notification_count');
4545

4646
if (!notificationCount.length) {
@@ -66,7 +66,7 @@ export async function initNotificationCount() {
6666
return;
6767
}
6868
if (event.data.type === 'notification-count') {
69-
receiveUpdateCount(event.data);
69+
const _promise = receiveUpdateCount(event.data);
7070
} else if (event.data.type === 'error') {
7171
console.error(event.data);
7272
} else if (event.data.type === 'logout') {

web_src/js/features/repo-commit.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,47 @@
1+
const {csrfToken} = window.config;
2+
13
export function initRepoCommitButton() {
24
$('.commit-button').on('click', function (e) {
35
e.preventDefault();
46
$(this).parent().find('.commit-body').toggle();
57
});
68
}
9+
10+
export function initRepoCommitLastCommitLoader() {
11+
const entryMap = {};
12+
13+
const entries = $('table#repo-files-table tr.notready')
14+
.map((_, v) => {
15+
entryMap[$(v).attr('data-entryname')] = $(v);
16+
return $(v).attr('data-entryname');
17+
})
18+
.get();
19+
20+
if (entries.length === 0) {
21+
return;
22+
}
23+
24+
const lastCommitLoaderURL = $('table#repo-files-table').data('lastCommitLoaderUrl');
25+
26+
if (entries.length > 200) {
27+
$.post(lastCommitLoaderURL, {
28+
_csrf: csrfToken,
29+
}, (data) => {
30+
$('table#repo-files-table').replaceWith(data);
31+
});
32+
return;
33+
}
34+
35+
$.post(lastCommitLoaderURL, {
36+
_csrf: csrfToken,
37+
'f': entries,
38+
}, (data) => {
39+
$(data).find('tr').each((_, row) => {
40+
if (row.className === 'commit-list') {
41+
$('table#repo-files-table .commit-list').replaceWith(row);
42+
return;
43+
}
44+
entryMap[$(row).attr('data-entryname')].replaceWith(row);
45+
});
46+
});
47+
}

web_src/js/features/repo-diff.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,28 @@ export function initRepoDiffConversationNav() {
7979
window.location.href = `#${anchor}`;
8080
});
8181
}
82+
83+
export function initRepoDiffShowMore() {
84+
$('#diff-files, #diff-file-boxes').on('click', '#diff-show-more-files, #diff-show-more-files-stats', (e) => {
85+
e.preventDefault();
86+
87+
if ($(e.target).hasClass('disabled')) {
88+
return;
89+
}
90+
$('#diff-show-more-files, #diff-show-more-files-stats').addClass('disabled');
91+
92+
const url = $('#diff-show-more-files, #diff-show-more-files-stats').data('href');
93+
$.ajax({
94+
type: 'GET',
95+
url,
96+
}).done((resp) => {
97+
if (!resp || resp.html === '' || resp.empty) {
98+
$('#diff-show-more-files, #diff-show-more-files-stats').removeClass('disabled');
99+
return;
100+
}
101+
$('#diff-too-many-files-stats').remove();
102+
$('#diff-files').append($(resp).find('#diff-files li'));
103+
$('#diff-incomplete').replaceWith($(resp).find('#diff-file-boxes').children());
104+
});
105+
});
106+
}

web_src/js/features/repo-editor.js

Lines changed: 42 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ function initEditPreviewTab($form) {
2424
_csrf: csrfToken,
2525
mode,
2626
context,
27-
text: $form.find(`.tab[data-tab="${$tabMenu.data('write')}"] textarea`).val()
27+
text: $form.find(`.tab[data-tab="${$tabMenu.data('write')}"] textarea`).val(),
2828
}, (data) => {
2929
const $previewPanel = $form.find(`.tab[data-tab="${$tabMenu.data('preview')}"]`);
3030
$previewPanel.html(data);
@@ -42,7 +42,7 @@ function initEditDiffTab($form) {
4242
$.post($this.data('url'), {
4343
_csrf: csrfToken,
4444
context: $this.data('context'),
45-
content: $form.find(`.tab[data-tab="${$tabMenu.data('write')}"] textarea`).val()
45+
content: $form.find(`.tab[data-tab="${$tabMenu.data('write')}"] textarea`).val(),
4646
}, (data) => {
4747
const $diffPreviewPanel = $form.find(`.tab[data-tab="${$tabMenu.data('diff')}"]`);
4848
$diffPreviewPanel.html(data);
@@ -75,7 +75,7 @@ function getCursorPosition($e) {
7575
return pos;
7676
}
7777

78-
export async function initRepoEditor() {
78+
export function initRepoEditor() {
7979
initEditorForm();
8080

8181
$('.js-quick-pull-choice-option').on('change', function () {
@@ -134,47 +134,49 @@ export async function initRepoEditor() {
134134
const $editArea = $('.repository.editor textarea#edit_area');
135135
if (!$editArea.length) return;
136136

137-
const editor = await createCodeEditor($editArea[0], $editFilename[0], previewFileModes);
137+
(async () => {
138+
const editor = await createCodeEditor($editArea[0], $editFilename[0], previewFileModes);
138139

139-
// Using events from https://github.com/codedance/jquery.AreYouSure#advanced-usage
140-
// to enable or disable the commit button
141-
const $commitButton = $('#commit-button');
142-
const $editForm = $('.ui.edit.form');
143-
const dirtyFileClass = 'dirty-file';
140+
// Using events from https://github.com/codedance/jquery.AreYouSure#advanced-usage
141+
// to enable or disable the commit button
142+
const $commitButton = $('#commit-button');
143+
const $editForm = $('.ui.edit.form');
144+
const dirtyFileClass = 'dirty-file';
144145

145-
// Disabling the button at the start
146-
if ($('input[name="page_has_posted"]').val() !== 'true') {
147-
$commitButton.prop('disabled', true);
148-
}
149-
150-
// Registering a custom listener for the file path and the file content
151-
$editForm.areYouSure({
152-
silent: true,
153-
dirtyClass: dirtyFileClass,
154-
fieldSelector: ':input:not(.commit-form-wrapper :input)',
155-
change() {
156-
const dirty = $(this).hasClass(dirtyFileClass);
157-
$commitButton.prop('disabled', !dirty);
146+
// Disabling the button at the start
147+
if ($('input[name="page_has_posted"]').val() !== 'true') {
148+
$commitButton.prop('disabled', true);
158149
}
159-
});
160150

161-
// Update the editor from query params, if available,
162-
// only after the dirtyFileClass initialization
163-
const params = new URLSearchParams(window.location.search);
164-
const value = params.get('value');
165-
if (value) {
166-
editor.setValue(value);
167-
}
151+
// Registering a custom listener for the file path and the file content
152+
$editForm.areYouSure({
153+
silent: true,
154+
dirtyClass: dirtyFileClass,
155+
fieldSelector: ':input:not(.commit-form-wrapper :input)',
156+
change() {
157+
const dirty = $(this).hasClass(dirtyFileClass);
158+
$commitButton.prop('disabled', !dirty);
159+
},
160+
});
168161

169-
$commitButton.on('click', (event) => {
170-
// A modal which asks if an empty file should be committed
171-
if ($editArea.val().length === 0) {
172-
$('#edit-empty-content-modal').modal({
173-
onApprove() {
174-
$('.edit.form').trigger('submit');
175-
}
176-
}).modal('show');
177-
event.preventDefault();
162+
// Update the editor from query params, if available,
163+
// only after the dirtyFileClass initialization
164+
const params = new URLSearchParams(window.location.search);
165+
const value = params.get('value');
166+
if (value) {
167+
editor.setValue(value);
178168
}
179-
});
169+
170+
$commitButton.on('click', (event) => {
171+
// A modal which asks if an empty file should be committed
172+
if ($editArea.val().length === 0) {
173+
$('#edit-empty-content-modal').modal({
174+
onApprove() {
175+
$('.edit.form').trigger('submit');
176+
},
177+
}).modal('show');
178+
event.preventDefault();
179+
}
180+
});
181+
})();
180182
}

web_src/js/features/gitgraph.js renamed to web_src/js/features/repo-graph.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export default async function initGitGraph() {
1+
export default function initRepoGraphGit() {
22
const graphContainer = document.getElementById('git-graph-container');
33
if (!graphContainer) return;
44

web_src/js/features/issue-content-history.js renamed to web_src/js/features/repo-issue-content.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ function showContentHistoryMenu(issueBaseUrl, $item, commentId) {
104104
});
105105
}
106106

107-
export function initIssueContentHistory() {
107+
export function initRepoIssueContentHistory() {
108108
const issueIndex = $('#issueIndex').val();
109109
const $itemIssue = $('.timeline-item.comment.first');
110110
if (!issueIndex || !$itemIssue.length) return;

web_src/js/features/repo-legacy.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ export function initRepoCommentForm() {
259259
}
260260

261261

262-
export async function initRepository() {
262+
export function initRepository() {
263263
if ($('.repository').length === 0) {
264264
return;
265265
}
@@ -363,7 +363,7 @@ export async function initRepository() {
363363
if ($editContentZone.html().length === 0) {
364364
$editContentZone.html($('#edit-content-form').html());
365365
$textarea = $editContentZone.find('textarea');
366-
attachTribute($textarea.get(), {mentions: true, emoji: true});
366+
await attachTribute($textarea.get(), {mentions: true, emoji: true});
367367

368368
let dz;
369369
const $dropzone = $editContentZone.find('.dropzone');

web_src/js/features/migration.js renamed to web_src/js/features/repo-migration.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const $lfsSettings = $('#lfs_settings');
88
const $lfsEndpoint = $('#lfs_endpoint');
99
const $items = $('#migrate_items').find('input[type=checkbox]');
1010

11-
export default function initMigration() {
11+
export default function initRepoMigration() {
1212
checkAuth();
1313
setLFSSettingsVisibility();
1414

0 commit comments

Comments
 (0)