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

  this.ProtectedBranchSelect = (function() {
    function ProtectedBranchSelect(currentProject) {
      this.toggleCreateNewButton = bind(this.toggleCreateNewButton, this);
      this.getProtectedBranches = bind(this.getProtectedBranches, this);
      $('.dropdown-footer').hide();
      this.dropdown = $('.js-protected-branch-select').glDropdown({
        data: this.getProtectedBranches,
        filterable: true,
        remote: false,
        search: {
          fields: ['title']
        },
        selectable: true,
        toggleLabel: function(selected) {
          if (selected && 'id' in selected) {
            return selected.title;
          } else {
            return 'Protected Branch';
          }
        },
        fieldName: 'protected_branch[name]',
        text: function(protected_branch) {
          return _.escape(protected_branch.title);
        },
        id: function(protected_branch) {
          return _.escape(protected_branch.id);
        },
        onFilter: this.toggleCreateNewButton,
        clicked: function() {
          return $('.protect-branch-btn').attr('disabled', false);
        }
      });
      $('.create-new-protected-branch').on('click', (function(_this) {
        return function(event) {
          _this.dropdown.data('glDropdown').remote.execute();
          return _this.dropdown.data('glDropdown').selectRowAtIndex(event, 0);
        };
      })(this));
    }

    ProtectedBranchSelect.prototype.getProtectedBranches = function(term, callback) {
      if (this.selectedBranch) {
        return callback(gon.open_branches.concat(this.selectedBranch));
      } else {
        return callback(gon.open_branches);
      }
    };

    ProtectedBranchSelect.prototype.toggleCreateNewButton = function(branchName) {
      this.selectedBranch = {
        title: branchName,
        id: branchName,
        text: branchName
      };
      if (branchName === '') {
        $('.protected-branch-select-footer-list').addClass('hidden');
        return $('.dropdown-footer').hide();
      } else {
        $('.create-new-protected-branch').text("Create Protected Branch: " + branchName);
        $('.protected-branch-select-footer-list').removeClass('hidden');
        return $('.dropdown-footer').show();
      }
    };

    return ProtectedBranchSelect;

  })();

}).call(this);