BigW Consortium Gitlab

project.js 4.71 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
(function() {
  this.Project = (function() {
    function Project() {
9 10 11 12
      const $cloneOptions = $('ul.clone-options-dropdown');
      const $projectCloneField = $('#project_clone');
      const $cloneBtnText = $('a.clone-dropdown-btn span');

13 14 15 16 17
      const selectedCloneOption = $cloneBtnText.text().trim();
      if (selectedCloneOption.length > 0) {
        $(`a:contains('${selectedCloneOption}')`, $cloneOptions).addClass('is-active');
      }

18 19 20 21 22 23
      $('a', $cloneOptions).on('click', (e) => {
        const $this = $(e.currentTarget);
        const url = $this.attr('href');

        e.preventDefault();

24 25
        $('.is-active', $cloneOptions).not($this).removeClass('is-active');
        $this.toggleClass('is-active');
26 27 28
        $projectCloneField.val(url);
        $cloneBtnText.text($this.text());

Fatih Acet committed
29 30
        return $('.clone').text(url);
      });
31
      // Ref switcher
Fatih Acet committed
32 33 34 35 36
      this.initRefSwitcher();
      $('.project-refs-select').on('change', function() {
        return $(this).parents('form').submit();
      });
      $('.hide-no-ssh-message').on('click', function(e) {
37
        Cookies.set('hide_no_ssh_message', 'false');
Fatih Acet committed
38 39 40 41
        $(this).parents('.no-ssh-key-message').remove();
        return e.preventDefault();
      });
      $('.hide-no-password-message').on('click', function(e) {
42
        Cookies.set('hide_no_password_message', 'false');
Fatih Acet committed
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
        $(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() {
67 68
      var refListItem = document.createElement('li');
      var refLink = document.createElement('a');
69 70 71

      refLink.href = '#';

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

Fatih Acet committed
98
            if (ref.header != null) {
99 100
              li.className = 'dropdown-header';
              li.textContent = ref.header;
Fatih Acet committed
101
            } else {
102 103
              var link = refLink.cloneNode(false);

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

108 109
              link.textContent = ref;
              link.dataset.ref = ref;
110 111

              li.appendChild(link);
Fatih Acet committed
112
            }
113

114
            return li;
Fatih Acet committed
115 116 117 118 119 120 121
          },
          id: function(obj, $el) {
            return $el.attr('data-ref');
          },
          toggleLabel: function(obj, $el) {
            return $el.text().trim();
          },
122 123
          clicked: function(options) {
            const { e } = options;
124
            e.preventDefault();
125
            if ($('input[name="ref"]').length) {
126
              var $form = $dropdown.closest('form');
127 128

              var $visit = $dropdown.data('visit');
Jacob Schatz committed
129
              var shouldVisit = $visit ? true : $visit;
130
              var action = $form.attr('action');
131
              var divider = action.indexOf('?') === -1 ? '?' : '&';
132
              if (shouldVisit) {
Bryce Johnson committed
133
                gl.utils.visitUrl(`${action}${divider}${$form.serialize()}`);
134
              }
135
            }
Fatih Acet committed
136 137 138 139 140 141 142
          }
        });
      });
    };

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