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.

repo-settings.js 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import $ from 'jquery';
  2. import {minimatch} from 'minimatch';
  3. import {createMonaco} from './codeeditor.js';
  4. import {onInputDebounce, toggleElem} from '../utils/dom.js';
  5. import {POST} from '../modules/fetch.js';
  6. const {appSubUrl, csrfToken} = window.config;
  7. export function initRepoSettingsCollaboration() {
  8. // Change collaborator access mode
  9. $('.page-content.repository .ui.dropdown.access-mode').each((_, el) => {
  10. const $dropdown = $(el);
  11. const $text = $dropdown.find('> .text');
  12. $dropdown.dropdown({
  13. async action(_text, value) {
  14. const lastValue = el.getAttribute('data-last-value');
  15. try {
  16. el.setAttribute('data-last-value', value);
  17. $dropdown.dropdown('hide');
  18. const data = new FormData();
  19. data.append('uid', el.getAttribute('data-uid'));
  20. data.append('mode', value);
  21. await POST(el.getAttribute('data-url'), {data});
  22. } catch {
  23. $text[0].textContent = '(error)'; // prevent from misleading users when error occurs
  24. el.setAttribute('data-last-value', lastValue);
  25. }
  26. },
  27. onChange(_value, text, _$choice) {
  28. $text[0].textContent = text; // update the text when using keyboard navigating
  29. },
  30. onHide() {
  31. // set to the really selected value, defer to next tick to make sure `action` has finished its work because the calling order might be onHide -> action
  32. setTimeout(() => {
  33. const $item = $dropdown.dropdown('get item', el.getAttribute('data-last-value'));
  34. if ($item) {
  35. $dropdown.dropdown('set selected', el.getAttribute('data-last-value'));
  36. } else {
  37. $text[0].textContent = '(none)'; // prevent from misleading users when the access mode is undefined
  38. }
  39. }, 0);
  40. },
  41. });
  42. });
  43. }
  44. export function initRepoSettingSearchTeamBox() {
  45. const searchTeamBox = document.getElementById('search-team-box');
  46. if (!searchTeamBox) return;
  47. $(searchTeamBox).search({
  48. minCharacters: 2,
  49. apiSettings: {
  50. url: `${appSubUrl}/org/${searchTeamBox.getAttribute('data-org-name')}/teams/-/search?q={query}`,
  51. headers: {'X-Csrf-Token': csrfToken},
  52. onResponse(response) {
  53. const items = [];
  54. $.each(response.data, (_i, item) => {
  55. items.push({
  56. title: item.name,
  57. description: `${item.permission} access`, // TODO: translate this string
  58. });
  59. });
  60. return {results: items};
  61. },
  62. },
  63. searchFields: ['name', 'description'],
  64. showNoResults: false,
  65. });
  66. }
  67. export function initRepoSettingGitHook() {
  68. if (!$('.edit.githook').length) return;
  69. const filename = document.querySelector('.hook-filename').textContent;
  70. const _promise = createMonaco($('#content')[0], filename, {language: 'shell'});
  71. }
  72. export function initRepoSettingBranches() {
  73. if (!document.querySelector('.repository.settings.branches')) return;
  74. for (const el of document.getElementsByClassName('toggle-target-enabled')) {
  75. el.addEventListener('change', function () {
  76. const target = document.querySelector(this.getAttribute('data-target'));
  77. target?.classList.toggle('disabled', !this.checked);
  78. });
  79. }
  80. for (const el of document.getElementsByClassName('toggle-target-disabled')) {
  81. el.addEventListener('change', function () {
  82. const target = document.querySelector(this.getAttribute('data-target'));
  83. if (this.checked) target?.classList.add('disabled'); // only disable, do not auto enable
  84. });
  85. }
  86. document.getElementById('dismiss_stale_approvals')?.addEventListener('change', function () {
  87. document.getElementById('ignore_stale_approvals_box')?.classList.toggle('disabled', this.checked);
  88. });
  89. // show the `Matched` mark for the status checks that match the pattern
  90. const markMatchedStatusChecks = () => {
  91. const patterns = (document.getElementById('status_check_contexts').value || '').split(/[\r\n]+/);
  92. const validPatterns = patterns.map((item) => item.trim()).filter(Boolean);
  93. const marks = document.getElementsByClassName('status-check-matched-mark');
  94. for (const el of marks) {
  95. let matched = false;
  96. const statusCheck = el.getAttribute('data-status-check');
  97. for (const pattern of validPatterns) {
  98. if (minimatch(statusCheck, pattern)) {
  99. matched = true;
  100. break;
  101. }
  102. }
  103. toggleElem(el, matched);
  104. }
  105. };
  106. markMatchedStatusChecks();
  107. document.getElementById('status_check_contexts').addEventListener('input', onInputDebounce(markMatchedStatusChecks));
  108. }