]>
Commit | Line | Data |
---|---|---|
06694509 DM |
1 | // fixme: how can we avoid those lint errors? |
2 | /*jslint confusion: true */ | |
3 | Ext.define('Proxmox.window.Edit', { | |
4 | extend: 'Ext.window.Window', | |
5 | alias: 'widget.proxmoxWindowEdit', | |
1b07a95a | 6 | |
06694509 DM |
7 | resizable: false, |
8 | ||
9 | // use this tio atimatically generate a title like | |
10 | // Create: <subject> | |
11 | subject: undefined, | |
12 | ||
1b07a95a DM |
13 | // set create to true if you want a Create button (instead |
14 | // OK and RESET) | |
15 | create: false, | |
06694509 DM |
16 | |
17 | // set to true if you want an Add button (instead of Create) | |
18 | isAdd: false, | |
19 | ||
20 | // set to true if you want an Remove button (instead of Create) | |
21 | isRemove: false, | |
22 | ||
23 | backgroundDelay: 0, | |
24 | ||
25 | showProgress: false, | |
26 | ||
27 | isValid: function() { | |
28 | var me = this; | |
29 | ||
30 | var form = me.formPanel.getForm(); | |
31 | return form.isValid(); | |
32 | }, | |
33 | ||
34 | getValues: function(dirtyOnly) { | |
35 | var me = this; | |
36 | ||
37 | var values = {}; | |
38 | ||
39 | var form = me.formPanel.getForm(); | |
40 | ||
41 | form.getFields().each(function(field) { | |
42 | if (!field.up('inputpanel') && (!dirtyOnly || field.isDirty())) { | |
43 | Proxmox.Utils.assemble_field_data(values, field.getSubmitData()); | |
44 | } | |
45 | }); | |
46 | ||
47 | Ext.Array.each(me.query('inputpanel'), function(panel) { | |
48 | Proxmox.Utils.assemble_field_data(values, panel.getValues(dirtyOnly)); | |
49 | }); | |
50 | ||
51 | return values; | |
52 | }, | |
53 | ||
54 | setValues: function(values) { | |
55 | var me = this; | |
56 | ||
57 | var form = me.formPanel.getForm(); | |
58 | ||
59 | Ext.iterate(values, function(fieldId, val) { | |
60 | var field = form.findField(fieldId); | |
61 | if (field && !field.up('inputpanel')) { | |
62 | field.setValue(val); | |
63 | if (form.trackResetOnLoad) { | |
64 | field.resetOriginalValue(); | |
65 | } | |
66 | } | |
67 | }); | |
1b07a95a | 68 | |
06694509 DM |
69 | Ext.Array.each(me.query('inputpanel'), function(panel) { |
70 | panel.setValues(values); | |
71 | }); | |
72 | }, | |
73 | ||
74 | submit: function() { | |
75 | var me = this; | |
76 | ||
77 | var form = me.formPanel.getForm(); | |
78 | ||
79 | var values = me.getValues(); | |
80 | Ext.Object.each(values, function(name, val) { | |
81 | if (values.hasOwnProperty(name)) { | |
82 | if (Ext.isArray(val) && !val.length) { | |
83 | values[name] = ''; | |
84 | } | |
85 | } | |
86 | }); | |
87 | ||
88 | if (me.digest) { | |
89 | values.digest = me.digest; | |
90 | } | |
91 | ||
92 | if (me.backgroundDelay) { | |
93 | values.background_delay = me.backgroundDelay; | |
94 | } | |
95 | ||
96 | var url = me.url; | |
97 | if (me.method === 'DELETE') { | |
98 | url = url + "?" + Ext.Object.toQueryString(values); | |
99 | values = undefined; | |
100 | } | |
101 | ||
102 | Proxmox.Utils.API2Request({ | |
103 | url: url, | |
104 | waitMsgTarget: me, | |
105 | method: me.method || (me.backgroundDelay ? 'POST' : 'PUT'), | |
106 | params: values, | |
107 | failure: function(response, options) { | |
108 | if (response.result && response.result.errors) { | |
109 | form.markInvalid(response.result.errors); | |
110 | } | |
111 | Ext.Msg.alert(gettext('Error'), response.htmlStatus); | |
112 | }, | |
113 | success: function(response, options) { | |
114 | var hasProgressBar = (me.backgroundDelay || me.showProgress) && | |
115 | response.result.data ? true : false; | |
116 | ||
117 | if (hasProgressBar) { | |
118 | // stay around so we can trigger our close events | |
119 | // when background action is completed | |
120 | me.hide(); | |
121 | ||
122 | var upid = response.result.data; | |
1b07a95a | 123 | var win = Ext.create('PVE.window.TaskProgress', { |
06694509 DM |
124 | upid: upid, |
125 | listeners: { | |
126 | destroy: function () { | |
127 | me.close(); | |
128 | } | |
129 | } | |
130 | }); | |
131 | win.show(); | |
132 | } else { | |
133 | me.close(); | |
134 | } | |
135 | } | |
136 | }); | |
137 | }, | |
138 | ||
139 | load: function(options) { | |
140 | var me = this; | |
141 | ||
142 | var form = me.formPanel.getForm(); | |
143 | ||
144 | options = options || {}; | |
145 | ||
146 | var newopts = Ext.apply({ | |
147 | waitMsgTarget: me | |
148 | }, options); | |
149 | ||
150 | var createWrapper = function(successFn) { | |
151 | Ext.apply(newopts, { | |
152 | url: me.url, | |
153 | method: 'GET', | |
154 | success: function(response, opts) { | |
155 | form.clearInvalid(); | |
156 | me.digest = response.result.data.digest; | |
157 | if (successFn) { | |
158 | successFn(response, opts); | |
159 | } else { | |
160 | me.setValues(response.result.data); | |
161 | } | |
162 | // hack: fix ExtJS bug | |
163 | Ext.Array.each(me.query('radiofield'), function(f) { | |
164 | f.resetOriginalValue(); | |
165 | }); | |
166 | }, | |
167 | failure: function(response, opts) { | |
168 | Ext.Msg.alert(gettext('Error'), response.htmlStatus, function() { | |
169 | me.close(); | |
170 | }); | |
171 | } | |
172 | }); | |
173 | }; | |
174 | ||
175 | createWrapper(options.success); | |
176 | ||
177 | Proxmox.Utils.API2Request(newopts); | |
178 | }, | |
179 | ||
180 | initComponent : function() { | |
181 | var me = this; | |
182 | ||
183 | if (!me.url) { | |
184 | throw "no url specified"; | |
185 | } | |
186 | ||
187 | var items = Ext.isArray(me.items) ? me.items : [ me.items ]; | |
188 | ||
189 | me.items = undefined; | |
190 | ||
191 | me.formPanel = Ext.create('Ext.form.Panel', { | |
192 | url: me.url, | |
193 | method: me.method || 'PUT', | |
194 | trackResetOnLoad: true, | |
195 | bodyPadding: 10, | |
196 | border: false, | |
197 | defaults: { | |
198 | border: false | |
199 | }, | |
200 | fieldDefaults: Ext.apply({}, me.fieldDefaults, { | |
201 | labelWidth: 100, | |
202 | anchor: '100%' | |
203 | }), | |
204 | items: items | |
205 | }); | |
206 | ||
1b07a95a DM |
207 | var inputPanel = me.formPanel.down('inputpanel'); |
208 | ||
06694509 DM |
209 | var form = me.formPanel.getForm(); |
210 | ||
211 | var submitText; | |
212 | if (me.create) { | |
213 | if (me.isAdd) { | |
214 | submitText = gettext('Add'); | |
215 | } else if (me.isRemove) { | |
216 | submitText = gettext('Remove'); | |
217 | } else { | |
218 | submitText = gettext('Create'); | |
219 | } | |
220 | } else { | |
221 | submitText = gettext('OK'); | |
222 | } | |
223 | ||
224 | var submitBtn = Ext.create('Ext.Button', { | |
225 | text: submitText, | |
226 | disabled: !me.create, | |
227 | handler: function() { | |
228 | me.submit(); | |
229 | } | |
230 | }); | |
231 | ||
232 | var resetBtn = Ext.create('Ext.Button', { | |
233 | text: 'Reset', | |
234 | disabled: true, | |
235 | handler: function(){ | |
236 | form.reset(); | |
237 | } | |
238 | }); | |
239 | ||
240 | var set_button_status = function() { | |
241 | var valid = form.isValid(); | |
242 | var dirty = form.isDirty(); | |
243 | submitBtn.setDisabled(!valid || !(dirty || me.create)); | |
244 | resetBtn.setDisabled(!dirty); | |
245 | }; | |
246 | ||
247 | form.on('dirtychange', set_button_status); | |
248 | form.on('validitychange', set_button_status); | |
249 | ||
250 | var colwidth = 300; | |
251 | if (me.fieldDefaults && me.fieldDefaults.labelWidth) { | |
252 | colwidth += me.fieldDefaults.labelWidth - 100; | |
253 | } | |
06694509 | 254 | |
1b07a95a DM |
255 | var twoColumn = inputPanel && |
256 | (inputPanel.column1 || inputPanel.column2); | |
06694509 DM |
257 | |
258 | if (me.subject && !me.title) { | |
259 | me.title = Proxmox.Utils.dialog_title(me.subject, me.create, me.isAdd); | |
260 | } | |
261 | ||
262 | if (me.create) { | |
263 | me.buttons = [ submitBtn ] ; | |
264 | } else { | |
265 | me.buttons = [ submitBtn, resetBtn ]; | |
266 | } | |
267 | ||
1b07a95a | 268 | if (inputPanel && inputPanel.onlineHelp) { |
06694509 DM |
269 | var helpButton = Ext.create('PVE.button.Help'); |
270 | me.buttons.unshift(helpButton, '->'); | |
271 | Ext.GlobalEvents.fireEvent('pveShowHelp', items[0].onlineHelp); | |
272 | } | |
273 | ||
274 | Ext.applyIf(me, { | |
275 | modal: true, | |
276 | width: twoColumn ? colwidth*2 : colwidth, | |
277 | border: false, | |
278 | items: [ me.formPanel ] | |
279 | }); | |
280 | ||
281 | me.callParent(); | |
282 | ||
283 | // always mark invalid fields | |
284 | me.on('afterlayout', function() { | |
285 | // on touch devices, the isValid function | |
286 | // triggers a layout, which triggers an isValid | |
287 | // and so on | |
288 | // to prevent this we disable the layouting here | |
289 | // and enable it afterwards | |
290 | me.suspendLayout = true; | |
291 | me.isValid(); | |
292 | me.suspendLayout = false; | |
293 | }); | |
294 | } | |
295 | }); |