4 * messagebox - can be used as a replace
8 * @class Roo.MessageBox
9 * Utility class for generating different styles of message boxes. The alias Roo.Msg can also be used.
13 Roo.Msg.alert('Status', 'Changes saved successfully.');
15 // Prompt for user data:
16 Roo.Msg.prompt('Name', 'Please enter your name:', function(btn, text){
18 // process text value...
22 // Show a dialog using config options:
24 title:'Save Changes?',
25 msg: 'Your are closing a tab that has unsaved changes. Would you like to save your changes?',
26 buttons: Roo.Msg.YESNOCANCEL,
33 Roo.bootstrap.MessageBox = function(){
34 var dlg, opt, mask, waitTimer;
35 var bodyEl, msgEl, textboxEl, textareaEl, progressEl, pp;
36 var buttons, activeTextEl, bwidth;
40 var handleButton = function(button){
42 Roo.callback(opt.fn, opt.scope||window, [button, activeTextEl.dom.value], 1);
46 var handleHide = function(){
48 dlg.el.removeClass(opt.cls);
51 // Roo.TaskMgr.stop(waitTimer);
57 var updateButtons = function(b){
61 buttons["cancel"].hide();
62 buttons["yes"].hide();
64 //dlg.footer.dom.style.display = 'none';
67 dlg.footerEl.dom.style.display = '';
68 for(var k in buttons){
69 if(typeof buttons[k] != "function"){
72 buttons[k].setText(typeof b[k] == "string" ? b[k] : Roo.bootstrap.MessageBox.buttonText[k]);
73 width += buttons[k].el.getWidth()+15;
83 var handleEsc = function(d, k, e){
84 if(opt && opt.closable !== false){
94 * Returns a reference to the underlying {@link Roo.BasicDialog} element
95 * @return {Roo.BasicDialog} The BasicDialog element
97 getDialog : function(){
99 dlg = new Roo.bootstrap.Modal( {
102 //constraintoviewport:false,
104 //collapsible : false,
109 //buttonAlign:"center",
110 closeClick : function(){
111 if(opt && opt.buttons && opt.buttons.no && !opt.buttons.cancel){
114 handleButton("cancel");
119 dlg.on("hide", handleHide);
121 //dlg.addKeyListener(27, handleEsc);
123 this.buttons = buttons;
124 var bt = this.buttonText;
125 buttons["ok"] = dlg.addButton(bt["ok"], handleButton.createCallback("ok"));
126 buttons["yes"] = dlg.addButton(bt["yes"], handleButton.createCallback("yes"));
127 buttons["no"] = dlg.addButton(bt["no"], handleButton.createCallback("no"));
128 buttons["cancel"] = dlg.addButton(bt["cancel"], handleButton.createCallback("cancel"));
130 bodyEl = dlg.bodyEl.createChild({
132 html:'<span class="roo-mb-text"></span><br /><input type="text" class="roo-mb-input" />' +
133 '<textarea class="roo-mb-textarea"></textarea>' +
134 '<div class="roo-mb-progress-wrap"><div class="roo-mb-progress"><div class="roo-mb-progress-bar"> </div></div></div>'
136 msgEl = bodyEl.dom.firstChild;
137 textboxEl = Roo.get(bodyEl.dom.childNodes[2]);
138 textboxEl.enableDisplayMode();
139 textboxEl.addKeyListener([10,13], function(){
140 if(dlg.isVisible() && opt && opt.buttons){
143 }else if(opt.buttons.yes){
148 textareaEl = Roo.get(bodyEl.dom.childNodes[3]);
149 textareaEl.enableDisplayMode();
150 progressEl = Roo.get(bodyEl.dom.childNodes[4]);
151 progressEl.enableDisplayMode();
153 // This is supposed to be the progessElement.. but I think it's controlling the height of everything..
154 var pf = progressEl.dom.firstChild;
156 pp = Roo.get(pf.firstChild);
157 pp.setHeight(pf.offsetHeight);
165 * Updates the message box body text
166 * @param {String} text (optional) Replaces the message box element's innerHTML with the specified string (defaults to
167 * the XHTML-compliant non-breaking space character '&#160;')
168 * @return {Roo.MessageBox} This message box
170 updateText : function(text)
172 if(!dlg.isVisible() && !opt.width){
173 dlg.dialogEl.setStyle({ 'max-width' : this.maxWidth});
174 // dlg.resizeTo(this.maxWidth, 100); // forcing the height breaks long alerts()
176 msgEl.innerHTML = text || ' ';
178 var cw = Math.max(msgEl.offsetWidth, msgEl.parentNode.scrollWidth);
179 //Roo.log("guesed size: " + JSON.stringify([cw,msgEl.offsetWidth, msgEl.parentNode.scrollWidth]));
181 Math.min(opt.width || cw , this.maxWidth),
182 Math.max(opt.minWidth || this.minWidth, bwidth)
185 activeTextEl.setWidth(w);
188 dlg.fixedcenter = false;
190 // to big, make it scroll. = But as usual stupid IE does not support
193 if ( bodyEl.getHeight() > (Roo.lib.Dom.getViewHeight() - 100)) {
194 bodyEl.setHeight ( Roo.lib.Dom.getViewHeight() - 100 );
195 bodyEl.dom.style.overflowY = 'auto' + ( Roo.isIE ? '' : ' !important');
197 bodyEl.dom.style.height = '';
198 bodyEl.dom.style.overflowY = '';
201 bodyEl.dom.style.get = 'auto' + ( Roo.isIE ? '' : ' !important');
203 bodyEl.dom.style.overflowX = '';
206 dlg.setContentSize(w, bodyEl.getHeight());
208 dlg.fixedcenter = true;
214 * Updates a progress-style message box's text and progress bar. Only relevant on message boxes
215 * initiated via {@link Roo.MessageBox#progress} or by calling {@link Roo.MessageBox#show} with progress: true.
216 * @param {Number} value Any number between 0 and 1 (e.g., .5)
217 * @param {String} text (optional) If defined, the message box's body text is replaced with the specified string (defaults to undefined)
218 * @return {Roo.MessageBox} This message box
220 updateProgress : function(value, text){
222 this.updateText(text);
225 if (pp) { // weird bug on my firefox - for some reason this is not defined
226 pp.setWidth(Math.floor(value*progressEl.dom.firstChild.offsetWidth));
232 * Returns true if the message box is currently displayed
233 * @return {Boolean} True if the message box is visible, else false
235 isVisible : function(){
236 return dlg && dlg.isVisible();
240 * Hides the message box if it is displayed
243 if(this.isVisible()){
249 * Displays a new message box, or reinitializes an existing message box, based on the config options
250 * passed in. All functions (e.g. prompt, alert, etc) on MessageBox call this function internally.
251 * The following config object properties are supported:
253 Property Type Description
254 ---------- --------------- ------------------------------------------------------------------------------------
255 animEl String/Element An id or Element from which the message box should animate as it opens and
256 closes (defaults to undefined)
257 buttons Object/Boolean A button config object (e.g., Roo.MessageBox.OKCANCEL or {ok:'Foo',
258 cancel:'Bar'}), or false to not show any buttons (defaults to false)
259 closable Boolean False to hide the top-right close button (defaults to true). Note that
260 progress and wait dialogs will ignore this property and always hide the
261 close button as they can only be closed programmatically.
262 cls String A custom CSS class to apply to the message box element
263 defaultTextHeight Number The default height in pixels of the message box's multiline textarea if
264 displayed (defaults to 75)
265 fn Function A callback function to execute after closing the dialog. The arguments to the
266 function will be btn (the name of the button that was clicked, if applicable,
267 e.g. "ok"), and text (the value of the active text field, if applicable).
268 Progress and wait dialogs will ignore this option since they do not respond to
269 user actions and can only be closed programmatically, so any required function
270 should be called by the same code after it closes the dialog.
271 icon String A CSS class that provides a background image to be used as an icon for
272 the dialog (e.g., Roo.MessageBox.WARNING or 'custom-class', defaults to '')
273 maxWidth Number The maximum width in pixels of the message box (defaults to 600)
274 minWidth Number The minimum width in pixels of the message box (defaults to 100)
275 modal Boolean False to allow user interaction with the page while the message box is
276 displayed (defaults to true)
277 msg String A string that will replace the existing message box body text (defaults
278 to the XHTML-compliant non-breaking space character ' ')
279 multiline Boolean True to prompt the user to enter multi-line text (defaults to false)
280 progress Boolean True to display a progress bar (defaults to false)
281 progressText String The text to display inside the progress bar if progress = true (defaults to '')
282 prompt Boolean True to prompt the user to enter single-line text (defaults to false)
283 proxyDrag Boolean True to display a lightweight proxy while dragging (defaults to false)
284 title String The title text
285 value String The string value to set into the active textbox element if displayed
286 wait Boolean True to display a progress bar (defaults to false)
287 width Number The width of the dialog in pixels
294 msg: 'Please enter your address:',
296 buttons: Roo.MessageBox.OKCANCEL,
299 animEl: 'addAddressBtn'
302 * @param {Object} config Configuration options
303 * @return {Roo.MessageBox} This message box
305 show : function(options)
308 // this causes nightmares if you show one dialog after another
309 // especially on callbacks..
311 if(this.isVisible()){
314 Roo.log("[Roo.Messagebox] Show called while message displayed:" );
315 Roo.log("Old Dialog Message:" + msgEl.innerHTML );
316 Roo.log("New Dialog Message:" + options.msg )
317 //this.alert("ERROR", "Multiple dialogs where displayed at the same time");
318 //throw "Roo.MessageBox ERROR : Multiple dialogs where displayed at the same time";
321 var d = this.getDialog();
323 d.setTitle(opt.title || " ");
324 d.closeEl.setDisplayed(opt.closable !== false);
325 activeTextEl = textboxEl;
326 opt.prompt = opt.prompt || (opt.multiline ? true : false);
331 textareaEl.setHeight(typeof opt.multiline == "number" ?
332 opt.multiline : this.defaultTextHeight);
333 activeTextEl = textareaEl;
342 progressEl.setDisplayed(opt.progress === true);
343 this.updateProgress(0);
344 activeTextEl.dom.value = opt.value || "";
346 dlg.setDefaultButton(activeTextEl);
348 var bs = opt.buttons;
352 }else if(bs && bs.yes){
355 dlg.setDefaultButton(db);
357 bwidth = updateButtons(opt.buttons);
358 this.updateText(opt.msg);
360 d.el.addClass(opt.cls);
362 d.proxyDrag = opt.proxyDrag === true;
363 d.modal = opt.modal !== false;
364 d.mask = opt.modal !== false ? mask : false;
366 // force it to the end of the z-index stack so it gets a cursor in FF
367 document.body.appendChild(dlg.el.dom);
368 d.animateTarget = null;
369 d.show(options.animEl);
375 * Displays a message box with a progress bar. This message box has no buttons and is not closeable by
376 * the user. You are responsible for updating the progress bar as needed via {@link Roo.MessageBox#updateProgress}
377 * and closing the message box when the process is complete.
378 * @param {String} title The title bar text
379 * @param {String} msg The message box body text
380 * @return {Roo.MessageBox} This message box
382 progress : function(title, msg){
389 minWidth: this.minProgressWidth,
396 * Displays a standard read-only message box with an OK button (comparable to the basic JavaScript Window.alert).
397 * If a callback function is passed it will be called after the user clicks the button, and the
398 * id of the button that was clicked will be passed as the only parameter to the callback
399 * (could also be the top-right close button).
400 * @param {String} title The title bar text
401 * @param {String} msg The message box body text
402 * @param {Function} fn (optional) The callback function invoked after the message box is closed
403 * @param {Object} scope (optional) The scope of the callback function
404 * @return {Roo.MessageBox} This message box
406 alert : function(title, msg, fn, scope)
421 * Displays a message box with an infinitely auto-updating progress bar. This can be used to block user
422 * interaction while waiting for a long-running process to complete that does not have defined intervals.
423 * You are responsible for closing the message box when the process is complete.
424 * @param {String} msg The message box body text
425 * @param {String} title (optional) The title bar text
426 * @return {Roo.MessageBox} This message box
428 wait : function(msg, title){
439 waitTimer = Roo.TaskMgr.start({
441 Roo.MessageBox.updateProgress(((((i+20)%20)+1)*5)*.01);
449 * Displays a confirmation message box with Yes and No buttons (comparable to JavaScript's Window.confirm).
450 * If a callback function is passed it will be called after the user clicks either button, and the id of the
451 * button that was clicked will be passed as the only parameter to the callback (could also be the top-right close button).
452 * @param {String} title The title bar text
453 * @param {String} msg The message box body text
454 * @param {Function} fn (optional) The callback function invoked after the message box is closed
455 * @param {Object} scope (optional) The scope of the callback function
456 * @return {Roo.MessageBox} This message box
458 confirm : function(title, msg, fn, scope){
471 * Displays a message box with OK and Cancel buttons prompting the user to enter some text (comparable to
472 * JavaScript's Window.prompt). The prompt can be a single-line or multi-line textbox. If a callback function
473 * is passed it will be called after the user clicks either button, and the id of the button that was clicked
474 * (could also be the top-right close button) and the text that was entered will be passed as the two
475 * parameters to the callback.
476 * @param {String} title The title bar text
477 * @param {String} msg The message box body text
478 * @param {Function} fn (optional) The callback function invoked after the message box is closed
479 * @param {Object} scope (optional) The scope of the callback function
480 * @param {Boolean/Number} multiline (optional) True to create a multiline textbox using the defaultTextHeight
481 * property, or the height in pixels to create the textbox (defaults to false / single-line)
482 * @return {Roo.MessageBox} This message box
484 prompt : function(title, msg, fn, scope, multiline){
488 buttons: this.OKCANCEL,
493 multiline: multiline,
500 * Button config that displays a single OK button
505 * Button config that displays Yes and No buttons
508 YESNO : {yes:true, no:true},
510 * Button config that displays OK and Cancel buttons
513 OKCANCEL : {ok:true, cancel:true},
515 * Button config that displays Yes, No and Cancel buttons
518 YESNOCANCEL : {yes:true, no:true, cancel:true},
521 * The default height in pixels of the message box's multiline textarea if displayed (defaults to 75)
524 defaultTextHeight : 75,
526 * The maximum width in pixels of the message box (defaults to 600)
531 * The minimum width in pixels of the message box (defaults to 100)
536 * The minimum width in pixels of the message box if it is a progress-style dialog. This is useful
537 * for setting a different minimum width than text-only dialogs may need (defaults to 250)
540 minProgressWidth : 250,
542 * An object containing the default button text strings that can be overriden for localized language support.
543 * Supported properties are: ok, cancel, yes and no.
544 * Customize the default text like so: Roo.MessageBox.buttonText.yes = "S?";
557 * Shorthand for {@link Roo.MessageBox}
559 Roo.MessageBox = Roo.MessageBox || Roo.bootstrap.MessageBox;
560 Roo.Msg = Roo.Msg || Roo.MessageBox;