]>
git.proxmox.com Git - proxmox-widget-toolkit.git/blob - src/window/Edit.js
1 Ext
.define('Proxmox.window.Edit', {
2 extend
: 'Ext.window.Window',
3 alias
: 'widget.proxmoxWindowEdit',
5 // autoLoad trigger a load() after component creation
7 // set extra options like params for the load request
8 autoLoadOptions
: undefined,
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
: {},
16 // use this to atimatically generate a title like `Create: <subject>`
19 // set isCreate to true if you want a Create button (instead OK and RESET)
22 // set to true if you want an Add button (instead of Create)
25 // set to true if you want an Remove button (instead of Create)
29 submitText
: undefined,
33 // needed for finding the reference to submitbutton
34 // because we do not have a controller
35 referenceHolder
: true,
36 defaultButton
: 'submitbutton',
38 // finds the first form field
39 defaultFocus
: 'field:focusable[disabled=false][hidden=false]',
43 showTaskViewer
: false,
45 // gets called if we have a progress bar or taskview and it detected that
46 // the task finished. function(success)
47 taskDone
: Ext
.emptyFn
,
49 // gets called when the api call is finished, right at the beginning
50 // function(success, response, options)
51 apiCallDone
: Ext
.emptyFn
,
53 // assign a reference from docs, to add a help button docked to the
54 // bottom of the window. If undefined we magically fall back to the
55 // onlineHelp of our first item, if set.
56 onlineHelp
: undefined,
61 let form
= me
.formPanel
.getForm();
62 return form
.isValid();
65 getValues: function(dirtyOnly
) {
69 Ext
.apply(values
, me
.extraRequestParams
);
71 let form
= me
.formPanel
.getForm();
73 form
.getFields().each(function(field
) {
74 if (!field
.up('inputpanel') && (!dirtyOnly
|| field
.isDirty())) {
75 Proxmox
.Utils
.assemble_field_data(values
, field
.getSubmitData());
79 Ext
.Array
.each(me
.query('inputpanel'), function(panel
) {
80 Proxmox
.Utils
.assemble_field_data(values
, panel
.getValues(dirtyOnly
));
86 setValues: function(values
) {
89 let form
= me
.formPanel
.getForm();
90 let formfields
= form
.getFields();
92 Ext
.iterate(values
, function(id
, val
) {
93 let fields
= formfields
.filterBy((f
) =>
94 (f
.id
=== id
|| f
.name
=== id
|| f
.dataIndex
=== id
) && !f
.up('inputpanel'),
96 fields
.each((field
) => {
98 if (form
.trackResetOnLoad
) {
99 field
.resetOriginalValue();
104 Ext
.Array
.each(me
.query('inputpanel'), function(panel
) {
105 panel
.setValues(values
);
109 setSubmitText: function(text
) {
110 this.lookup('submitbutton').setText(text
);
116 let form
= me
.formPanel
.getForm();
118 let values
= me
.getValues();
119 Ext
.Object
.each(values
, function(name
, val
) {
120 if (Object
.prototype.hasOwnProperty
.call(values
, name
)) {
121 if (Ext
.isArray(val
) && !val
.length
) {
128 values
.digest
= me
.digest
;
131 if (me
.backgroundDelay
) {
132 values
.background_delay
= me
.backgroundDelay
;
136 if (me
.method
=== 'DELETE') {
137 url
= url
+ "?" + Ext
.Object
.toQueryString(values
);
141 Proxmox
.Utils
.API2Request({
144 method
: me
.method
|| (me
.backgroundDelay
? 'POST' : 'PUT'),
146 failure: function(response
, options
) {
147 me
.apiCallDone(false, response
, options
);
149 if (response
.result
&& response
.result
.errors
) {
150 form
.markInvalid(response
.result
.errors
);
152 Ext
.Msg
.alert(gettext('Error'), response
.htmlStatus
);
154 success: function(response
, options
) {
156 (me
.backgroundDelay
|| me
.showProgress
|| me
.showTaskViewer
) &&
157 response
.result
.data
;
159 me
.apiCallDone(true, response
, options
);
161 if (hasProgressBar
) {
162 // stay around so we can trigger our close events
163 // when background action is completed
166 let upid
= response
.result
.data
;
167 let viewerClass
= me
.showTaskViewer
? 'Viewer' : 'Progress';
168 Ext
.create('Proxmox.window.Task' + viewerClass
, {
171 taskDone
: me
.taskDone
,
173 destroy: function() {
185 load: function(options
) {
188 let form
= me
.formPanel
.getForm();
190 options
= options
|| {};
192 let newopts
= Ext
.apply({
196 if (Object
.keys(me
.extraRequestParams
).length
> 0) {
197 let params
= newopts
.params
|| {};
198 Ext
.applyIf(params
, me
.extraRequestParams
);
199 newopts
.params
= params
;
202 let createWrapper = function(successFn
) {
206 success: function(response
, opts
) {
208 me
.digest
= response
.result
.digest
|| response
.result
.data
.digest
;
210 successFn(response
, opts
);
212 me
.setValues(response
.result
.data
);
214 // hack: fix ExtJS bug
215 Ext
.Array
.each(me
.query('radiofield'), function(f
) {
216 f
.resetOriginalValue();
219 failure: function(response
, opts
) {
220 Ext
.Msg
.alert(gettext('Error'), response
.htmlStatus
, function() {
227 createWrapper(options
.success
);
229 Proxmox
.Utils
.API2Request(newopts
);
232 initComponent: function() {
236 throw "no url specified";
239 if (me
.create
) {throw "deprecated parameter, use isCreate";}
241 let items
= Ext
.isArray(me
.items
) ? me
.items
: [me
.items
];
243 me
.items
= undefined;
245 me
.formPanel
= Ext
.create('Ext.form.Panel', {
247 method
: me
.method
|| 'PUT',
248 trackResetOnLoad
: true,
249 bodyPadding
: me
.bodyPadding
!== undefined ? me
.bodyPadding
: 10,
251 defaults
: Ext
.apply({}, me
.defaults
, {
254 fieldDefaults
: Ext
.apply({}, me
.fieldDefaults
, {
261 let inputPanel
= me
.formPanel
.down('inputpanel');
263 let form
= me
.formPanel
.getForm();
268 submitText
= me
.submitText
;
269 } else if (me
.isAdd
) {
270 submitText
= gettext('Add');
271 } else if (me
.isRemove
) {
272 submitText
= gettext('Remove');
274 submitText
= gettext('Create');
277 submitText
= me
.submitText
|| gettext('OK');
280 let submitBtn
= Ext
.create('Ext.Button', {
281 reference
: 'submitbutton',
283 disabled
: !me
.isCreate
,
284 handler: function() {
289 let resetBtn
= Ext
.create('Ext.Button', {
292 handler: function() {
297 let set_button_status = function() {
298 let valid
= form
.isValid();
299 let dirty
= form
.isDirty();
300 submitBtn
.setDisabled(!valid
|| !(dirty
|| me
.isCreate
));
301 resetBtn
.setDisabled(!dirty
);
303 if (inputPanel
&& inputPanel
.hasAdvanced
) {
304 // we want to show the advanced options
305 // as soon as some of it is not valid
306 let advancedItems
= me
.down('#advancedContainer').query('field');
307 let allAdvancedValid
= true;
308 advancedItems
.forEach(function(field
) {
309 if (!field
.isValid()) {
310 allAdvancedValid
= false;
314 if (!allAdvancedValid
) {
315 inputPanel
.setAdvancedVisible(true);
316 me
.down('#advancedcb').setValue(true);
321 form
.on('dirtychange', set_button_status
);
322 form
.on('validitychange', set_button_status
);
325 if (me
.fieldDefaults
&& me
.fieldDefaults
.labelWidth
) {
326 colwidth
+= me
.fieldDefaults
.labelWidth
- 100;
329 let twoColumn
= inputPanel
&& (inputPanel
.column1
|| inputPanel
.column2
);
331 if (me
.subject
&& !me
.title
) {
332 me
.title
= Proxmox
.Utils
.dialog_title(me
.subject
, me
.isCreate
, me
.isAdd
);
336 me
.buttons
= [submitBtn
];
338 me
.buttons
= [submitBtn
, resetBtn
];
341 if (inputPanel
&& inputPanel
.hasAdvanced
) {
342 let sp
= Ext
.state
.Manager
.getProvider();
343 let advchecked
= sp
.get('proxmox-advanced-cb');
344 inputPanel
.setAdvancedVisible(advchecked
);
347 xtype
: 'proxmoxcheckbox',
348 itemId
: 'advancedcb',
349 boxLabelAlign
: 'before',
350 boxLabel
: gettext('Advanced'),
351 stateId
: 'proxmox-advanced-cb',
354 change: function(cb
, val
) {
355 inputPanel
.setAdvancedVisible(val
);
356 sp
.set('proxmox-advanced-cb', val
);
363 let onlineHelp
= me
.onlineHelp
;
364 if (!onlineHelp
&& inputPanel
&& inputPanel
.onlineHelp
) {
365 onlineHelp
= inputPanel
.onlineHelp
;
369 let helpButton
= Ext
.create('Proxmox.button.Help');
370 me
.buttons
.unshift(helpButton
, '->');
371 Ext
.GlobalEvents
.fireEvent('proxmoxShowHelp', onlineHelp
);
376 width
: twoColumn
? colwidth
*2 : colwidth
,
378 items
: [me
.formPanel
],
383 // always mark invalid fields
384 me
.on('afterlayout', function() {
385 // on touch devices, the isValid function
386 // triggers a layout, which triggers an isValid
388 // to prevent this we disable the layouting here
389 // and enable it afterwards
390 me
.suspendLayout
= true;
392 me
.suspendLayout
= false;
396 me
.load(me
.autoLoadOptions
);