-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Refactor LabelEdit #32752
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
Merged
Merged
Refactor LabelEdit #32752
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
47a9665
fix
wxiaoguang 5aa7c75
rename data-repo-link to data-current-page-link
wxiaoguang bc63562
rename repoLink to curPageLink
wxiaoguang 8698325
fine tune comment
wxiaoguang 9cb65c5
Merge branch 'main' into refactor-label-editor
wxiaoguang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,13 @@ | ||
{{template "org/settings/layout_head" (dict "ctxData" . "pageClass" "organization settings labels")}} | ||
<div class="org-setting-content"> | ||
<div class="tw-flex tw-items-center"> | ||
<div class="tw-flex-1"> | ||
{{ctx.Locale.Tr "org.settings.labels_desc"}} | ||
</div> | ||
<button class="ui small primary new-label button">{{ctx.Locale.Tr "repo.issues.new_label"}}</button> | ||
</div> | ||
<div class="divider"></div> | ||
{{template "repo/issue/labels/label_new" .}} | ||
{{template "repo/issue/labels/label_list" .}} | ||
</div> | ||
{{template "repo/issue/labels/edit_delete_label" .}} | ||
<div class="org-setting-content"> | ||
<div class="tw-flex tw-items-center"> | ||
<div class="tw-flex-1"> | ||
{{ctx.Locale.Tr "org.settings.labels_desc"}} | ||
</div> | ||
<button class="ui small primary new-label button">{{ctx.Locale.Tr "repo.issues.new_label"}}</button> | ||
</div> | ||
<div class="divider"></div> | ||
{{template "repo/issue/labels/label_list" .}} | ||
{{template "repo/issue/labels/label_edit_modal" .}} | ||
</div> | ||
{{template "org/settings/layout_footer" .}} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 8 additions & 17 deletions
25
.../repo/issue/labels/edit_delete_label.tmpl → ...s/repo/issue/labels/label_edit_modal.tmpl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,96 +1,77 @@ | ||
import $ from 'jquery'; | ||
import {toggleElem} from '../../utils/dom.ts'; | ||
import {fomanticQuery} from '../../modules/fomantic/base.ts'; | ||
|
||
function isExclusiveScopeName(name) { | ||
function nameHasScope(name: string): boolean { | ||
return /.*[^/]\/[^/].*/.test(name); | ||
} | ||
|
||
function updateExclusiveLabelEdit(form) { | ||
const nameInput = document.querySelector(`${form} .label-name-input`); | ||
const exclusiveField = document.querySelector(`${form} .label-exclusive-input-field`); | ||
const exclusiveCheckbox = document.querySelector(`${form} .label-exclusive-input`); | ||
const exclusiveWarning = document.querySelector(`${form} .label-exclusive-warning`); | ||
export function initCompLabelEdit(pageSelector: string) { | ||
const pageContent = document.querySelector<HTMLElement>(pageSelector); | ||
if (!pageContent) return; | ||
|
||
if (isExclusiveScopeName(nameInput.value)) { | ||
exclusiveField?.classList.remove('muted'); | ||
exclusiveField?.removeAttribute('aria-disabled'); | ||
if (exclusiveCheckbox.checked && exclusiveCheckbox.getAttribute('data-exclusive-warn')) { | ||
exclusiveWarning?.classList.remove('tw-hidden'); | ||
} else { | ||
exclusiveWarning?.classList.add('tw-hidden'); | ||
} | ||
} else { | ||
exclusiveField?.classList.add('muted'); | ||
exclusiveField?.setAttribute('aria-disabled', 'true'); | ||
exclusiveWarning?.classList.add('tw-hidden'); | ||
} | ||
} | ||
const elModal = pageContent.querySelector<HTMLElement>('#issue-label-edit-modal'); | ||
if (!elModal) return; | ||
|
||
export function initCompLabelEdit(selector) { | ||
if (!$(selector).length) return; | ||
const elLabelId = elModal.querySelector<HTMLInputElement>('input[name="id"]'); | ||
const elNameInput = elModal.querySelector<HTMLInputElement>('.label-name-input'); | ||
const elExclusiveField = elModal.querySelector('.label-exclusive-input-field'); | ||
const elExclusiveInput = elModal.querySelector<HTMLInputElement>('.label-exclusive-input'); | ||
const elExclusiveWarning = elModal.querySelector('.label-exclusive-warning'); | ||
const elIsArchivedField = elModal.querySelector('.label-is-archived-input-field'); | ||
const elIsArchivedInput = elModal.querySelector<HTMLInputElement>('.label-is-archived-input'); | ||
const elDescInput = elModal.querySelector<HTMLInputElement>('.label-desc-input'); | ||
const elColorInput = elModal.querySelector<HTMLInputElement>('.js-color-picker-input input'); | ||
|
||
// Create label | ||
$('.new-label.button').on('click', () => { | ||
updateExclusiveLabelEdit('.new-label'); | ||
$('.new-label.modal').modal({ | ||
onApprove() { | ||
const form = document.querySelector('.new-label.form'); | ||
if (!form.checkValidity()) { | ||
form.reportValidity(); | ||
return false; | ||
} | ||
$('.new-label.form').trigger('submit'); | ||
}, | ||
}).modal('show'); | ||
return false; | ||
}); | ||
|
||
// Edit label | ||
$('.edit-label-button').on('click', function () { | ||
$('#label-modal-id').val($(this).data('id')); | ||
|
||
const $nameInput = $('.edit-label .label-name-input'); | ||
$nameInput.val($(this).data('title')); | ||
|
||
const $isArchivedCheckbox = $('.edit-label .label-is-archived-input'); | ||
$isArchivedCheckbox[0].checked = this.hasAttribute('data-is-archived'); | ||
const syncModalUi = () => { | ||
const hasScope = nameHasScope(elNameInput.value); | ||
elExclusiveField.classList.toggle('disabled', !hasScope); | ||
const showExclusiveWarning = hasScope && elExclusiveInput.checked && elModal.hasAttribute('data-need-warn-exclusive'); | ||
toggleElem(elExclusiveWarning, showExclusiveWarning); | ||
if (!hasScope) elExclusiveInput.checked = false; | ||
}; | ||
|
||
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', | ||
$(this).data('num-issues') > 0 && | ||
(!this.hasAttribute('data-exclusive') || !isExclusiveScopeName($nameInput.val()))); | ||
updateExclusiveLabelEdit('.edit-label'); | ||
const showLabelEditModal = (btn:HTMLElement) => { | ||
const form = elModal.querySelector<HTMLFormElement>('form'); | ||
elLabelId.value = btn.getAttribute('data-label-id') || ''; | ||
elNameInput.value = btn.getAttribute('data-label-name') || ''; | ||
elIsArchivedInput.checked = btn.getAttribute('data-label-is-archived') === 'true'; | ||
elExclusiveInput.checked = btn.getAttribute('data-label-exclusive') === 'true'; | ||
elDescInput.value = btn.getAttribute('data-label-description') || ''; | ||
elColorInput.value = btn.getAttribute('data-label-color') || ''; | ||
elColorInput.dispatchEvent(new Event('input', {bubbles: true})); // trigger the color picker | ||
|
||
$('.edit-label .label-desc-input').val(this.getAttribute('data-description')); | ||
const isEdit = Boolean(elLabelId.value); | ||
|
||
const colorInput = document.querySelector('.edit-label .js-color-picker-input input'); | ||
colorInput.value = this.getAttribute('data-color'); | ||
colorInput.dispatchEvent(new Event('input', {bubbles: true})); | ||
// if a label was not exclusive but has issues, then it should warn user if it will become exclusive | ||
const numIssues = parseInt(btn.getAttribute('data-label-num-issues') || '0'); | ||
elModal.toggleAttribute('data-need-warn-exclusive', !elExclusiveInput.checked && numIssues > 0); | ||
elModal.querySelector('.header').textContent = isEdit ? elModal.getAttribute('data-text-edit-label') : elModal.getAttribute('data-text-new-label'); | ||
|
||
$('.edit-label.modal').modal({ | ||
const repoLink = elModal.getAttribute('data-repo-link'); | ||
form.action = isEdit ? `${repoLink}/edit` : `${repoLink}/new`; | ||
toggleElem(elIsArchivedField, isEdit); | ||
syncModalUi(); | ||
fomanticQuery(elModal).modal({ | ||
onApprove() { | ||
const form = document.querySelector('.edit-label.form'); | ||
if (!form.checkValidity()) { | ||
form.reportValidity(); | ||
return false; | ||
} | ||
$('.edit-label.form').trigger('submit'); | ||
form.submit(); | ||
}, | ||
}).modal('show'); | ||
return false; | ||
}); | ||
}; | ||
|
||
$('.new-label .label-name-input').on('input', () => { | ||
updateExclusiveLabelEdit('.new-label'); | ||
}); | ||
$('.new-label .label-exclusive-input').on('change', () => { | ||
updateExclusiveLabelEdit('.new-label'); | ||
}); | ||
$('.edit-label .label-name-input').on('input', () => { | ||
updateExclusiveLabelEdit('.edit-label'); | ||
}); | ||
$('.edit-label .label-exclusive-input').on('change', () => { | ||
updateExclusiveLabelEdit('.edit-label'); | ||
}); | ||
elModal.addEventListener('input', () => syncModalUi()); | ||
|
||
const elNewLabel = pageContent.querySelector<HTMLElement>('.ui.button.new-label'); | ||
elNewLabel?.addEventListener('click', () => showLabelEditModal(elNewLabel)); | ||
|
||
const elEditLabelButtons = pageContent.querySelectorAll<HTMLElement>('.edit-label-button'); | ||
for (const btn of elEditLabelButtons) { | ||
btn.addEventListener('click', (e) => { | ||
e.preventDefault(); | ||
showLabelEditModal(btn); | ||
}); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.