Welcome to mirror list, hosted at ThFree Co, Russian Federation.

wrapper.js « components « visual_review_toolbar « javascripts « assets « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: fdf8ad7c41f75e89e175012716cd29bf6037ffb4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import { CLEAR, FORM, FORM_CONTAINER, WHITE } from '../shared';
import {
  selectCollapseButton,
  selectForm,
  selectFormContainer,
  selectNoteContainer,
} from './utils';
import { collapseButton, commentIcon, compressIcon } from './wrapper_icons';

const form = content => `
  <form id="${FORM}" novalidate>
    ${content}
  </form>
`;

const buttonAndForm = content => `
  <div id="${FORM_CONTAINER}" class="gitlab-form-open">
    ${collapseButton}
    ${form(content)}
  </div>
`;

const addForm = nextForm => {
  const formWrapper = selectForm();
  formWrapper.innerHTML = nextForm;
};

function toggleForm() {
  const toggleButton = selectCollapseButton();
  const currentForm = selectForm();
  const formContainer = selectFormContainer();
  const noteContainer = selectNoteContainer();
  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();
  const openContainerClasses = ['gitlab-wrapper-closed', 'gitlab-wrapper-open'];
  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,
    },
  };

  const nextState = toggleButton.classList.contains('gitlab-collapse-open') ? CLOSED : OPEN;
  const currentVals = stateVals[nextState];

  formContainer.classList.replace(...currentVals.containerClasses);
  formContainer.style.backgroundColor = currentVals.backgroundColor;
  formContainer.classList.toggle('gitlab-form-open');
  currentForm.style.display = currentVals.display;
  toggleButton.classList.replace(...currentVals.buttonClasses);
  toggleButton.innerHTML = currentVals.icon;

  if (noteContainer && noteContainer.innerText.length > 0) {
    noteContainer.style.display = currentVals.display;
  }
}

export { addForm, buttonAndForm, toggleForm };