summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/clone_panel.js
blob: 79280c13f0f4c3a830904ce64b0560bde9bf32b6 (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
import $ from 'jquery';

export default function initClonePanel() {
  const $cloneOptions = $('ul.clone-options-dropdown');
  if ($cloneOptions.length) {
    const $cloneUrlField = $('#clone_url');
    const $cloneBtnLabel = $('.js-git-clone-holder .js-clone-dropdown-label');
    const mobileCloneField = document.querySelector(
      '.js-mobile-git-clone .js-clone-dropdown-label',
    );

    const selectedCloneOption = $cloneBtnLabel.text().trim();
    if (selectedCloneOption.length > 0) {
      $(`a:contains('${selectedCloneOption}')`, $cloneOptions).addClass('is-active');
    }

    $('a', $cloneOptions).on('click', (e) => {
      const $this = $(e.currentTarget);
      const url = $this.attr('href');
      if (
        url &&
        (url.startsWith('vscode://') ||
          url.startsWith('xcode://') ||
          url.startsWith('jetbrains://'))
      ) {
        // Clone with "..." should open like a normal link
        return;
      }
      e.preventDefault();
      const cloneType = $this.data('cloneType');

      $('.is-active', $cloneOptions).removeClass('is-active');
      $(`a[data-clone-type="${cloneType}"]`).each(function switchProtocol() {
        const $el = $(this);
        const activeText = $el.find('.dropdown-menu-inner-title').text();
        const $container = $el.closest('.js-git-clone-holder, .js-mobile-git-clone');
        const $label = $container.find('.js-clone-dropdown-label');

        $el.toggleClass('is-active');
        $label.text(activeText);
      });

      if (mobileCloneField) {
        mobileCloneField.dataset.clipboardText = url;
      } else {
        $cloneUrlField.val(url);
      }
      $('.js-git-empty .js-clone').text(url);
    });
  }
}