debian-mirror-gitlab/app/assets/javascripts/visual_review_toolbar/components/wrapper.js

80 lines
2.3 KiB
JavaScript
Raw Normal View History

2019-10-12 21:52:04 +05:30
import { CLEAR, FORM, FORM_CONTAINER, WHITE } from '../shared';
2019-09-30 21:07:59 +05:30
import {
selectCollapseButton,
selectForm,
selectFormContainer,
selectNoteContainer,
} from './utils';
2019-10-12 21:52:04 +05:30
import { collapseButton, commentIcon, compressIcon } from './wrapper_icons';
2019-09-04 21:01:54 +05:30
const form = content => `
2019-10-12 21:52:04 +05:30
<form id="${FORM}" novalidate>
2019-09-04 21:01:54 +05:30
${content}
</form>
`;
2019-10-12 21:52:04 +05:30
const buttonAndForm = content => `
2019-09-30 21:07:59 +05:30
<div id="${FORM_CONTAINER}" class="gitlab-form-open">
2019-10-12 21:52:04 +05:30
${collapseButton}
2019-09-30 21:07:59 +05:30
${form(content)}
</div>
`;
2019-10-12 21:52:04 +05:30
const addForm = nextForm => {
2019-09-04 21:01:54 +05:30
const formWrapper = selectForm();
2019-10-12 21:52:04 +05:30
formWrapper.innerHTML = nextForm;
2019-09-04 21:01:54 +05:30
};
function toggleForm() {
2019-10-12 21:52:04 +05:30
const toggleButton = selectCollapseButton();
2019-09-04 21:01:54 +05:30
const currentForm = selectForm();
2019-09-30 21:07:59 +05:30
const formContainer = selectFormContainer();
const noteContainer = selectNoteContainer();
2019-09-04 21:01:54 +05:30
const OPEN = 'open';
const CLOSED = 'closed';
/*
You may wonder why we spread the arrays before we reverse them.
In the immortal words of MDN,
Careful: reverse is destructive. It also changes the original array
*/
const openButtonClasses = ['gitlab-collapse-closed', 'gitlab-collapse-open'];
const closedButtonClasses = [...openButtonClasses].reverse();
2019-09-30 21:07:59 +05:30
const openContainerClasses = ['gitlab-wrapper-closed', 'gitlab-wrapper-open'];
2019-09-04 21:01:54 +05:30
const closedContainerClasses = [...openContainerClasses].reverse();
const stateVals = {
[OPEN]: {
buttonClasses: openButtonClasses,
containerClasses: openContainerClasses,
icon: compressIcon,
display: 'flex',
backgroundColor: WHITE,
},
[CLOSED]: {
buttonClasses: closedButtonClasses,
containerClasses: closedContainerClasses,
icon: commentIcon,
display: 'none',
backgroundColor: CLEAR,
},
};
2019-10-12 21:52:04 +05:30
const nextState = toggleButton.classList.contains('gitlab-collapse-open') ? CLOSED : OPEN;
2019-09-04 21:01:54 +05:30
const currentVals = stateVals[nextState];
2019-09-30 21:07:59 +05:30
formContainer.classList.replace(...currentVals.containerClasses);
formContainer.style.backgroundColor = currentVals.backgroundColor;
formContainer.classList.toggle('gitlab-form-open');
2019-09-04 21:01:54 +05:30
currentForm.style.display = currentVals.display;
2019-10-12 21:52:04 +05:30
toggleButton.classList.replace(...currentVals.buttonClasses);
toggleButton.innerHTML = currentVals.icon;
2019-09-30 21:07:59 +05:30
if (noteContainer && noteContainer.innerText.length > 0) {
noteContainer.style.display = currentVals.display;
}
2019-09-04 21:01:54 +05:30
}
2019-10-12 21:52:04 +05:30
export { addForm, buttonAndForm, toggleForm };