]> git.proxmox.com Git - proxmox-widget-toolkit.git/blob - button/Button.js
93c773cbbc029201b15fb85e392a518cdbb3e3c0
[proxmox-widget-toolkit.git] / button / Button.js
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)
13 enableFn: function(record) {
14 // return undefined by default
15 },
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 // is used to get the parent container for its selection model
24 parentXType: 'grid',
25
26 initComponent: function() {
27 let me = this;
28
29 if (me.handler) {
30 // Note: me.realHandler may be a string (see named scopes)
31 let realHandler = me.handler;
32
33 me.handler = function(button, event) {
34 let rec, msg;
35 if (me.selModel) {
36 rec = me.selModel.getSelection()[0];
37 if (!rec || me.enableFn(rec) === false) {
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,
51 message: msg,
52 buttons: Ext.Msg.YESNO,
53 defaultFocus: me.dangerous ? 'no' : 'yes',
54 callback: function(btn) {
55 if (btn !== 'yes') {
56 return;
57 }
58 Ext.callback(realHandler, me.scope, [button, event, rec], 0, me);
59 },
60 });
61 } else {
62 Ext.callback(realHandler, me.scope, [button, event, rec], 0, me);
63 }
64 };
65 }
66
67 me.callParent();
68
69 let grid;
70 if (!me.selModel && me.selModel !== null && me.selModel !== false) {
71 let parent = me.up(me.parentXType);
72 if (parent && parent.selModel) {
73 me.selModel = parent.selModel;
74 }
75 }
76
77 if (me.waitMsgTarget === true) {
78 grid = me.up('grid');
79 if (grid) {
80 me.waitMsgTarget = grid;
81 } else {
82 throw "unable to find waitMsgTarget";
83 }
84 }
85
86 if (me.selModel) {
87 me.mon(me.selModel, "selectionchange", function() {
88 let rec = me.selModel.getSelection()[0];
89 if (!rec || me.enableFn(rec) === false) {
90 me.setDisabled(true);
91 } else {
92 me.setDisabled(false);
93 }
94 });
95 }
96 },
97 });
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
108 // time to wait for removal task to finish
109 delay: undefined,
110
111 config: {
112 baseurl: undefined,
113 },
114
115 getUrl: function(rec) {
116 let me = this;
117
118 if (me.selModel) {
119 return me.baseurl + '/' + rec.getId();
120 } else {
121 return me.baseurl;
122 }
123 },
124
125 // also works with names scopes
126 callback: function(options, success, response) {
127 // do nothing by default
128 },
129
130 getRecordName: (rec) => rec.getId(),
131
132 confirmMsg: function(rec) {
133 let me = this;
134
135 let name = me.getRecordName(rec);
136 return Ext.String.format(gettext('Are you sure you want to remove entry {0}'), `'${name}'`);
137 },
138
139 handler: function(btn, event, rec) {
140 let me = this;
141
142 let url = me.getUrl(rec);
143
144 if (typeof me.delay !== 'undefined' && me.delay >= 0) {
145 url += "?delay=" + me.delay;
146 }
147
148 Proxmox.Utils.API2Request({
149 url: url,
150 method: 'DELETE',
151 waitMsgTarget: me.waitMsgTarget,
152 callback: function(options, success, response) {
153 Ext.callback(me.callback, me.scope, [options, success, response], 0, me);
154 },
155 failure: function(response, opts) {
156 Ext.Msg.alert(gettext('Error'), response.htmlStatus);
157 },
158 });
159 },
160 initComponent: function() {
161 let me = this;
162
163 // enable by default if no seleModel is there and disabled not set
164 if (me.initialConfig.disabled === undefined &&
165 (me.selModel === null || me.selModel === false)) {
166 me.disabled = false;
167 }
168
169 me.callParent();
170 },
171 });