Guest User

jquery.alerts.js

a guest
Oct 24th, 2013
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* jQuery Alert Dialogs Plugin
  2.  
  3. jAlert( message, [title, callback] )
  4.  
  5. jConfirm( message, [title, callback] )
  6.  
  7. jPrompt( message, [value, title, callback] )
  8.  
  9. */
  10.  
  11. (function($) {
  12.    
  13.     $.alerts = {
  14.        
  15.         // These properties can be read/written by accessing $.alerts.propertyName from your scripts at any time
  16.        
  17.         verticalOffset: -75,                // vertical offset of the dialog from center screen, in pixels
  18.         horizontalOffset: 0,                // horizontal offset of the dialog from center screen, in pixels/
  19.         repositionOnResize: true,           // re-centers the dialog on window resize
  20.         overlayOpacity: .01,                // transparency level of overlay
  21.         overlayColor: '#FFF',               // base color of overlay
  22.         draggable: true,                    // make the dialogs draggable (requires UI Draggables plugin)
  23.         okButton: ' OK ',         // text for the OK button
  24.         cancelButton: ' Cancel ', // text for the Cancel button
  25.         dialogClass: null,                  // if specified, this class will be applied to all dialogs
  26.        
  27.         // Public methods
  28.        
  29.         alert: function(message, title, callback) {
  30.             if( title == null ) title = 'Alert';
  31.             $.alerts._show(title, message, null, 'alert', function(result) {
  32.                 if( callback ) callback(result);
  33.             });
  34.         },
  35.        
  36.         confirm: function(message, title, callback) {
  37.             if( title == null ) title = 'Confirm';
  38.             $.alerts._show(title, message, null, 'confirm', function(result) {
  39.                 if( callback ) callback(result);
  40.             });
  41.         },
  42.            
  43.         prompt: function(message, value, title, callback) {
  44.             if( title == null ) title = 'Prompt';
  45.             $.alerts._show(title, message, value, 'prompt', function(result) {
  46.                 if( callback ) callback(result);
  47.             });
  48.         },
  49.        
  50.         // Private methods
  51.        
  52.         _show: function(title, msg, value, type, callback) {
  53.            
  54.             $.alerts._hide();
  55.             $.alerts._overlay('show');
  56.            
  57.             $("BODY").append(
  58.               '<div id="popup_container">' +
  59.                 '<h1 id="popup_title"></h1>' +
  60.                 '<div id="popup_content">' +
  61.                   '<div id="popup_message"></div>' +
  62.                 '</div>' +
  63.               '</div>');
  64.            
  65.             if( $.alerts.dialogClass ) $("#popup_container").addClass($.alerts.dialogClass);
  66.            
  67.             // IE6 Fix
  68.             var pos = ($.browser.msie && parseInt($.browser.version) <= 6 ) ? 'absolute' : 'fixed';
  69.            
  70.             $("#popup_container").css({
  71.                 position: pos,
  72.                 zIndex: 99999,
  73.                 padding: 0,
  74.                 margin: 0
  75.             });
  76.            
  77.             $("#popup_title").text(title);
  78.             $("#popup_content").addClass(type);
  79.             $("#popup_message").text(msg);
  80.             $("#popup_message").html( $("#popup_message").text().replace(/\n/g, '<br />') );
  81.            
  82.             $("#popup_container").css({
  83.                 minWidth: $("#popup_container").outerWidth(),
  84.                 maxWidth: $("#popup_container").outerWidth()
  85.             });
  86.            
  87.             $.alerts._reposition();
  88.             $.alerts._maintainPosition(true);
  89.            
  90.             switch( type ) {
  91.                 case 'alert':
  92.                     $("#popup_message").after('<div id="popup_panel"><input type="button" value="' + $.alerts.okButton + '" id="popup_ok" /></div>');
  93.                     $("#popup_ok").click( function() {
  94.                         $.alerts._hide();
  95.                         callback(true);
  96.                     });
  97.                     $("#popup_ok").focus().keypress( function(e) {
  98.                         if( e.keyCode == 13 || e.keyCode == 27 ) $("#popup_ok").trigger('click');
  99.                     });
  100.                 break;
  101.                 case 'confirm':
  102.                     $("#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>');
  103.                     $("#popup_ok").click( function() {
  104.                         $.alerts._hide();
  105.                         if( callback ) callback(true);
  106.                     });
  107.                     $("#popup_cancel").click( function() {
  108.                         $.alerts._hide();
  109.                         if( callback ) callback(false);
  110.                     });
  111.                     $("#popup_ok").focus();
  112.                     $("#popup_ok, #popup_cancel").keypress( function(e) {
  113.                         if( e.keyCode == 13 ) $("#popup_ok").trigger('click');
  114.                         if( e.keyCode == 27 ) $("#popup_cancel").trigger('click');
  115.                     });
  116.                 break;
  117.                 case 'prompt':
  118.                     $("#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>');
  119.                     $("#popup_prompt").width( $("#popup_message").width() );
  120.                     $("#popup_ok").click( function() {
  121.                         var val = $("#popup_prompt").val();
  122.                         $.alerts._hide();
  123.                         if( callback ) callback( val );
  124.                     });
  125.                     $("#popup_cancel").click( function() {
  126.                         $.alerts._hide();
  127.                         if( callback ) callback( null );
  128.                     });
  129.                     $("#popup_prompt, #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.                     if( value ) $("#popup_prompt").val(value);
  134.                     $("#popup_prompt").focus().select();
  135.                 break;
  136.             }
  137.            
  138.             // Make draggable
  139.             if( $.alerts.draggable ) {
  140.                 try {
  141.                     $("#popup_container").draggable({ handle: $("#popup_title") });
  142.                     $("#popup_title").css({ cursor: 'move' });
  143.                 } catch(e) { /* requires jQuery UI draggables */ }
  144.             }
  145.         },
  146.        
  147.         _hide: function() {
  148.             $("#popup_container").remove();
  149.             $.alerts._overlay('hide');
  150.             $.alerts._maintainPosition(false);
  151.         },
  152.        
  153.         _overlay: function(status) {
  154.             switch( status ) {
  155.                 case 'show':
  156.                     $.alerts._overlay('hide');
  157.                     $("BODY").append('<div id="popup_overlay"></div>');
  158.                     $("#popup_overlay").css({
  159.                         position: 'absolute',
  160.                         zIndex: 99998,
  161.                         top: '0px',
  162.                         left: '0px',
  163.                         width: '100%',
  164.                         height: $(document).height(),
  165.                         background: $.alerts.overlayColor,
  166.                         opacity: $.alerts.overlayOpacity
  167.                     });
  168.                 break;
  169.                 case 'hide':
  170.                     $("#popup_overlay").remove();
  171.                 break;
  172.             }
  173.         },
  174.        
  175.         _reposition: function() {
  176.             var top = (($(window).height() / 2) - ($("#popup_container").outerHeight() / 2)) + $.alerts.verticalOffset;
  177.             var left = (($(window).width() / 2) - ($("#popup_container").outerWidth() / 2)) + $.alerts.horizontalOffset;
  178.             if( top < 0 ) top = 0;
  179.             if( left < 0 ) left = 0;
  180.            
  181.             // IE6 fix
  182.             if( $.browser.msie && parseInt($.browser.version) <= 6 ) top = top + $(window).scrollTop();
  183.            
  184.             $("#popup_container").css({
  185.                 top: top + 'px',
  186.                 left: left + 'px'
  187.             });
  188.             $("#popup_overlay").height( $(document).height() );
  189.         },
  190.        
  191.         _maintainPosition: function(status) {
  192.             if( $.alerts.repositionOnResize ) {
  193.                 switch(status) {
  194.                     case true:
  195.                         $(window).bind('resize', $.alerts._reposition);
  196.                     break;
  197.                     case false:
  198.                         $(window).unbind('resize', $.alerts._reposition);
  199.                     break;
  200.                 }
  201.             }
  202.         }
  203.        
  204.     }
  205.    
  206.     // Shortuct functions
  207.     jAlert = function(message, title, callback) {
  208.         $.alerts.alert(message, title, callback);
  209.     }
  210.    
  211.     jConfirm = function(message, title, callback) {
  212.         $.alerts.confirm(message, title, callback);
  213.     };
  214.        
  215.     jPrompt = function(message, value, title, callback) {
  216.         $.alerts.prompt(message, value, title, callback);
  217.     };
  218.    
  219. })(jQuery);
Advertisement
Add Comment
Please, Sign In to add comment