summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/blob/template_selector.js
blob: b0a37ef0e0a4f57b1b76b51ab24c3f63e48bc432 (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
(function() {
  var bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };

  this.TemplateSelector = (function() {
    function TemplateSelector(opts) {
      var ref;
      if (opts == null) {
        opts = {};
      }
      this.onClick = bind(this.onClick, this);
      this.dropdown = opts.dropdown, this.data = opts.data, this.pattern = opts.pattern, this.wrapper = opts.wrapper, this.editor = opts.editor, this.fileEndpoint = opts.fileEndpoint, this.$input = (ref = opts.$input) != null ? ref : $('#file_name');
      this.dropdownIcon = $('.fa-chevron-down', this.dropdown);
      this.buildDropdown();
      this.bindEvents();
      this.onFilenameUpdate();
    }

    TemplateSelector.prototype.buildDropdown = function() {
      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;
        }
      });
    };

    TemplateSelector.prototype.bindEvents = function() {
      return this.$input.on('keyup blur', (function(_this) {
        return function(e) {
          return _this.onFilenameUpdate();
        };
      })(this));
    };

    TemplateSelector.prototype.toggleLabel = function(item) {
      return item.name;
    };

    TemplateSelector.prototype.onFilenameUpdate = function() {
      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');
    };

    TemplateSelector.prototype.onClick = function(item, el, e) {
      e.preventDefault();
      return this.requestFile(item);
    };

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

    TemplateSelector.prototype.requestFileSuccess = function(file, skipFocus) {
      this.editor.setValue(file.content, 1);
      if (!skipFocus) this.editor.focus();
    };

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

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

    return TemplateSelector;

  })();

}).call(this);