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