]> git.proxmox.com Git - proxmox-widget-toolkit.git/blob - button/Button.js
button/Button.js: make it work with named scopes
[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
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) {
27
28 // Note: me.realHandler may be a string (see named scopes)
29 var realHandler = me.handler;
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 }
55 Ext.callback(realHandler, me.scope, [button, event, rec], 0, me);
56 }
57 });
58 } else {
59 Ext.callback(realHandler, me.scope, [button, event, rec], 0, me);
60 }
61 };
62 }
63
64 me.callParent();
65
66 if (!me.selModel) {
67 var grid = me.up('grid');
68 if (grid && grid.selModel) {
69 me.selModel = grid.selModel;
70 if (me.waitMsgTarget === true) {
71 me.waitMsgTarget = grid;
72 }
73 }
74 }
75
76 if (me.waitMsgTarget === true) {
77 throw "unable to find waitMsgTarget"; // no grid found
78 }
79
80 if (me.selModel) {
81
82 me.mon(me.selModel, "selectionchange", function() {
83 var rec = me.selModel.getSelection()[0];
84 if (!rec || (me.enableFn(rec) === false)) {
85 me.setDisabled(true);
86 } else {
87 me.setDisabled(false);
88 }
89 });
90 }
91 }
92 });
93
94
95 Ext.define('Proxmox.button.StdRemoveButton', {
96 extend: 'Proxmox.button.Button',
97 alias: 'widget.proxmoxStdRemoveButton',
98
99 text: gettext('Remove'),
100
101 disabled: true,
102
103 baseurl: undefined,
104
105 getUrl: function(rec) {
106 var me = this;
107
108 return me.baseurl + '/' + rec.getId();
109 },
110
111 // also works with names scopes
112 callback: function(options, success, response) {},
113
114 getRecordName: function(rec) { return rec.getId() },
115
116 confirmMsg: function (rec) {
117 var me = this;
118
119 var name = me.getRecordName(rec);
120 return Ext.String.format(
121 gettext('Are you sure you want to remove entry {0}'),
122 "'" + name + "'");
123 },
124
125 handler: function(btn, event, rec) {
126 var me = this;
127
128 Proxmox.Utils.API2Request({
129 url: me.getUrl(rec),
130 method: 'DELETE',
131 waitMsgTarget: me.waitMsgTarget,
132 callback: function(options, success, response) {
133 Ext.callback(me.callback, me.scope, [options, success, response], 0, me);
134 },
135 failure: function (response, opts) {
136 Ext.Msg.alert(gettext('Error'), response.htmlStatus);
137 }
138 });
139 }
140 });