code.gitea.io/gitea@v1.21.7/web_src/js/features/comp/LabelEdit.js (about) 1 import $ from 'jquery'; 2 import {initCompColorPicker} from './ColorPicker.js'; 3 4 function isExclusiveScopeName(name) { 5 return /.*[^/]\/[^/].*/.test(name); 6 } 7 8 function updateExclusiveLabelEdit(form) { 9 const nameInput = $(`${form} .label-name-input`); 10 const exclusiveField = $(`${form} .label-exclusive-input-field`); 11 const exclusiveCheckbox = $(`${form} .label-exclusive-input`); 12 const exclusiveWarning = $(`${form} .label-exclusive-warning`); 13 14 if (isExclusiveScopeName(nameInput.val())) { 15 exclusiveField.removeClass('muted'); 16 exclusiveField.removeAttr('aria-disabled'); 17 if (exclusiveCheckbox.prop('checked') && exclusiveCheckbox.data('exclusive-warn')) { 18 exclusiveWarning.removeClass('gt-hidden'); 19 } else { 20 exclusiveWarning.addClass('gt-hidden'); 21 } 22 } else { 23 exclusiveField.addClass('muted'); 24 exclusiveField.attr('aria-disabled', 'true'); 25 exclusiveWarning.addClass('gt-hidden'); 26 } 27 } 28 29 export function initCompLabelEdit(selector) { 30 if (!$(selector).length) return; 31 initCompColorPicker(); 32 33 // Create label 34 $('.new-label.button').on('click', () => { 35 updateExclusiveLabelEdit('.new-label'); 36 $('.new-label.modal').modal({ 37 onApprove() { 38 $('.new-label.form').trigger('submit'); 39 }, 40 }).modal('show'); 41 return false; 42 }); 43 44 // Edit label 45 $('.edit-label-button').on('click', function () { 46 $('.edit-label .color-picker').minicolors('value', $(this).data('color')); 47 $('#label-modal-id').val($(this).data('id')); 48 49 const nameInput = $('.edit-label .label-name-input'); 50 nameInput.val($(this).data('title')); 51 52 const isArchivedCheckbox = $('.edit-label .label-is-archived-input'); 53 isArchivedCheckbox.prop('checked', this.hasAttribute('data-is-archived')); 54 55 const exclusiveCheckbox = $('.edit-label .label-exclusive-input'); 56 exclusiveCheckbox.prop('checked', this.hasAttribute('data-exclusive')); 57 // Warn when label was previously not exclusive and used in issues 58 exclusiveCheckbox.data('exclusive-warn', 59 $(this).data('num-issues') > 0 && 60 (!this.hasAttribute('data-exclusive') || !isExclusiveScopeName(nameInput.val()))); 61 updateExclusiveLabelEdit('.edit-label'); 62 63 $('.edit-label .label-desc-input').val($(this).data('description')); 64 $('.edit-label .color-picker').val($(this).data('color')); 65 $('.edit-label .minicolors-swatch-color').css('background-color', $(this).data('color')); 66 67 $('.edit-label.modal').modal({ 68 onApprove() { 69 $('.edit-label.form').trigger('submit'); 70 }, 71 }).modal('show'); 72 return false; 73 }); 74 75 $('.new-label .label-name-input').on('input', () => { 76 updateExclusiveLabelEdit('.new-label'); 77 }); 78 $('.new-label .label-exclusive-input').on('change', () => { 79 updateExclusiveLabelEdit('.new-label'); 80 }); 81 $('.edit-label .label-name-input').on('input', () => { 82 updateExclusiveLabelEdit('.edit-label'); 83 }); 84 $('.edit-label .label-exclusive-input').on('change', () => { 85 updateExclusiveLabelEdit('.edit-label'); 86 }); 87 }