summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/blob/template_selector.js.es6
blob: 5434a19bcecf3b68237ae77fa9ca115ba03bf674 (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
/* eslint-disable */
((global) => {
    class TemplateSelector {
      constructor({ dropdown, data, pattern, wrapper, editor, fileEndpoint, $input } = {}) {
        this.onClick = this.onClick.bind(this);
        this.dropdown = dropdown;
        this.data = data;
        this.pattern = pattern;
        this.wrapper = wrapper;
        this.editor = editor;
        this.fileEndpoint = fileEndpoint;
        this.$input = $input || $('#file_name');
        this.dropdownIcon = $('.fa-chevron-down', this.dropdown);
        this.buildDropdown();
        this.bindEvents();
        this.onFilenameUpdate();

        this.autosizeUpdateEvent = document.createEvent('Event');
        this.autosizeUpdateEvent.initEvent('autosize:update', true, false);
      }

      buildDropdown() {
        return this.dropdown.glDropdown({
          data: this.data,
          filterable: true,
          selectable: true,
          toggleLabel: this.toggleLabel,
          search: {
            fields: ['name']
          },
          clicked: this.onClick,
          text: function(item) {
            return item.name;
          }
        });
      }

      bindEvents() {
        return this.$input.on('keyup blur', (e) => this.onFilenameUpdate());
      }

      toggleLabel(item) {
        return item.name;
      }

      onFilenameUpdate() {
        var filenameMatches;
        if (!this.$input.length) {
          return;
        }
        filenameMatches = this.pattern.test(this.$input.val().trim());
        if (!filenameMatches) {
          this.wrapper.addClass('hidden');
          return;
        }
        return this.wrapper.removeClass('hidden');
      }

      onClick(item, el, e) {
        e.preventDefault();
        return this.requestFile(item);
      }

      requestFile(item) {
        // This `requestFile` method is an abstract method that should
        // be added by all subclasses.
      }

      // To be implemented on the extending class
      // e.g.
      // Api.gitignoreText item.name, @requestFileSuccess.bind(@)
      requestFileSuccess(file, { skipFocus } = {}) {
        const oldValue = this.editor.getValue();
        let newValue = file.content;

        this.editor.setValue(newValue, 1);
        if (!skipFocus) this.editor.focus();

        if (this.editor instanceof jQuery) {
          this.editor.get(0).dispatchEvent(this.autosizeUpdateEvent);
        }
      }

      startLoadingSpinner() {
        this.dropdownIcon
          .addClass('fa-spinner fa-spin')
          .removeClass('fa-chevron-down');
      }

      stopLoadingSpinner() {
        this.dropdownIcon
          .addClass('fa-chevron-down')
          .removeClass('fa-spinner fa-spin');
      }
    }

    global.TemplateSelector = TemplateSelector;
  })(window.gl || ( window.gl = {}));