project_new.js 3.3 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

F
Fatih Acet 已提交
3
(function() {
4
  var bind = function(fn, me) { return function() { return fn.apply(me, arguments); }; };
F
Fatih Acet 已提交
5 6 7 8

  this.ProjectNew = (function() {
    function ProjectNew() {
      this.toggleSettings = bind(this.toggleSettings, this);
9 10
      this.$selects = $('.features select');
      this.$repoSelects = this.$selects.filter('.js-repo-select');
11

F
Fatih Acet 已提交
12 13 14 15 16 17
      $('.project-edit-container').on('ajax:before', (function(_this) {
        return function() {
          $('.project-edit-container').hide();
          return $('.save-project-loader').show();
        };
      })(this));
L
Luke "Jared" Bennett 已提交
18 19 20

      this.initVisibilitySelect();

F
Fatih Acet 已提交
21 22
      this.toggleSettings();
      this.toggleSettingsOnclick();
23
      this.toggleRepoVisibility();
F
Fatih Acet 已提交
24 25
    }

L
Luke "Jared" Bennett 已提交
26 27 28 29 30 31 32
    ProjectNew.prototype.initVisibilitySelect = function() {
      const visibilityContainer = document.querySelector('.js-visibility-select');
      if (!visibilityContainer) return;
      const visibilitySelect = new gl.VisibilitySelect(visibilityContainer);
      visibilitySelect.init();
    };

F
Fatih Acet 已提交
33
    ProjectNew.prototype.toggleSettings = function() {
34 35 36
      var self = this;

      this.$selects.each(function () {
37 38 39 40
        var $select = $(this);
        var className = $select.data('field')
          .replace(/_/g, '-')
          .replace('access-level', 'feature');
41 42
        self._showOrHide($select, '.' + className);
      });
F
Fatih Acet 已提交
43 44 45
    };

    ProjectNew.prototype.toggleSettingsOnclick = function() {
46
      this.$selects.on('change', this.toggleSettings);
F
Fatih Acet 已提交
47 48 49
    };

    ProjectNew.prototype._showOrHide = function(checkElement, container) {
50 51
      var $container = $(container);

52
      if ($(checkElement).val() !== '0') {
F
Fatih Acet 已提交
53 54 55 56 57 58
        return $container.show();
      } else {
        return $container.hide();
      }
    };

59
    ProjectNew.prototype.toggleRepoVisibility = function () {
60 61 62
      var $repoAccessLevel = $('.js-repo-access-level select');
      var containerRegistry = document.querySelectorAll('.js-container-registry')[0];
      var containerRegistryCheckbox = document.getElementById('project_container_registry_enabled');
63 64 65 66 67 68 69

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

      $repoAccessLevel.off('change')
        .on('change', function () {
70
          var selectedVal = parseInt($repoAccessLevel.val(), 10);
71 72

          this.$repoSelects.each(function () {
73
            var $this = $(this);
74
            var repoSelectVal = parseInt($this.val(), 10);
75 76 77 78 79 80 81 82 83 84 85 86

            $this.find('option').show();

            if (selectedVal < repoSelectVal) {
              $this.val(selectedVal);
            }

            $this.find("option[value='" + selectedVal + "']").nextAll().hide();
          });

          if (selectedVal) {
            this.$repoSelects.removeClass('disabled');
87 88 89 90

            if (containerRegistry) {
              containerRegistry.style.display = '';
            }
91 92
          } else {
            this.$repoSelects.addClass('disabled');
93 94 95 96 97

            if (containerRegistry) {
              containerRegistry.style.display = 'none';
              containerRegistryCheckbox.checked = false;
            }
98 99 100 101
          }
        }.bind(this));
    };

F
Fatih Acet 已提交
102 103 104
    return ProjectNew;
  })();
}).call(this);