4 * Copyright(c) 2006-2007, Ext JS, LLC.
6 * Originally Released Under LGPL - original licence link has changed is not relivant.
9 * <script type="text/javascript">
13 * @class Roo.MessageBox
15 * Utility class for generating different styles of message boxes. The alias Roo.Msg can also be used.
19 Roo.Msg.alert('Status', 'Changes saved successfully.');
21 // Prompt for user data:
22 Roo.Msg.prompt('Name', 'Please enter your name:', function(btn, text){
24 // process text value...
28 // Show a dialog using config options:
30 title:'Save Changes?',
31 msg: 'Your are closing a tab that has unsaved changes. Would you like to save your changes?',
32 buttons: Roo.Msg.YESNOCANCEL,
39 Roo.MessageBox = function(){
40 var dlg, opt, mask, waitTimer;
41 var bodyEl, msgEl, textboxEl, textareaEl, progressEl, pp;
42 var buttons, activeTextEl, bwidth;
45 var handleButton = function(button){
47 Roo.callback(opt.fn, opt.scope||window, [button, activeTextEl.dom.value], 1);
51 var handleHide = function(){
53 dlg.el.removeClass(opt.cls);
56 Roo.TaskMgr.stop(waitTimer);
62 var updateButtons = function(b){
66 buttons["cancel"].hide();
67 buttons["yes"].hide();
69 dlg.footer.dom.style.display = 'none';
72 dlg.footer.dom.style.display = '';
73 for(var k in buttons){
74 if(typeof buttons[k] != "function"){
77 buttons[k].setText(typeof b[k] == "string" ? b[k] : Roo.MessageBox.buttonText[k]);
78 width += buttons[k].el.getWidth()+15;
88 var handleEsc = function(d, k, e){
89 if(opt && opt.closable !== false){
99 * Returns a reference to the underlying {@link Roo.BasicDialog} element
100 * @return {Roo.BasicDialog} The BasicDialog element
102 getDialog : function(){
104 dlg = new Roo.BasicDialog("x-msg-box", {
109 constraintoviewport:false,
114 width:400, height:100,
115 buttonAlign:"center",
116 closeClick : function(){
117 if(opt && opt.buttons && opt.buttons.no && !opt.buttons.cancel){
120 handleButton("cancel");
124 dlg.on("hide", handleHide);
126 dlg.addKeyListener(27, handleEsc);
128 var bt = this.buttonText;
129 buttons["ok"] = dlg.addButton(bt["ok"], handleButton.createCallback("ok"));
130 buttons["yes"] = dlg.addButton(bt["yes"], handleButton.createCallback("yes"));
131 buttons["no"] = dlg.addButton(bt["no"], handleButton.createCallback("no"));
132 buttons["cancel"] = dlg.addButton(bt["cancel"], handleButton.createCallback("cancel"));
133 bodyEl = dlg.body.createChild({
135 html:'<span class="roo-mb-text"></span><br /><input type="text" class="roo-mb-input" /><textarea class="roo-mb-textarea"></textarea><div class="roo-mb-progress-wrap"><div class="roo-mb-progress"><div class="roo-mb-progress-bar"> </div></div></div>'
137 msgEl = bodyEl.dom.firstChild;
138 textboxEl = Roo.get(bodyEl.dom.childNodes[2]);
139 textboxEl.enableDisplayMode();
140 textboxEl.addKeyListener([10,13], function(){
141 if(dlg.isVisible() && opt && opt.buttons){
144 }else if(opt.buttons.yes){
149 textareaEl = Roo.get(bodyEl.dom.childNodes[3]);
150 textareaEl.enableDisplayMode();
151 progressEl = Roo.get(bodyEl.dom.childNodes[4]);
152 progressEl.enableDisplayMode();
153 var pf = progressEl.dom.firstChild;
155 pp = Roo.get(pf.firstChild);
156 pp.setHeight(pf.offsetHeight);
164 * Updates the message box body text
165 * @param {String} text (optional) Replaces the message box element's innerHTML with the specified string (defaults to
166 * the XHTML-compliant non-breaking space character '&#160;')
167 * @return {Roo.MessageBox} This message box
169 updateText : function(text){
170 if(!dlg.isVisible() && !opt.width){
171 dlg.resizeTo(this.maxWidth, 100); // resize first so content is never clipped from previous shows
173 msgEl.innerHTML = text || ' ';
175 var cw = Math.max(msgEl.offsetWidth, msgEl.parentNode.scrollWidth);
176 //Roo.log("guesed size: " + JSON.stringify([cw,msgEl.offsetWidth, msgEl.parentNode.scrollWidth]));
178 Math.min(opt.width || cw , this.maxWidth),
179 Math.max(opt.minWidth || this.minWidth, bwidth)
182 activeTextEl.setWidth(w);
185 dlg.fixedcenter = false;
187 // to big, make it scroll. = But as usual stupid IE does not support
190 if ( bodyEl.getHeight() > (Roo.lib.Dom.getViewHeight() - 100)) {
191 bodyEl.setHeight ( Roo.lib.Dom.getViewHeight() - 100 );
192 bodyEl.dom.style.overflowY = 'auto' + ( Roo.isIE ? '' : ' !important');
194 bodyEl.dom.style.height = '';
195 bodyEl.dom.style.overflowY = '';
198 bodyEl.dom.style.get = 'auto' + ( Roo.isIE ? '' : ' !important');
200 bodyEl.dom.style.overflowX = '';
203 dlg.setContentSize(w, bodyEl.getHeight());
205 dlg.fixedcenter = true;
211 * Updates a progress-style message box's text and progress bar. Only relevant on message boxes
212 * initiated via {@link Roo.MessageBox#progress} or by calling {@link Roo.MessageBox#show} with progress: true.
213 * @param {Number} value Any number between 0 and 1 (e.g., .5)
214 * @param {String} text (optional) If defined, the message box's body text is replaced with the specified string (defaults to undefined)
215 * @return {Roo.MessageBox} This message box
217 updateProgress : function(value, text){
219 this.updateText(text);
221 if (pp) { // weird bug on my firefox - for some reason this is not defined
222 pp.setWidth(Math.floor(value*progressEl.dom.firstChild.offsetWidth));
228 * Returns true if the message box is currently displayed
229 * @return {Boolean} True if the message box is visible, else false
231 isVisible : function(){
232 return dlg && dlg.isVisible();
236 * Hides the message box if it is displayed
239 if(this.isVisible()){
245 * Displays a new message box, or reinitializes an existing message box, based on the config options
246 * passed in. All functions (e.g. prompt, alert, etc) on MessageBox call this function internally.
247 * The following config object properties are supported:
249 Property Type Description
250 ---------- --------------- ------------------------------------------------------------------------------------
251 animEl String/Element An id or Element from which the message box should animate as it opens and
252 closes (defaults to undefined)
253 buttons Object/Boolean A button config object (e.g., Roo.MessageBox.OKCANCEL or {ok:'Foo',
254 cancel:'Bar'}), or false to not show any buttons (defaults to false)
255 closable Boolean False to hide the top-right close button (defaults to true). Note that
256 progress and wait dialogs will ignore this property and always hide the
257 close button as they can only be closed programmatically.
258 cls String A custom CSS class to apply to the message box element
259 defaultTextHeight Number The default height in pixels of the message box's multiline textarea if
260 displayed (defaults to 75)
261 fn Function A callback function to execute after closing the dialog. The arguments to the
262 function will be btn (the name of the button that was clicked, if applicable,
263 e.g. "ok"), and text (the value of the active text field, if applicable).
264 Progress and wait dialogs will ignore this option since they do not respond to
265 user actions and can only be closed programmatically, so any required function
266 should be called by the same code after it closes the dialog.
267 icon String A CSS class that provides a background image to be used as an icon for
268 the dialog (e.g., Roo.MessageBox.WARNING or 'custom-class', defaults to '')
269 maxWidth Number The maximum width in pixels of the message box (defaults to 600)
270 minWidth Number The minimum width in pixels of the message box (defaults to 100)
271 modal Boolean False to allow user interaction with the page while the message box is
272 displayed (defaults to true)
273 msg String A string that will replace the existing message box body text (defaults
274 to the XHTML-compliant non-breaking space character ' ')
275 multiline Boolean True to prompt the user to enter multi-line text (defaults to false)
276 progress Boolean True to display a progress bar (defaults to false)
277 progressText String The text to display inside the progress bar if progress = true (defaults to '')
278 prompt Boolean True to prompt the user to enter single-line text (defaults to false)
279 proxyDrag Boolean True to display a lightweight proxy while dragging (defaults to false)
280 title String The title text
281 value String The string value to set into the active textbox element if displayed
282 wait Boolean True to display a progress bar (defaults to false)
283 width Number The width of the dialog in pixels
290 msg: 'Please enter your address:',
292 buttons: Roo.MessageBox.OKCANCEL,
295 animEl: 'addAddressBtn'
298 * @param {Object} config Configuration options
299 * @return {Roo.MessageBox} This message box
301 show : function(options)
304 // this causes nightmares if you show one dialog after another
305 // especially on callbacks..
307 if(this.isVisible()){
310 Roo.log("[Roo.Messagebox] Show called while message displayed:" );
311 Roo.log("Old Dialog Message:" + msgEl.innerHTML );
312 Roo.log("New Dialog Message:" + options.msg )
313 //this.alert("ERROR", "Multiple dialogs where displayed at the same time");
314 //throw "Roo.MessageBox ERROR : Multiple dialogs where displayed at the same time";
317 var d = this.getDialog();
319 d.setTitle(opt.title || " ");
320 d.close.setDisplayed(opt.closable !== false);
321 activeTextEl = textboxEl;
322 opt.prompt = opt.prompt || (opt.multiline ? true : false);
327 textareaEl.setHeight(typeof opt.multiline == "number" ?
328 opt.multiline : this.defaultTextHeight);
329 activeTextEl = textareaEl;
338 progressEl.setDisplayed(opt.progress === true);
339 this.updateProgress(0);
340 activeTextEl.dom.value = opt.value || "";
342 dlg.setDefaultButton(activeTextEl);
344 var bs = opt.buttons;
348 }else if(bs && bs.yes){
351 dlg.setDefaultButton(db);
353 bwidth = updateButtons(opt.buttons);
354 this.updateText(opt.msg);
356 d.el.addClass(opt.cls);
358 d.proxyDrag = opt.proxyDrag === true;
359 d.modal = opt.modal !== false;
360 d.mask = opt.modal !== false ? mask : false;
362 // force it to the end of the z-index stack so it gets a cursor in FF
363 document.body.appendChild(dlg.el.dom);
364 d.animateTarget = null;
365 d.show(options.animEl);
371 * Displays a message box with a progress bar. This message box has no buttons and is not closeable by
372 * the user. You are responsible for updating the progress bar as needed via {@link Roo.MessageBox#updateProgress}
373 * and closing the message box when the process is complete.
374 * @param {String} title The title bar text
375 * @param {String} msg The message box body text
376 * @return {Roo.MessageBox} This message box
378 progress : function(title, msg){
385 minWidth: this.minProgressWidth,
392 * Displays a standard read-only message box with an OK button (comparable to the basic JavaScript Window.alert).
393 * If a callback function is passed it will be called after the user clicks the button, and the
394 * id of the button that was clicked will be passed as the only parameter to the callback
395 * (could also be the top-right close button).
396 * @param {String} title The title bar text
397 * @param {String} msg The message box body text
398 * @param {Function} fn (optional) The callback function invoked after the message box is closed
399 * @param {Object} scope (optional) The scope of the callback function
400 * @return {Roo.MessageBox} This message box
402 alert : function(title, msg, fn, scope){
415 * Displays a message box with an infinitely auto-updating progress bar. This can be used to block user
416 * interaction while waiting for a long-running process to complete that does not have defined intervals.
417 * You are responsible for closing the message box when the process is complete.
418 * @param {String} msg The message box body text
419 * @param {String} title (optional) The title bar text
420 * @return {Roo.MessageBox} This message box
422 wait : function(msg, title){
433 waitTimer = Roo.TaskMgr.start({
435 Roo.MessageBox.updateProgress(((((i+20)%20)+1)*5)*.01);
443 * Displays a confirmation message box with Yes and No buttons (comparable to JavaScript's Window.confirm).
444 * If a callback function is passed it will be called after the user clicks either button, and the id of the
445 * button that was clicked will be passed as the only parameter to the callback (could also be the top-right close button).
446 * @param {String} title The title bar text
447 * @param {String} msg The message box body text
448 * @param {Function} fn (optional) The callback function invoked after the message box is closed
449 * @param {Object} scope (optional) The scope of the callback function
450 * @return {Roo.MessageBox} This message box
452 confirm : function(title, msg, fn, scope){
465 * Displays a message box with OK and Cancel buttons prompting the user to enter some text (comparable to
466 * JavaScript's Window.prompt). The prompt can be a single-line or multi-line textbox. If a callback function
467 * is passed it will be called after the user clicks either button, and the id of the button that was clicked
468 * (could also be the top-right close button) and the text that was entered will be passed as the two
469 * parameters to the callback.
470 * @param {String} title The title bar text
471 * @param {String} msg The message box body text
472 * @param {Function} fn (optional) The callback function invoked after the message box is closed
473 * @param {Object} scope (optional) The scope of the callback function
474 * @param {Boolean/Number} multiline (optional) True to create a multiline textbox using the defaultTextHeight
475 * property, or the height in pixels to create the textbox (defaults to false / single-line)
476 * @return {Roo.MessageBox} This message box
478 prompt : function(title, msg, fn, scope, multiline){
482 buttons: this.OKCANCEL,
487 multiline: multiline,
494 * Button config that displays a single OK button
499 * Button config that displays Yes and No buttons
502 YESNO : {yes:true, no:true},
504 * Button config that displays OK and Cancel buttons
507 OKCANCEL : {ok:true, cancel:true},
509 * Button config that displays Yes, No and Cancel buttons
512 YESNOCANCEL : {yes:true, no:true, cancel:true},
515 * The default height in pixels of the message box's multiline textarea if displayed (defaults to 75)
518 defaultTextHeight : 75,
520 * The maximum width in pixels of the message box (defaults to 600)
525 * The minimum width in pixels of the message box (defaults to 100)
530 * The minimum width in pixels of the message box if it is a progress-style dialog. This is useful
531 * for setting a different minimum width than text-only dialogs may need (defaults to 250)
534 minProgressWidth : 250,
536 * An object containing the default button text strings that can be overriden for localized language support.
537 * Supported properties are: ok, cancel, yes and no.
538 * Customize the default text like so: Roo.MessageBox.buttonText.yes = "S?";
551 * Shorthand for {@link Roo.MessageBox}
553 Roo.Msg = Roo.MessageBox;