summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/comment_type_toggle.js
blob: c74184949dfa870a040bc13293afef98b9ca0a10 (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
import DropLab from './droplab/drop_lab';
import ISetter from './droplab/plugins/input_setter';

// Todo: Remove this when fixing issue in input_setter plugin
const InputSetter = Object.assign({}, ISetter);

class CommentTypeToggle {
  constructor(opts = {}) {
    this.dropdownTrigger = opts.dropdownTrigger;
    this.dropdownList = opts.dropdownList;
    this.noteTypeInput = opts.noteTypeInput;
    this.submitButton = opts.submitButton;
    this.closeButton = opts.closeButton;
    this.reopenButton = opts.reopenButton;
  }

  initDroplab() {
    this.droplab = new DropLab();

    const config = this.setConfig();

    this.droplab.init(this.dropdownTrigger, this.dropdownList, [InputSetter], config);
  }

  setConfig() {
    const config = {
      InputSetter: [{
        input: this.noteTypeInput,
        valueAttribute: 'data-value',
      },
      {
        input: this.submitButton,
        valueAttribute: 'data-submit-text',
      }],
    };

    if (this.closeButton) {
      config.InputSetter.push({
        input: this.closeButton,
        valueAttribute: 'data-close-text',
      }, {
        input: this.closeButton,
        valueAttribute: 'data-close-text',
        inputAttribute: 'data-alternative-text',
      });
    }

    if (this.reopenButton) {
      config.InputSetter.push({
        input: this.reopenButton,
        valueAttribute: 'data-reopen-text',
      }, {
        input: this.reopenButton,
        valueAttribute: 'data-reopen-text',
        inputAttribute: 'data-alternative-text',
      });
    }

    return config;
  }
}

export default CommentTypeToggle;