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
14 * Utility class for generating different styles of message boxes. The alias Roo.Msg can also be used.
18 Roo.Msg.alert('Status', 'Changes saved successfully.');
20 // Prompt for user data:
21 Roo.Msg.prompt('Name', 'Please enter your name:', function(btn, text){
23 // process text value...
27 // Show a dialog using config options:
29 title:'Save Changes?',
30 msg: 'Your are closing a tab that has unsaved changes. Would you like to save your changes?',
31 buttons: Roo.Msg.YESNOCANCEL,
38 Roo.MessageBox = function(){
39 var dlg, opt, mask, waitTimer;
40 var bodyEl, msgEl, textboxEl, textareaEl, progressEl, pp;
41 var buttons, activeTextEl, bwidth;
44 var handleButton = function(button){
46 Roo.callback(opt.fn, opt.scope||window, [button, activeTextEl.dom.value], 1);
50 var handleHide = function(){
52 dlg.el.removeClass(opt.cls);
55 Roo.TaskMgr.stop(waitTimer);
61 var updateButtons = function(b){
65 buttons["cancel"].hide();
66 buttons["yes"].hide();
68 dlg.footer.dom.style.display = 'none';
71 dlg.footer.dom.style.display = '';
72 for(var k in buttons){
73 if(typeof buttons[k] != "function"){
76 buttons[k].setText(typeof b[k] == "string" ? b[k] : Roo.MessageBox.buttonText[k]);
77 width += buttons[k].el.getWidth()+15;
87 var handleEsc = function(d, k, e){
88 if(opt && opt.closable !== false){
98 * Returns a reference to the underlying {@link Roo.BasicDialog} element
99 * @return {Roo.BasicDialog} The BasicDialog element
101 getDialog : function(){
103 dlg = new Roo.BasicDialog("x-msg-box", {
108 constraintoviewport:false,
113 width:400, height:100,
114 buttonAlign:"center",
115 closeClick : function(){
116 if(opt && opt.buttons && opt.buttons.no && !opt.buttons.cancel){
119 handleButton("cancel");
123 dlg.on("hide", handleHide);
125 dlg.addKeyListener(27, handleEsc);
127 var bt = this.buttonText;
128 buttons["ok"] = dlg.addButton(bt["ok"], handleButton.createCallback("ok"));
129 buttons["yes"] = dlg.addButton(bt["yes"], handleButton.createCallback("yes"));
130 buttons["no"] = dlg.addButton(bt["no"], handleButton.createCallback("no"));
131 buttons["cancel"] = dlg.addButton(bt["cancel"], handleButton.createCallback("cancel"));
132 bodyEl = dlg.body.createChild({
134 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>'
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();
152 var pf = progressEl.dom.firstChild;
154 pp = Roo.get(pf.firstChild);
155 pp.setHeight(pf.offsetHeight);
163 * Updates the message box body text
164 * @param {String} text (optional) Replaces the message box element's innerHTML with the specified string (defaults to
165 * the XHTML-compliant non-breaking space character '&#160;')
166 * @return {Roo.MessageBox} This message box
168 updateText : function(text){
169 if(!dlg.isVisible() && !opt.width){
170 dlg.resizeTo(this.maxWidth, 100); // resize first so content is never clipped from previous shows
172 msgEl.innerHTML = text || ' ';
174 var cw = Math.max(msgEl.offsetWidth, bodyEl.scrollWidth);
175 Roo.log("guesed size: " + JSON.stringify([cw,msgEl.offsetWidth, msgEl.parentNode.scrollWidth]));
177 Math.min(opt.width || cw , this.maxWidth),
178 Math.max(opt.minWidth || this.minWidth, bwidth)
181 activeTextEl.setWidth(w);
184 dlg.fixedcenter = false;
186 // to big, make it scoll.
187 if ( bodyEl.getHeight() > (Roo.lib.Dom.getViewHeight() - 100)) {
188 bodyEl.setHeight ( Roo.lib.Dom.getViewHeight() - 100 );
189 bodyEl.dom.style.overflowY = 'auto !important';
191 bodyEl.dom.style.height = '';
192 bodyEl.dom.style.overflowY = '';
195 bodyEl.dom.style.overflowX = 'auto !important';
197 bodyEl.dom.style.overflowX = '';
200 dlg.setContentSize(w, bodyEl.getHeight());
202 dlg.fixedcenter = true;
208 * Updates a progress-style message box's text and progress bar. Only relevant on message boxes
209 * initiated via {@link Roo.MessageBox#progress} or by calling {@link Roo.MessageBox#show} with progress: true.
210 * @param {Number} value Any number between 0 and 1 (e.g., .5)
211 * @param {String} text (optional) If defined, the message box's body text is replaced with the specified string (defaults to undefined)
212 * @return {Roo.MessageBox} This message box
214 updateProgress : function(value, text){
216 this.updateText(text);
218 if (pp) { // weird bug on my firefox - for some reason this is not defined
219 pp.setWidth(Math.floor(value*progressEl.dom.firstChild.offsetWidth));
225 * Returns true if the message box is currently displayed
226 * @return {Boolean} True if the message box is visible, else false
228 isVisible : function(){
229 return dlg && dlg.isVisible();
233 * Hides the message box if it is displayed
236 if(this.isVisible()){
242 * Displays a new message box, or reinitializes an existing message box, based on the config options
243 * passed in. All functions (e.g. prompt, alert, etc) on MessageBox call this function internally.
244 * The following config object properties are supported:
246 Property Type Description
247 ---------- --------------- ------------------------------------------------------------------------------------
248 animEl String/Element An id or Element from which the message box should animate as it opens and
249 closes (defaults to undefined)
250 buttons Object/Boolean A button config object (e.g., Roo.MessageBox.OKCANCEL or {ok:'Foo',
251 cancel:'Bar'}), or false to not show any buttons (defaults to false)
252 closable Boolean False to hide the top-right close button (defaults to true). Note that
253 progress and wait dialogs will ignore this property and always hide the
254 close button as they can only be closed programmatically.
255 cls String A custom CSS class to apply to the message box element
256 defaultTextHeight Number The default height in pixels of the message box's multiline textarea if
257 displayed (defaults to 75)
258 fn Function A callback function to execute after closing the dialog. The arguments to the
259 function will be btn (the name of the button that was clicked, if applicable,
260 e.g. "ok"), and text (the value of the active text field, if applicable).
261 Progress and wait dialogs will ignore this option since they do not respond to
262 user actions and can only be closed programmatically, so any required function
263 should be called by the same code after it closes the dialog.
264 icon String A CSS class that provides a background image to be used as an icon for
265 the dialog (e.g., Roo.MessageBox.WARNING or 'custom-class', defaults to '')
266 maxWidth Number The maximum width in pixels of the message box (defaults to 600)
267 minWidth Number The minimum width in pixels of the message box (defaults to 100)
268 modal Boolean False to allow user interaction with the page while the message box is
269 displayed (defaults to true)
270 msg String A string that will replace the existing message box body text (defaults
271 to the XHTML-compliant non-breaking space character ' ')
272 multiline Boolean True to prompt the user to enter multi-line text (defaults to false)
273 progress Boolean True to display a progress bar (defaults to false)
274 progressText String The text to display inside the progress bar if progress = true (defaults to '')
275 prompt Boolean True to prompt the user to enter single-line text (defaults to false)
276 proxyDrag Boolean True to display a lightweight proxy while dragging (defaults to false)
277 title String The title text
278 value String The string value to set into the active textbox element if displayed
279 wait Boolean True to display a progress bar (defaults to false)
280 width Number The width of the dialog in pixels
287 msg: 'Please enter your address:',
289 buttons: Roo.MessageBox.OKCANCEL,
292 animEl: 'addAddressBtn'
295 * @param {Object} config Configuration options
296 * @return {Roo.MessageBox} This message box
298 show : function(options)
301 // this causes nightmares if you show one dialog after another
302 // especially on callbacks..
304 if(this.isVisible()){
307 Roo.log("Old Dialog Message:" + msgEl.innerHTML )
308 //this.alert("ERROR", "Multiple dialogs where displayed at the same time");
309 //throw "Roo.MessageBox ERROR : Multiple dialogs where displayed at the same time";
312 var d = this.getDialog();
314 d.setTitle(opt.title || " ");
315 d.close.setDisplayed(opt.closable !== false);
316 activeTextEl = textboxEl;
317 opt.prompt = opt.prompt || (opt.multiline ? true : false);
322 textareaEl.setHeight(typeof opt.multiline == "number" ?
323 opt.multiline : this.defaultTextHeight);
324 activeTextEl = textareaEl;
333 progressEl.setDisplayed(opt.progress === true);
334 this.updateProgress(0);
335 activeTextEl.dom.value = opt.value || "";
337 dlg.setDefaultButton(activeTextEl);
339 var bs = opt.buttons;
343 }else if(bs && bs.yes){
346 dlg.setDefaultButton(db);
348 bwidth = updateButtons(opt.buttons);
349 this.updateText(opt.msg);
351 d.el.addClass(opt.cls);
353 d.proxyDrag = opt.proxyDrag === true;
354 d.modal = opt.modal !== false;
355 d.mask = opt.modal !== false ? mask : false;
357 // force it to the end of the z-index stack so it gets a cursor in FF
358 document.body.appendChild(dlg.el.dom);
359 d.animateTarget = null;
360 d.show(options.animEl);
366 * Displays a message box with a progress bar. This message box has no buttons and is not closeable by
367 * the user. You are responsible for updating the progress bar as needed via {@link Roo.MessageBox#updateProgress}
368 * and closing the message box when the process is complete.
369 * @param {String} title The title bar text
370 * @param {String} msg The message box body text
371 * @return {Roo.MessageBox} This message box
373 progress : function(title, msg){
380 minWidth: this.minProgressWidth,
387 * Displays a standard read-only message box with an OK button (comparable to the basic JavaScript Window.alert).
388 * If a callback function is passed it will be called after the user clicks the button, and the
389 * id of the button that was clicked will be passed as the only parameter to the callback
390 * (could also be the top-right close button).
391 * @param {String} title The title bar text
392 * @param {String} msg The message box body text
393 * @param {Function} fn (optional) The callback function invoked after the message box is closed
394 * @param {Object} scope (optional) The scope of the callback function
395 * @return {Roo.MessageBox} This message box
397 alert : function(title, msg, fn, scope){
410 * Displays a message box with an infinitely auto-updating progress bar. This can be used to block user
411 * interaction while waiting for a long-running process to complete that does not have defined intervals.
412 * You are responsible for closing the message box when the process is complete.
413 * @param {String} msg The message box body text
414 * @param {String} title (optional) The title bar text
415 * @return {Roo.MessageBox} This message box
417 wait : function(msg, title){
428 waitTimer = Roo.TaskMgr.start({
430 Roo.MessageBox.updateProgress(((((i+20)%20)+1)*5)*.01);
438 * Displays a confirmation message box with Yes and No buttons (comparable to JavaScript's Window.confirm).
439 * If a callback function is passed it will be called after the user clicks either button, and the id of the
440 * button that was clicked will be passed as the only parameter to the callback (could also be the top-right close button).
441 * @param {String} title The title bar text
442 * @param {String} msg The message box body text
443 * @param {Function} fn (optional) The callback function invoked after the message box is closed
444 * @param {Object} scope (optional) The scope of the callback function
445 * @return {Roo.MessageBox} This message box
447 confirm : function(title, msg, fn, scope){
460 * Displays a message box with OK and Cancel buttons prompting the user to enter some text (comparable to
461 * JavaScript's Window.prompt). The prompt can be a single-line or multi-line textbox. If a callback function
462 * is passed it will be called after the user clicks either button, and the id of the button that was clicked
463 * (could also be the top-right close button) and the text that was entered will be passed as the two
464 * parameters to the callback.
465 * @param {String} title The title bar text
466 * @param {String} msg The message box body text
467 * @param {Function} fn (optional) The callback function invoked after the message box is closed
468 * @param {Object} scope (optional) The scope of the callback function
469 * @param {Boolean/Number} multiline (optional) True to create a multiline textbox using the defaultTextHeight
470 * property, or the height in pixels to create the textbox (defaults to false / single-line)
471 * @return {Roo.MessageBox} This message box
473 prompt : function(title, msg, fn, scope, multiline){
477 buttons: this.OKCANCEL,
482 multiline: multiline,
489 * Button config that displays a single OK button
494 * Button config that displays Yes and No buttons
497 YESNO : {yes:true, no:true},
499 * Button config that displays OK and Cancel buttons
502 OKCANCEL : {ok:true, cancel:true},
504 * Button config that displays Yes, No and Cancel buttons
507 YESNOCANCEL : {yes:true, no:true, cancel:true},
510 * The default height in pixels of the message box's multiline textarea if displayed (defaults to 75)
513 defaultTextHeight : 75,
515 * The maximum width in pixels of the message box (defaults to 600)
520 * The minimum width in pixels of the message box (defaults to 100)
525 * The minimum width in pixels of the message box if it is a progress-style dialog. This is useful
526 * for setting a different minimum width than text-only dialogs may need (defaults to 250)
529 minProgressWidth : 250,
531 * An object containing the default button text strings that can be overriden for localized language support.
532 * Supported properties are: ok, cancel, yes and no.
533 * Customize the default text like so: Roo.MessageBox.buttonText.yes = "S?";
546 * Shorthand for {@link Roo.MessageBox}
548 Roo.Msg = Roo.MessageBox;