1 /** |
|
2 * MessageBox.js |
|
3 * |
|
4 * Copyright, Moxiecode Systems AB |
|
5 * Released under LGPL License. |
|
6 * |
|
7 * License: http://www.tinymce.com/license |
|
8 * Contributing: http://www.tinymce.com/contributing |
|
9 */ |
|
10 |
|
11 /** |
|
12 * This class is used to create MessageBoxes like alerts/confirms etc. |
|
13 * |
|
14 * @class tinymce.ui.MessageBox |
|
15 * @extends tinymce.ui.Window |
|
16 */ |
|
17 define("tinymce/ui/MessageBox", [ |
|
18 "tinymce/ui/Window" |
|
19 ], function(Window) { |
|
20 "use strict"; |
|
21 |
|
22 var MessageBox = Window.extend({ |
|
23 /** |
|
24 * Constructs a instance with the specified settings. |
|
25 * |
|
26 * @constructor |
|
27 * @param {Object} settings Name/value object with settings. |
|
28 */ |
|
29 init: function(settings) { |
|
30 settings = { |
|
31 border: 1, |
|
32 padding: 20, |
|
33 layout: 'flex', |
|
34 pack: "center", |
|
35 align: "center", |
|
36 containerCls: 'panel', |
|
37 autoScroll: true, |
|
38 buttons: {type: "button", text: "Ok", action: "ok"}, |
|
39 items: { |
|
40 type: "label", |
|
41 multiline: true, |
|
42 maxWidth: 500, |
|
43 maxHeight: 200 |
|
44 } |
|
45 }; |
|
46 |
|
47 this._super(settings); |
|
48 }, |
|
49 |
|
50 Statics: { |
|
51 /** |
|
52 * Ok buttons constant. |
|
53 * |
|
54 * @static |
|
55 * @final |
|
56 * @field {Number} OK |
|
57 */ |
|
58 OK: 1, |
|
59 |
|
60 /** |
|
61 * Ok/cancel buttons constant. |
|
62 * |
|
63 * @static |
|
64 * @final |
|
65 * @field {Number} OK_CANCEL |
|
66 */ |
|
67 OK_CANCEL: 2, |
|
68 |
|
69 /** |
|
70 * yes/no buttons constant. |
|
71 * |
|
72 * @static |
|
73 * @final |
|
74 * @field {Number} YES_NO |
|
75 */ |
|
76 YES_NO: 3, |
|
77 |
|
78 /** |
|
79 * yes/no/cancel buttons constant. |
|
80 * |
|
81 * @static |
|
82 * @final |
|
83 * @field {Number} YES_NO_CANCEL |
|
84 */ |
|
85 YES_NO_CANCEL: 4, |
|
86 |
|
87 /** |
|
88 * Constructs a new message box and renders it to the body element. |
|
89 * |
|
90 * @static |
|
91 * @method msgBox |
|
92 * @param {Object} settings Name/value object with settings. |
|
93 */ |
|
94 msgBox: function(settings) { |
|
95 var buttons, callback = settings.callback || function() {}; |
|
96 |
|
97 function createButton(text, status, primary) { |
|
98 return { |
|
99 type: "button", |
|
100 text: text, |
|
101 subtype: primary ? 'primary' : '', |
|
102 onClick: function(e) { |
|
103 e.control.parents()[1].close(); |
|
104 callback(status); |
|
105 } |
|
106 }; |
|
107 } |
|
108 |
|
109 switch (settings.buttons) { |
|
110 case MessageBox.OK_CANCEL: |
|
111 buttons = [ |
|
112 createButton('Ok', true, true), |
|
113 createButton('Cancel', false) |
|
114 ]; |
|
115 break; |
|
116 |
|
117 case MessageBox.YES_NO: |
|
118 case MessageBox.YES_NO_CANCEL: |
|
119 buttons = [ |
|
120 createButton('Yes', 1, true), |
|
121 createButton('No', 0) |
|
122 ]; |
|
123 |
|
124 if (settings.buttons == MessageBox.YES_NO_CANCEL) { |
|
125 buttons.push(createButton('Cancel', -1)); |
|
126 } |
|
127 break; |
|
128 |
|
129 default: |
|
130 buttons = [ |
|
131 createButton('Ok', true, true) |
|
132 ]; |
|
133 break; |
|
134 } |
|
135 |
|
136 return new Window({ |
|
137 padding: 20, |
|
138 x: settings.x, |
|
139 y: settings.y, |
|
140 minWidth: 300, |
|
141 minHeight: 100, |
|
142 layout: "flex", |
|
143 pack: "center", |
|
144 align: "center", |
|
145 buttons: buttons, |
|
146 title: settings.title, |
|
147 role: 'alertdialog', |
|
148 items: { |
|
149 type: "label", |
|
150 multiline: true, |
|
151 maxWidth: 500, |
|
152 maxHeight: 200, |
|
153 text: settings.text |
|
154 }, |
|
155 onPostRender: function() { |
|
156 this.aria('describedby', this.items()[0]._id); |
|
157 }, |
|
158 onClose: settings.onClose, |
|
159 onCancel: function() { |
|
160 callback(false); |
|
161 } |
|
162 }).renderTo(document.body).reflow(); |
|
163 }, |
|
164 |
|
165 /** |
|
166 * Creates a new alert dialog. |
|
167 * |
|
168 * @method alert |
|
169 * @param {Object} settings Settings for the alert dialog. |
|
170 * @param {function} [callback] Callback to execute when the user makes a choice. |
|
171 */ |
|
172 alert: function(settings, callback) { |
|
173 if (typeof settings == "string") { |
|
174 settings = {text: settings}; |
|
175 } |
|
176 |
|
177 settings.callback = callback; |
|
178 return MessageBox.msgBox(settings); |
|
179 }, |
|
180 |
|
181 /** |
|
182 * Creates a new confirm dialog. |
|
183 * |
|
184 * @method confirm |
|
185 * @param {Object} settings Settings for the confirm dialog. |
|
186 * @param {function} [callback] Callback to execute when the user makes a choice. |
|
187 */ |
|
188 confirm: function(settings, callback) { |
|
189 if (typeof settings == "string") { |
|
190 settings = {text: settings}; |
|
191 } |
|
192 |
|
193 settings.callback = callback; |
|
194 settings.buttons = MessageBox.OK_CANCEL; |
|
195 |
|
196 return MessageBox.msgBox(settings); |
|
197 } |
|
198 } |
|
199 }); |
|
200 |
|
201 return MessageBox; |
|
202 }); |
|