summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/project.js
blob: 7c03c8b72d4b15a11ee6e2642b9af2b711712706 (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
119
120
121
122
123
124
125
126
127
128
129
/* eslint-disable func-names, space-before-function-paren, wrap-iife, no-var, quotes, consistent-return, no-new, prefer-arrow-callback, no-return-assign, one-var, one-var-declaration-per-line, object-shorthand, comma-dangle, no-else-return, newline-per-chained-call, no-shadow, vars-on-top, prefer-template, max-len */
/* global Cookies */
/* global ProjectSelect */

(function() {
  this.Project = (function() {
    function Project() {
      $('ul.clone-options-dropdown a').click(function() {
        var url;
        if ($(this).hasClass('active')) {
          return;
        }
        $('.active').not($(this)).removeClass('active');
        $(this).toggleClass('active');
        url = $("#project_clone").val();
        $('#project_clone').val(url);
        return $('.clone').text(url);
      // Git protocol switcher
      // Remove the active class for all buttons (ssh, http, kerberos if shown)
      // Add the active class for the clicked button
      // Update the input field
      // Update the command line instructions
      });
      // Ref switcher
      this.initRefSwitcher();
      $('.project-refs-select').on('change', function() {
        return $(this).parents('form').submit();
      });
      $('.hide-no-ssh-message').on('click', function(e) {
        Cookies.set('hide_no_ssh_message', 'false');
        $(this).parents('.no-ssh-key-message').remove();
        return e.preventDefault();
      });
      $('.hide-no-password-message').on('click', function(e) {
        Cookies.set('hide_no_password_message', 'false');
        $(this).parents('.no-password-message').remove();
        return e.preventDefault();
      });
      this.projectSelectDropdown();
    }

    Project.prototype.projectSelectDropdown = function() {
      new ProjectSelect();
      $('.project-item-select').on('click', (function(_this) {
        return function(e) {
          return _this.changeProject($(e.currentTarget).val());
        };
      })(this));
      return $('.js-projects-dropdown-toggle').on('click', function(e) {
        e.preventDefault();
        return $('.js-projects-dropdown').select2('open');
      });
    };

    Project.prototype.changeProject = function(url) {
      return window.location = url;
    };

    Project.prototype.initRefSwitcher = function() {
      var refListItem = document.createElement('li');
      var refLink = document.createElement('a');

      refLink.href = '#';

      return $('.js-project-refs-dropdown').each(function() {
        var $dropdown, selected;
        $dropdown = $(this);
        selected = $dropdown.data('selected');
        return $dropdown.glDropdown({
          data: function(term, callback) {
            return $.ajax({
              url: $dropdown.data('refs-url'),
              data: {
                ref: $dropdown.data('ref'),
                search: term
              },
              dataType: "json"
            }).done(function(refs) {
              return callback(refs);
            });
          },
          selectable: true,
          filterable: true,
          filterRemote: true,
          filterByText: true,
          fieldName: $dropdown.data('field-name'),
          renderRow: function(ref) {
            var li = refListItem.cloneNode(false);

            if (ref.header != null) {
              li.className = 'dropdown-header';
              li.textContent = ref.header;
            } else {
              var link = refLink.cloneNode(false);

              if (ref === selected) {
                link.className = 'is-active';
              }

              link.textContent = ref;
              link.dataset.ref = ref;

              li.appendChild(link);
            }

            return li;
          },
          id: function(obj, $el) {
            return $el.attr('data-ref');
          },
          toggleLabel: function(obj, $el) {
            return $el.text().trim();
          },
          clicked: function(selected, $el, e) {
            e.preventDefault();
            if ($('input[name="ref"]').length) {
              var $form = $dropdown.closest('form');
              var action = $form.attr('action');
              var divider = action.indexOf('?') < 0 ? '?' : '&';
              gl.utils.visitUrl(action + '' + divider + '' + $form.serialize());
            }
          }
        });
      });
    };

    return Project;
  })();
}).call(window);