2016-12-13 22:01:05 -05:00
|
|
|
/* eslint-disable func-names, space-before-function-paren, prefer-arrow-callback, comma-dangle, prefer-template, quotes, no-param-reassign, wrap-iife, max-len */
|
2017-05-19 17:22:46 -04:00
|
|
|
import Api from './api';
|
2016-12-13 22:01:05 -05:00
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
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();
|
|
|
|
}
|
2016-08-08 10:43:07 -04:00
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
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');
|
|
|
|
}
|
2016-08-08 10:43:07 -04:00
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
addBinding () {
|
|
|
|
const self = this;
|
2016-08-08 10:43:07 -04:00
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
this.$colorSuggestions.on('click', function (e) {
|
|
|
|
const $this = $(this);
|
|
|
|
self.addColorValue(e, $this);
|
|
|
|
});
|
2016-08-08 10:43:07 -04:00
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
this.$newLabelField.on('keyup change', this.enableLabelCreateButton.bind(this));
|
|
|
|
this.$newColorField.on('keyup change', this.enableLabelCreateButton.bind(this));
|
2016-08-08 10:43:07 -04:00
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
this.$dropdownBack.on('click', this.resetForm.bind(this));
|
2016-08-08 10:43:07 -04:00
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
this.$cancelButton.on('click', function(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
2016-08-08 10:43:07 -04:00
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
self.resetForm();
|
|
|
|
self.$dropdownBack.trigger('click');
|
|
|
|
});
|
2016-08-08 10:43:07 -04:00
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
this.$newLabelCreateButton.on('click', this.saveLabel.bind(this));
|
|
|
|
}
|
2016-08-08 10:43:07 -04:00
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
addColorValue (e, $this) {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
2016-08-08 10:43:07 -04:00
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
this.$newColorField.val($this.data('color')).trigger('change');
|
|
|
|
this.$colorPreview
|
|
|
|
.css('background-color', $this.data('color'))
|
|
|
|
.parent()
|
|
|
|
.addClass('is-active');
|
|
|
|
}
|
2016-08-08 10:43:07 -04:00
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
enableLabelCreateButton () {
|
|
|
|
if (this.$newLabelField.val() !== '' && this.$newColorField.val() !== '') {
|
|
|
|
this.$newLabelError.hide();
|
|
|
|
this.$newLabelCreateButton.enable();
|
|
|
|
} else {
|
|
|
|
this.$newLabelCreateButton.disable();
|
2016-08-08 10:43:07 -04:00
|
|
|
}
|
2017-03-11 02:30:44 -05:00
|
|
|
}
|
2016-08-08 10:43:07 -04:00
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
resetForm () {
|
|
|
|
this.$newLabelField
|
|
|
|
.val('')
|
|
|
|
.trigger('change');
|
2016-08-08 10:43:07 -04:00
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
this.$newColorField
|
|
|
|
.val('')
|
|
|
|
.trigger('change');
|
2016-08-08 10:43:07 -04:00
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
this.$colorPreview
|
|
|
|
.css('background-color', '')
|
|
|
|
.parent()
|
|
|
|
.removeClass('is-active');
|
|
|
|
}
|
2016-08-08 10:43:07 -04:00
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
saveLabel (e) {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
2016-08-08 10:43:07 -04:00
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
Api.newLabel(this.namespacePath, this.projectPath, {
|
|
|
|
title: this.$newLabelField.val(),
|
|
|
|
color: this.$newColorField.val()
|
|
|
|
}, (label) => {
|
|
|
|
this.$newLabelCreateButton.enable();
|
2016-08-08 10:43:07 -04:00
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
if (label.message) {
|
|
|
|
let errors;
|
2016-10-07 11:18:42 -04:00
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
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/>");
|
2016-08-08 10:43:07 -04:00
|
|
|
}
|
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
this.$newLabelError
|
|
|
|
.html(errors)
|
|
|
|
.show();
|
|
|
|
} else {
|
|
|
|
this.$dropdownBack.trigger('click');
|
|
|
|
|
|
|
|
$(document).trigger('created.label', label);
|
|
|
|
}
|
|
|
|
});
|
2016-08-08 10:43:07 -04:00
|
|
|
}
|
2017-03-11 02:30:44 -05:00
|
|
|
}
|
2016-08-08 10:43:07 -04:00
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
window.gl = window.gl || {};
|
|
|
|
gl.CreateLabelDropdown = CreateLabelDropdown;
|