]> git.proxmox.com Git - proxmox-widget-toolkit.git/blame - button/Button.js
utils: task descriptions: allow to add/override entries
[proxmox-widget-toolkit.git] / button / Button.js
CommitLineData
d1661fde
DM
1/* Button features:
2 * - observe selection changes to enable/disable the button using enableFn()
3 * - pop up confirmation dialog using confirmMsg()
4 */
5Ext.define('Proxmox.button.Button', {
6 extend: 'Ext.button.Button',
7 alias: 'widget.proxmoxButton',
8
9 // the selection model to observe
10 selModel: undefined,
11
12 // if 'false' handler will not be called (button disabled)
1f315ec8
TL
13 enableFn: function(record) {
14 // return undefined by default
15 },
d1661fde
DM
16
17 // function(record) or text
18 confirmMsg: false,
19
20 // take special care in confirm box (select no as default).
21 dangerous: false,
22
23 initComponent: function() {
d1661fde
DM
24 var me = this;
25
26 if (me.handler) {
81244aa1
DM
27 // Note: me.realHandler may be a string (see named scopes)
28 var realHandler = me.handler;
d1661fde
DM
29
30 me.handler = function(button, event) {
31 var rec, msg;
32 if (me.selModel) {
33 rec = me.selModel.getSelection()[0];
1f315ec8 34 if (!rec || me.enableFn(rec) === false) {
d1661fde
DM
35 return;
36 }
37 }
38
39 if (me.confirmMsg) {
40 msg = me.confirmMsg;
41 if (Ext.isFunction(me.confirmMsg)) {
42 msg = me.confirmMsg(rec);
43 }
44 Ext.MessageBox.defaultButton = me.dangerous ? 2 : 1;
45 Ext.Msg.show({
46 title: gettext('Confirm'),
47 icon: me.dangerous ? Ext.Msg.WARNING : Ext.Msg.QUESTION,
16c0a0b5 48 message: msg,
d1661fde 49 buttons: Ext.Msg.YESNO,
a147a8cb 50 defaultFocus: me.dangerous ? 'no' : 'yes',
d1661fde
DM
51 callback: function(btn) {
52 if (btn !== 'yes') {
53 return;
54 }
81244aa1 55 Ext.callback(realHandler, me.scope, [button, event, rec], 0, me);
1f315ec8 56 },
d1661fde
DM
57 });
58 } else {
81244aa1 59 Ext.callback(realHandler, me.scope, [button, event, rec], 0, me);
d1661fde
DM
60 }
61 };
62 }
63
64 me.callParent();
65
4c2b4242 66 var grid;
791f4ae3 67 if (!me.selModel && me.selModel !== null && me.selModel !== false) {
4c2b4242 68 grid = me.up('grid');
81244aa1
DM
69 if (grid && grid.selModel) {
70 me.selModel = grid.selModel;
81244aa1
DM
71 }
72 }
73
74 if (me.waitMsgTarget === true) {
4c2b4242 75 grid = me.up('grid');
6a42adff
DM
76 if (grid) {
77 me.waitMsgTarget = grid;
78 } else {
79 throw "unable to find waitMsgTarget";
80 }
81244aa1 81 }
13fc756d 82
d1661fde 83 if (me.selModel) {
d1661fde
DM
84 me.mon(me.selModel, "selectionchange", function() {
85 var rec = me.selModel.getSelection()[0];
1f315ec8 86 if (!rec || me.enableFn(rec) === false) {
d1661fde 87 me.setDisabled(true);
1f315ec8 88 } else {
d1661fde
DM
89 me.setDisabled(false);
90 }
91 });
92 }
1f315ec8 93 },
d1661fde 94});
a1d5d064
DM
95
96
97Ext.define('Proxmox.button.StdRemoveButton', {
98 extend: 'Proxmox.button.Button',
99 alias: 'widget.proxmoxStdRemoveButton',
100
101 text: gettext('Remove'),
102
103 disabled: true,
104
2280ae09
DJ
105 // time to wait for removal task to finish
106 delay: undefined,
107
3c93b430 108 config: {
1f315ec8 109 baseurl: undefined,
3c93b430 110 },
a1d5d064 111
375c055b 112 getUrl: function(rec) {
3c08d49a 113 var me = this;
13fc756d 114
842203fd
TL
115 if (me.selModel) {
116 return me.baseurl + '/' + rec.getId();
117 } else {
118 return me.baseurl;
119 }
375c055b
DM
120 },
121
81244aa1 122 // also works with names scopes
1f315ec8
TL
123 callback: function(options, success, response) {
124 // do nothing by default
125 },
a1d5d064 126
1f315ec8 127 getRecordName: (rec) => rec.getId(),
a1d5d064 128
1f315ec8 129 confirmMsg: function(rec) {
a1d5d064
DM
130 var me = this;
131
132 var name = me.getRecordName(rec);
1f315ec8 133 return Ext.String.format(gettext('Are you sure you want to remove entry {0}'), `'${name}'`);
a1d5d064
DM
134 },
135
136 handler: function(btn, event, rec) {
137 var me = this;
d0af6584 138
2280ae09 139 var url = me.getUrl(rec);
d0af6584 140
1f315ec8 141 if (typeof me.delay !== 'undefined' && me.delay >= 0) {
d0af6584
TL
142 url += "?delay=" + me.delay;
143 }
a1d5d064
DM
144
145 Proxmox.Utils.API2Request({
2280ae09 146 url: url,
a1d5d064
DM
147 method: 'DELETE',
148 waitMsgTarget: me.waitMsgTarget,
81244aa1
DM
149 callback: function(options, success, response) {
150 Ext.callback(me.callback, me.scope, [options, success, response], 0, me);
151 },
1f315ec8 152 failure: function(response, opts) {
a1d5d064 153 Ext.Msg.alert(gettext('Error'), response.htmlStatus);
1f315ec8 154 },
a1d5d064 155 });
8886099f
TL
156 },
157 initComponent: function() {
158 let me = this;
159
160 // enable by default if no seleModel is there and disabled not set
1f315ec8
TL
161 if (me.initialConfig.disabled === undefined &&
162 (me.selModel === null || me.selModel === false)) {
8886099f
TL
163 me.disabled = false;
164 }
165
166 me.callParent();
167 },
a1d5d064 168});