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

add_ssh_key_validation.js « profile « javascripts « assets « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5c78de7ffb0dba74d807cb6eb5ef07c4328d1b86 (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
export default class AddSshKeyValidation {
  constructor(inputElement, warningElement, originalSubmitElement, confirmSubmitElement) {
    this.inputElement = inputElement;
    this.form = inputElement.form;

    this.warningElement = warningElement;

    this.originalSubmitElement = originalSubmitElement;
    this.confirmSubmitElement = confirmSubmitElement;

    this.isValid = false;
  }

  register() {
    this.form.addEventListener('submit', (event) => this.submit(event));

    this.confirmSubmitElement.addEventListener('click', () => {
      this.isValid = true;
      this.form.submit();
    });

    this.inputElement.addEventListener('input', () => this.toggleWarning(false));
  }

  submit(event) {
    this.isValid = AddSshKeyValidation.isPublicKey(this.inputElement.value);

    if (this.isValid) return true;

    event.preventDefault();
    this.toggleWarning(true);
    return false;
  }

  toggleWarning(isVisible) {
    this.warningElement.classList.toggle('hide', !isVisible);
    this.originalSubmitElement.classList.toggle('hide', isVisible);
  }

  static isPublicKey(value) {
    return /^(ssh|ecdsa-sha2)-/.test(value);
  }
}