479 lines
No EOL
21 KiB
HTML
479 lines
No EOL
21 KiB
HTML
<html><head><title>MessageBox.js</title><link rel="stylesheet" type="text/css" href="../resources/style.css" media="screen"/></head><body><h1>MessageBox.js</h1><pre class="highlighted"><code><i>/**
|
|
* @class Ext.MessageBox
|
|
* Utility class <b>for</b> generating different styles of message boxes. The alias Ext.Msg can also be used.
|
|
* Example usage:
|
|
*<pre><code>
|
|
<i>// Basic alert:</i>
|
|
Ext.Msg.alert('Status', 'Changes saved successfully.');
|
|
|
|
<i>// Prompt <b>for</b> user data:</i>
|
|
Ext.Msg.prompt('Name', 'Please enter your name:', <b>function</b>(btn, text){
|
|
<b>if</b> (btn == 'ok'){
|
|
<i>// process text value...</i>
|
|
}
|
|
});
|
|
|
|
<i>// Show a dialog using config options:</i>
|
|
Ext.Msg.show({
|
|
title:'Save Changes?',
|
|
msg: 'Your are closing a tab that has unsaved changes. Would you like to save your changes?',
|
|
buttons: Ext.Msg.YESNOCANCEL,
|
|
fn: processResult,
|
|
animEl: 'elId'
|
|
});
|
|
</code></pre>
|
|
* @singleton
|
|
*/</i>
|
|
Ext.MessageBox = <b>function</b>(){
|
|
<b>var</b> dlg, opt, mask, waitTimer;
|
|
<b>var</b> bodyEl, msgEl, textboxEl, textareaEl, progressEl, pp;
|
|
<b>var</b> buttons, activeTextEl, bwidth;
|
|
|
|
<i>// private</i>
|
|
<b>var</b> handleButton = <b>function</b>(button){
|
|
dlg.hide();
|
|
Ext.callback(opt.fn, opt.scope||window, [button, activeTextEl.dom.value], 1);
|
|
};
|
|
|
|
<i>// private</i>
|
|
<b>var</b> handleHide = <b>function</b>(){
|
|
<b>if</b>(opt && opt.cls){
|
|
dlg.el.removeClass(opt.cls);
|
|
}
|
|
<b>if</b>(waitTimer){
|
|
Ext.TaskMgr.stop(waitTimer);
|
|
waitTimer = null;
|
|
}
|
|
};
|
|
|
|
<i>// private</i>
|
|
<b>var</b> updateButtons = <b>function</b>(b){
|
|
<b>var</b> width = 0;
|
|
<b>if</b>(!b){
|
|
buttons["ok"].hide();
|
|
buttons["cancel"].hide();
|
|
buttons["yes"].hide();
|
|
buttons["no"].hide();
|
|
dlg.footer.dom.style.display = 'none';
|
|
<b>return</b> width;
|
|
}
|
|
dlg.footer.dom.style.display = '';
|
|
<b>for</b>(var k <b>in</b> buttons){
|
|
<b>if</b>(typeof buttons[k] != "<b>function</b>"){
|
|
<b>if</b>(b[k]){
|
|
buttons[k].show();
|
|
buttons[k].setText(<b>typeof</b> b[k] == "string" ? b[k] : Ext.MessageBox.buttonText[k]);
|
|
width += buttons[k].el.getWidth()+15;
|
|
}<b>else</b>{
|
|
buttons[k].hide();
|
|
}
|
|
}
|
|
}
|
|
<b>return</b> width;
|
|
};
|
|
|
|
<i>// private</i>
|
|
<b>var</b> handleEsc = <b>function</b>(d, k, e){
|
|
<b>if</b>(opt && opt.closable !== false){
|
|
dlg.hide();
|
|
}
|
|
<b>if</b>(e){
|
|
e.stopEvent();
|
|
}
|
|
};
|
|
|
|
<b>return</b> {
|
|
<i>/**
|
|
* Returns a reference to the underlying {@link Ext.BasicDialog} element
|
|
* @<b>return</b> {Ext.BasicDialog} dialog The BasicDialog element
|
|
*/</i>
|
|
getDialog : <b>function</b>(){
|
|
<b>if</b>(!dlg){
|
|
dlg = <b>new</b> Ext.BasicDialog("x-msg-box", {
|
|
autoCreate : true,
|
|
shadow: true,
|
|
draggable: true,
|
|
resizable:false,
|
|
constraintoviewport:false,
|
|
fixedcenter:true,
|
|
collapsible : false,
|
|
shim:true,
|
|
modal: true,
|
|
width:400, height:100,
|
|
buttonAlign:"center",
|
|
closeClick : <b>function</b>(){
|
|
<b>if</b>(opt && opt.buttons && opt.buttons.no && !opt.buttons.cancel){
|
|
handleButton("no");
|
|
}<b>else</b>{
|
|
handleButton("cancel");
|
|
}
|
|
}
|
|
});
|
|
dlg.on("hide", handleHide);
|
|
mask = dlg.mask;
|
|
dlg.addKeyListener(27, handleEsc);
|
|
buttons = {};
|
|
<b>var</b> bt = <b>this</b>.buttonText;
|
|
buttons["ok"] = dlg.addButton(bt["ok"], handleButton.createCallback("ok"));
|
|
buttons["yes"] = dlg.addButton(bt["yes"], handleButton.createCallback("yes"));
|
|
buttons["no"] = dlg.addButton(bt["no"], handleButton.createCallback("no"));
|
|
buttons["cancel"] = dlg.addButton(bt["cancel"], handleButton.createCallback("cancel"));
|
|
bodyEl = dlg.body.createChild({
|
|
tag:"div",
|
|
html:'<span class="ext-mb-text"></span><br /><input type="text" class="ext-mb-input" /><textarea class="ext-mb-textarea"></textarea><div class="ext-mb-progress-wrap"><div class="ext-mb-progress"><div class="ext-mb-progress-bar">&#160;</div></div></div>'
|
|
});
|
|
msgEl = bodyEl.dom.firstChild;
|
|
textboxEl = Ext.get(bodyEl.dom.childNodes[2]);
|
|
textboxEl.enableDisplayMode();
|
|
textboxEl.addKeyListener([10,13], <b>function</b>(){
|
|
<b>if</b>(dlg.isVisible() && opt && opt.buttons){
|
|
<b>if</b>(opt.buttons.ok){
|
|
handleButton("ok");
|
|
}<b>else</b> if(opt.buttons.yes){
|
|
handleButton("yes");
|
|
}
|
|
}
|
|
});
|
|
textareaEl = Ext.get(bodyEl.dom.childNodes[3]);
|
|
textareaEl.enableDisplayMode();
|
|
progressEl = Ext.get(bodyEl.dom.childNodes[4]);
|
|
progressEl.enableDisplayMode();
|
|
<b>var</b> pf = progressEl.dom.firstChild;
|
|
pp = Ext.get(pf.firstChild);
|
|
pp.setHeight(pf.offsetHeight);
|
|
}
|
|
<b>return</b> dlg;
|
|
},
|
|
|
|
<i>/**
|
|
* Updates the message box body text
|
|
* @param {String} text Replaces the message box element's innerHTML <b>with</b> the specified string (defaults to
|
|
* the XHTML-compliant non-breaking space character &#160;)
|
|
* @<b>return</b> {Ext.MessageBox} messageBox This message box
|
|
*/</i>
|
|
updateText : <b>function</b>(text){
|
|
<b>if</b>(!dlg.isVisible() && !opt.width){
|
|
dlg.resizeTo(<b>this</b>.maxWidth, 100); <i>// resize first so content is never clipped from previous shows</i>
|
|
}
|
|
msgEl.innerHTML = text || '&#160;';
|
|
<b>var</b> w = Math.max(Math.min(opt.width || msgEl.offsetWidth, <b>this</b>.maxWidth),
|
|
Math.max(opt.minWidth || <b>this</b>.minWidth, bwidth));
|
|
<b>if</b>(opt.prompt){
|
|
activeTextEl.setWidth(w);
|
|
}
|
|
<b>if</b>(dlg.isVisible()){
|
|
dlg.fixedcenter = false;
|
|
}
|
|
dlg.setContentSize(w, bodyEl.getHeight());
|
|
<b>if</b>(dlg.isVisible()){
|
|
dlg.fixedcenter = true;
|
|
}
|
|
<b>return</b> this;
|
|
},
|
|
|
|
<i>/**
|
|
* Updates a progress-style message box's text and progress bar. Only relevant on message boxes
|
|
* initiated via {@link Ext.MessageBox#progress} or by calling {@link Ext.MessageBox#show} <b>with</b> progress: true.
|
|
* @param {Number} value Any number between 0 and 1 (e.g., .5)
|
|
* @param {String} text If defined, the message box's body text is replaced <b>with</b> the specified string (defaults to undefined)
|
|
* @<b>return</b> {Ext.MessageBox} messageBox This message box
|
|
*/</i>
|
|
updateProgress : <b>function</b>(value, text){
|
|
<b>if</b>(text){
|
|
<b>this</b>.updateText(text);
|
|
}
|
|
pp.setWidth(Math.floor(value*progressEl.dom.firstChild.offsetWidth));
|
|
<b>return</b> this;
|
|
},
|
|
|
|
<i>/**
|
|
* Returns true <b>if</b> the message box is currently displayed
|
|
* @<b>return</b> {Boolean} isVisible True <b>if</b> the message box is visible, <b>else</b> false
|
|
*/</i>
|
|
isVisible : <b>function</b>(){
|
|
<b>return</b> dlg && dlg.isVisible();
|
|
},
|
|
|
|
<i>/**
|
|
* Hides the message box <b>if</b> it is displayed
|
|
*/</i>
|
|
hide : <b>function</b>(){
|
|
<b>if</b>(this.isVisible()){
|
|
dlg.hide();
|
|
}
|
|
},
|
|
|
|
<i>/**
|
|
* Displays a <b>new</b> message box, or reinitializes an existing message box, based on the config options
|
|
* passed <b>in</b>. All functions (e.g. prompt, alert, etc) on MessageBox call <b>this</b> function internally.
|
|
* The following config object properties are supported:
|
|
* <pre>
|
|
Property Type Description
|
|
---------- --------------- ----------------------------------------------------------------------
|
|
title String The title text
|
|
closable Boolean False to hide the top-right close box (defaults to true)
|
|
prompt Boolean True to prompt the user to enter single-line text (defaults to false)
|
|
multiline Boolean True to prompt the user to enter multi-line text (defaults to false)
|
|
progress Boolean True to display a progress bar (defaults to false)
|
|
value String The string value to set into the active textbox element <b>if</b> displayed
|
|
buttons Object/Boolean A button config object (e.g., Ext.MessageBox.OKCANCEL or {ok:'Foo',
|
|
cancel:'Bar'}), or false to not show any buttons (defaults to false)
|
|
msg String A string that will replace the existing message box body text (defaults
|
|
to the XHTML-compliant non-breaking space character &#160;)
|
|
cls String A custom CSS class to apply to the message box element
|
|
proxyDrag Boolean True to display a lightweight proxy <b>while</b> dragging (defaults to false)
|
|
modal Boolean False to allow user interaction <b>with</b> the page <b>while</b> the message box is
|
|
displayed (defaults to true)
|
|
</pre>
|
|
*
|
|
* Example usage:
|
|
* <pre><code>
|
|
Ext.Msg.show({
|
|
title: 'Address',
|
|
msg: 'Please enter your address:',
|
|
width: 300,
|
|
buttons: Ext.MessageBox.OKCANCEL,
|
|
multiline: true,
|
|
fn: saveAddress,
|
|
animEl: 'addAddressBtn'
|
|
});
|
|
</code></pre>
|
|
* @param {Object} config Configuration options
|
|
* @<b>return</b> {Ext.MessageBox} messageBox This message box
|
|
*/</i>
|
|
show : <b>function</b>(options){
|
|
<b>if</b>(this.isVisible()){
|
|
<b>this</b>.hide();
|
|
}
|
|
<b>var</b> d = <b>this</b>.getDialog();
|
|
opt = options;
|
|
d.setTitle(opt.title || "&#160;");
|
|
d.close.setDisplayed(opt.closable !== false);
|
|
activeTextEl = textboxEl;
|
|
opt.prompt = opt.prompt || (opt.multiline ? true : false);
|
|
<b>if</b>(opt.prompt){
|
|
<b>if</b>(opt.multiline){
|
|
textboxEl.hide();
|
|
textareaEl.show();
|
|
textareaEl.setHeight(<b>typeof</b> opt.multiline == "number" ?
|
|
opt.multiline : <b>this</b>.defaultTextHeight);
|
|
activeTextEl = textareaEl;
|
|
}<b>else</b>{
|
|
textboxEl.show();
|
|
textareaEl.hide();
|
|
}
|
|
}<b>else</b>{
|
|
textboxEl.hide();
|
|
textareaEl.hide();
|
|
}
|
|
progressEl.setDisplayed(opt.progress === true);
|
|
<b>this</b>.updateProgress(0);
|
|
activeTextEl.dom.value = opt.value || "";
|
|
<b>if</b>(opt.prompt){
|
|
dlg.setDefaultButton(activeTextEl);
|
|
}<b>else</b>{
|
|
<b>var</b> bs = opt.buttons;
|
|
<b>var</b> db = null;
|
|
<b>if</b>(bs && bs.ok){
|
|
db = buttons["ok"];
|
|
}<b>else</b> if(bs && bs.yes){
|
|
db = buttons["yes"];
|
|
}
|
|
dlg.setDefaultButton(db);
|
|
}
|
|
bwidth = updateButtons(opt.buttons);
|
|
<b>this</b>.updateText(opt.msg);
|
|
<b>if</b>(opt.cls){
|
|
d.el.addClass(opt.cls);
|
|
}
|
|
d.proxyDrag = opt.proxyDrag === true;
|
|
d.modal = opt.modal !== false;
|
|
d.mask = opt.modal !== false ? mask : false;
|
|
<b>if</b>(!d.isVisible()){
|
|
<i>// force it to the end of the z-index stack so it gets a cursor <b>in</b> FF</i>
|
|
document.body.appendChild(dlg.el.dom);
|
|
d.animateTarget = null;
|
|
d.show(options.animEl);
|
|
}
|
|
<b>return</b> this;
|
|
},
|
|
|
|
<i>/**
|
|
* Displays a message box <b>with</b> a progress bar. This message box has no buttons and is not closeable by
|
|
* the user. You are responsible <b>for</b> updating the progress bar as needed via {@link Ext.MessageBox#updateProgress}
|
|
* and closing the message box when the process is complete.
|
|
* @param {String} title The title bar text
|
|
* @param {String} msg The message box body text
|
|
* @<b>return</b> {Ext.MessageBox} messageBox This message box
|
|
*/</i>
|
|
progress : <b>function</b>(title, msg){
|
|
<b>this</b>.show({
|
|
title : title,
|
|
msg : msg,
|
|
buttons: false,
|
|
progress:true,
|
|
closable:false,
|
|
minWidth: <b>this</b>.minProgressWidth
|
|
});
|
|
<b>return</b> this;
|
|
},
|
|
|
|
<i>/**
|
|
* Displays a standard read-only message box (comparable to the basic JavaScript alert prompt) <b>with</b> an OK
|
|
* button. If a callback <b>function</b> is passed it will be called after the user clicks the button, and the
|
|
* id of the button that was clicked will be passed as the only parameter to the callback
|
|
* (could also be the top-right close button).
|
|
* @param {String} title The title bar text
|
|
* @param {String} msg The message box body text
|
|
* @param {Function} fn (optional) The callback <b>function</b> invoked after the message box is closed
|
|
* @param {Object} scope (optional) The scope of the callback <b>function</b>
|
|
* @<b>return</b> {Ext.MessageBox} messageBox This message box
|
|
*/</i>
|
|
alert : <b>function</b>(title, msg, fn, scope){
|
|
<b>this</b>.show({
|
|
title : title,
|
|
msg : msg,
|
|
buttons: <b>this</b>.OK,
|
|
fn: fn,
|
|
scope : scope
|
|
});
|
|
<b>return</b> this;
|
|
},
|
|
|
|
<i>/**
|
|
* Displays a message box <b>with</b> an infinitely auto-updating progress bar. This can be used to block user
|
|
* interaction <b>while</b> waiting <b>for</b> a long-running process to complete that does not have defined intervals.
|
|
* You are responsible <b>for</b> closing the message box when the process is complete.
|
|
* @param {String} msg The message box body text
|
|
* @param {String} title (optional) The title bar text
|
|
* @<b>return</b> {Ext.MessageBox} messageBox This message box
|
|
*/</i>
|
|
wait : <b>function</b>(msg, title){
|
|
<b>this</b>.show({
|
|
title : title,
|
|
msg : msg,
|
|
buttons: false,
|
|
closable:false,
|
|
progress:true,
|
|
modal:true,
|
|
width:300,
|
|
wait:true
|
|
});
|
|
waitTimer = Ext.TaskMgr.start({
|
|
run: <b>function</b>(i){
|
|
Ext.MessageBox.updateProgress(((((i+20)%20)+1)*5)*.01);
|
|
},
|
|
interval: 1000
|
|
});
|
|
<b>return</b> this;
|
|
},
|
|
|
|
<i>/**
|
|
* Displays a confirmation message box <b>with</b> Yes and No buttons. If a callback <b>function</b> is passed it will
|
|
* be called after the user clicks either button, and the id of the button that was clicked
|
|
* will be passed as the only parameter to the callback (could also be the top-right close button).
|
|
* @param {String} title The title bar text
|
|
* @param {String} msg The message box body text
|
|
* @param {Function} fn (optional) The callback <b>function</b> invoked after the message box is closed
|
|
* @param {Object} scope (optional) The scope of the callback <b>function</b>
|
|
* @<b>return</b> {Ext.MessageBox} messageBox This message box
|
|
*/</i>
|
|
confirm : <b>function</b>(title, msg, fn, scope){
|
|
<b>this</b>.show({
|
|
title : title,
|
|
msg : msg,
|
|
buttons: <b>this</b>.YESNO,
|
|
fn: fn,
|
|
scope : scope
|
|
});
|
|
<b>return</b> this;
|
|
},
|
|
|
|
<i>/**
|
|
* Displays a message box <b>with</b> OK and Cancel buttons prompting the user to enter some text. The prompt can
|
|
* be a single-line or multi-line textbox. If a callback <b>function</b> is passed it will be called after the user
|
|
* clicks either button, and the id of the button that was clicked (could also be the top-right
|
|
* close button) and the text that was entered will be passed as the two parameters to the callback.
|
|
* @param {String} title The title bar text
|
|
* @param {String} msg The message box body text
|
|
* @param {Function} fn (optional) The callback <b>function</b> invoked after the message box is closed
|
|
* @param {Object} scope (optional) The scope of the callback <b>function</b>
|
|
* @param {Boolean/Number} multiline (optional) True to create a multiline textbox using the defaultTextHeight
|
|
* property, or the height <b>in</b> pixels to create the textbox (defaults to false / single-line)
|
|
* @<b>return</b> {Ext.MessageBox} messageBox This message box
|
|
*/</i>
|
|
prompt : <b>function</b>(title, msg, fn, scope, multiline){
|
|
<b>this</b>.show({
|
|
title : title,
|
|
msg : msg,
|
|
buttons: <b>this</b>.OKCANCEL,
|
|
fn: fn,
|
|
minWidth:250,
|
|
scope : scope,
|
|
prompt:true,
|
|
multiline: multiline
|
|
});
|
|
<b>return</b> this;
|
|
},
|
|
|
|
<i>/**
|
|
* Button config that displays a single OK button
|
|
* @type Object
|
|
*/</i>
|
|
OK : {ok:true},
|
|
<i>/**
|
|
* Button config that displays Yes and No buttons
|
|
* @type Object
|
|
*/</i>
|
|
YESNO : {yes:true, no:true},
|
|
<i>/**
|
|
* Button config that displays OK and Cancel buttons
|
|
* @type Object
|
|
*/</i>
|
|
OKCANCEL : {ok:true, cancel:true},
|
|
<i>/**
|
|
* Button config that displays Yes, No and Cancel buttons
|
|
* @type Object
|
|
*/</i>
|
|
YESNOCANCEL : {yes:true, no:true, cancel:true},
|
|
|
|
<i>/**
|
|
* The <b>default</b> height <b>in</b> pixels of the message box's multiline textarea <b>if</b> displayed (defaults to 75)
|
|
* @type Number
|
|
*/</i>
|
|
defaultTextHeight : 75,
|
|
<i>/**
|
|
* The maximum width <b>in</b> pixels of the message box (defaults to 600)
|
|
* @type Number
|
|
*/</i>
|
|
maxWidth : 600,
|
|
<i>/**
|
|
* The minimum width <b>in</b> pixels of the message box (defaults to 100)
|
|
* @type Number
|
|
*/</i>
|
|
minWidth : 100,
|
|
<i>/**
|
|
* The minimum width <b>in</b> pixels of the message box progress bar <b>if</b> displayed (defaults to 250)
|
|
* @type Number
|
|
*/</i>
|
|
minProgressWidth : 250,
|
|
<i>/**
|
|
* An object containing the <b>default</b> button text strings that can be overriden <b>for</b> localized language support.
|
|
* Supported properties are: ok, cancel, yes and no.
|
|
* Customize the <b>default</b> text like so: Ext.MessageBox.buttonText.yes = "Si";
|
|
* @type Object
|
|
*/</i>
|
|
buttonText : {
|
|
ok : "OK",
|
|
cancel : "Cancel",
|
|
yes : "Yes",
|
|
no : "No"
|
|
}
|
|
};
|
|
}();
|
|
|
|
<i>/**
|
|
* Shorthand <b>for</b> {@link Ext.MessageBox}
|
|
*/</i>
|
|
Ext.Msg = Ext.MessageBox;</code></pre><hr><div style="font-size:10px;text-align:center;color:gray;">Ext - Copyright © 2006-2007 Ext JS, LLC<br />All rights reserved.</div>
|
|
</body></html> |