]>
Commit | Line | Data |
---|---|---|
06694509 DM |
1 | Ext.define('Proxmox.window.Edit', { |
2 | extend: 'Ext.window.Window', | |
3 | alias: 'widget.proxmoxWindowEdit', | |
1b07a95a | 4 | |
a33ba257 DM |
5 | // autoLoad trigger a load() after component creation |
6 | autoLoad: false, | |
ca204f31 TL |
7 | // set extra options like params for the load request |
8 | autoLoadOptions: undefined, | |
a33ba257 | 9 | |
9244bb72 TL |
10 | // to submit extra params on load and submit, useful, e.g., if not all ID |
11 | // parameters are included in the URL | |
12 | extraRequestParams: {}, | |
13 | ||
06694509 DM |
14 | resizable: false, |
15 | ||
1289d326 | 16 | // use this to atimatically generate a title like `Create: <subject>` |
06694509 DM |
17 | subject: undefined, |
18 | ||
1289d326 | 19 | // set isCreate to true if you want a Create button (instead OK and RESET) |
b33f451f | 20 | isCreate: false, |
06694509 DM |
21 | |
22 | // set to true if you want an Add button (instead of Create) | |
23 | isAdd: false, | |
24 | ||
25 | // set to true if you want an Remove button (instead of Create) | |
26 | isRemove: false, | |
27 | ||
95bba12d AL |
28 | // set to false, if you don't want the reset button present |
29 | showReset: true, | |
30 | ||
ffea05ec DC |
31 | // custom submitText |
32 | submitText: undefined, | |
33 | ||
06694509 DM |
34 | backgroundDelay: 0, |
35 | ||
4d07ee6c TL |
36 | // string or function, called as (url, values) - useful if the ID of the |
37 | // new object is part of the URL, or that URL differs from GET/PUT URL | |
38 | submitUrl: Ext.identityFn, | |
39 | ||
8b121714 TL |
40 | // string or function, called as (url, initialConfig) - mostly for |
41 | // consistency with submitUrl existing. If both are set `url` gets optional | |
42 | loadUrl: Ext.identityFn, | |
43 | ||
a7dcbdeb DM |
44 | // needed for finding the reference to submitbutton |
45 | // because we do not have a controller | |
46 | referenceHolder: true, | |
47 | defaultButton: 'submitbutton', | |
48 | ||
49 | // finds the first form field | |
4f3b2a93 | 50 | defaultFocus: 'field:focusable[disabled=false][hidden=false]', |
a7dcbdeb | 51 | |
06694509 DM |
52 | showProgress: false, |
53 | ||
8d8dbfc5 TL |
54 | showTaskViewer: false, |
55 | ||
fde8e8bb TL |
56 | // gets called if we have a progress bar or taskview and it detected that |
57 | // the task finished. function(success) | |
58 | taskDone: Ext.emptyFn, | |
59 | ||
a498f279 DC |
60 | // gets called when the api call is finished, right at the beginning |
61 | // function(success, response, options) | |
62 | apiCallDone: Ext.emptyFn, | |
63 | ||
42a9df8b DC |
64 | // assign a reference from docs, to add a help button docked to the |
65 | // bottom of the window. If undefined we magically fall back to the | |
66 | // onlineHelp of our first item, if set. | |
67 | onlineHelp: undefined, | |
68 | ||
06694509 | 69 | isValid: function() { |
4b5c6b3f | 70 | let me = this; |
06694509 | 71 | |
4b5c6b3f | 72 | let form = me.formPanel.getForm(); |
06694509 DM |
73 | return form.isValid(); |
74 | }, | |
75 | ||
76 | getValues: function(dirtyOnly) { | |
4b5c6b3f | 77 | let me = this; |
06694509 | 78 | |
1289d326 | 79 | let values = {}; |
9244bb72 TL |
80 | Ext.apply(values, me.extraRequestParams); |
81 | ||
4b5c6b3f | 82 | let form = me.formPanel.getForm(); |
06694509 | 83 | |
1289d326 TL |
84 | form.getFields().each(function(field) { |
85 | if (!field.up('inputpanel') && (!dirtyOnly || field.isDirty())) { | |
86 | Proxmox.Utils.assemble_field_data(values, field.getSubmitData()); | |
87 | } | |
88 | }); | |
06694509 DM |
89 | |
90 | Ext.Array.each(me.query('inputpanel'), function(panel) { | |
91 | Proxmox.Utils.assemble_field_data(values, panel.getValues(dirtyOnly)); | |
92 | }); | |
93 | ||
94 | return values; | |
95 | }, | |
96 | ||
97 | setValues: function(values) { | |
4b5c6b3f | 98 | let me = this; |
06694509 | 99 | |
4b5c6b3f | 100 | let form = me.formPanel.getForm(); |
b24601af TL |
101 | let formfields = form.getFields(); |
102 | ||
103 | Ext.iterate(values, function(id, val) { | |
104 | let fields = formfields.filterBy((f) => | |
01031528 | 105 | (f.id === id || f.name === id || f.dataIndex === id) && !f.up('inputpanel'), |
b24601af TL |
106 | ); |
107 | fields.each((field) => { | |
108 | field.setValue(val); | |
68689d73 TL |
109 | if (form.trackResetOnLoad) { |
110 | field.resetOriginalValue(); | |
111 | } | |
b24601af | 112 | }); |
06694509 | 113 | }); |
1b07a95a | 114 | |
06694509 DM |
115 | Ext.Array.each(me.query('inputpanel'), function(panel) { |
116 | panel.setValues(values); | |
117 | }); | |
118 | }, | |
119 | ||
4e78d719 TL |
120 | setSubmitText: function(text) { |
121 | this.lookup('submitbutton').setText(text); | |
122 | }, | |
123 | ||
06694509 | 124 | submit: function() { |
4b5c6b3f | 125 | let me = this; |
06694509 | 126 | |
4b5c6b3f | 127 | let form = me.formPanel.getForm(); |
06694509 | 128 | |
4b5c6b3f | 129 | let values = me.getValues(); |
06694509 | 130 | Ext.Object.each(values, function(name, val) { |
4b5c6b3f TL |
131 | if (Object.prototype.hasOwnProperty.call(values, name)) { |
132 | if (Ext.isArray(val) && !val.length) { | |
06694509 DM |
133 | values[name] = ''; |
134 | } | |
135 | } | |
136 | }); | |
137 | ||
138 | if (me.digest) { | |
139 | values.digest = me.digest; | |
140 | } | |
141 | ||
142 | if (me.backgroundDelay) { | |
143 | values.background_delay = me.backgroundDelay; | |
144 | } | |
145 | ||
4d07ee6c TL |
146 | let url = Ext.isFunction(me.submitUrl) |
147 | ? me.submitUrl(me.url, values) | |
148 | : me.submitUrl || me.url; | |
06694509 DM |
149 | if (me.method === 'DELETE') { |
150 | url = url + "?" + Ext.Object.toQueryString(values); | |
151 | values = undefined; | |
152 | } | |
153 | ||
154 | Proxmox.Utils.API2Request({ | |
155 | url: url, | |
156 | waitMsgTarget: me, | |
157 | method: me.method || (me.backgroundDelay ? 'POST' : 'PUT'), | |
158 | params: values, | |
159 | failure: function(response, options) { | |
a498f279 DC |
160 | me.apiCallDone(false, response, options); |
161 | ||
06694509 DM |
162 | if (response.result && response.result.errors) { |
163 | form.markInvalid(response.result.errors); | |
164 | } | |
165 | Ext.Msg.alert(gettext('Error'), response.htmlStatus); | |
166 | }, | |
167 | success: function(response, options) { | |
4b5c6b3f TL |
168 | let hasProgressBar = |
169 | (me.backgroundDelay || me.showProgress || me.showTaskViewer) && | |
170 | response.result.data; | |
06694509 | 171 | |
a498f279 DC |
172 | me.apiCallDone(true, response, options); |
173 | ||
06694509 DM |
174 | if (hasProgressBar) { |
175 | // stay around so we can trigger our close events | |
176 | // when background action is completed | |
177 | me.hide(); | |
178 | ||
4b5c6b3f TL |
179 | let upid = response.result.data; |
180 | let viewerClass = me.showTaskViewer ? 'Viewer' : 'Progress'; | |
181 | Ext.create('Proxmox.window.Task' + viewerClass, { | |
182 | autoShow: true, | |
06694509 | 183 | upid: upid, |
fde8e8bb | 184 | taskDone: me.taskDone, |
06694509 | 185 | listeners: { |
4b5c6b3f | 186 | destroy: function() { |
06694509 | 187 | me.close(); |
4b5c6b3f TL |
188 | }, |
189 | }, | |
06694509 | 190 | }); |
06694509 DM |
191 | } else { |
192 | me.close(); | |
193 | } | |
4b5c6b3f | 194 | }, |
06694509 DM |
195 | }); |
196 | }, | |
197 | ||
198 | load: function(options) { | |
4b5c6b3f | 199 | let me = this; |
06694509 | 200 | |
4b5c6b3f | 201 | let form = me.formPanel.getForm(); |
06694509 DM |
202 | |
203 | options = options || {}; | |
204 | ||
4b5c6b3f TL |
205 | let newopts = Ext.apply({ |
206 | waitMsgTarget: me, | |
06694509 DM |
207 | }, options); |
208 | ||
9244bb72 TL |
209 | if (Object.keys(me.extraRequestParams).length > 0) { |
210 | let params = newopts.params || {}; | |
211 | Ext.applyIf(params, me.extraRequestParams); | |
212 | newopts.params = params; | |
213 | } | |
214 | ||
8b121714 TL |
215 | let url = Ext.isFunction(me.loadUrl) |
216 | ? me.loadUrl(me.url, me.initialConfig) | |
217 | : me.loadUrl || me.url; | |
218 | ||
4b5c6b3f | 219 | let createWrapper = function(successFn) { |
06694509 | 220 | Ext.apply(newopts, { |
8b121714 | 221 | url: url, |
06694509 DM |
222 | method: 'GET', |
223 | success: function(response, opts) { | |
224 | form.clearInvalid(); | |
8ae2f8c8 | 225 | me.digest = response.result.digest || response.result.data.digest; |
06694509 DM |
226 | if (successFn) { |
227 | successFn(response, opts); | |
228 | } else { | |
229 | me.setValues(response.result.data); | |
230 | } | |
231 | // hack: fix ExtJS bug | |
232 | Ext.Array.each(me.query('radiofield'), function(f) { | |
233 | f.resetOriginalValue(); | |
234 | }); | |
235 | }, | |
236 | failure: function(response, opts) { | |
237 | Ext.Msg.alert(gettext('Error'), response.htmlStatus, function() { | |
238 | me.close(); | |
239 | }); | |
4b5c6b3f | 240 | }, |
06694509 DM |
241 | }); |
242 | }; | |
243 | ||
244 | createWrapper(options.success); | |
245 | ||
246 | Proxmox.Utils.API2Request(newopts); | |
247 | }, | |
248 | ||
4b5c6b3f TL |
249 | initComponent: function() { |
250 | let me = this; | |
06694509 | 251 | |
8969bdad TL |
252 | if (!me.url && ( |
253 | !me.submitUrl || !me.loadUrl || me.submitUrl === Ext.identityFn || | |
254 | me.loadUrl === Ext.identityFn | |
255 | ) | |
256 | ) { | |
257 | throw "neither 'url' nor both, submitUrl and loadUrl specified"; | |
06694509 DM |
258 | } |
259 | ||
b33f451f DC |
260 | if (me.create) {throw "deprecated parameter, use isCreate";} |
261 | ||
4b5c6b3f | 262 | let items = Ext.isArray(me.items) ? me.items : [me.items]; |
06694509 DM |
263 | |
264 | me.items = undefined; | |
265 | ||
266 | me.formPanel = Ext.create('Ext.form.Panel', { | |
8969bdad | 267 | url: me.url, // FIXME: not in 'form' class, safe to remove?? |
06694509 DM |
268 | method: me.method || 'PUT', |
269 | trackResetOnLoad: true, | |
271171f8 | 270 | bodyPadding: me.bodyPadding !== undefined ? me.bodyPadding : 10, |
06694509 | 271 | border: false, |
31a50251 | 272 | defaults: Ext.apply({}, me.defaults, { |
4b5c6b3f | 273 | border: false, |
31a50251 | 274 | }), |
06694509 DM |
275 | fieldDefaults: Ext.apply({}, me.fieldDefaults, { |
276 | labelWidth: 100, | |
4b5c6b3f | 277 | anchor: '100%', |
06694509 | 278 | }), |
4b5c6b3f | 279 | items: items, |
06694509 DM |
280 | }); |
281 | ||
4b5c6b3f | 282 | let inputPanel = me.formPanel.down('inputpanel'); |
1b07a95a | 283 | |
4b5c6b3f | 284 | let form = me.formPanel.getForm(); |
06694509 | 285 | |
4b5c6b3f | 286 | let submitText; |
b33f451f | 287 | if (me.isCreate) { |
ffea05ec DC |
288 | if (me.submitText) { |
289 | submitText = me.submitText; | |
290 | } else if (me.isAdd) { | |
06694509 DM |
291 | submitText = gettext('Add'); |
292 | } else if (me.isRemove) { | |
293 | submitText = gettext('Remove'); | |
294 | } else { | |
295 | submitText = gettext('Create'); | |
296 | } | |
297 | } else { | |
ffea05ec | 298 | submitText = me.submitText || gettext('OK'); |
06694509 DM |
299 | } |
300 | ||
4b5c6b3f | 301 | let submitBtn = Ext.create('Ext.Button', { |
a7dcbdeb | 302 | reference: 'submitbutton', |
06694509 | 303 | text: submitText, |
b33f451f | 304 | disabled: !me.isCreate, |
06694509 DM |
305 | handler: function() { |
306 | me.submit(); | |
4b5c6b3f | 307 | }, |
06694509 DM |
308 | }); |
309 | ||
4b5c6b3f | 310 | let resetBtn = Ext.create('Ext.Button', { |
06694509 DM |
311 | text: 'Reset', |
312 | disabled: true, | |
4b5c6b3f | 313 | handler: function() { |
06694509 | 314 | form.reset(); |
4b5c6b3f | 315 | }, |
06694509 DM |
316 | }); |
317 | ||
4b5c6b3f TL |
318 | let set_button_status = function() { |
319 | let valid = form.isValid(); | |
320 | let dirty = form.isDirty(); | |
b33f451f | 321 | submitBtn.setDisabled(!valid || !(dirty || me.isCreate)); |
06694509 | 322 | resetBtn.setDisabled(!dirty); |
880df5d5 DC |
323 | |
324 | if (inputPanel && inputPanel.hasAdvanced) { | |
325 | // we want to show the advanced options | |
326 | // as soon as some of it is not valid | |
4b5c6b3f TL |
327 | let advancedItems = me.down('#advancedContainer').query('field'); |
328 | let allAdvancedValid = true; | |
880df5d5 DC |
329 | advancedItems.forEach(function(field) { |
330 | if (!field.isValid()) { | |
4b5c6b3f | 331 | allAdvancedValid = false; |
880df5d5 DC |
332 | } |
333 | }); | |
334 | ||
4b5c6b3f | 335 | if (!allAdvancedValid) { |
880df5d5 | 336 | inputPanel.setAdvancedVisible(true); |
880df5d5 DC |
337 | } |
338 | } | |
06694509 DM |
339 | }; |
340 | ||
341 | form.on('dirtychange', set_button_status); | |
342 | form.on('validitychange', set_button_status); | |
343 | ||
4b5c6b3f | 344 | let colwidth = 300; |
06694509 DM |
345 | if (me.fieldDefaults && me.fieldDefaults.labelWidth) { |
346 | colwidth += me.fieldDefaults.labelWidth - 100; | |
347 | } | |
06694509 | 348 | |
4b5c6b3f | 349 | let twoColumn = inputPanel && (inputPanel.column1 || inputPanel.column2); |
06694509 DM |
350 | |
351 | if (me.subject && !me.title) { | |
b33f451f | 352 | me.title = Proxmox.Utils.dialog_title(me.subject, me.isCreate, me.isAdd); |
06694509 DM |
353 | } |
354 | ||
95bba12d | 355 | if (me.isCreate || !me.showReset) { |
4b5c6b3f | 356 | me.buttons = [submitBtn]; |
06694509 | 357 | } else { |
4b5c6b3f | 358 | me.buttons = [submitBtn, resetBtn]; |
06694509 DM |
359 | } |
360 | ||
880df5d5 | 361 | if (inputPanel && inputPanel.hasAdvanced) { |
4b5c6b3f TL |
362 | let sp = Ext.state.Manager.getProvider(); |
363 | let advchecked = sp.get('proxmox-advanced-cb'); | |
880df5d5 DC |
364 | inputPanel.setAdvancedVisible(advchecked); |
365 | me.buttons.unshift( | |
366 | { | |
367 | xtype: 'proxmoxcheckbox', | |
368 | itemId: 'advancedcb', | |
369 | boxLabelAlign: 'before', | |
370 | boxLabel: gettext('Advanced'), | |
371 | stateId: 'proxmox-advanced-cb', | |
372 | value: advchecked, | |
373 | listeners: { | |
374 | change: function(cb, val) { | |
375 | inputPanel.setAdvancedVisible(val); | |
376 | sp.set('proxmox-advanced-cb', val); | |
4b5c6b3f TL |
377 | }, |
378 | }, | |
379 | }, | |
880df5d5 DC |
380 | ); |
381 | } | |
382 | ||
4b5c6b3f | 383 | let onlineHelp = me.onlineHelp; |
c3457485 DM |
384 | if (!onlineHelp && inputPanel && inputPanel.onlineHelp) { |
385 | onlineHelp = inputPanel.onlineHelp; | |
386 | } | |
387 | ||
388 | if (onlineHelp) { | |
4b5c6b3f | 389 | let helpButton = Ext.create('Proxmox.button.Help'); |
06694509 | 390 | me.buttons.unshift(helpButton, '->'); |
c3457485 | 391 | Ext.GlobalEvents.fireEvent('proxmoxShowHelp', onlineHelp); |
06694509 DM |
392 | } |
393 | ||
394 | Ext.applyIf(me, { | |
395 | modal: true, | |
396 | width: twoColumn ? colwidth*2 : colwidth, | |
397 | border: false, | |
4b5c6b3f | 398 | items: [me.formPanel], |
06694509 DM |
399 | }); |
400 | ||
401 | me.callParent(); | |
402 | ||
403 | // always mark invalid fields | |
404 | me.on('afterlayout', function() { | |
405 | // on touch devices, the isValid function | |
406 | // triggers a layout, which triggers an isValid | |
407 | // and so on | |
408 | // to prevent this we disable the layouting here | |
409 | // and enable it afterwards | |
410 | me.suspendLayout = true; | |
411 | me.isValid(); | |
412 | me.suspendLayout = false; | |
413 | }); | |
a33ba257 DM |
414 | |
415 | if (me.autoLoad) { | |
ca204f31 | 416 | me.load(me.autoLoadOptions); |
a33ba257 | 417 | } |
4b5c6b3f | 418 | }, |
06694509 | 419 | }); |