Welcome to mirror list, hosted at ThFree Co, Russian Federation.

create_item_dropdown.js « javascripts « assets « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 75e8523adfa2f68594269693587c23cf1cb333f0 (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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import { escape } from 'lodash';
import initDeprecatedJQueryDropdown from '~/deprecated_jquery_dropdown';

export default class CreateItemDropdown {
  /**
   * @param {Object} options containing
   *                         `$dropdown` target element
   *                          `onSelect` event callback
   * $dropdown must be an element created using `dropdown_tag()` rails helper
   */
  constructor(options) {
    this.defaultToggleLabel = options.defaultToggleLabel;
    this.fieldName = options.fieldName;
    this.onSelect = options.onSelect || (() => {});
    this.getDataOption = options.getData;
    this.getDataRemote = Boolean(options.filterRemote);
    this.createNewItemFromValueOption = options.createNewItemFromValue;
    this.$dropdown = options.$dropdown;
    this.$dropdownContainer = this.$dropdown.parent();
    this.$dropdownFooter = this.$dropdownContainer.find('.dropdown-footer');
    this.$createButton = this.$dropdownContainer.find('.js-dropdown-create-new-item');

    this.buildDropdown();
    this.bindEvents();

    // Hide footer
    this.toggleFooter(true);
  }

  buildDropdown() {
    initDeprecatedJQueryDropdown(this.$dropdown, {
      data: this.getData.bind(this),
      filterable: true,
      filterRemote: this.getDataRemote,
      search: {
        fields: ['text'],
      },
      selectable: true,
      toggleLabel(selected) {
        return selected && 'id' in selected ? escape(selected.title) : this.defaultToggleLabel;
      },
      fieldName: this.fieldName,
      text(item) {
        return escape(item.text);
      },
      id(item) {
        return escape(item.id);
      },
      onFilter: this.toggleCreateNewButton.bind(this),
      clicked: options => {
        options.e.preventDefault();
        this.onSelect();
      },
    });
  }

  clearDropdown() {
    this.$dropdownContainer.find('.dropdown-content').html('');
    this.$dropdownContainer.find('.dropdown-input-field').val('');
  }

  bindEvents() {
    this.$createButton.on('click', this.onClickCreateWildcard.bind(this));
  }

  onClickCreateWildcard(e) {
    e.preventDefault();

    this.refreshData();
    this.$dropdown.data('deprecatedJQueryDropdown').selectRowAtIndex();
  }

  refreshData() {
    // Refresh the dropdown's data, which ends up calling `getData`
    this.$dropdown.data('deprecatedJQueryDropdown').remote.execute();
  }

  getData(term, callback) {
    this.getDataOption(term, (data = []) => {
      // Ensure the selected item isn't already in the data to avoid duplicates
      const alreadyHasSelectedItem =
        this.selectedItem && data.some(item => item.id === this.selectedItem.id);

      let uniqueData = data;
      if (!alreadyHasSelectedItem) {
        uniqueData = data.concat(this.selectedItem || []);
      }

      callback(uniqueData);
    });
  }

  createNewItemFromValue(newValue) {
    if (this.createNewItemFromValueOption) {
      return this.createNewItemFromValueOption(newValue);
    }

    return {
      title: newValue,
      id: newValue,
      text: newValue,
    };
  }

  toggleCreateNewButton(newValue) {
    if (newValue) {
      this.selectedItem = this.createNewItemFromValue(newValue);

      this.$dropdownContainer.find('.js-dropdown-create-new-item code').text(newValue);
    }

    this.toggleFooter(!newValue);
  }

  toggleFooter(toggleState) {
    this.$dropdownFooter.toggleClass('hidden', toggleState);
  }
}