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

470 lines
14 KiB
JavaScript
Raw Normal View History

2017-08-17 22:00:37 +05:30
/* eslint-disable no-new */
2018-03-17 18:26:18 +05:30
import _ from 'underscore';
import axios from './lib/utils/axios_utils';
import Flash from './flash';
2017-08-17 22:00:37 +05:30
import DropLab from './droplab/drop_lab';
import ISetter from './droplab/plugins/input_setter';
2018-03-17 18:26:18 +05:30
import { __, sprintf } from './locale';
2017-08-17 22:00:37 +05:30
// Todo: Remove this when fixing issue in input_setter plugin
const InputSetter = Object.assign({}, ISetter);
const CREATE_MERGE_REQUEST = 'create-mr';
const CREATE_BRANCH = 'create-branch';
export default class CreateMergeRequestDropdown {
constructor(wrapperEl) {
this.wrapperEl = wrapperEl;
2018-03-17 18:26:18 +05:30
this.availableButton = this.wrapperEl.querySelector('.available');
this.branchInput = this.wrapperEl.querySelector('.js-branch-name');
this.branchMessage = this.wrapperEl.querySelector('.js-branch-message');
2017-08-17 22:00:37 +05:30
this.createMergeRequestButton = this.wrapperEl.querySelector('.js-create-merge-request');
2018-03-17 18:26:18 +05:30
this.createTargetButton = this.wrapperEl.querySelector('.js-create-target');
2017-08-17 22:00:37 +05:30
this.dropdownList = this.wrapperEl.querySelector('.dropdown-menu');
2018-03-17 18:26:18 +05:30
this.dropdownToggle = this.wrapperEl.querySelector('.js-dropdown-toggle');
this.refInput = this.wrapperEl.querySelector('.js-ref');
this.refMessage = this.wrapperEl.querySelector('.js-ref-message');
2017-08-17 22:00:37 +05:30
this.unavailableButton = this.wrapperEl.querySelector('.unavailable');
this.unavailableButtonArrow = this.unavailableButton.querySelector('.fa');
this.unavailableButtonText = this.unavailableButton.querySelector('.text');
2018-03-17 18:26:18 +05:30
this.branchCreated = false;
this.branchIsValid = true;
2017-08-17 22:00:37 +05:30
this.canCreatePath = this.wrapperEl.dataset.canCreatePath;
2018-03-17 18:26:18 +05:30
this.createBranchPath = this.wrapperEl.dataset.createBranchPath;
2017-08-17 22:00:37 +05:30
this.createMrPath = this.wrapperEl.dataset.createMrPath;
this.droplabInitialized = false;
2018-03-17 18:26:18 +05:30
this.isCreatingBranch = false;
2017-08-17 22:00:37 +05:30
this.isCreatingMergeRequest = false;
2018-03-17 18:26:18 +05:30
this.isGettingRef = false;
2017-08-17 22:00:37 +05:30
this.mergeRequestCreated = false;
2018-03-17 18:26:18 +05:30
this.refDebounce = _.debounce((value, target) => this.getRef(value, target), 500);
this.refIsValid = true;
this.refsPath = this.wrapperEl.dataset.refsPath;
this.suggestedRef = this.refInput.value;
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
// These regexps are used to replace
// a backend generated new branch name and its source (ref)
// with user's inputs.
this.regexps = {
branch: {
createBranchPath: new RegExp('(branch_name=)(.+?)(?=&issue)'),
createMrPath: new RegExp('(branch_name=)(.+?)(?=&ref)'),
},
ref: {
createBranchPath: new RegExp('(ref=)(.+?)$'),
createMrPath: new RegExp('(ref=)(.+?)$'),
},
};
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
this.init();
2017-08-17 22:00:37 +05:30
}
available() {
this.availableButton.classList.remove('hide');
this.unavailableButton.classList.add('hide');
}
2018-03-17 18:26:18 +05:30
bindEvents() {
this.createMergeRequestButton.addEventListener('click', this.onClickCreateMergeRequestButton.bind(this));
this.createTargetButton.addEventListener('click', this.onClickCreateMergeRequestButton.bind(this));
this.branchInput.addEventListener('keyup', this.onChangeInput.bind(this));
this.dropdownToggle.addEventListener('click', this.onClickSetFocusOnBranchNameInput.bind(this));
this.refInput.addEventListener('keyup', this.onChangeInput.bind(this));
this.refInput.addEventListener('keydown', CreateMergeRequestDropdown.processTab.bind(this));
}
checkAbilityToCreateBranch() {
this.setUnavailableButtonState();
axios.get(this.canCreatePath)
.then(({ data }) => {
this.setUnavailableButtonState(false);
if (data.can_create_branch) {
this.available();
this.enable();
2018-10-15 14:42:47 +05:30
this.updateBranchName(data.suggested_branch_name);
2018-03-17 18:26:18 +05:30
if (!this.droplabInitialized) {
this.droplabInitialized = true;
this.initDroplab();
this.bindEvents();
}
2018-10-15 14:42:47 +05:30
} else {
2018-03-17 18:26:18 +05:30
this.hide();
}
})
.catch(() => {
this.unavailable();
this.disable();
2018-10-15 14:42:47 +05:30
Flash(__('Failed to check related branches.'));
2018-03-17 18:26:18 +05:30
});
}
createBranch() {
this.isCreatingBranch = true;
return axios.post(this.createBranchPath)
.then(({ data }) => {
this.branchCreated = true;
window.location.href = data.url;
})
.catch(() => Flash('Failed to create a branch for this issue. Please try again.'));
}
createMergeRequest() {
this.isCreatingMergeRequest = true;
return axios.post(this.createMrPath)
.then(({ data }) => {
this.mergeRequestCreated = true;
window.location.href = data.url;
})
.catch(() => Flash('Failed to create Merge Request. Please try again.'));
}
disable() {
this.disableCreateAction();
this.dropdownToggle.classList.add('disabled');
this.dropdownToggle.setAttribute('disabled', 'disabled');
}
disableCreateAction() {
this.createMergeRequestButton.classList.add('disabled');
this.createMergeRequestButton.setAttribute('disabled', 'disabled');
this.createTargetButton.classList.add('disabled');
this.createTargetButton.setAttribute('disabled', 'disabled');
2017-08-17 22:00:37 +05:30
}
enable() {
this.createMergeRequestButton.classList.remove('disabled');
this.createMergeRequestButton.removeAttribute('disabled');
2018-03-17 18:26:18 +05:30
this.createTargetButton.classList.remove('disabled');
this.createTargetButton.removeAttribute('disabled');
2017-08-17 22:00:37 +05:30
this.dropdownToggle.classList.remove('disabled');
this.dropdownToggle.removeAttribute('disabled');
}
2018-03-17 18:26:18 +05:30
static findByValue(objects, ref, returnFirstMatch = false) {
if (!objects || !objects.length) return false;
if (objects.indexOf(ref) > -1) return ref;
if (returnFirstMatch) return objects.find(item => new RegExp(`^${ref}`).test(item));
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
return false;
2017-08-17 22:00:37 +05:30
}
2018-03-17 18:26:18 +05:30
getDroplabConfig() {
return {
addActiveClassToDropdownButton: true,
InputSetter: [
{
input: this.createMergeRequestButton,
valueAttribute: 'data-value',
inputAttribute: 'data-action',
},
{
input: this.createMergeRequestButton,
valueAttribute: 'data-text',
},
{
input: this.createTargetButton,
valueAttribute: 'data-value',
inputAttribute: 'data-action',
},
{
input: this.createTargetButton,
valueAttribute: 'data-text',
},
],
hideOnClick: false,
};
2017-08-17 22:00:37 +05:30
}
2018-03-17 18:26:18 +05:30
static getInputSelectedText(input) {
const start = input.selectionStart;
const end = input.selectionEnd;
return input.value.substr(start, end - start);
2017-08-17 22:00:37 +05:30
}
2018-03-17 18:26:18 +05:30
getRef(ref, target = 'all') {
if (!ref) return false;
return axios.get(this.refsPath + ref)
.then(({ data }) => {
const branches = data[Object.keys(data)[0]];
const tags = data[Object.keys(data)[1]];
let result;
if (target === 'branch') {
result = CreateMergeRequestDropdown.findByValue(branches, ref);
} else {
result = CreateMergeRequestDropdown.findByValue(branches, ref, true) ||
CreateMergeRequestDropdown.findByValue(tags, ref, true);
this.suggestedRef = result;
2017-08-17 22:00:37 +05:30
}
2018-03-17 18:26:18 +05:30
this.isGettingRef = false;
return this.updateInputState(target, ref, result);
})
.catch(() => {
this.unavailable();
this.disable();
new Flash('Failed to get ref.');
this.isGettingRef = false;
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
return false;
});
2017-08-17 22:00:37 +05:30
}
2018-03-17 18:26:18 +05:30
getTargetData(target) {
2017-08-17 22:00:37 +05:30
return {
2018-03-17 18:26:18 +05:30
input: this[`${target}Input`],
message: this[`${target}Message`],
2017-08-17 22:00:37 +05:30
};
}
2018-03-17 18:26:18 +05:30
hide() {
this.wrapperEl.classList.add('hide');
}
init() {
this.checkAbilityToCreateBranch();
}
initDroplab() {
this.droplab = new DropLab();
this.droplab.init(
this.dropdownToggle,
this.dropdownList,
[InputSetter],
this.getDroplabConfig(),
);
}
inputsAreValid() {
return this.branchIsValid && this.refIsValid;
2017-08-17 22:00:37 +05:30
}
isBusy() {
return this.isCreatingMergeRequest ||
this.mergeRequestCreated ||
this.isCreatingBranch ||
2018-03-17 18:26:18 +05:30
this.branchCreated ||
this.isGettingRef;
2017-08-17 22:00:37 +05:30
}
2018-03-17 18:26:18 +05:30
onChangeInput(event) {
let target;
let value;
if (event.target === this.branchInput) {
target = 'branch';
value = this.branchInput.value;
} else if (event.target === this.refInput) {
target = 'ref';
value = event.target.value.slice(0, event.target.selectionStart) +
event.target.value.slice(event.target.selectionEnd);
} else {
return false;
}
if (this.isGettingRef) return false;
// `ENTER` key submits the data.
if (event.keyCode === 13 && this.inputsAreValid()) {
event.preventDefault();
return this.createMergeRequestButton.click();
}
// If the input is empty, use the original value generated by the backend.
if (!value) {
this.createBranchPath = this.wrapperEl.dataset.createBranchPath;
this.createMrPath = this.wrapperEl.dataset.createMrPath;
if (target === 'branch') {
this.branchIsValid = true;
} else {
this.refIsValid = true;
}
this.enable();
this.showAvailableMessage(target);
return true;
}
this.showCheckingMessage(target);
this.refDebounce(value, target);
return true;
}
onClickCreateMergeRequestButton(event) {
2017-08-17 22:00:37 +05:30
let xhr = null;
2018-03-17 18:26:18 +05:30
event.preventDefault();
2017-08-17 22:00:37 +05:30
if (this.isBusy()) {
return;
}
2018-03-17 18:26:18 +05:30
if (event.target.dataset.action === CREATE_MERGE_REQUEST) {
2017-08-17 22:00:37 +05:30
xhr = this.createMergeRequest();
2018-03-17 18:26:18 +05:30
} else if (event.target.dataset.action === CREATE_BRANCH) {
2017-08-17 22:00:37 +05:30
xhr = this.createBranch();
}
2018-03-17 18:26:18 +05:30
xhr.catch(() => {
2017-08-17 22:00:37 +05:30
this.isCreatingMergeRequest = false;
this.isCreatingBranch = false;
2018-03-17 18:26:18 +05:30
this.enable();
});
2017-08-17 22:00:37 +05:30
this.disable();
}
2018-03-17 18:26:18 +05:30
onClickSetFocusOnBranchNameInput() {
this.branchInput.focus();
2017-08-17 22:00:37 +05:30
}
2018-03-17 18:26:18 +05:30
// `TAB` autocompletes the source.
static processTab(event) {
if (event.keyCode !== 9 || this.isGettingRef) return;
const selectedText = CreateMergeRequestDropdown.getInputSelectedText(this.refInput);
// if nothing selected, we don't need to autocomplete anything. Do the default TAB action.
// If a user manually selected text, don't autocomplete anything. Do the default TAB action.
if (!selectedText || this.refInput.dataset.value === this.suggestedRef) return;
event.preventDefault();
window.getSelection().removeAllRanges();
}
removeMessage(target) {
const { input, message } = this.getTargetData(target);
const inputClasses = ['gl-field-error-outline', 'gl-field-success-outline'];
const messageClasses = ['gl-field-hint', 'gl-field-error-message', 'gl-field-success-message'];
inputClasses.forEach(cssClass => input.classList.remove(cssClass));
messageClasses.forEach(cssClass => message.classList.remove(cssClass));
message.style.display = 'none';
}
setUnavailableButtonState(isLoading = true) {
if (isLoading) {
this.unavailableButtonArrow.classList.add('fa-spin');
this.unavailableButtonArrow.classList.add('fa-spinner');
this.unavailableButtonArrow.classList.remove('fa-exclamation-triangle');
this.unavailableButtonText.textContent = __('Checking branch availability...');
} else {
this.unavailableButtonArrow.classList.remove('fa-spin');
this.unavailableButtonArrow.classList.remove('fa-spinner');
this.unavailableButtonArrow.classList.add('fa-exclamation-triangle');
this.unavailableButtonText.textContent = __('New branch unavailable');
}
}
showAvailableMessage(target) {
const { input, message } = this.getTargetData(target);
const text = target === 'branch' ? __('Branch name') : __('Source');
this.removeMessage(target);
input.classList.add('gl-field-success-outline');
message.classList.add('gl-field-success-message');
message.textContent = sprintf(__('%{text} is available'), { text });
message.style.display = 'inline-block';
}
showCheckingMessage(target) {
const { message } = this.getTargetData(target);
const text = target === 'branch' ? __('branch name') : __('source');
this.removeMessage(target);
message.classList.add('gl-field-hint');
message.textContent = sprintf(__('Checking %{text} availability…'), { text });
message.style.display = 'inline-block';
}
showNotAvailableMessage(target) {
const { input, message } = this.getTargetData(target);
const text = target === 'branch' ? __('Branch is already taken') : __('Source is not available');
this.removeMessage(target);
input.classList.add('gl-field-error-outline');
message.classList.add('gl-field-error-message');
message.textContent = text;
message.style.display = 'inline-block';
}
unavailable() {
this.availableButton.classList.add('hide');
this.unavailableButton.classList.remove('hide');
}
2018-10-15 14:42:47 +05:30
updateBranchName(suggestedBranchName) {
this.branchInput.value = suggestedBranchName;
this.updateCreatePaths('branch', suggestedBranchName);
}
2018-03-17 18:26:18 +05:30
updateInputState(target, ref, result) {
// target - 'branch' or 'ref' - which the input field we are searching a ref for.
// ref - string - what a user typed.
// result - string - what has been found on backend.
// If a found branch equals exact the same text a user typed,
// that means a new branch cannot be created as it already exists.
if (ref === result) {
if (target === 'branch') {
this.branchIsValid = false;
this.showNotAvailableMessage('branch');
} else {
this.refIsValid = true;
this.refInput.dataset.value = ref;
this.showAvailableMessage('ref');
2018-10-15 14:42:47 +05:30
this.updateCreatePaths(target, ref);
2018-03-17 18:26:18 +05:30
}
} else if (target === 'branch') {
this.branchIsValid = true;
this.showAvailableMessage('branch');
2018-10-15 14:42:47 +05:30
this.updateCreatePaths(target, ref);
2018-03-17 18:26:18 +05:30
} else {
this.refIsValid = false;
this.refInput.dataset.value = ref;
this.disableCreateAction();
this.showNotAvailableMessage('ref');
// Show ref hint.
if (result) {
this.refInput.value = result;
this.refInput.setSelectionRange(ref.length, result.length);
}
}
if (this.inputsAreValid()) {
this.enable();
} else {
this.disableCreateAction();
}
2017-08-17 22:00:37 +05:30
}
2018-10-15 14:42:47 +05:30
// target - 'branch' or 'ref'
// ref - string - the new value to use as branch or ref
updateCreatePaths(target, ref) {
const pathReplacement = `$1${ref}`;
this.createBranchPath = this.createBranchPath.replace(this.regexps[target].createBranchPath,
pathReplacement);
this.createMrPath = this.createMrPath.replace(this.regexps[target].createMrPath,
pathReplacement);
}
2017-08-17 22:00:37 +05:30
}