Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve async/await usage, and sort init calls in index.js #17386

Merged
merged 17 commits into from
Nov 9, 2021
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions web_src/js/features/common-global.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,11 @@ export function initGlobalCommon() {
});
}

export async function initGlobalDropzone() {
export function initGlobalDropzone() {
wxiaoguang marked this conversation as resolved.
Show resolved Hide resolved
// Dropzone
for (const el of document.querySelectorAll('.dropzone')) {
const $dropzone = $(el);
await createDropzone(el, {
const _promise = createDropzone(el, {
wxiaoguang marked this conversation as resolved.
Show resolved Hide resolved
url: $dropzone.data('upload-url'),
headers: {'X-Csrf-Token': csrfToken},
maxFiles: $dropzone.data('max-file'),
Expand Down
2 changes: 1 addition & 1 deletion web_src/js/features/comp/SearchUserBox.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {htmlEscape} from 'escape-goat';

const {appSubUrl} = window.config;

export function initSearchUserBox() {
export function initCompSearchUserBox() {
wxiaoguang marked this conversation as resolved.
Show resolved Hide resolved
const $searchUserBox = $('#search-user-box');
$searchUserBox.search({
minCharacters: 2,
Expand Down
2 changes: 1 addition & 1 deletion web_src/js/features/comp/WebHookEditor.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const {csrfToken} = window.config;

export function initWebHookEditor() {
export function initCompWebHookEditor() {
if ($('.new.webhook').length === 0) {
return;
}
Expand Down
24 changes: 0 additions & 24 deletions web_src/js/features/diff.js

This file was deleted.

1 change: 0 additions & 1 deletion web_src/js/features/dropzone.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ export default async function createDropzone(el, opts) {
import(/* webpackChunkName: "dropzone" */'dropzone'),
import(/* webpackChunkName: "dropzone" */'dropzone/dist/dropzone.css'),
]);

Dropzone.autoDiscover = false;
return new Dropzone(el, opts);
}
4 changes: 2 additions & 2 deletions web_src/js/features/heatmap.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Vue from 'vue';

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

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

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

new View().$mount(el);
} catch (err) {
console.error(err);
console.error('Heatmap failed to load', err);
el.textContent = 'Heatmap failed to load';
}
}
2 changes: 1 addition & 1 deletion web_src/js/features/imagediff.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function getDefaultSvgBoundsIfUndefined(svgXml, src) {
}
}

export default async function initImageDiff() {
export default function initImageDiff() {
function createContext(image1, image2) {
const size1 = {
width: image1 && image1.width || 0,
Expand Down
40 changes: 0 additions & 40 deletions web_src/js/features/lastcommitloader.js

This file was deleted.

4 changes: 2 additions & 2 deletions web_src/js/features/notification.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ async function receiveUpdateCount(event) {
}
}

export async function initNotificationCount() {
export function initNotificationCount() {
const notificationCount = $('.notification_count');

if (!notificationCount.length) {
Expand All @@ -66,7 +66,7 @@ export async function initNotificationCount() {
return;
}
if (event.data.type === 'notification-count') {
receiveUpdateCount(event.data);
const _promise = receiveUpdateCount(event.data);
wxiaoguang marked this conversation as resolved.
Show resolved Hide resolved
} else if (event.data.type === 'error') {
console.error(event.data);
} else if (event.data.type === 'logout') {
Expand Down
41 changes: 41 additions & 0 deletions web_src/js/features/repo-commit.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,47 @@
const {csrfToken} = window.config;

export function initRepoCommitButton() {
$('.commit-button').on('click', function (e) {
e.preventDefault();
$(this).parent().find('.commit-body').toggle();
});
}

export function initRepoCommitLastCommitLoader() {
wxiaoguang marked this conversation as resolved.
Show resolved Hide resolved
const entryMap = {};

const entries = $('table#repo-files-table tr.notready')
.map((_, v) => {
entryMap[$(v).attr('data-entryname')] = $(v);
return $(v).attr('data-entryname');
})
.get();

if (entries.length === 0) {
return;
}

const lastCommitLoaderURL = $('table#repo-files-table').data('lastCommitLoaderUrl');

if (entries.length > 200) {
$.post(lastCommitLoaderURL, {
_csrf: csrfToken,
}, (data) => {
$('table#repo-files-table').replaceWith(data);
});
return;
}

$.post(lastCommitLoaderURL, {
_csrf: csrfToken,
'f': entries,
}, (data) => {
$(data).find('tr').each((_, row) => {
if (row.className === 'commit-list') {
$('table#repo-files-table .commit-list').replaceWith(row);
return;
}
entryMap[$(row).attr('data-entryname')].replaceWith(row);
});
});
}
25 changes: 25 additions & 0 deletions web_src/js/features/repo-diff.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,28 @@ export function initRepoDiffConversationNav() {
window.location.href = `#${anchor}`;
});
}

export function initRepoDiffShowMore() {
wxiaoguang marked this conversation as resolved.
Show resolved Hide resolved
$('#diff-files, #diff-file-boxes').on('click', '#diff-show-more-files, #diff-show-more-files-stats', (e) => {
e.preventDefault();

if ($(e.target).hasClass('disabled')) {
return;
}
$('#diff-show-more-files, #diff-show-more-files-stats').addClass('disabled');

const url = $('#diff-show-more-files, #diff-show-more-files-stats').data('href');
$.ajax({
type: 'GET',
url,
}).done((resp) => {
if (!resp || resp.html === '' || resp.empty) {
$('#diff-show-more-files, #diff-show-more-files-stats').removeClass('disabled');
return;
}
$('#diff-too-many-files-stats').remove();
$('#diff-files').append($(resp).find('#diff-files li'));
$('#diff-incomplete').replaceWith($(resp).find('#diff-file-boxes').children());
});
});
}
82 changes: 42 additions & 40 deletions web_src/js/features/repo-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function initEditPreviewTab($form) {
_csrf: csrfToken,
mode,
context,
text: $form.find(`.tab[data-tab="${$tabMenu.data('write')}"] textarea`).val()
text: $form.find(`.tab[data-tab="${$tabMenu.data('write')}"] textarea`).val(),
wxiaoguang marked this conversation as resolved.
Show resolved Hide resolved
}, (data) => {
const $previewPanel = $form.find(`.tab[data-tab="${$tabMenu.data('preview')}"]`);
$previewPanel.html(data);
Expand All @@ -42,7 +42,7 @@ function initEditDiffTab($form) {
$.post($this.data('url'), {
_csrf: csrfToken,
context: $this.data('context'),
content: $form.find(`.tab[data-tab="${$tabMenu.data('write')}"] textarea`).val()
content: $form.find(`.tab[data-tab="${$tabMenu.data('write')}"] textarea`).val(),
}, (data) => {
const $diffPreviewPanel = $form.find(`.tab[data-tab="${$tabMenu.data('diff')}"]`);
$diffPreviewPanel.html(data);
Expand Down Expand Up @@ -75,7 +75,7 @@ function getCursorPosition($e) {
return pos;
}

export async function initRepoEditor() {
export function initRepoEditor() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, function will return before editor is initialized, I don't like it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need the real reasons why it doesn't work. There should be no dependency between modules.

Most modules (features) should be able to be initialized at any order.

If there was a dependency, we should make it clear now. I can not bear the noodle-like logic.

initEditorForm();

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

const editor = await createCodeEditor($editArea[0], $editFilename[0], previewFileModes);
(async () => {
const editor = await createCodeEditor($editArea[0], $editFilename[0], previewFileModes);
wxiaoguang marked this conversation as resolved.
Show resolved Hide resolved

// Using events from https://github.com/codedance/jquery.AreYouSure#advanced-usage
// to enable or disable the commit button
const $commitButton = $('#commit-button');
const $editForm = $('.ui.edit.form');
const dirtyFileClass = 'dirty-file';
// Using events from https://github.com/codedance/jquery.AreYouSure#advanced-usage
// to enable or disable the commit button
const $commitButton = $('#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);
}

// Registering a custom listener for the file path and the file content
$editForm.areYouSure({
silent: true,
dirtyClass: dirtyFileClass,
fieldSelector: ':input:not(.commit-form-wrapper :input)',
change() {
const dirty = $(this).hasClass(dirtyFileClass);
$commitButton.prop('disabled', !dirty);
// Disabling the button at the start
if ($('input[name="page_has_posted"]').val() !== 'true') {
$commitButton.prop('disabled', true);
}
});

// Update the editor from query params, if available,
// only after the dirtyFileClass initialization
const params = new URLSearchParams(window.location.search);
const value = params.get('value');
if (value) {
editor.setValue(value);
}
// Registering a custom listener for the file path and the file content
$editForm.areYouSure({
silent: true,
dirtyClass: dirtyFileClass,
fieldSelector: ':input:not(.commit-form-wrapper :input)',
change() {
const dirty = $(this).hasClass(dirtyFileClass);
$commitButton.prop('disabled', !dirty);
},
});

$commitButton.on('click', (event) => {
// A modal which asks if an empty file should be committed
if ($editArea.val().length === 0) {
$('#edit-empty-content-modal').modal({
onApprove() {
$('.edit.form').trigger('submit');
}
}).modal('show');
event.preventDefault();
// Update the editor from query params, if available,
// only after the dirtyFileClass initialization
const params = new URLSearchParams(window.location.search);
const value = params.get('value');
if (value) {
editor.setValue(value);
}
});

$commitButton.on('click', (event) => {
// A modal which asks if an empty file should be committed
if ($editArea.val().length === 0) {
$('#edit-empty-content-modal').modal({
onApprove() {
$('.edit.form').trigger('submit');
},
}).modal('show');
event.preventDefault();
}
});
})();
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export default async function initGitGraph() {
export default function initRepoGraphGit() {
const graphContainer = document.getElementById('git-graph-container');
if (!graphContainer) return;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ function showContentHistoryMenu(issueBaseUrl, $item, commentId) {
});
}

export function initIssueContentHistory() {
export function initRepoIssueContentHistory() {
wxiaoguang marked this conversation as resolved.
Show resolved Hide resolved
const issueIndex = $('#issueIndex').val();
const $itemIssue = $('.timeline-item.comment.first');
if (!issueIndex || !$itemIssue.length) return;
Expand Down
4 changes: 2 additions & 2 deletions web_src/js/features/repo-legacy.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ export function initRepoCommentForm() {
}


export async function initRepository() {
export function initRepository() {
if ($('.repository').length === 0) {
return;
}
Expand Down Expand Up @@ -360,7 +360,7 @@ export async function initRepository() {
if ($editContentZone.html().length === 0) {
$editContentZone.html($('#edit-content-form').html());
$textarea = $editContentZone.find('textarea');
attachTribute($textarea.get(), {mentions: true, emoji: true});
await attachTribute($textarea.get(), {mentions: true, emoji: true});

let dz;
const $dropzone = $editContentZone.find('.dropzone');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const $lfsSettings = $('#lfs_settings');
const $lfsEndpoint = $('#lfs_endpoint');
const $items = $('#migrate_items').find('input[type=checkbox]');

export default function initMigration() {
export default function initRepoMigration() {
checkAuth();
setLFSSettingsVisibility();

Expand Down
Loading