summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/protected_tags/protected_tag_create.js
blob: 91bd140bd12665a0af8e5a57aa559ed1a5fb9a25 (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
import ProtectedTagAccessDropdown from './protected_tag_access_dropdown';
import ProtectedTagDropdown from './protected_tag_dropdown';

export default class ProtectedTagCreate {
  constructor() {
    this.$form = $('.js-new-protected-tag');
    this.buildDropdowns();
  }

  buildDropdowns() {
    const $allowedToCreateDropdown = this.$form.find('.js-allowed-to-create');

    // Cache callback
    this.onSelectCallback = this.onSelect.bind(this);

    // Allowed to Create dropdown
    this.protectedTagAccessDropdown = new ProtectedTagAccessDropdown({
      $dropdown: $allowedToCreateDropdown,
      data: gon.create_access_levels,
      onSelect: this.onSelectCallback,
    });

    // Select default
    $allowedToCreateDropdown.data('glDropdown').selectRowAtIndex(0);

    // Protected tag dropdown
    this.protectedTagDropdown = new ProtectedTagDropdown({
      $dropdown: this.$form.find('.js-protected-tag-select'),
      onSelect: this.onSelectCallback,
    });
  }

  // This will run after clicked callback
  onSelect() {
    // Enable submit button
    const $tagInput = this.$form.find('input[name="protected_tag[name]"]');
    const $allowedToCreateInput = this.$form.find('#create_access_levels_attributes');

    this.$form.find('input[type="submit"]').attr('disabled', !($tagInput.val() && $allowedToCreateInput.length));
  }
}