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