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.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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((_, e) => {
  10. const $dropdown = $(e);
  11. const $text = $dropdown.find('> .text');
  12. $dropdown.dropdown({
  13. async action(_text, value) {
  14. const lastValue = $dropdown.attr('data-last-value');
  15. try {
  16. $dropdown.attr('data-last-value', value);
  17. $dropdown.dropdown('hide');
  18. const data = new FormData();
  19. data.append('uid', $dropdown.attr('data-uid'));
  20. data.append('mode', value);
  21. await POST($dropdown.attr('data-url'), {data});
  22. } catch {
  23. $text.text('(error)'); // prevent from misleading users when error occurs
  24. $dropdown.attr('data-last-value', lastValue);
  25. }
  26. },
  27. onChange(_value, text, _$choice) {
  28. $text.text(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', $dropdown.attr('data-last-value'));
  34. if ($item) {
  35. $dropdown.dropdown('set selected', $dropdown.attr('data-last-value'));
  36. } else {
  37. $text.text('(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 = $('#search-team-box');
  46. $searchTeamBox.search({
  47. minCharacters: 2,
  48. apiSettings: {
  49. url: `${appSubUrl}/org/${$searchTeamBox.attr('data-org-name')}/teams/-/search?q={query}`,
  50. headers: {'X-Csrf-Token': csrfToken},
  51. onResponse(response) {
  52. const items = [];
  53. $.each(response.data, (_i, item) => {
  54. items.push({
  55. title: item.name,
  56. description: `${item.permission} access`, // TODO: translate this string
  57. });
  58. });
  59. return {results: items};
  60. },
  61. },
  62. searchFields: ['name', 'description'],
  63. showNoResults: false,
  64. });
  65. }
  66. export function initRepoSettingGitHook() {
  67. if ($('.edit.githook').length === 0) return;
  68. const filename = document.querySelector('.hook-filename').textContent;
  69. const _promise = createMonaco($('#content')[0], filename, {language: 'shell'});
  70. }
  71. export function initRepoSettingBranches() {
  72. if (!$('.repository.settings.branches').length) return;
  73. $('.toggle-target-enabled').on('change', function () {
  74. const $target = $($(this).attr('data-target'));
  75. $target.toggleClass('disabled', !this.checked);
  76. });
  77. $('.toggle-target-disabled').on('change', function () {
  78. const $target = $($(this).attr('data-target'));
  79. if (this.checked) $target.addClass('disabled'); // only disable, do not auto enable
  80. });
  81. $('#dismiss_stale_approvals').on('change', function () {
  82. const $target = $('#ignore_stale_approvals_box');
  83. $target.toggleClass('disabled', this.checked);
  84. });
  85. // show the `Matched` mark for the status checks that match the pattern
  86. const markMatchedStatusChecks = () => {
  87. const patterns = (document.getElementById('status_check_contexts').value || '').split(/[\r\n]+/);
  88. const validPatterns = patterns.map((item) => item.trim()).filter(Boolean);
  89. const marks = document.getElementsByClassName('status-check-matched-mark');
  90. for (const el of marks) {
  91. let matched = false;
  92. const statusCheck = el.getAttribute('data-status-check');
  93. for (const pattern of validPatterns) {
  94. if (minimatch(statusCheck, pattern)) {
  95. matched = true;
  96. break;
  97. }
  98. }
  99. toggleElem(el, matched);
  100. }
  101. };
  102. markMatchedStatusChecks();
  103. document.getElementById('status_check_contexts').addEventListener('input', onInputDebounce(markMatchedStatusChecks));
  104. }