-
-
Notifications
You must be signed in to change notification settings - Fork 571
/
PromptForm.ts
209 lines (201 loc) · 8.57 KB
/
PromptForm.ts
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import { DEFAULT_CONFIG } from "@goauthentik/web/api/Config";
import "@goauthentik/web/elements/CodeMirror";
import "@goauthentik/web/elements/forms/HorizontalFormElement";
import { ModelForm } from "@goauthentik/web/elements/forms/ModelForm";
import { first } from "@goauthentik/web/utils";
import { t } from "@lingui/macro";
import { TemplateResult, html } from "lit";
import { customElement } from "lit/decorators.js";
import { ifDefined } from "lit/directives/if-defined.js";
import { Prompt, PromptTypeEnum, StagesApi } from "@goauthentik/api";
@customElement("ak-prompt-form")
export class PromptForm extends ModelForm<Prompt, string> {
loadInstance(pk: string): Promise<Prompt> {
return new StagesApi(DEFAULT_CONFIG).stagesPromptPromptsRetrieve({
promptUuid: pk,
});
}
getSuccessMessage(): string {
if (this.instance) {
return t`Successfully updated prompt.`;
} else {
return t`Successfully created prompt.`;
}
}
send = (data: Prompt): Promise<Prompt> => {
if (this.instance) {
return new StagesApi(DEFAULT_CONFIG).stagesPromptPromptsUpdate({
promptUuid: this.instance.pk || "",
promptRequest: data,
});
} else {
return new StagesApi(DEFAULT_CONFIG).stagesPromptPromptsCreate({
promptRequest: data,
});
}
};
renderTypes(): TemplateResult {
return html`
<option
value=${PromptTypeEnum.Text}
?selected=${this.instance?.type === PromptTypeEnum.Text}
>
${t`Text: Simple Text input`}
</option>
<option
value=${PromptTypeEnum.TextReadOnly}
?selected=${this.instance?.type === PromptTypeEnum.TextReadOnly}
>
${t`Text (read-only): Simple Text input, but cannot be edited.`}
</option>
<option
value=${PromptTypeEnum.Username}
?selected=${this.instance?.type === PromptTypeEnum.Username}
>
${t`Username: Same as Text input, but checks for and prevents duplicate usernames.`}
</option>
<option
value=${PromptTypeEnum.Email}
?selected=${this.instance?.type === PromptTypeEnum.Email}
>
${t`Email: Text field with Email type.`}
</option>
<option
value=${PromptTypeEnum.Password}
?selected=${this.instance?.type === PromptTypeEnum.Password}
>
${t`Password: Masked input, password is validated against sources. Policies still have to be applied to this Stage. If two of these are used in the same stage, they are ensured to be identical.`}
</option>
<option
value=${PromptTypeEnum.Number}
?selected=${this.instance?.type === PromptTypeEnum.Number}
>
${t`Number`}
</option>
<option
value=${PromptTypeEnum.Checkbox}
?selected=${this.instance?.type === PromptTypeEnum.Checkbox}
>
${t`Checkbox`}
</option>
<option
value=${PromptTypeEnum.Date}
?selected=${this.instance?.type === PromptTypeEnum.Date}
>
${t`Date`}
</option>
<option
value=${PromptTypeEnum.DateTime}
?selected=${this.instance?.type === PromptTypeEnum.DateTime}
>
${t`Date Time`}
</option>
<option
value=${PromptTypeEnum.File}
?selected=${this.instance?.type === PromptTypeEnum.File}
>
${t`File`}
</option>
<option
value=${PromptTypeEnum.Separator}
?selected=${this.instance?.type === PromptTypeEnum.Separator}
>
${t`Separator: Static Separator Line`}
</option>
<option
value=${PromptTypeEnum.Hidden}
?selected=${this.instance?.type === PromptTypeEnum.Hidden}
>
${t`Hidden: Hidden field, can be used to insert data into form.`}
</option>
<option
value=${PromptTypeEnum.Static}
?selected=${this.instance?.type === PromptTypeEnum.Static}
>
${t`Static: Static value, displayed as-is.`}
</option>
<option
value=${PromptTypeEnum.AkLocale}
?selected=${this.instance?.type === PromptTypeEnum.AkLocale}
>
${t`authentik: Locale: Displays a list of locales authentik supports.`}
</option>
`;
}
renderForm(): TemplateResult {
return html`<form class="pf-c-form pf-m-horizontal">
<ak-form-element-horizontal label=${t`Field Key`} ?required=${true} name="fieldKey">
<input
type="text"
value="${ifDefined(this.instance?.fieldKey)}"
class="pf-c-form-control"
required
/>
<p class="pf-c-form__helper-text">
${t`Name of the form field, also used to store the value.`}
</p>
<p class="pf-c-form__helper-text">
${t`When used in conjunction with a User Write stage, use attributes.foo to write attributes.`}
</p>
</ak-form-element-horizontal>
<ak-form-element-horizontal label=${t`Label`} ?required=${true} name="label">
<input
type="text"
value="${ifDefined(this.instance?.label)}"
class="pf-c-form-control"
required
/>
<p class="pf-c-form__helper-text">${t`Label shown next to/above the prompt.`}</p>
</ak-form-element-horizontal>
<ak-form-element-horizontal label=${t`Type`} ?required=${true} name="type">
<select class="pf-c-form-control">
${this.renderTypes()}
</select>
</ak-form-element-horizontal>
<ak-form-element-horizontal name="required">
<div class="pf-c-check">
<input
type="checkbox"
class="pf-c-check__input"
?checked=${first(this.instance?.required, false)}
/>
<label class="pf-c-check__label"> ${t`Required`} </label>
</div>
</ak-form-element-horizontal>
<ak-form-element-horizontal name="placeholderExpression">
<div class="pf-c-check">
<input
type="checkbox"
class="pf-c-check__input"
?checked=${first(this.instance?.placeholderExpression, false)}
/>
<label class="pf-c-check__label"
>${t`Interpret placeholder as expression`}</label
>
</div>
<p class="pf-c-form__helper-text">
${t`When checked, the placeholder will be evaluated in the same way environment as a property mapping.
If the evaluation failed, the placeholder itself is returned.`}
</p>
</ak-form-element-horizontal>
<ak-form-element-horizontal label=${t`Placeholder`} name="placeholder">
<ak-codemirror mode="python" value="${ifDefined(this.instance?.placeholder)}">
</ak-codemirror>
<p class="pf-c-form__helper-text">${t`Optionally pre-fill the input value`}</p>
</ak-form-element-horizontal>
<ak-form-element-horizontal label=${t`Help text`} name="subText">
<ak-codemirror mode="htmlmixed" value="${ifDefined(this.instance?.subText)}">
</ak-codemirror>
<p class="pf-c-form__helper-text">${t`Any HTML can be used.`}</p>
</ak-form-element-horizontal>
<ak-form-element-horizontal label=${t`Order`} ?required=${true} name="order">
<input
type="number"
value="${first(this.instance?.order, 0)}"
class="pf-c-form-control"
required
/>
</ak-form-element-horizontal>
</form>`;
}
}