]>
Commit | Line | Data |
---|---|---|
45708891 DC |
1 | Ext.define('PVE.dc.ACMEPluginEditor', { |
2 | extend: 'Proxmox.window.Edit', | |
3 | xtype: 'pveACMEPluginEditor', | |
4 | mixins: ['Proxmox.Mixin.CBind'], | |
5 | ||
4a566eda TL |
6 | onlineHelp: 'sysadmin_certs_acme_plugins', |
7 | ||
45708891 DC |
8 | isAdd: true, |
9 | isCreate: false, | |
10 | ||
22884647 | 11 | width: 550, |
45708891 DC |
12 | url: '/cluster/acme/plugins/', |
13 | ||
22884647 TL |
14 | subject: gettext('ACME DNS Plugin'), |
15 | ||
45708891 DC |
16 | items: [ |
17 | { | |
18 | xtype: 'inputpanel', | |
19 | // we dynamically create fields from the given schema | |
20 | // things we have to do here: | |
21 | // * save which fields we created to remove them again | |
22 | // * split the data from the generic 'data' field into the boxes | |
23 | // * on deletion collect those values again | |
24 | // * save the original values of the data field | |
25 | createdFields: {}, | |
26 | createdInitially: false, | |
27 | originalValues: {}, | |
28 | createSchemaFields: function(schema) { | |
29 | let me = this; | |
30 | // we know where to add because we define it right below | |
31 | let container = me.down('container'); | |
32 | let datafield = me.down('field[name=data]'); | |
33 | if (!me.createdInitially) { | |
34 | [me.originalValues] = PVE.Parser.parseACMEPluginData(datafield.getValue()); | |
35 | } | |
36 | ||
37 | // collect values from custom fields and add it to 'data'', | |
38 | // then remove the custom fields | |
39 | let data = []; | |
40 | for (const [name, field] of Object.entries(me.createdFields)) { | |
41 | let value = field.getValue(); | |
42 | if (value !== undefined && value !== null && value !== '') { | |
43 | data.push(`${name}=${value}`); | |
44 | } | |
45 | container.remove(field); | |
46 | } | |
47 | let datavalue = datafield.getValue(); | |
48 | if (datavalue !== undefined && datavalue !== null && datavalue !== '') { | |
49 | data.push(datavalue); | |
50 | } | |
51 | datafield.setValue(data.join('\n')); | |
52 | ||
53 | me.createdFields = {}; | |
54 | ||
56e7fc7b TL |
55 | if (typeof schema.fields !== 'object') { |
56 | schema.fields = {}; | |
57 | } | |
45708891 | 58 | // create custom fields according to schema |
b9cab976 | 59 | let gotSchemaField = false; |
9994a6f9 | 60 | for (const [name, definition] of Object.entries(schema.fields).sort((a, b) => a[0].localeCompare(b[0]))) { |
45708891 DC |
61 | let xtype; |
62 | switch (definition.type) { | |
63 | case 'string': | |
64 | xtype = 'proxmoxtextfield'; | |
65 | break; | |
66 | case 'integer': | |
67 | xtype = 'proxmoxintegerfield'; | |
68 | break; | |
69 | case 'number': | |
70 | xtype = 'numberfield'; | |
71 | break; | |
72 | default: | |
73 | console.warn(`unknown type '${definition.type}'`); | |
74 | xtype = 'proxmoxtextfield'; | |
75 | break; | |
76 | } | |
77 | ||
78 | let field = Ext.create({ | |
79 | xtype, | |
80 | name: `custom_${name}`, | |
81 | fieldLabel: name, | |
82 | width: '100%', | |
22884647 TL |
83 | labelWidth: 150, |
84 | labelSeparator: '=', | |
2fb0b2fe | 85 | emptyText: definition.default || '', |
45708891 DC |
86 | autoEl: definition.description ? { |
87 | tag: 'div', | |
88 | 'data-qtip': definition.description, | |
89 | } : undefined, | |
90 | }); | |
91 | ||
92 | me.createdFields[name] = field; | |
93 | container.add(field); | |
b9cab976 | 94 | gotSchemaField = true; |
45708891 | 95 | } |
b9cab976 | 96 | datafield.setHidden(gotSchemaField); // prefer schema-fields |
45708891 DC |
97 | |
98 | // parse data from field and set it to the custom ones | |
99 | let extradata = []; | |
100 | [data, extradata] = PVE.Parser.parseACMEPluginData(datafield.getValue()); | |
101 | for (const [key, value] of Object.entries(data)) { | |
102 | if (me.createdFields[key]) { | |
103 | me.createdFields[key].setValue(value); | |
104 | me.createdFields[key].originalValue = me.originalValues[key]; | |
105 | } else { | |
106 | extradata.push(`${key}=${value}`); | |
107 | } | |
108 | } | |
109 | datafield.setValue(extradata.join('\n')); | |
110 | if (!me.createdInitially) { | |
111 | datafield.resetOriginalValue(); | |
112 | me.createdInitially = true; // save that we initally set that | |
113 | } | |
114 | }, | |
115 | onGetValues: function(values) { | |
116 | let me = this; | |
117 | let win = me.up('pveACMEPluginEditor'); | |
118 | if (win.isCreate) { | |
119 | values.id = values.plugin; | |
120 | values.type = 'dns'; // the only one for now | |
121 | } | |
122 | delete values.plugin; | |
123 | ||
124 | PVE.Utils.delete_if_default(values, 'validation-delay', '30', win.isCreate); | |
125 | ||
126 | let data = ''; | |
127 | for (const [name, field] of Object.entries(me.createdFields)) { | |
128 | let value = field.getValue(); | |
129 | if (value !== null && value !== undefined && value !== '') { | |
130 | data += `${name}=${value}\n`; | |
131 | } | |
132 | delete values[`custom_${name}`]; | |
133 | } | |
134 | values.data = Ext.util.Base64.encode(data + values.data); | |
135 | return values; | |
136 | }, | |
137 | items: [ | |
138 | { | |
139 | xtype: 'pmxDisplayEditField', | |
140 | cbind: { | |
141 | editable: (get) => get('isCreate'), | |
142 | submitValue: (get) => get('isCreate'), | |
143 | }, | |
144 | editConfig: { | |
145 | flex: 1, | |
146 | xtype: 'proxmoxtextfield', | |
147 | allowBlank: false, | |
148 | }, | |
149 | name: 'plugin', | |
22884647 | 150 | labelWidth: 150, |
7daaa52c | 151 | fieldLabel: gettext('Plugin ID'), |
45708891 DC |
152 | }, |
153 | { | |
154 | xtype: 'proxmoxintegerfield', | |
155 | name: 'validation-delay', | |
22884647 | 156 | labelWidth: 150, |
45708891 DC |
157 | fieldLabel: gettext('Validation Delay'), |
158 | emptyText: 30, | |
159 | cbind: { | |
160 | deleteEmpty: '{!isCreate}', | |
161 | }, | |
162 | minValue: 0, | |
7daaa52c | 163 | maxValue: 48*60*60, |
45708891 DC |
164 | }, |
165 | { | |
166 | xtype: 'pveACMEApiSelector', | |
167 | name: 'api', | |
22884647 | 168 | labelWidth: 150, |
45708891 DC |
169 | listeners: { |
170 | change: function(selector) { | |
171 | let schema = selector.getSchema(); | |
172 | selector.up('inputpanel').createSchemaFields(schema); | |
173 | }, | |
174 | }, | |
175 | }, | |
176 | { | |
7daaa52c | 177 | xtype: 'textarea', |
45708891 | 178 | fieldLabel: gettext('API Data'), |
22884647 | 179 | labelWidth: 150, |
45708891 DC |
180 | name: 'data', |
181 | }, | |
182 | ], | |
183 | }, | |
184 | ], | |
185 | ||
186 | initComponent: function() { | |
187 | var me = this; | |
188 | ||
189 | me.callParent(); | |
190 | ||
191 | if (!me.isCreate) { | |
192 | me.load({ | |
193 | success: function(response, opts) { | |
194 | me.setValues(response.result.data); | |
195 | }, | |
196 | }); | |
197 | } else { | |
198 | me.method = 'POST'; | |
199 | } | |
200 | }, | |
201 | }); |