code.gitea.io/gitea@v1.21.7/web_src/js/features/repo-settings.js (about)

     1  import $ from 'jquery';
     2  import {minimatch} from 'minimatch';
     3  import {createMonaco} from './codeeditor.js';
     4  import {onInputDebounce, toggleElem} from '../utils/dom.js';
     5  
     6  const {appSubUrl, csrfToken} = window.config;
     7  
     8  export function initRepoSettingsCollaboration() {
     9    // Change collaborator access mode
    10    $('.page-content.repository .ui.dropdown.access-mode').each((_, e) => {
    11      const $dropdown = $(e);
    12      const $text = $dropdown.find('> .text');
    13      $dropdown.dropdown({
    14        action(_text, value) {
    15          const lastValue = $dropdown.attr('data-last-value');
    16          $.post($dropdown.attr('data-url'), {
    17            _csrf: csrfToken,
    18            uid: $dropdown.attr('data-uid'),
    19            mode: value,
    20          }).fail(() => {
    21            $text.text('(error)'); // prevent from misleading users when error occurs
    22            $dropdown.attr('data-last-value', lastValue);
    23          });
    24          $dropdown.attr('data-last-value', value);
    25          $dropdown.dropdown('hide');
    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  
    45  export function initRepoSettingSearchTeamBox() {
    46    const $searchTeamBox = $('#search-team-box');
    47    $searchTeamBox.search({
    48      minCharacters: 2,
    49      apiSettings: {
    50        url: `${appSubUrl}/org/${$searchTeamBox.attr('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            const title = `${item.name} (${item.permission} access)`;
    56            items.push({
    57              title,
    58            });
    59          });
    60  
    61          return {results: items};
    62        }
    63      },
    64      searchFields: ['name', 'description'],
    65      showNoResults: false
    66    });
    67  }
    68  
    69  
    70  export function initRepoSettingGitHook() {
    71    if ($('.edit.githook').length === 0) return;
    72    const filename = document.querySelector('.hook-filename').textContent;
    73    const _promise = createMonaco($('#content')[0], filename, {language: 'shell'});
    74  }
    75  
    76  export function initRepoSettingBranches() {
    77    if (!$('.repository.settings.branches').length) return;
    78    $('.toggle-target-enabled').on('change', function () {
    79      const $target = $($(this).attr('data-target'));
    80      $target.toggleClass('disabled', !this.checked);
    81    });
    82    $('.toggle-target-disabled').on('change', function () {
    83      const $target = $($(this).attr('data-target'));
    84      if (this.checked) $target.addClass('disabled'); // only disable, do not auto enable
    85    });
    86  
    87    // show the `Matched` mark for the status checks that match the pattern
    88    const markMatchedStatusChecks = () => {
    89      const patterns = (document.getElementById('status_check_contexts').value || '').split(/[\r\n]+/);
    90      const validPatterns = patterns.map((item) => item.trim()).filter(Boolean);
    91      const marks = document.getElementsByClassName('status-check-matched-mark');
    92  
    93      for (const el of marks) {
    94        let matched = false;
    95        const statusCheck = el.getAttribute('data-status-check');
    96        for (const pattern of validPatterns) {
    97          if (minimatch(statusCheck, pattern)) {
    98            matched = true;
    99            break;
   100          }
   101        }
   102  
   103        toggleElem(el, matched);
   104      }
   105    };
   106    markMatchedStatusChecks();
   107    document.getElementById('status_check_contexts').addEventListener('input', onInputDebounce(markMatchedStatusChecks));
   108  }