project.js 4.64 KB
Newer Older
1 2
/* eslint-disable func-names, no-var, no-return-assign, one-var,
 one-var-declaration-per-line, object-shorthand, vars-on-top */
3

4
import $ from 'jquery';
5
import Cookies from 'js-cookie';
Clement Ho's avatar
Clement Ho committed
6 7 8 9
import { __ } from '~/locale';
import { visitUrl } from '~/lib/utils/url_utility';
import axios from '~/lib/utils/axios_utils';
import flash from '~/flash';
10
import projectSelect from '../../project_select';
11

12 13 14 15 16
export default class Project {
  constructor() {
    const $cloneOptions = $('ul.clone-options-dropdown');
    const $projectCloneField = $('#project_clone');
    const $cloneBtnText = $('a.clone-dropdown-btn span');
Phil Hughes's avatar
Phil Hughes committed
17

18 19 20 21
    const selectedCloneOption = $cloneBtnText.text().trim();
    if (selectedCloneOption.length > 0) {
      $(`a:contains('${selectedCloneOption}')`, $cloneOptions).addClass('is-active');
    }
Phil Hughes's avatar
Phil Hughes committed
22

23 24 25
    $('a', $cloneOptions).on('click', (e) => {
      const $this = $(e.currentTarget);
      const url = $this.attr('href');
26
      const activeText = $this.find('.dropdown-menu-inner-title').text();
Phil Hughes's avatar
Phil Hughes committed
27

28
      e.preventDefault();
Phil Hughes's avatar
Phil Hughes committed
29

30 31 32
      $('.is-active', $cloneOptions).not($this).removeClass('is-active');
      $this.toggleClass('is-active');
      $projectCloneField.val(url);
33
      $cloneBtnText.text(activeText);
Phil Hughes's avatar
Phil Hughes committed
34

35 36 37
      $('#modal-geo-info').data({
        cloneUrlSecondary: $this.attr('href'),
        cloneUrlPrimary: $this.data('primaryUrl') || '',
38
      });
Fatih Acet's avatar
Fatih Acet committed
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
      return $('.clone').text(url);
    });
    // Ref switcher
    Project.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();
    });
    $('.hide-shared-runner-limit-message').on('click', function(e) {
      var $alert = $(this).parents('.shared-runner-quota-message');
      var scope = $alert.data('scope');
      Cookies.set('hide_shared_runner_quota_message', 'false', { path: scope });
      $alert.remove();
      e.preventDefault();
    });
    Project.projectSelectDropdown();
  }

  static projectSelectDropdown() {
68
    projectSelect();
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
    $('.project-item-select').on('click', e => Project.changeProject($(e.currentTarget).val()));
  }

  static changeProject(url) {
    return window.location = url;
  }

  static initRefSwitcher() {
    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({
Clement Ho's avatar
Clement Ho committed
87
        data(term, callback) {
Phil Hughes's avatar
Phil Hughes committed
88
          axios.get($dropdown.data('refsUrl'), {
Clement Ho's avatar
Clement Ho committed
89
            params: {
90 91 92
              ref: $dropdown.data('ref'),
              search: term,
            },
Clement Ho's avatar
Clement Ho committed
93 94 95
          })
          .then(({ data }) => callback(data))
          .catch(() => flash(__('An error occurred while getting projects')));
96 97 98 99 100
        },
        selectable: true,
        filterable: true,
        filterRemote: true,
        filterByText: true,
Phil Hughes's avatar
Phil Hughes committed
101 102
        inputFieldName: $dropdown.data('inputFieldName'),
        fieldName: $dropdown.data('fieldName'),
103 104 105 106 107 108 109 110 111 112 113
        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';
Fatih Acet's avatar
Fatih Acet committed
114
            }
115

116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
            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(options) {
          const { e } = options;
          e.preventDefault();
          if ($('input[name="ref"]').length) {
            var $form = $dropdown.closest('form');

            var $visit = $dropdown.data('visit');
            var shouldVisit = $visit ? true : $visit;
            var action = $form.attr('action');
            var divider = action.indexOf('?') === -1 ? '?' : '&';
            if (shouldVisit) {
141
              visitUrl(`${action}${divider}${$form.serialize()}`);
142
            }
Fatih Acet's avatar
Fatih Acet committed
143
          }
144
        },
Fatih Acet's avatar
Fatih Acet committed
145
      });
146 147 148
    });
  }
}