]> git.proxmox.com Git - pve-manager.git/blob - www/manager6/tree/SnapshotTree.js
Hide 'Include RAM' when VM isn't running
[pve-manager.git] / www / manager6 / tree / SnapshotTree.js
1 Ext.define('PVE.guest.SnapshotTree', {
2 extend: 'Ext.tree.Panel',
3 xtype: 'pveGuestSnapshotTree',
4
5 stateful: true,
6 stateId: 'grid-snapshots',
7
8 viewModel: {
9 data: {
10 // should be 'qemu' or 'lxc'
11 type: undefined,
12 nodename: undefined,
13 vmid: undefined,
14 snapshotAllowed: false,
15 rollbackAllowed: false,
16 snapshotFeature: false,
17 running: false,
18 selected: '',
19 load_delay: 3000,
20 },
21 formulas: {
22 canSnapshot: (get) => get('snapshotAllowed') && get('snapshotFeature'),
23 canRollback: (get) => get('rollbackAllowed') && get('isSnapshot'),
24 canRemove: (get) => get('snapshotAllowed') && get('isSnapshot'),
25 isSnapshot: (get) => get('selected') && get('selected') !== 'current',
26 buttonText: (get) => get('snapshotAllowed') ? gettext('Edit') : gettext('View'),
27 showMemory: (get) => get('type') === 'qemu',
28 },
29 },
30
31 controller: {
32 xclass: 'Ext.app.ViewController',
33
34 newSnapshot: function() {
35 this.run_editor(false);
36 },
37
38 editSnapshot: function() {
39 this.run_editor(true);
40 },
41
42 run_editor: function(edit) {
43 let me = this;
44 let vm = me.getViewModel();
45 let snapname;
46 if (edit) {
47 snapname = vm.get('selected');
48 if (!snapname || snapname === 'current') { return; }
49 }
50 let win = Ext.create('PVE.window.Snapshot', {
51 nodename: vm.get('nodename'),
52 vmid: vm.get('vmid'),
53 viewonly: !vm.get('snapshotAllowed'),
54 type: vm.get('type'),
55 isCreate: !edit,
56 submitText: !edit ? gettext('Take Snapshot') : undefined,
57 snapname: snapname,
58 running: vm.get('running'),
59 });
60 win.show();
61 me.mon(win, 'destroy', me.reload, me);
62 },
63
64 snapshotAction: function(action, method) {
65 let me = this;
66 let view = me.getView();
67 let vm = me.getViewModel();
68 let snapname = vm.get('selected');
69 if (!snapname) { return; }
70
71 let nodename = vm.get('nodename');
72 let type = vm.get('type');
73 let vmid = vm.get('vmid');
74
75 Proxmox.Utils.API2Request({
76 url: `/nodes/${nodename}/${type}/${vmid}/snapshot/${snapname}/${action}`,
77 method: method,
78 waitMsgTarget: view,
79 callback: function() {
80 me.reload();
81 },
82 failure: function (response, opts) {
83 Ext.Msg.alert(gettext('Error'), response.htmlStatus);
84 },
85 success: function(response, options) {
86 var upid = response.result.data;
87 var win = Ext.create('Proxmox.window.TaskProgress', { upid: upid });
88 win.show();
89 }
90 });
91 },
92
93 rollback: function() {
94 this.snapshotAction('rollback', 'POST');
95 },
96 remove: function() {
97 this.snapshotAction('', 'DELETE');
98 },
99 cancel: function() {
100 this.load_task.cancel();
101 },
102
103 reload: function() {
104 let me = this;
105 let view = me.getView();
106 let vm = me.getViewModel();
107 let nodename = vm.get('nodename');
108 let vmid = vm.get('vmid');
109 let type = vm.get('type');
110 let load_delay = vm.get('load_delay');
111
112 Proxmox.Utils.API2Request({
113 url: `/nodes/${nodename}/${type}/${vmid}/snapshot`,
114 method: 'GET',
115 failure: function(response, opts) {
116 if (me.destroyed) return;
117 Proxmox.Utils.setErrorMask(view, response.htmlStatus);
118 me.load_task.delay(load_delay);
119 },
120 success: function(response, opts) {
121 if (me.destroyed) {
122 // this is in a delayed task, avoid dragons if view has
123 // been destroyed already and go home.
124 return;
125 }
126 Proxmox.Utils.setErrorMask(view, false);
127 var digest = 'invalid';
128 var idhash = {};
129 var root = { name: '__root', expanded: true, children: [] };
130 Ext.Array.each(response.result.data, function(item) {
131 item.leaf = true;
132 item.children = [];
133 if (item.name === 'current') {
134 vm.set('running', !!item.running);
135 digest = item.digest + item.running;
136 item.iconCls = PVE.Utils.get_object_icon_class(vm.get('type'), item);
137 } else {
138 item.iconCls = 'fa fa-fw fa-history x-fa-tree';
139 }
140 idhash[item.name] = item;
141 });
142
143 if (digest !== me.old_digest) {
144 me.old_digest = digest;
145
146 Ext.Array.each(response.result.data, function(item) {
147 if (item.parent && idhash[item.parent]) {
148 var parent_item = idhash[item.parent];
149 parent_item.children.push(item);
150 parent_item.leaf = false;
151 parent_item.expanded = true;
152 parent_item.expandable = false;
153 } else {
154 root.children.push(item);
155 }
156 });
157
158 me.getView().setRootNode(root);
159 }
160
161 me.load_task.delay(load_delay);
162 }
163 });
164
165 // if we do not have the permissions, we don't have to check
166 // if we can create a snapshot, since the butten stays disabled
167 if (!vm.get('snapshotAllowed')) {
168 return;
169 }
170
171 Proxmox.Utils.API2Request({
172 url: `/nodes/${nodename}/${type}/${vmid}/feature`,
173 params: { feature: 'snapshot' },
174 method: 'GET',
175 success: function(response, options) {
176 if (me.destroyed) {
177 // this is in a delayed task, the current view could been
178 // destroyed already; then we mustn't do viemodel set
179 return;
180 }
181 let res = response.result.data;
182 vm.set('snapshotFeature', !!res.hasFeature);
183 }
184 });
185 },
186
187 select: function(grid, val) {
188 let vm = this.getViewModel();
189 if (val.length < 1) {
190 vm.set('selected', '');
191 return;
192 }
193 vm.set('selected', val[0].data.name);
194 },
195
196 init: function(view) {
197 let me = this;
198 let vm = me.getViewModel();
199 me.load_task = new Ext.util.DelayedTask(me.reload, me);
200
201 if (!view.type) {
202 throw 'guest type not set';
203 }
204 vm.set('type', view.type);
205
206 if (!view.pveSelNode.data.node) {
207 throw "no node name specified";
208 }
209 vm.set('nodename', view.pveSelNode.data.node);
210
211 if (!view.pveSelNode.data.vmid) {
212 throw "no VM ID specified";
213 }
214 vm.set('vmid', view.pveSelNode.data.vmid);
215
216 let caps = Ext.state.Manager.get('GuiCap');
217 vm.set('snapshotAllowed', !!caps.vms['VM.Snapshot']);
218 vm.set('rollbackAllowed', !!caps.vms['VM.Snapshot.Rollback']);
219
220 view.getStore().sorters.add({
221 property: 'order',
222 direction: 'ASC',
223 });
224
225 me.reload();
226 },
227 },
228
229 listeners: {
230 selectionchange: 'select',
231 itemdblclick: 'editSnapshot',
232 destroy: 'cancel',
233 },
234
235 layout: 'fit',
236 rootVisible: false,
237 animate: false,
238 sortableColumns: false,
239
240 tbar: [
241 {
242 xtype: 'proxmoxButton',
243 text: gettext('Take Snapshot'),
244 disabled: true,
245 bind: {
246 disabled: "{!canSnapshot}",
247 },
248 handler: 'newSnapshot',
249 },
250 '-',
251 {
252 xtype: 'proxmoxButton',
253 text: gettext('Rollback'),
254 disabled: true,
255 bind: {
256 disabled: '{!canRollback}',
257 },
258 confirmMsg: function() {
259 let view = this.up('treepanel');
260 let rec = view.getSelection()[0];
261 let vmid = view.getViewModel().get('vmid');
262 return Proxmox.Utils.format_task_description('qmrollback', vmid) +
263 " '" + rec.data.name + "'";
264 },
265 handler: 'rollback',
266 },
267 {
268 xtype: 'proxmoxButton',
269 text: gettext('Remove'),
270 disabled: true,
271 bind: {
272 disabled: '{!canRemove}',
273 },
274 confirmMsg: function() {
275 let view = this.up('treepanel');
276 let rec = view.getSelection()[0];
277 return Ext.String.format(
278 gettext('Are you sure you want to remove entry {0}'),
279 `'${rec.data.name}'`
280 );
281 },
282 handler: 'remove',
283 },
284 {
285 xtype: 'proxmoxButton',
286 text: gettext('Edit'),
287 bind: {
288 text: '{buttonText}',
289 disabled: '{!isSnapshot}',
290 },
291 disabled: true,
292 edit: true,
293 handler: 'editSnapshot',
294 },
295 {
296 xtype: 'label',
297 text: gettext("The current guest configuration does not support taking new snapshots"),
298 hidden: true,
299 bind: {
300 hidden: "{canSnapshot}",
301 },
302 },
303 ],
304
305 columnLines: true,
306
307 fields: [
308 'name', 'description', 'snapstate', 'vmstate', 'running',
309 { name: 'snaptime', type: 'date', dateFormat: 'timestamp' },
310 {
311 name: 'order',
312 calculate: function(data) {
313 return data.snaptime || (data.name === 'current' ? 'ZZZ' : data.snapstate);
314 }
315 }
316 ],
317
318 columns: [
319 {
320 xtype: 'treecolumn',
321 text: gettext('Name'),
322 dataIndex: 'name',
323 width: 200,
324 renderer: function(value, metaData, record) {
325 if (value === 'current') {
326 return gettext('NOW');
327 } else {
328 return value;
329 }
330 }
331 },
332 {
333 text: gettext('RAM'),
334 hidden: true,
335 bind: {
336 hidden: '{!showMemory}',
337 },
338 align: 'center',
339 resizable: false,
340 dataIndex: 'vmstate',
341 width: 50,
342 renderer: function(value, metaData, record) {
343 if (record.data.name !== 'current') {
344 return Proxmox.Utils.format_boolean(value);
345 }
346 }
347 },
348 {
349 text: gettext('Date') + "/" + gettext("Status"),
350 dataIndex: 'snaptime',
351 width: 150,
352 renderer: function(value, metaData, record) {
353 if (record.data.snapstate) {
354 return record.data.snapstate;
355 }
356 if (value) {
357 return Ext.Date.format(value,'Y-m-d H:i:s');
358 }
359 }
360 },
361 {
362 text: gettext('Description'),
363 dataIndex: 'description',
364 flex: 1,
365 renderer: function(value, metaData, record) {
366 if (record.data.name === 'current') {
367 return gettext("You are here!");
368 } else {
369 return Ext.String.htmlEncode(value);
370 }
371 }
372 }
373 ],
374
375 });