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