project.js 4.0 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 4 5
/* global Cookies */
/* global Turbolinks */
/* global ProjectSelect */

F
Fatih Acet 已提交
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
F
Fatih Acet 已提交
24
      });
25
      // Ref switcher
F
Fatih Acet 已提交
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');
F
Fatih Acet 已提交
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');
F
Fatih Acet 已提交
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
        $(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() {
      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')
71 72
              },
              dataType: "json"
F
Fatih Acet 已提交
73 74 75 76 77 78 79
            }).done(function(refs) {
              return callback(refs);
            });
          },
          selectable: true,
          filterable: true,
          filterByText: true,
80
          fieldName: $dropdown.data('field-name'),
F
Fatih Acet 已提交
81 82 83 84 85
          renderRow: function(ref) {
            var link;
            if (ref.header != null) {
              return $('<li />').addClass('dropdown-header').text(ref.header);
            } else {
86
              link = $('<a />').attr('href', '#').addClass(ref === selected ? 'is-active' : '').text(ref).attr('data-ref', ref);
F
Fatih Acet 已提交
87 88 89 90 91 92 93 94 95
              return $('<li />').append(link);
            }
          },
          id: function(obj, $el) {
            return $el.attr('data-ref');
          },
          toggleLabel: function(obj, $el) {
            return $el.text().trim();
          },
96
          clicked: function(selected, $el, e) {
97
            e.preventDefault();
98
            if ($('input[name="ref"]').length) {
99 100 101
              var $form = $dropdown.closest('form');
              var action = $form.attr('action');
              var divider = action.indexOf('?') < 0 ? '?' : '&';
102 103
              Turbolinks.visit(action + '' + divider + '' + $form.serialize());
            }
F
Fatih Acet 已提交
104 105 106 107 108 109 110 111
          }
        });
      });
    };

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