You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LabelEdit.js 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import $ from 'jquery';
  2. function isExclusiveScopeName(name) {
  3. return /.*[^/]\/[^/].*/.test(name);
  4. }
  5. function updateExclusiveLabelEdit(form) {
  6. const nameInput = document.querySelector(`${form} .label-name-input`);
  7. const exclusiveField = document.querySelector(`${form} .label-exclusive-input-field`);
  8. const exclusiveCheckbox = document.querySelector(`${form} .label-exclusive-input`);
  9. const exclusiveWarning = document.querySelector(`${form} .label-exclusive-warning`);
  10. if (isExclusiveScopeName(nameInput.value)) {
  11. exclusiveField?.classList.remove('muted');
  12. exclusiveField?.removeAttribute('aria-disabled');
  13. if (exclusiveCheckbox.checked && exclusiveCheckbox.getAttribute('data-exclusive-warn')) {
  14. exclusiveWarning?.classList.remove('tw-hidden');
  15. } else {
  16. exclusiveWarning?.classList.add('tw-hidden');
  17. }
  18. } else {
  19. exclusiveField?.classList.add('muted');
  20. exclusiveField?.setAttribute('aria-disabled', 'true');
  21. exclusiveWarning?.classList.add('tw-hidden');
  22. }
  23. }
  24. export function initCompLabelEdit(selector) {
  25. if (!$(selector).length) return;
  26. // Create label
  27. $('.new-label.button').on('click', () => {
  28. updateExclusiveLabelEdit('.new-label');
  29. $('.new-label.modal').modal({
  30. onApprove() {
  31. const form = document.querySelector('.new-label.form');
  32. if (!form.checkValidity()) {
  33. form.reportValidity();
  34. return false;
  35. }
  36. $('.new-label.form').trigger('submit');
  37. },
  38. }).modal('show');
  39. return false;
  40. });
  41. // Edit label
  42. $('.edit-label-button').on('click', function () {
  43. $('#label-modal-id').val($(this).data('id'));
  44. const $nameInput = $('.edit-label .label-name-input');
  45. $nameInput.val($(this).data('title'));
  46. const $isArchivedCheckbox = $('.edit-label .label-is-archived-input');
  47. $isArchivedCheckbox[0].checked = this.hasAttribute('data-is-archived');
  48. const $exclusiveCheckbox = $('.edit-label .label-exclusive-input');
  49. $exclusiveCheckbox[0].checked = this.hasAttribute('data-exclusive');
  50. // Warn when label was previously not exclusive and used in issues
  51. $exclusiveCheckbox.data('exclusive-warn',
  52. $(this).data('num-issues') > 0 &&
  53. (!this.hasAttribute('data-exclusive') || !isExclusiveScopeName($nameInput.val())));
  54. updateExclusiveLabelEdit('.edit-label');
  55. $('.edit-label .label-desc-input').val(this.getAttribute('data-description'));
  56. const colorInput = document.querySelector('.edit-label .js-color-picker-input input');
  57. colorInput.value = this.getAttribute('data-color');
  58. colorInput.dispatchEvent(new Event('input', {bubbles: true}));
  59. $('.edit-label.modal').modal({
  60. onApprove() {
  61. const form = document.querySelector('.edit-label.form');
  62. if (!form.checkValidity()) {
  63. form.reportValidity();
  64. return false;
  65. }
  66. $('.edit-label.form').trigger('submit');
  67. },
  68. }).modal('show');
  69. return false;
  70. });
  71. $('.new-label .label-name-input').on('input', () => {
  72. updateExclusiveLabelEdit('.new-label');
  73. });
  74. $('.new-label .label-exclusive-input').on('change', () => {
  75. updateExclusiveLabelEdit('.new-label');
  76. });
  77. $('.edit-label .label-name-input').on('input', () => {
  78. updateExclusiveLabelEdit('.edit-label');
  79. });
  80. $('.edit-label .label-exclusive-input').on('change', () => {
  81. updateExclusiveLabelEdit('.edit-label');
  82. });
  83. }