]> git.proxmox.com Git - pve-manager.git/blame - www/manager6/dc/ACMEPluginEdit.js
ui: acme view: sort fields lexically by id
[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
56e7fc7b
TL
52 if (typeof schema.fields !== 'object') {
53 schema.fields = {};
54 }
45708891 55 // create custom fields according to schema
9994a6f9 56 for (const [name, definition] of Object.entries(schema.fields).sort((a, b) => a[0].localeCompare(b[0]))) {
45708891
DC
57 let xtype;
58 switch (definition.type) {
59 case 'string':
60 xtype = 'proxmoxtextfield';
61 break;
62 case 'integer':
63 xtype = 'proxmoxintegerfield';
64 break;
65 case 'number':
66 xtype = 'numberfield';
67 break;
68 default:
69 console.warn(`unknown type '${definition.type}'`);
70 xtype = 'proxmoxtextfield';
71 break;
72 }
73
74 let field = Ext.create({
75 xtype,
76 name: `custom_${name}`,
77 fieldLabel: name,
78 width: '100%',
79 labelWidth: 120,
2fb0b2fe 80 emptyText: definition.default || '',
45708891
DC
81 autoEl: definition.description ? {
82 tag: 'div',
83 'data-qtip': definition.description,
84 } : undefined,
85 });
86
87 me.createdFields[name] = field;
88 container.add(field);
89 }
90
91 // parse data from field and set it to the custom ones
92 let extradata = [];
93 [data, extradata] = PVE.Parser.parseACMEPluginData(datafield.getValue());
94 for (const [key, value] of Object.entries(data)) {
95 if (me.createdFields[key]) {
96 me.createdFields[key].setValue(value);
97 me.createdFields[key].originalValue = me.originalValues[key];
98 } else {
99 extradata.push(`${key}=${value}`);
100 }
101 }
102 datafield.setValue(extradata.join('\n'));
103 if (!me.createdInitially) {
104 datafield.resetOriginalValue();
105 me.createdInitially = true; // save that we initally set that
106 }
107 },
108 onGetValues: function(values) {
109 let me = this;
110 let win = me.up('pveACMEPluginEditor');
111 if (win.isCreate) {
112 values.id = values.plugin;
113 values.type = 'dns'; // the only one for now
114 }
115 delete values.plugin;
116
117 PVE.Utils.delete_if_default(values, 'validation-delay', '30', win.isCreate);
118
119 let data = '';
120 for (const [name, field] of Object.entries(me.createdFields)) {
121 let value = field.getValue();
122 if (value !== null && value !== undefined && value !== '') {
123 data += `${name}=${value}\n`;
124 }
125 delete values[`custom_${name}`];
126 }
127 values.data = Ext.util.Base64.encode(data + values.data);
128 return values;
129 },
130 items: [
131 {
132 xtype: 'pmxDisplayEditField',
133 cbind: {
134 editable: (get) => get('isCreate'),
135 submitValue: (get) => get('isCreate'),
136 },
137 editConfig: {
138 flex: 1,
139 xtype: 'proxmoxtextfield',
140 allowBlank: false,
141 },
142 name: 'plugin',
143 labelWidth: 120,
144 fieldLabel: gettext('Plugin'),
145 },
146 {
147 xtype: 'proxmoxintegerfield',
148 name: 'validation-delay',
149 labelWidth: 120,
150 fieldLabel: gettext('Validation Delay'),
151 emptyText: 30,
152 cbind: {
153 deleteEmpty: '{!isCreate}',
154 },
155 minValue: 0,
156 maxValue: 172800,
157 },
158 {
159 xtype: 'pveACMEApiSelector',
160 name: 'api',
161 labelWidth: 120,
162 listeners: {
163 change: function(selector) {
164 let schema = selector.getSchema();
165 selector.up('inputpanel').createSchemaFields(schema);
166 },
167 },
168 },
169 {
170 fieldLabel: gettext('API Data'),
171 labelWidth: 120,
172 xtype: 'textarea',
173 name: 'data',
174 },
175 ],
176 },
177 ],
178
179 initComponent: function() {
180 var me = this;
181
182 me.callParent();
183
184 if (!me.isCreate) {
185 me.load({
186 success: function(response, opts) {
187 me.setValues(response.result.data);
188 },
189 });
190 } else {
191 me.method = 'POST';
192 }
193 },
194});