.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / enquirer / lib / prompts / editable.js
1 'use strict';
2
3 const Select = require('./select');
4 const Form = require('./form');
5 const form = Form.prototype;
6
7 class Editable extends Select {
8   constructor(options) {
9     super({ ...options, multiple: true });
10     this.align = [this.options.align, 'left'].find(v => v != null);
11     this.emptyError = '';
12     this.values = {};
13   }
14
15   dispatch(char, key) {
16     let choice = this.focused;
17     let parent = choice.parent || {};
18     if (!choice.editable && !parent.editable) {
19       if (char === 'a' || char === 'i') return super[char]();
20     }
21     return form.dispatch.call(this, char, key);
22   }
23
24   append(char, key) {
25     return form.append.call(this, char, key);
26   }
27
28   delete(char, key) {
29     return form.delete.call(this, char, key);
30   }
31
32   space(char) {
33     return this.focused.editable ? this.append(char) : super.space();
34   }
35
36   number(char) {
37     return this.focused.editable ? this.append(char) : super.number(char);
38   }
39
40   next() {
41     return this.focused.editable ? form.next.call(this) : super.next();
42   }
43
44   prev() {
45     return this.focused.editable ? form.prev.call(this) : super.prev();
46   }
47
48   async indicator(choice, i) {
49     let symbol = choice.indicator || '';
50     let value = choice.editable ? symbol : super.indicator(choice, i);
51     return await this.resolve(value, this.state, choice, i) || '';
52   }
53
54   indent(choice) {
55     return choice.role === 'heading' ? '' : (choice.editable ? ' ' : '  ');
56   }
57
58   async renderChoice(choice, i) {
59     choice.indent = '';
60     if (choice.editable) return form.renderChoice.call(this, choice, i);
61     return super.renderChoice(choice, i);
62   }
63
64   error() {
65     return '';
66   }
67
68   footer() {
69     return this.state.error;
70   }
71
72   async validate() {
73     let result = true;
74
75     for (let choice of this.choices) {
76       if (typeof choice.validate !== 'function') {
77         continue;
78       }
79
80       if (choice.role === 'heading') {
81         continue;
82       }
83
84       let val = choice.parent ? this.value[choice.parent.name] : this.value;
85
86       if (choice.editable) {
87         val = choice.value === choice.name ? choice.initial || '' : choice.value;
88       } else if (!this.isDisabled(choice)) {
89         val = choice.enabled === true;
90       }
91
92       result = await choice.validate(val, this.state);
93
94       if (result !== true) {
95         break;
96       }
97     }
98
99     if (result !== true) {
100       this.state.error = typeof result === 'string' ? result : 'Invalid Input';
101     }
102
103     return result;
104   }
105
106   submit() {
107     if (this.focused.newChoice === true) return super.submit();
108     if (this.choices.some(ch => ch.newChoice)) {
109       return this.alert();
110     }
111
112     this.value = {};
113
114     for (let choice of this.choices) {
115       let val = choice.parent ? this.value[choice.parent.name] : this.value;
116
117       if (choice.role === 'heading') {
118         this.value[choice.name] = {};
119         continue;
120       }
121
122       if (choice.editable) {
123         val[choice.name] = choice.value === choice.name
124           ? (choice.initial || '')
125           : choice.value;
126
127       } else if (!this.isDisabled(choice)) {
128         val[choice.name] = choice.enabled === true;
129       }
130     }
131
132     return this.base.submit.call(this);
133   }
134 }
135
136 module.exports = Editable;