1// jQuery Alert Dialogs Plugin 2// 3// Version 1.1 4// 5// Cory S.N. LaViska 6// A Beautiful Site (http://abeautifulsite.net/) 7// 14 May 2009 8// 9// Visit http://abeautifulsite.net/notebook/87 for more information 10// 11// Usage: 12// jAlert( message, [title, callback] ) 13// jConfirm( message, [title, callback] ) 14// jPrompt( message, [value, title, callback] ) 15// 16// History: 17// 18// 1.00 - Released (29 December 2008) 19// 20// 1.01 - Fixed bug where unbinding would destroy all resize events 21// 22// License: 23// 24// This plugin is dual-licensed under the GNU General Public License and the MIT License and 25// is copyright 2008 A Beautiful Site, LLC. 26// 27(function($) { 28 29 $.alerts = { 30 31 // These properties can be read/written by accessing $.alerts.propertyName from your scripts at any time 32 33 verticalOffset: -75, // vertical offset of the dialog from center screen, in pixels 34 horizontalOffset: 0, // horizontal offset of the dialog from center screen, in pixels/ 35 repositionOnResize: true, // re-centers the dialog on window resize 36 overlayOpacity: .01, // transparency level of overlay 37 overlayColor: '#FFF', // base color of overlay 38 draggable: true, // make the dialogs draggable (requires UI Draggables plugin) 39 okButton: ' OK ', // text for the OK button 40 cancelButton: ' Cancel ', // text for the Cancel button 41 dialogClass: null, // if specified, this class will be applied to all dialogs 42 43 // Public methods 44 45 alert: function(message, title, callback) { 46 if( title == null ) title = 'Alert'; 47 $.alerts._show(title, message, null, 'alert', function(result) { 48 if( callback ) callback(result); 49 }); 50 }, 51 52 confirm: function(message, title, callback) { 53 if( title == null ) title = 'Confirm'; 54 $.alerts._show(title, message, null, 'confirm', function(result) { 55 if( callback ) callback(result); 56 }); 57 }, 58 59 prompt: function(message, value, title, callback) { 60 if( title == null ) title = 'Prompt'; 61 $.alerts._show(title, message, value, 'prompt', function(result) { 62 if( callback ) callback(result); 63 }); 64 }, 65 66 // Private methods 67 68 _show: function(title, msg, value, type, callback) { 69 70 $.alerts._hide(); 71 $.alerts._overlay('show'); 72 73 $("BODY").append( 74 '<div id="popup_container">' + 75 '<h1 id="popup_title"></h1>' + 76 '<div id="popup_content">' + 77 '<div id="popup_message"></div>' + 78 '</div>' + 79 '</div>'); 80 81 if( $.alerts.dialogClass ) $("#popup_container").addClass($.alerts.dialogClass); 82 83 // IE6 Fix 84 //if ('undefined' == typeof(document.body.style.maxHeight)) {} 85 var pos = ('undefined' == typeof(document.body.style.maxHeight)) ? 'absolute' : 'fixed'; 86 87 $("#popup_container").css({ 88 position: pos, 89 zIndex: 99999, 90 padding: 0, 91 margin: 0 92 }); 93 94 $("#popup_title").text(title); 95 $("#popup_content").addClass(type); 96 $("#popup_message").text(msg); 97 $("#popup_message").html( $("#popup_message").text().replace(/\n/g, '<br />') ); 98 99 $("#popup_container").css({ 100 minWidth: $("#popup_container").outerWidth(), 101 maxWidth: $("#popup_container").outerWidth() 102 }); 103 104 $.alerts._reposition(); 105 $.alerts._maintainPosition(true); 106 107 switch( type ) { 108 case 'alert': 109 $("#popup_message").after('<div id="popup_panel"><input type="button" value="' + $.alerts.okButton + '" id="popup_ok" /></div>'); 110 $("#popup_ok").click( function() { 111 $.alerts._hide(); 112 callback(true); 113 }); 114 $("#popup_ok").focus().keypress( function(e) { 115 if( e.keyCode == 13 || e.keyCode == 27 ) $("#popup_ok").trigger('click'); 116 }); 117 break; 118 case 'confirm': 119 $("#popup_message").after('<div id="popup_panel"><input type="button" value="' + $.alerts.okButton + '" id="popup_ok" /> <input type="button" value="' + $.alerts.cancelButton + '" id="popup_cancel" /></div>'); 120 $("#popup_ok").click( function() { 121 $.alerts._hide(); 122 if( callback ) callback(true); 123 });
124 $("#popup_cancel").click( function() { 125 $.alerts._hide(); 126 if( callback ) callback(false); 127 }); 128 $("#popup_ok").focus(); 129 $("#popup_ok, #popup_cancel").keypress( function(e) { 130 if( e.keyCode == 13 ) $("#popup_ok").trigger('click'); 131 if( e.keyCode == 27 ) $("#popup_cancel").trigger('click'); 132 }); 133 break; 134 case 'prompt': 135 $("#popup_message").append('<br /><input type="text" size="30" id="popup_prompt" />').after('<div id="popup_panel"><input type="button" value="' + $.alerts.okButton + '" id="popup_ok" /> <input type="button" value="' + $.alerts.cancelButton + '" id="popup_cancel" /></div>'); 136 $("#popup_prompt").width( $("#popup_message").width() ); 137 $("#popup_ok").click( function() { 138 var val = $("#popup_prompt").val(); 139 $.alerts._hide(); 140 if( callback ) callback( val ); 141 }); 142 $("#popup_cancel").click( function() { 143 $.alerts._hide(); 144 if( callback ) callback( null ); 145 }); 146 $("#popup_prompt, #popup_ok, #popup_cancel").keypress( function(e) { 147 if( e.keyCode == 13 ) $("#popup_ok").trigger('click'); 148 if( e.keyCode == 27 ) $("#popup_cancel").trigger('click'); 149 }); 150 if( value ) $("#popup_prompt").val(value); 151 $("#popup_prompt").focus().select(); 152 break; 153 } 154 155 // Make draggable 156 if( $.alerts.draggable ) { 157 try { 158 $("#popup_container").draggable({ handle: $("#popup_title") }); 159 $("#popup_title").css({ cursor: 'move' }); 160 } catch(e) { /* requires jQuery UI draggables */ } 161 } 162 }, 163 164 _hide: function() { 165 $("#popup_container").remove(); 166 $.alerts._overlay('hide'); 167 $.alerts._maintainPosition(false); 168 }, 169 170 _overlay: function(status) { 171 switch( status ) { 172 case 'show': 173 $.alerts._overlay('hide'); 174 $("BODY").append('<div id="popup_overlay"></div>'); 175 $("#popup_overlay").css({ 176 position: 'absolute', 177 zIndex: 99998, 178 top: '0px', 179 left: '0px', 180 width: '100%', 181 height: $(document).height(), 182 background: $.alerts.overlayColor, 183 opacity: $.alerts.overlayOpacity 184 }); 185 break; 186 case 'hide': 187 $("#popup_overlay").remove(); 188 break; 189 } 190 }, 191 192 _reposition: function() { 193 var top = (($(window).height() / 2) - ($("#popup_container").outerHeight() / 2)) + $.alerts.verticalOffset; 194 var left = (($(window).width() / 2) - ($("#popup_container").outerWidth() / 2)) + $.alerts.horizontalOffset; 195 if( top < 0 ) top = 0; 196 if( left < 0 ) left = 0; 197 198 // IE6 fix 199 // if( $.browser.msie && parseInt($.browser.version) <= 6 ) top = top + $(window).scrollTop(); 200 if('undefined' == typeof(document.body.style.maxHeight)) top = top + $(window).scrollTop(); 201 202 $("#popup_container").css({ 203 top: top + 'px', 204 left: left + 'px' 205 }); 206 $("#popup_overlay").height( $(document).height() ); 207 }, 208 209 _maintainPosition: function(status) { 210 if( $.alerts.repositionOnResize ) { 211 switch(status) { 212 case true: 213 $(window).bind('resize', $.alerts._reposition); 214 break; 215 case false: 216 $(window).unbind('resize', $.alerts._reposition); 217 break; 218 } 219 } 220 } 221 222 } 223 224 // Shortuct functions 225 jAlert = function(message, title, callback) { 226 $.alerts.alert(message, title, callback); 227 } 228 229 jConfirm = function(message, title, callback) { 230 $.alerts.confirm(message, title, callback); 231 }; 232 233 jPrompt = function(message, value, title, callback) { 234 $.alerts.prompt(message, value, title, callback); 235 }; 236 237})(jQuery);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.