2017-10-09 05:04:13 -04:00
|
|
|
/* eslint-disable func-names, prefer-arrow-callback */
|
2018-03-09 15:18:59 -05:00
|
|
|
|
|
|
|
import $ from 'jquery';
|
2017-05-19 17:22:46 -04:00
|
|
|
import Api from './api';
|
2017-11-13 04:11:54 -05:00
|
|
|
import { humanize } from './lib/utils/text_utility';
|
2016-12-13 22:01:05 -05:00
|
|
|
|
2017-10-09 05:04:13 -04:00
|
|
|
export default class CreateLabelDropdown {
|
|
|
|
constructor($el, namespacePath, projectPath) {
|
2017-03-11 02:30:44 -05:00
|
|
|
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);
|
2019-04-05 11:50:21 -04:00
|
|
|
this.$addList = $('.js-add-list', this.$el);
|
2017-03-11 02:30:44 -05:00
|
|
|
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();
|
|
|
|
|
2019-04-05 11:50:21 -04:00
|
|
|
this.addListDefault = this.$addList.is(':checked');
|
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
this.cleanBinding();
|
|
|
|
this.addBinding();
|
|
|
|
}
|
2016-08-08 10:43:07 -04:00
|
|
|
|
2017-10-09 05:04:13 -04:00
|
|
|
cleanBinding() {
|
2017-03-11 02:30:44 -05:00
|
|
|
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-10-09 05:04:13 -04:00
|
|
|
addBinding() {
|
2017-03-11 02:30:44 -05:00
|
|
|
const self = this;
|
2016-08-08 10:43:07 -04:00
|
|
|
|
2018-10-24 15:17:03 -04:00
|
|
|
this.$colorSuggestions.on('click', function(e) {
|
2017-03-11 02:30:44 -05:00
|
|
|
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
|
|
|
|
2018-10-24 15:17:03 -04:00
|
|
|
this.$cancelButton.on('click', function(e) {
|
2017-03-11 02:30:44 -05:00
|
|
|
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-10-09 05:04:13 -04:00
|
|
|
addColorValue(e, $this) {
|
2017-03-11 02:30:44 -05:00
|
|
|
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-10-09 05:04:13 -04:00
|
|
|
enableLabelCreateButton() {
|
2017-03-11 02:30:44 -05:00
|
|
|
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-10-09 05:04:13 -04:00
|
|
|
resetForm() {
|
2018-10-24 15:17:03 -04:00
|
|
|
this.$newLabelField.val('').trigger('change');
|
2016-08-08 10:43:07 -04:00
|
|
|
|
2018-10-24 15:17:03 -04:00
|
|
|
this.$newColorField.val('').trigger('change');
|
2016-08-08 10:43:07 -04:00
|
|
|
|
2019-04-05 11:50:21 -04:00
|
|
|
this.$addList.prop('checked', this.addListDefault);
|
|
|
|
|
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-10-09 05:04:13 -04:00
|
|
|
saveLabel(e) {
|
2017-03-11 02:30:44 -05:00
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
2016-08-08 10:43:07 -04:00
|
|
|
|
2018-10-24 15:17:03 -04:00
|
|
|
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 => `${humanize(key)} ${label.message[key].join(', ')}`)
|
|
|
|
.join('<br/>');
|
|
|
|
}
|
|
|
|
|
|
|
|
this.$newLabelError.html(errors).show();
|
2017-03-11 02:30:44 -05:00
|
|
|
} else {
|
2019-04-05 11:50:21 -04:00
|
|
|
const addNewList = this.$addList.is(':checked');
|
2018-10-24 15:17:03 -04:00
|
|
|
this.$dropdownBack.trigger('click');
|
2019-04-05 11:50:21 -04:00
|
|
|
$(document).trigger('created.label', [label, addNewList]);
|
2018-10-24 15:17:03 -04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
2016-08-08 10:43:07 -04:00
|
|
|
}
|
2017-03-11 02:30:44 -05:00
|
|
|
}
|