debian-mirror-gitlab/app/assets/javascripts/create_label.js

128 lines
3.4 KiB
JavaScript
Raw Normal View History

2018-03-17 18:26:18 +05:30
/* eslint-disable func-names, prefer-arrow-callback */
2018-05-09 12:01:36 +05:30
import $ from 'jquery';
2017-09-10 17:25:29 +05:30
import Api from './api';
2018-03-17 18:26:18 +05:30
import { humanize } from './lib/utils/text_utility';
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
export default class CreateLabelDropdown {
constructor($el, namespacePath, projectPath) {
2017-08-17 22:00:37 +05:30
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();
}
2018-03-17 18:26:18 +05:30
cleanBinding() {
2017-08-17 22:00:37 +05:30
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');
}
2018-03-17 18:26:18 +05:30
addBinding() {
2017-08-17 22:00:37 +05:30
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));
2018-03-17 18:26:18 +05:30
this.$cancelButton.on('click', function (e) {
2017-08-17 22:00:37 +05:30
e.preventDefault();
e.stopPropagation();
self.resetForm();
self.$dropdownBack.trigger('click');
});
this.$newLabelCreateButton.on('click', this.saveLabel.bind(this));
}
2018-03-17 18:26:18 +05:30
addColorValue(e, $this) {
2017-08-17 22:00:37 +05:30
e.preventDefault();
e.stopPropagation();
this.$newColorField.val($this.data('color')).trigger('change');
this.$colorPreview
.css('background-color', $this.data('color'))
.parent()
.addClass('is-active');
}
2018-03-17 18:26:18 +05:30
enableLabelCreateButton() {
2017-08-17 22:00:37 +05:30
if (this.$newLabelField.val() !== '' && this.$newColorField.val() !== '') {
this.$newLabelError.hide();
this.$newLabelCreateButton.enable();
} else {
this.$newLabelCreateButton.disable();
}
}
2018-03-17 18:26:18 +05:30
resetForm() {
2017-08-17 22:00:37 +05:30
this.$newLabelField
.val('')
.trigger('change');
this.$newColorField
.val('')
.trigger('change');
this.$colorPreview
.css('background-color', '')
.parent()
.removeClass('is-active');
}
2018-03-17 18:26:18 +05:30
saveLabel(e) {
2017-08-17 22:00:37 +05:30
e.preventDefault();
e.stopPropagation();
Api.newLabel(this.namespacePath, this.projectPath, {
title: this.$newLabelField.val(),
2018-03-17 18:26:18 +05:30
color: this.$newColorField.val(),
2017-08-17 22:00:37 +05:30
}, (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 =>
2018-03-17 18:26:18 +05:30
`${humanize(key)} ${label.message[key].join(', ')}`,
).join('<br/>');
2017-08-17 22:00:37 +05:30
}
this.$newLabelError
.html(errors)
.show();
} else {
this.$dropdownBack.trigger('click');
$(document).trigger('created.label', label);
}
});
}
}