summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/project_visibility.js
blob: 1b57a69d4647a3618a655720489def749cebabf4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import $ from 'jquery';
import eventHub from '~/projects/new/event_hub';

// Values are from lib/gitlab/visibility_level.rb
const visibilityLevel = {
  private: 0,
  internal: 10,
  public: 20,
};

function setVisibilityOptions({ name, visibility, showPath, editPath }) {
  document.querySelectorAll('.visibility-level-setting .form-check').forEach((option) => {
    // Don't change anything if the option is restricted by admin
    if (option.classList.contains('restricted')) {
      return;
    }

    const optionInput = option.querySelector('input[type=radio]');
    const optionValue = optionInput ? parseInt(optionInput.value, 10) : 0;

    if (visibilityLevel[visibility] < optionValue) {
      option.classList.add('disabled');
      optionInput.disabled = true;
      const reason = option.querySelector('.option-disabled-reason');
      if (reason) {
        const optionTitle = option.querySelector('.option-title');
        const optionName = optionTitle ? optionTitle.innerText.toLowerCase() : '';
        reason.innerHTML = `This project cannot be ${optionName} because the visibility of
            <a href="${showPath}">${name}</a> is ${visibility}. To make this project
            ${optionName}, you must first <a href="${editPath}">change the visibility</a>
            of the parent group.`;
      }
    } else {
      option.classList.remove('disabled');
      optionInput.disabled = false;
    }
  });
}

function handleSelect2DropdownChange(namespaceSelector) {
  if (!namespaceSelector || !('selectedIndex' in namespaceSelector)) {
    return;
  }
  const selectedNamespace = namespaceSelector.options[namespaceSelector.selectedIndex];
  setVisibilityOptions(selectedNamespace.dataset);
}

export default function initProjectVisibilitySelector() {
  eventHub.$on('update-visibility', setVisibilityOptions);

  const namespaceSelector = document.querySelector('select.js-select-namespace');
  if (namespaceSelector) {
    $('.select2.js-select-namespace').on('change', () =>
      handleSelect2DropdownChange(namespaceSelector),
    );
    handleSelect2DropdownChange(namespaceSelector);
  }
}