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();
69 for(var k in buttons){
70 if(typeof buttons[k] != "function"){
73 buttons[k].setText(typeof b[k] == "string" ? b[k] : Roo.bootstrap.MessageBox.buttonText[k]);
74 width += buttons[k].el.getWidth()+15;
84 var handleEsc = function(d, k, e){
85 if(opt && opt.closable !== false){
95 * Returns a reference to the underlying {@link Roo.BasicDialog} element
96 * @return {Roo.BasicDialog} The BasicDialog element
98 getDialog : function(){
100 dlg = new Roo.bootstrap.Modal( {
103 //constraintoviewport:false,
105 //collapsible : false,
110 //buttonAlign:"center",
111 closeClick : function(){
112 if(opt && opt.buttons && opt.buttons.no && !opt.buttons.cancel){
115 handleButton("cancel");
120 dlg.on("hide", handleHide);
122 //dlg.addKeyListener(27, handleEsc);
124 this.buttons = buttons;
125 var bt = this.buttonText;
126 buttons["ok"] = dlg.addButton(bt["ok"], handleButton.createCallback("ok"));
127 buttons["yes"] = dlg.addButton(bt["yes"], handleButton.createCallback("yes"));
128 buttons["no"] = dlg.addButton(bt["no"], handleButton.createCallback("no"));
129 buttons["cancel"] = dlg.addButton(bt["cancel"], handleButton.createCallback("cancel"));
131 bodyEl = dlg.bodyEl.createChild({
133 html:'<span class="roo-mb-text"></span><br /><input type="text" class="roo-mb-input" />' +
134 '<textarea class="roo-mb-textarea"></textarea>' +
135 '<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();
154 // This is supposed to be the progessElement.. but I think it's controlling the height of everything..
155 var pf = progressEl.dom.firstChild;
157 pp = Roo.get(pf.firstChild);
158 pp.setHeight(pf.offsetHeight);
166 * Updates the message box body text
167 * @param {String} text (optional) Replaces the message box element's innerHTML with the specified string (defaults to
168 * the XHTML-compliant non-breaking space character '&#160;')
169 * @return {Roo.MessageBox} This message box
171 updateText : function(text)
173 if(!dlg.isVisible() && !opt.width){
174 dlg.dialogEl.setStyle({ 'max-width' : this.maxWidth});
175 // dlg.resizeTo(this.maxWidth, 100); // forcing the height breaks long alerts()
177 msgEl.innerHTML = text || ' ';
179 var cw = Math.max(msgEl.offsetWidth, msgEl.parentNode.scrollWidth);
180 //Roo.log("guesed size: " + JSON.stringify([cw,msgEl.offsetWidth, msgEl.parentNode.scrollWidth]));
182 Math.min(opt.width || cw , this.maxWidth),
183 Math.max(opt.minWidth || this.minWidth, bwidth)
186 activeTextEl.setWidth(w);
189 dlg.fixedcenter = false;
191 // to big, make it scroll. = But as usual stupid IE does not support
194 if ( bodyEl.getHeight() > (Roo.lib.Dom.getViewHeight() - 100)) {
195 bodyEl.setHeight ( Roo.lib.Dom.getViewHeight() - 100 );
196 bodyEl.dom.style.overflowY = 'auto' + ( Roo.isIE ? '' : ' !important');
198 bodyEl.dom.style.height = '';
199 bodyEl.dom.style.overflowY = '';
202 bodyEl.dom.style.get = 'auto' + ( Roo.isIE ? '' : ' !important');
204 bodyEl.dom.style.overflowX = '';
207 dlg.setContentSize(w, bodyEl.getHeight());
209 dlg.fixedcenter = true;
215 * Updates a progress-style message box's text and progress bar. Only relevant on message boxes
216 * initiated via {@link Roo.MessageBox#progress} or by calling {@link Roo.MessageBox#show} with progress: true.
217 * @param {Number} value Any number between 0 and 1 (e.g., .5)
218 * @param {String} text (optional) If defined, the message box's body text is replaced with the specified string (defaults to undefined)
219 * @return {Roo.MessageBox} This message box
221 updateProgress : function(value, text){
223 this.updateText(text);
226 if (pp) { // weird bug on my firefox - for some reason this is not defined
227 pp.setWidth(Math.floor(value*progressEl.dom.firstChild.offsetWidth));
228 pp.setHeight(Math.floor(progressEl.dom.firstChild.offsetHeight));
234 * Returns true if the message box is currently displayed
235 * @return {Boolean} True if the message box is visible, else false
237 isVisible : function(){
238 return dlg && dlg.isVisible();
242 * Hides the message box if it is displayed
245 if(this.isVisible()){
251 * Displays a new message box, or reinitializes an existing message box, based on the config options
252 * passed in. All functions (e.g. prompt, alert, etc) on MessageBox call this function internally.
253 * The following config object properties are supported:
255 Property Type Description
256 ---------- --------------- ------------------------------------------------------------------------------------
257 animEl String/Element An id or Element from which the message box should animate as it opens and
258 closes (defaults to undefined)
259 buttons Object/Boolean A button config object (e.g., Roo.MessageBox.OKCANCEL or {ok:'Foo',
260 cancel:'Bar'}), or false to not show any buttons (defaults to false)
261 closable Boolean False to hide the top-right close button (defaults to true). Note that
262 progress and wait dialogs will ignore this property and always hide the
263 close button as they can only be closed programmatically.
264 cls String A custom CSS class to apply to the message box element
265 defaultTextHeight Number The default height in pixels of the message box's multiline textarea if
266 displayed (defaults to 75)
267 fn Function A callback function to execute after closing the dialog. The arguments to the
268 function will be btn (the name of the button that was clicked, if applicable,
269 e.g. "ok"), and text (the value of the active text field, if applicable).
270 Progress and wait dialogs will ignore this option since they do not respond to
271 user actions and can only be closed programmatically, so any required function
272 should be called by the same code after it closes the dialog.
273 icon String A CSS class that provides a background image to be used as an icon for
274 the dialog (e.g., Roo.MessageBox.WARNING or 'custom-class', defaults to '')
275 maxWidth Number The maximum width in pixels of the message box (defaults to 600)
276 minWidth Number The minimum width in pixels of the message box (defaults to 100)
277 modal Boolean False to allow user interaction with the page while the message box is
278 displayed (defaults to true)
279 msg String A string that will replace the existing message box body text (defaults
280 to the XHTML-compliant non-breaking space character ' ')
281 multiline Boolean True to prompt the user to enter multi-line text (defaults to false)
282 progress Boolean True to display a progress bar (defaults to false)
283 progressText String The text to display inside the progress bar if progress = true (defaults to '')
284 prompt Boolean True to prompt the user to enter single-line text (defaults to false)
285 proxyDrag Boolean True to display a lightweight proxy while dragging (defaults to false)
286 title String The title text
287 value String The string value to set into the active textbox element if displayed
288 wait Boolean True to display a progress bar (defaults to false)
289 width Number The width of the dialog in pixels
296 msg: 'Please enter your address:',
298 buttons: Roo.MessageBox.OKCANCEL,
301 animEl: 'addAddressBtn'
304 * @param {Object} config Configuration options
305 * @return {Roo.MessageBox} This message box
307 show : function(options)
310 // this causes nightmares if you show one dialog after another
311 // especially on callbacks..
313 if(this.isVisible()){
316 Roo.log("[Roo.Messagebox] Show called while message displayed:" );
317 Roo.log("Old Dialog Message:" + msgEl.innerHTML );
318 Roo.log("New Dialog Message:" + options.msg )
319 //this.alert("ERROR", "Multiple dialogs where displayed at the same time");
320 //throw "Roo.MessageBox ERROR : Multiple dialogs where displayed at the same time";
323 var d = this.getDialog();
325 d.setTitle(opt.title || " ");
326 d.closeEl.setDisplayed(opt.closable !== false);
327 activeTextEl = textboxEl;
328 opt.prompt = opt.prompt || (opt.multiline ? true : false);
333 textareaEl.setHeight(typeof opt.multiline == "number" ?
334 opt.multiline : this.defaultTextHeight);
335 activeTextEl = textareaEl;
344 progressEl.setDisplayed(opt.progress === true);
346 d.animate = false; // do not animate progress, as it may not have finished animating before we close it..
348 this.updateProgress(0);
349 activeTextEl.dom.value = opt.value || "";
351 dlg.setDefaultButton(activeTextEl);
353 var bs = opt.buttons;
357 }else if(bs && bs.yes){
360 dlg.setDefaultButton(db);
362 bwidth = updateButtons(opt.buttons);
363 this.updateText(opt.msg);
365 d.el.addClass(opt.cls);
367 d.proxyDrag = opt.proxyDrag === true;
368 d.modal = opt.modal !== false;
369 d.mask = opt.modal !== false ? mask : false;
371 // force it to the end of the z-index stack so it gets a cursor in FF
372 document.body.appendChild(dlg.el.dom);
373 d.animateTarget = null;
374 d.show(options.animEl);
380 * Displays a message box with a progress bar. This message box has no buttons and is not closeable by
381 * the user. You are responsible for updating the progress bar as needed via {@link Roo.MessageBox#updateProgress}
382 * and closing the message box when the process is complete.
383 * @param {String} title The title bar text
384 * @param {String} msg The message box body text
385 * @return {Roo.MessageBox} This message box
387 progress : function(title, msg){
394 minWidth: this.minProgressWidth,
401 * Displays a standard read-only message box with an OK button (comparable to the basic JavaScript Window.alert).
402 * If a callback function is passed it will be called after the user clicks the button, and the
403 * id of the button that was clicked will be passed as the only parameter to the callback
404 * (could also be the top-right close button).
405 * @param {String} title The title bar text
406 * @param {String} msg The message box body text
407 * @param {Function} fn (optional) The callback function invoked after the message box is closed
408 * @param {Object} scope (optional) The scope of the callback function
409 * @return {Roo.MessageBox} This message box
411 alert : function(title, msg, fn, scope)
426 * Displays a message box with an infinitely auto-updating progress bar. This can be used to block user
427 * interaction while waiting for a long-running process to complete that does not have defined intervals.
428 * You are responsible for closing the message box when the process is complete.
429 * @param {String} msg The message box body text
430 * @param {String} title (optional) The title bar text
431 * @return {Roo.MessageBox} This message box
433 wait : function(msg, title){
444 waitTimer = Roo.TaskMgr.start({
446 Roo.MessageBox.updateProgress(((((i+20)%20)+1)*5)*.01);
454 * Displays a confirmation message box with Yes and No buttons (comparable to JavaScript's Window.confirm).
455 * If a callback function is passed it will be called after the user clicks either button, and the id of the
456 * button that was clicked will be passed as the only parameter to the callback (could also be the top-right close button).
457 * @param {String} title The title bar text
458 * @param {String} msg The message box body text
459 * @param {Function} fn (optional) The callback function invoked after the message box is closed
460 * @param {Object} scope (optional) The scope of the callback function
461 * @return {Roo.MessageBox} This message box
463 confirm : function(title, msg, fn, scope){
476 * Displays a message box with OK and Cancel buttons prompting the user to enter some text (comparable to
477 * JavaScript's Window.prompt). The prompt can be a single-line or multi-line textbox. If a callback function
478 * is passed it will be called after the user clicks either button, and the id of the button that was clicked
479 * (could also be the top-right close button) and the text that was entered will be passed as the two
480 * parameters to the callback.
481 * @param {String} title The title bar text
482 * @param {String} msg The message box body text
483 * @param {Function} fn (optional) The callback function invoked after the message box is closed
484 * @param {Object} scope (optional) The scope of the callback function
485 * @param {Boolean/Number} multiline (optional) True to create a multiline textbox using the defaultTextHeight
486 * property, or the height in pixels to create the textbox (defaults to false / single-line)
487 * @return {Roo.MessageBox} This message box
489 prompt : function(title, msg, fn, scope, multiline){
493 buttons: this.OKCANCEL,
498 multiline: multiline,
505 * Button config that displays a single OK button
510 * Button config that displays Yes and No buttons
513 YESNO : {yes:true, no:true},
515 * Button config that displays OK and Cancel buttons
518 OKCANCEL : {ok:true, cancel:true},
520 * Button config that displays Yes, No and Cancel buttons
523 YESNOCANCEL : {yes:true, no:true, cancel:true},
526 * The default height in pixels of the message box's multiline textarea if displayed (defaults to 75)
529 defaultTextHeight : 75,
531 * The maximum width in pixels of the message box (defaults to 600)
536 * The minimum width in pixels of the message box (defaults to 100)
541 * The minimum width in pixels of the message box if it is a progress-style dialog. This is useful
542 * for setting a different minimum width than text-only dialogs may need (defaults to 250)
545 minProgressWidth : 250,
547 * An object containing the default button text strings that can be overriden for localized language support.
548 * Supported properties are: ok, cancel, yes and no.
549 * Customize the default text like so: Roo.MessageBox.buttonText.yes = "S?";
562 * Shorthand for {@link Roo.MessageBox}
564 Roo.MessageBox = Roo.MessageBox || Roo.bootstrap.MessageBox;
565 Roo.Msg = Roo.Msg || Roo.MessageBox;