BigW Consortium Gitlab

project.js 4.36 KB
Newer Older
1
/* 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 */
2 3
/* global ProjectSelect */

4 5
import Cookies from 'js-cookie';

Fatih Acet committed
6 7 8 9 10 11 12 13 14 15 16 17 18
(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);
19 20 21 22 23
      // 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
Fatih Acet committed
24
      });
25
      // Ref switcher
Fatih Acet committed
26 27 28 29 30
      this.initRefSwitcher();
      $('.project-refs-select').on('change', function() {
        return $(this).parents('form').submit();
      });
      $('.hide-no-ssh-message').on('click', function(e) {
31
        Cookies.set('hide_no_ssh_message', 'false');
Fatih Acet committed
32 33 34 35
        $(this).parents('.no-ssh-key-message').remove();
        return e.preventDefault();
      });
      $('.hide-no-password-message').on('click', function(e) {
36
        Cookies.set('hide_no_password_message', 'false');
Fatih Acet committed
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
        $(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() {
61 62
      var refListItem = document.createElement('li');
      var refLink = document.createElement('a');
63 64 65

      refLink.href = '#';

Fatih Acet committed
66 67 68 69 70 71 72 73 74
      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: {
75 76
                ref: $dropdown.data('ref'),
                search: term
77 78
              },
              dataType: "json"
Fatih Acet committed
79
            }).done(function(refs) {
80
              console.log(refs);
Fatih Acet committed
81 82 83 84 85
              return callback(refs);
            });
          },
          selectable: true,
          filterable: true,
86
          filterRemote: true,
Fatih Acet committed
87
          filterByText: true,
88
          fieldName: $dropdown.data('field-name'),
Fatih Acet committed
89
          renderRow: function(ref) {
90
            var li = refListItem.cloneNode(false);
91

Fatih Acet committed
92
            if (ref.header != null) {
93 94
              li.className = 'dropdown-header';
              li.textContent = ref.header;
Fatih Acet committed
95
            } else {
96 97
              var link = refLink.cloneNode(false);

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

102 103
              link.textContent = ref;
              link.dataset.ref = ref;
104 105

              li.appendChild(link);
Fatih Acet committed
106
            }
107

108
            return li;
Fatih Acet committed
109 110 111 112 113 114 115
          },
          id: function(obj, $el) {
            return $el.attr('data-ref');
          },
          toggleLabel: function(obj, $el) {
            return $el.text().trim();
          },
116 117
          clicked: function(options) {
            const { e } = options;
118
            e.preventDefault();
119
            if ($('input[name="ref"]').length) {
120 121
              var $form = $dropdown.closest('form');
              var action = $form.attr('action');
122
              var divider = action.indexOf('?') === -1 ? '?' : '&';
Bryce Johnson committed
123
              gl.utils.visitUrl(action + '' + divider + '' + $form.serialize());
124
            }
Fatih Acet committed
125 126 127 128 129 130 131
          }
        });
      });
    };

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