Skip to content
Snippets Groups Projects
Select Git revision
  • move-gl-dropdown
  • improve-table-pagination-spec
  • move-markdown-preview
  • winh-fix-merge-request-spec
  • master default
  • index-namespaces-lower-name
  • winh-single-karma-test
  • 10-3-stable
  • 36782-replace-team-user-role-with-add_role-user-in-specs
  • winh-modal-internal-state
  • tz-ide-file-icons
  • 38869-milestone-select
  • update-autodevops-template
  • jivl-activate-repo-cookie-preferences
  • qa-add-deploy-key
  • docs-move-article-ldap
  • 40780-choose-file
  • 22643-manual-job-page
  • refactor-cluster-show-page-conservative
  • dm-sidekiq-versioning
  • v10.4.0.pre
  • v10.3.0
  • v10.3.0-rc5
  • v10.3.0-rc4
  • v10.3.0-rc3
  • v10.3.0-rc2
  • v10.2.5
  • v10.3.0-rc1
  • v10.0.7
  • v10.1.5
  • v10.2.4
  • v10.2.3
  • v10.2.2
  • v10.2.1
  • v10.3.0.pre
  • v10.2.0
  • v10.2.0-rc4
  • v10.2.0-rc3
  • v10.1.4
  • v10.2.0-rc2
40 results

create_label.js

Forked from GitLab.org / GitLab FOSS
2 commits behind, 5108 commits ahead of the upstream repository.
create_label.js 3.53 KiB
/* eslint-disable func-names, space-before-function-paren, prefer-arrow-callback, comma-dangle, prefer-template, quotes, no-param-reassign, wrap-iife, max-len */
import Api from './api';

class CreateLabelDropdown {
  constructor ($el, namespacePath, projectPath) {
    this.$el = $el;
    this.namespacePath = namespacePath;
    this.projectPath = projectPath;
    this.$dropdownBack = $('.dropdown-menu-back', this.$el.closest('.dropdown'));
    this.$cancelButton = $('.js-cancel-label-btn', this.$el);
    this.$newLabelField = $('#new_label_name', this.$el);
    this.$newColorField = $('#new_label_color', this.$el);
    this.$colorPreview = $('.js-dropdown-label-color-preview', this.$el);
    this.$newLabelError = $('.js-label-error', this.$el);
    this.$newLabelCreateButton = $('.js-new-label-btn', this.$el);
    this.$colorSuggestions = $('.suggest-colors-dropdown a', this.$el);

    this.$newLabelError.hide();
    this.$newLabelCreateButton.disable();

    this.cleanBinding();
    this.addBinding();
  }

  cleanBinding () {
    this.$colorSuggestions.off('click');
    this.$newLabelField.off('keyup change');
    this.$newColorField.off('keyup change');
    this.$dropdownBack.off('click');
    this.$cancelButton.off('click');
    this.$newLabelCreateButton.off('click');
  }

  addBinding () {
    const self = this;

    this.$colorSuggestions.on('click', function (e) {
      const $this = $(this);
      self.addColorValue(e, $this);
    });

    this.$newLabelField.on('keyup change', this.enableLabelCreateButton.bind(this));
    this.$newColorField.on('keyup change', this.enableLabelCreateButton.bind(this));

    this.$dropdownBack.on('click', this.resetForm.bind(this));

    this.$cancelButton.on('click', function(e) {
      e.preventDefault();
      e.stopPropagation();

      self.resetForm();
      self.$dropdownBack.trigger('click');
    });

    this.$newLabelCreateButton.on('click', this.saveLabel.bind(this));
  }

  addColorValue (e, $this) {
    e.preventDefault();
    e.stopPropagation();

    this.$newColorField.val($this.data('color')).trigger('change');
    this.$colorPreview
      .css('background-color', $this.data('color'))
      .parent()
      .addClass('is-active');
  }

  enableLabelCreateButton () {
    if (this.$newLabelField.val() !== '' && this.$newColorField.val() !== '') {
      this.$newLabelError.hide();
      this.$newLabelCreateButton.enable();
    } else {
      this.$newLabelCreateButton.disable();
    }
  }

  resetForm () {
    this.$newLabelField
      .val('')
      .trigger('change');

    this.$newColorField
      .val('')
      .trigger('change');

    this.$colorPreview
      .css('background-color', '')
      .parent()
      .removeClass('is-active');
  }

  saveLabel (e) {
    e.preventDefault();
    e.stopPropagation();

    Api.newLabel(this.namespacePath, this.projectPath, {
      title: this.$newLabelField.val(),
      color: this.$newColorField.val()
    }, (label) => {
      this.$newLabelCreateButton.enable();

      if (label.message) {
        let errors;

        if (typeof label.message === 'string') {
          errors = label.message;
        } else {
          errors = Object.keys(label.message).map(key =>
            `${gl.text.humanize(key)} ${label.message[key].join(', ')}`
          ).join("<br/>");
        }

        this.$newLabelError
          .html(errors)
          .show();
      } else {
        this.$dropdownBack.trigger('click');

        $(document).trigger('created.label', label);
      }
    });
  }
}

window.gl = window.gl || {};
gl.CreateLabelDropdown = CreateLabelDropdown;