BigW Consortium Gitlab

project_new.js 5.46 KB
Newer Older
1
/* eslint-disable func-names, space-before-function-paren, no-var, prefer-rest-params, wrap-iife, no-unused-vars, one-var, no-underscore-dangle, prefer-template, no-else-return, prefer-arrow-callback, max-len */
2

3 4 5 6 7
function highlightChanges($elm) {
  $elm.addClass('highlight-changes');
  setTimeout(() => $elm.removeClass('highlight-changes'), 10);
}

Fatih Acet committed
8 9 10
(function() {
  this.ProjectNew = (function() {
    function ProjectNew() {
11
      this.toggleSettings = this.toggleSettings.bind(this);
12 13
      this.$selects = $('.features select');
      this.$repoSelects = this.$selects.filter('.js-repo-select');
14
      this.$projectSelects = this.$selects.not('.js-repo-select');
15

Fatih Acet committed
16 17 18 19 20 21
      $('.project-edit-container').on('ajax:before', (function(_this) {
        return function() {
          $('.project-edit-container').hide();
          return $('.save-project-loader').show();
        };
      })(this));
Luke "Jared" Bennett committed
22 23 24

      this.initVisibilitySelect();

Fatih Acet committed
25 26
      this.toggleSettings();
      this.toggleSettingsOnclick();
27
      this.toggleRepoVisibility();
Fatih Acet committed
28 29
    }

Luke "Jared" Bennett committed
30 31 32 33 34
    ProjectNew.prototype.initVisibilitySelect = function() {
      const visibilityContainer = document.querySelector('.js-visibility-select');
      if (!visibilityContainer) return;
      const visibilitySelect = new gl.VisibilitySelect(visibilityContainer);
      visibilitySelect.init();
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70

      const $visibilitySelect = $(visibilityContainer).find('select');
      let projectVisibility = $visibilitySelect.val();
      const PROJECT_VISIBILITY_PRIVATE = '0';

      $visibilitySelect.on('change', () => {
        const newProjectVisibility = $visibilitySelect.val();

        if (projectVisibility !== newProjectVisibility) {
          this.$projectSelects.each((idx, select) => {
            const $select = $(select);
            const $options = $select.find('option');
            const values = $.map($options, e => e.value);

            // if switched to "private", limit visibility options
            if (newProjectVisibility === PROJECT_VISIBILITY_PRIVATE) {
              if ($select.val() !== values[0] && $select.val() !== values[1]) {
                $select.val(values[1]).trigger('change');
                highlightChanges($select);
              }
              $options.slice(2).disable();
            }

            // if switched from "private", increase visibility for non-disabled options
            if (projectVisibility === PROJECT_VISIBILITY_PRIVATE) {
              $options.enable();
              if ($select.val() !== values[0] && $select.val() !== values[values.length - 1]) {
                $select.val(values[values.length - 1]).trigger('change');
                highlightChanges($select);
              }
            }
          });

          projectVisibility = newProjectVisibility;
        }
      });
Luke "Jared" Bennett committed
71 72
    };

Fatih Acet committed
73
    ProjectNew.prototype.toggleSettings = function() {
74 75 76
      var self = this;

      this.$selects.each(function () {
77 78 79 80
        var $select = $(this);
        var className = $select.data('field')
          .replace(/_/g, '-')
          .replace('access-level', 'feature');
81 82
        self._showOrHide($select, '.' + className);
      });
Fatih Acet committed
83 84 85
    };

    ProjectNew.prototype.toggleSettingsOnclick = function() {
86
      this.$selects.on('change', this.toggleSettings);
Fatih Acet committed
87 88 89
    };

    ProjectNew.prototype._showOrHide = function(checkElement, container) {
90 91
      var $container = $(container);

92
      if ($(checkElement).val() !== '0') {
Fatih Acet committed
93 94 95 96 97 98
        return $container.show();
      } else {
        return $container.hide();
      }
    };

99
    ProjectNew.prototype.toggleRepoVisibility = function () {
100
      var $repoAccessLevel = $('.js-repo-access-level select');
101
      var $lfsEnabledOption = $('.js-lfs-enabled select');
102 103
      var containerRegistry = document.querySelectorAll('.js-container-registry')[0];
      var containerRegistryCheckbox = document.getElementById('project_container_registry_enabled');
104
      var prevSelectedVal = parseInt($repoAccessLevel.val(), 10);
105 106 107 108 109 110 111

      this.$repoSelects.find("option[value='" + $repoAccessLevel.val() + "']")
        .nextAll()
        .hide();

      $repoAccessLevel.off('change')
        .on('change', function () {
112
          var selectedVal = parseInt($repoAccessLevel.val(), 10);
113 114

          this.$repoSelects.each(function () {
115
            var $this = $(this);
116
            var repoSelectVal = parseInt($this.val(), 10);
117

118
            $this.find('option').enable();
119

120 121 122
            if (selectedVal < repoSelectVal || repoSelectVal === prevSelectedVal) {
              $this.val(selectedVal).trigger('change');
              highlightChanges($this);
123 124
            }

125
            $this.find("option[value='" + selectedVal + "']").nextAll().disable();
126 127 128 129
          });

          if (selectedVal) {
            this.$repoSelects.removeClass('disabled');
130

131 132 133 134
            if ($lfsEnabledOption.length) {
              $lfsEnabledOption.removeClass('disabled');
              highlightChanges($lfsEnabledOption);
            }
135 136 137
            if (containerRegistry) {
              containerRegistry.style.display = '';
            }
138 139
          } else {
            this.$repoSelects.addClass('disabled');
140

141 142 143 144
            if ($lfsEnabledOption.length) {
              $lfsEnabledOption.val('false').addClass('disabled');
              highlightChanges($lfsEnabledOption);
            }
145 146 147 148
            if (containerRegistry) {
              containerRegistry.style.display = 'none';
              containerRegistryCheckbox.checked = false;
            }
149
          }
150 151

          prevSelectedVal = selectedVal;
152 153 154
        }.bind(this));
    };

Fatih Acet committed
155 156
    return ProjectNew;
  })();
157
}).call(window);