]>
Commit | Line | Data |
---|---|---|
ee93ce96 DM |
1 | Ext.define('PVE.window.Wizard', { |
2 | extend: 'Ext.window.Window', | |
f677074b DM |
3 | |
4 | activeTitle: '', // used for automated testing | |
bd2118c6 TL |
5 | |
6 | width: 700, | |
7 | height: 510, | |
8 | ||
9 | modal: true, | |
10 | border: false, | |
11 | ||
12 | draggable: true, | |
13 | closable: true, | |
14 | resizable: false, | |
15 | ||
16 | layout: 'border', | |
17 | ||
ee93ce96 DM |
18 | getValues: function(dirtyOnly) { |
19 | var me = this; | |
20 | ||
21 | var values = {}; | |
22 | ||
23 | var form = me.down('form').getForm(); | |
24 | ||
25 | form.getFields().each(function(field) { | |
26 | if (!field.up('inputpanel') && (!dirtyOnly || field.isDirty())) { | |
e7ade592 | 27 | Proxmox.Utils.assemble_field_data(values, field.getSubmitData()); |
ee93ce96 DM |
28 | } |
29 | }); | |
30 | ||
31 | Ext.Array.each(me.query('inputpanel'), function(panel) { | |
e7ade592 | 32 | Proxmox.Utils.assemble_field_data(values, panel.getValues(dirtyOnly)); |
ee93ce96 DM |
33 | }); |
34 | ||
35 | return values; | |
36 | }, | |
37 | ||
38 | initComponent: function() { | |
39 | var me = this; | |
40 | ||
41 | var tabs = me.items || []; | |
42 | delete me.items; | |
43 | ||
44 | /* | |
45 | * Items may have the following functions: | |
46 | * validator(): per tab custom validation | |
47 | * onSubmit(): submit handler | |
48 | * onGetValues(): overwrite getValues results | |
49 | */ | |
50 | ||
51 | Ext.Array.each(tabs, function(tab) { | |
52 | tab.disabled = true; | |
53 | }); | |
54 | tabs[0].disabled = false; | |
55 | ||
373d7ea4 DC |
56 | var maxidx = 0; |
57 | var curidx = 0; | |
58 | ||
ee93ce96 DM |
59 | var check_card = function(card) { |
60 | var valid = true; | |
61 | var fields = card.query('field, fieldcontainer'); | |
62 | if (card.isXType('fieldcontainer')) { | |
63 | fields.unshift(card); | |
64 | } | |
65 | Ext.Array.each(fields, function(field) { | |
66 | // Note: not all fielcontainer have isValid() | |
67 | if (Ext.isFunction(field.isValid) && !field.isValid()) { | |
68 | valid = false; | |
69 | } | |
70 | }); | |
71 | ||
72 | if (Ext.isFunction(card.validator)) { | |
73 | return card.validator(); | |
74 | } | |
75 | ||
76 | return valid; | |
77 | }; | |
78 | ||
ee93ce96 DM |
79 | var disable_at = function(card) { |
80 | var tp = me.down('#wizcontent'); | |
81 | var idx = tp.items.indexOf(card); | |
82 | for(;idx < tp.items.getCount();idx++) { | |
83 | var nc = tp.items.getAt(idx); | |
84 | if (nc) { | |
85 | nc.disable(); | |
86 | } | |
87 | } | |
88 | }; | |
89 | ||
90 | var tabchange = function(tp, newcard, oldcard) { | |
91 | if (newcard.onSubmit) { | |
92 | me.down('#next').setVisible(false); | |
93 | me.down('#submit').setVisible(true); | |
94 | } else { | |
95 | me.down('#next').setVisible(true); | |
96 | me.down('#submit').setVisible(false); | |
97 | } | |
98 | var valid = check_card(newcard); | |
99 | me.down('#next').setDisabled(!valid); | |
100 | me.down('#submit').setDisabled(!valid); | |
101 | me.down('#back').setDisabled(tp.items.indexOf(newcard) == 0); | |
102 | ||
373d7ea4 DC |
103 | var idx = tp.items.indexOf(newcard); |
104 | if (idx > maxidx) { | |
105 | maxidx = idx; | |
106 | } | |
107 | curidx = idx; | |
108 | ||
109 | var next = idx + 1; | |
ee93ce96 DM |
110 | var ntab = tp.items.getAt(next); |
111 | if (valid && ntab && !newcard.onSubmit) { | |
112 | ntab.enable(); | |
113 | } | |
114 | }; | |
115 | ||
116 | if (me.subject && !me.title) { | |
e7ade592 | 117 | me.title = Proxmox.Utils.dialog_title(me.subject, true, false); |
ee93ce96 DM |
118 | } |
119 | ||
60575f19 | 120 | Ext.apply(me, { |
ee93ce96 | 121 | items: [ |
ee93ce96 DM |
122 | { |
123 | xtype: 'form', | |
124 | region: 'center', | |
125 | layout: 'fit', | |
126 | border: false, | |
127 | margins: '5 5 0 5', | |
128 | fieldDefaults: { | |
129 | labelWidth: 100, | |
130 | anchor: '100%' | |
131 | }, | |
132 | items: [{ | |
133 | itemId: 'wizcontent', | |
134 | xtype: 'tabpanel', | |
135 | activeItem: 0, | |
136 | bodyPadding: 10, | |
137 | listeners: { | |
138 | afterrender: function(tp) { | |
139 | var atab = this.getActiveTab(); | |
140 | tabchange(tp, atab); | |
141 | }, | |
142 | tabchange: function(tp, newcard, oldcard) { | |
ee93ce96 DM |
143 | tabchange(tp, newcard, oldcard); |
144 | } | |
145 | }, | |
146 | items: tabs | |
147 | }] | |
18f4718f TL |
148 | } |
149 | ], | |
150 | fbar: [ | |
151 | { | |
152 | xtype: 'proxmoxHelpButton', | |
153 | itemId: 'help' | |
154 | }, | |
155 | '->', | |
156 | { | |
157 | text: gettext('Back'), | |
158 | disabled: true, | |
159 | itemId: 'back', | |
160 | minWidth: 60, | |
161 | handler: function() { | |
162 | var tp = me.down('#wizcontent'); | |
163 | var atab = tp.getActiveTab(); | |
164 | var prev = tp.items.indexOf(atab) - 1; | |
165 | if (prev < 0) { | |
166 | return; | |
167 | } | |
168 | var ntab = tp.items.getAt(prev); | |
169 | if (ntab) { | |
170 | tp.setActiveTab(ntab); | |
171 | } | |
172 | } | |
173 | }, | |
174 | { | |
175 | text: gettext('Next'), | |
176 | disabled: true, | |
177 | itemId: 'next', | |
178 | minWidth: 60, | |
179 | handler: function() { | |
180 | ||
181 | var form = me.down('form').getForm(); | |
182 | ||
183 | var tp = me.down('#wizcontent'); | |
184 | var atab = tp.getActiveTab(); | |
185 | if (!check_card(atab)) { | |
186 | return; | |
187 | } | |
188 | ||
189 | var next = tp.items.indexOf(atab) + 1; | |
190 | var ntab = tp.items.getAt(next); | |
191 | if (ntab) { | |
192 | ntab.enable(); | |
193 | tp.setActiveTab(ntab); | |
194 | } | |
195 | ||
196 | } | |
ee93ce96 | 197 | }, |
18f4718f TL |
198 | { |
199 | text: gettext('Finish'), | |
200 | minWidth: 60, | |
201 | hidden: true, | |
202 | itemId: 'submit', | |
203 | handler: function() { | |
204 | var tp = me.down('#wizcontent'); | |
205 | var atab = tp.getActiveTab(); | |
206 | atab.onSubmit(); | |
207 | } | |
208 | } | |
ee93ce96 DM |
209 | ] |
210 | }); | |
211 | me.callParent(); | |
ee93ce96 DM |
212 | |
213 | Ext.Array.each(me.query('field'), function(field) { | |
a1304e1e | 214 | var validcheck = function() { |
ee93ce96 | 215 | var tp = me.down('#wizcontent'); |
373d7ea4 DC |
216 | |
217 | // check tabs from current to the last enabled for validity | |
218 | // since we might have changed a validity on a later one | |
219 | var i; | |
220 | for (i = curidx; i <= maxidx && i < tp.items.getCount(); i++) { | |
221 | var tab = tp.items.getAt(i); | |
222 | var valid = check_card(tab); | |
223 | ||
224 | // only set the buttons on the current panel | |
225 | if (i === curidx) { | |
226 | me.down('#next').setDisabled(!valid); | |
227 | me.down('#submit').setDisabled(!valid); | |
228 | } | |
229 | ||
230 | // if a panel is invalid, then disable it and all following, | |
231 | // else enable it and go to the next | |
232 | var ntab = tp.items.getAt(i + 1); | |
233 | if (!valid) { | |
234 | disable_at(ntab); | |
235 | return; | |
236 | } else if (ntab && !tab.onSubmit) { | |
237 | ntab.enable(); | |
238 | } | |
ee93ce96 | 239 | } |
a1304e1e DC |
240 | }; |
241 | field.on('change', validcheck); | |
242 | field.on('validitychange', validcheck); | |
ee93ce96 DM |
243 | }); |
244 | } | |
245 | }); |