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