summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/protect_branch.js.es6
blob: be987adc4a762d753a24eed11451aa8ab7dd492f (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
class ProtectedBranchesAccessDropdown {
  constructor(options) {
    const { $dropdown, data, onSelect } = options;

    $dropdown.glDropdown({
      data: data,
      selectable: true,
      fieldName: $dropdown.data('field-name'),
      toggleLabel(item) {
        return item.text;
      },
      clicked(item, $el, e) {
        e.preventDefault();
        onSelect();
      }
    });
  }
}

class AllowedToMergeDropdowns {
  constructor (options) {
    const { $dropdowns, onSelect } = options;

    $dropdowns.each((i, el) => {
      new ProtectedBranchesAccessDropdown({
        $dropdown: $(el),
        data: gon.merge_access_levels,
        onSelect: onSelect
      });
    });
  }
}

class AllowedToPushSelects {
  constructor (options) {
    const { $dropdowns, onSelect } = options;

    $dropdowns.each((i, el) => {
      new ProtectedBranchesAccessDropdown({
        $dropdown: $(el),
        data: gon.push_access_levels,
        onSelect: onSelect
      });
    });
  }
}

class CreateProtectedBranch {
  constructor() {
    this.$wrap = this.$form = $('#new_protected_branch');
    this.buildDropdowns();
  }

  buildDropdowns() {
    // Allowed to Merge dropdowns
    new AllowedToMergeDropdowns({
      $dropdowns: this.$wrap.find('.js-allowed-to-merge'),
      onSelect: this.onSelect.bind(this)
    });

    // Allowed to Push dropdowns
    new AllowedToPushSelects({
      $dropdowns: this.$wrap.find('.js-allowed-to-push'),
      onSelect: this.onSelect.bind(this)
    });

    new ProtectedBranchSelects({
      $dropdowns: this.$wrap.find('.js-protected-branch-select'),
      onSelect: this.onSelect.bind(this)
    });
  }

  // This will run after clicked callback
  onSelect() {
    // Enable submit button
    const $branchInput = this.$wrap.find('input[name="protected_branch[name]"]');
    const $allowedToMergeInput = this.$wrap.find('input[name="protected_branch[merge_access_level_attributes][access_level]"]');
    const $allowedToPushInput = this.$wrap.find('input[name="protected_branch[push_access_level_attributes][access_level]"]');

    if ($branchInput.val() && $allowedToMergeInput.val() && $allowedToPushInput.val()){
      this.$form.find('[type="submit"]').removeAttr('disabled');
    }
  }
}