]> git.proxmox.com Git - pve-manager.git/blame - www/manager6/lxc/Snapshot.js
lxc: refactor stop button
[pve-manager.git] / www / manager6 / lxc / Snapshot.js
CommitLineData
113495a2
DM
1Ext.define('PVE.window.LxcSnapshot', {
2 extend: 'Ext.window.Window',
3
4 resizable: false,
5
46183321
DC
6 // needed for finding the reference to submitbutton
7 // because we do not have a controller
8 referenceHolder: true,
9 defaultButton: 'submitbutton',
10 defaultFocus: 'field',
11
113495a2
DM
12 take_snapshot: function(snapname, descr, vmstate) {
13 var me = this;
14 var params = { snapname: snapname };
15 if (descr) {
16 params.description = descr;
17 }
18
e7ade592 19 Proxmox.Utils.API2Request({
113495a2
DM
20 params: params,
21 url: '/nodes/' + me.nodename + '/lxc/' + me.vmid + "/snapshot",
22 waitMsgTarget: me,
23 method: 'POST',
24 failure: function(response, opts) {
25 Ext.Msg.alert(gettext('Error'), response.htmlStatus);
26 },
27 success: function(response, options) {
28 var upid = response.result.data;
8cbc11a7 29 var win = Ext.create('Proxmox.window.TaskProgress', { upid: upid });
113495a2
DM
30 win.show();
31 me.close();
32 }
33 });
34 },
35
36 update_snapshot: function(snapname, descr) {
37 var me = this;
e7ade592 38 Proxmox.Utils.API2Request({
113495a2
DM
39 params: { description: descr },
40 url: '/nodes/' + me.nodename + '/lxc/' + me.vmid + "/snapshot/" +
41 snapname + '/config',
42 waitMsgTarget: me,
43 method: 'PUT',
44 failure: function(response, opts) {
45 Ext.Msg.alert(gettext('Error'), response.htmlStatus);
46 },
47 success: function(response, options) {
48 me.close();
49 }
50 });
51 },
52
53 initComponent : function() {
54 var me = this;
55
56 if (!me.nodename) {
57 throw "no node name specified";
58 }
59
60 if (!me.vmid) {
61 throw "no VM ID specified";
62 }
63
64 var summarystore = Ext.create('Ext.data.Store', {
65 model: 'KeyValue',
66 sorters: [
67 {
68 property : 'key',
69 direction: 'ASC'
70 }
71 ]
72 });
73
74 var items = [
75 {
76 xtype: me.snapname ? 'displayfield' : 'textfield',
77 name: 'snapname',
78 value: me.snapname,
79 fieldLabel: gettext('Name'),
80 vtype: 'ConfigId',
81 allowBlank: false
82 }
83 ];
84
85 if (me.snapname) {
86 items.push({
87 xtype: 'displayfield',
88 name: 'snaptime',
57e780d7 89 renderer: PVE.Utils.render_timestamp_human_readable,
113495a2
DM
90 fieldLabel: gettext('Timestamp')
91 });
92 }
93
94 items.push({
95 xtype: 'textareafield',
96 grow: true,
97 name: 'description',
98 fieldLabel: gettext('Description')
99 });
100
101 if (me.snapname) {
102 items.push({
103 title: gettext('Settings'),
104 xtype: 'grid',
105 height: 200,
106 store: summarystore,
107 columns: [
108 {header: gettext('Key'), width: 150, dataIndex: 'key'},
109 {header: gettext('Value'), flex: 1, dataIndex: 'value'}
110 ]
111 });
112 }
113
114 me.formPanel = Ext.create('Ext.form.Panel', {
115 bodyPadding: 10,
116 border: false,
117 fieldDefaults: {
118 labelWidth: 100,
119 anchor: '100%'
120 },
121 items: items
122 });
123
124 var form = me.formPanel.getForm();
125
126 var submitBtn;
127
128 if (me.snapname) {
129 me.title = gettext('Edit') + ': ' + gettext('Snapshot');
130 submitBtn = Ext.create('Ext.Button', {
131 text: gettext('Update'),
132 handler: function() {
133 if (form.isValid()) {
134 var values = form.getValues();
135 me.update_snapshot(me.snapname, values.description);
136 }
137 }
138 });
139 } else {
140 me.title ="VM " + me.vmid + ': ' + gettext('Take Snapshot');
141 submitBtn = Ext.create('Ext.Button', {
142 text: gettext('Take Snapshot'),
46183321 143 reference: 'submitbutton',
113495a2
DM
144 handler: function() {
145 if (form.isValid()) {
146 var values = form.getValues();
147 me.take_snapshot(values.snapname, values.description);
148 }
149 }
150 });
151 }
152
153 Ext.apply(me, {
154 modal: true,
155 width: 450,
156 border: false,
157 layout: 'fit',
158 buttons: [ submitBtn ],
159 items: [ me.formPanel ]
160 });
161
162 if (me.snapname) {
163 Ext.apply(me, {
164 width: 620,
98722a21 165 height: 420
113495a2
DM
166 });
167 }
168
169 me.callParent();
170
171 if (!me.snapname) {
172 return;
173 }
174
175 // else load data
e7ade592 176 Proxmox.Utils.API2Request({
113495a2
DM
177 url: '/nodes/' + me.nodename + '/lxc/' + me.vmid + "/snapshot/" +
178 me.snapname + '/config',
179 waitMsgTarget: me,
180 method: 'GET',
181 failure: function(response, opts) {
182 Ext.Msg.alert(gettext('Error'), response.htmlStatus);
183 me.close();
184 },
185 success: function(response, options) {
186 var data = response.result.data;
187 var kvarray = [];
188 Ext.Object.each(data, function(key, value) {
189 if (key === 'description' || key === 'snaptime') {
190 return;
191 }
192 kvarray.push({ key: key, value: value });
193 });
194
195 summarystore.suspendEvents();
196 summarystore.add(kvarray);
197 summarystore.sort();
198 summarystore.resumeEvents();
1cd91337 199 summarystore.fireEvent('refresh', summarystore);
113495a2 200
57e780d7 201 form.findField('snaptime').setValue(data.snaptime);
113495a2
DM
202 form.findField('description').setValue(data.description);
203 }
204 });
205 }
206});