]>
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, | |
51 | callback: function(btn) { | |
52 | if (btn !== 'yes') { | |
53 | return; | |
54 | } | |
81244aa1 | 55 | Ext.callback(realHandler, me.scope, [button, event, rec], 0, me); |
d1661fde DM |
56 | } |
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 | ||
6a42adff | 66 | if (!me.selModel && me.selModel !== null) { |
81244aa1 DM |
67 | var grid = me.up('grid'); |
68 | if (grid && grid.selModel) { | |
69 | me.selModel = grid.selModel; | |
81244aa1 DM |
70 | } |
71 | } | |
72 | ||
73 | if (me.waitMsgTarget === true) { | |
6a42adff DM |
74 | var grid = me.up('grid'); |
75 | if (grid) { | |
76 | me.waitMsgTarget = grid; | |
77 | } else { | |
78 | throw "unable to find waitMsgTarget"; | |
79 | } | |
81244aa1 DM |
80 | } |
81 | ||
d1661fde DM |
82 | if (me.selModel) { |
83 | ||
84 | me.mon(me.selModel, "selectionchange", function() { | |
85 | var rec = me.selModel.getSelection()[0]; | |
86 | if (!rec || (me.enableFn(rec) === false)) { | |
87 | me.setDisabled(true); | |
88 | } else { | |
89 | me.setDisabled(false); | |
90 | } | |
91 | }); | |
92 | } | |
93 | } | |
94 | }); | |
a1d5d064 DM |
95 | |
96 | ||
97 | Ext.define('Proxmox.button.StdRemoveButton', { | |
98 | extend: 'Proxmox.button.Button', | |
99 | alias: 'widget.proxmoxStdRemoveButton', | |
100 | ||
101 | text: gettext('Remove'), | |
102 | ||
103 | disabled: true, | |
104 | ||
3c93b430 DC |
105 | config: { |
106 | baseurl: undefined | |
107 | }, | |
a1d5d064 | 108 | |
375c055b | 109 | getUrl: function(rec) { |
3c08d49a DM |
110 | var me = this; |
111 | ||
375c055b DM |
112 | return me.baseurl + '/' + rec.getId(); |
113 | }, | |
114 | ||
81244aa1 | 115 | // also works with names scopes |
a1d5d064 DM |
116 | callback: function(options, success, response) {}, |
117 | ||
118 | getRecordName: function(rec) { return rec.getId() }, | |
119 | ||
120 | confirmMsg: function (rec) { | |
121 | var me = this; | |
122 | ||
123 | var name = me.getRecordName(rec); | |
124 | return Ext.String.format( | |
125 | gettext('Are you sure you want to remove entry {0}'), | |
126 | "'" + name + "'"); | |
127 | }, | |
128 | ||
129 | handler: function(btn, event, rec) { | |
130 | var me = this; | |
131 | ||
132 | Proxmox.Utils.API2Request({ | |
375c055b | 133 | url: me.getUrl(rec), |
a1d5d064 DM |
134 | method: 'DELETE', |
135 | waitMsgTarget: me.waitMsgTarget, | |
81244aa1 DM |
136 | callback: function(options, success, response) { |
137 | Ext.callback(me.callback, me.scope, [options, success, response], 0, me); | |
138 | }, | |
a1d5d064 DM |
139 | failure: function (response, opts) { |
140 | Ext.Msg.alert(gettext('Error'), response.htmlStatus); | |
141 | } | |
142 | }); | |
143 | } | |
144 | }); |