// jQuery Alert Dialogs Plugin // // Version 2.0 // // Cory S.N. LaViska // A Beautiful Site (http://abeautifulsite.net/) // 14 May 2009 // // Visit http://abeautifulsite.net/notebook/87 for more information // // Usage: // jLoad( message, [title, callback] ) // jAlert( message, [title, callback] ) // jConfirm( message, [title, callback] ) // jPrompt( message, [value, title, callback] ) // // History: // // 1.00 - Released (29 December 2008) // 1.01 - Fixed bug where unbinding would destroy all resize events // 2.0 - Released (01 April 2015) // // License: // // This plugin is dual-licensed under the GNU General Public License and the MIT License and // is copyright 2008 A Beautiful Site, LLC. // var CUSTOM_OK_BUTTON_LABEL = ' Ok '; // The value of click button, only used in var CUSTOM_ALERT_BUTTON_OK_CLICK = 1; var CUSTOM_ALERT_BUTTON_CUSTOM_CLICK = 2; var CUSTOM_ALERT_BUTTON_CUSTOM_2_CLICK = 21; var CUSTOM_ALERT_BUTTON_CANCEL_CLICK = 3; var touchend = false; (function($) { $.alerts = { // These properties can be read/written by accessing $.alerts.propertyName from your scripts at any time verticalOffset: -75, // vertical offset of the dialog from center screen, in pixels horizontalOffset: 0, // horizontal offset of the dialog from center screen, in pixels/ repositionOnResize: true, // re-centers the dialog on window resize overlayOpacity: .01, // transparency level of overlay overlayColor: '#FFF', // base color of overlay draggable: true, // make the dialogs draggable (requires UI Draggables plugin) okButton: CUSTOM_OK_BUTTON_LABEL, // text for the OK button cancelButton: ' Cancel ', // text for the Cancel button dialogClass: null, // if specified, this class will be applied to all dialogs cancelHide: false, // hide Cancel button cssTop: '', // Public methods alert: function(message, title, callback) { if( title == null ) title = lbl_alert; $.alerts._show(title, message, null, 'alert', function(result) { if( callback ) callback(result); }); }, // tdchien :: Custom when showing dialog by extraConfig confirm: function(message, title, callback, extraConfig) { if( title == null ) title = 'Confirm'; $.alerts._show(title, message, null, 'confirm', function(result, data) { if( callback ) callback(result, data); }, extraConfig); }, prompt: function(message, value, title, callback) { if( title == null ) title = 'Prompt'; $.alerts._show(title, message, value, 'prompt', function(result) { if( callback ) callback(result); }); }, load: function(message, title, callback) { if( title == null ) title = 'Load'; $.alerts._show(title, message, null, 'load', function(result) { if( callback ) callback(result); }); }, // Private methods // tdchien :: Custom when showing dialog by extraConfig _show: function(title, msg, value, type, callback, extraConfig) { $.alerts._hide(); $.alerts._overlay('show'); $("BODY").append( ''); if( $.alerts.dialogClass ) $("#popup_container").addClass($.alerts.dialogClass); // IE6 Fix //var pos = ($.browser.msie && parseInt($.browser.version) <= 6 ) ? 'absolute' : 'fixed'; var pos = 'fixed'; $("#popup_container").css({ position: pos, zIndex: 99999, padding: 0, margin: 0 }); $("#popup_title").text(title); $("#popup_content").addClass(type); $("#popup_message").text(msg); $("#popup_message").html( $("#popup_message").text().replace(/\n/g, '
') ); $("#popup_container").css({ minWidth: $("#popup_container").outerWidth(), maxWidth: $("#popup_container").outerWidth() }); $.alerts._reposition(); $.alerts._maintainPosition(true); // Fix bug 0024217: Please replace "OK" to "Ok" on all popup dialog contain this character // Auto replace value of $.alerts.okButton if needed var lowerOKValue = $.alerts.okButton.toLowerCase(); if (lowerOKValue === CUSTOM_OK_BUTTON_LABEL.toLowerCase() || lowerOKValue === 'ok') { $.alerts.okButton = CUSTOM_OK_BUTTON_LABEL; } switch( type ) { case 'alert': $("#popup_message").after(''); $("#popup_ok").click( function() { if(!touchend) { $.alerts._hide(); callback(true); } }); //fix for iphone navi = navigator.userAgent.toLowerCase(); if(!/chrome/.test(navi) && /safari/.test(navi)) { $('#popup_ok').bind('touchend', function(){ touchend = true; setTimeout(function(){ $.alerts._hide(); callback(true); },500); }); } $("#popup_ok").focus().keypress( function(e) { if( e.keyCode == 13 || e.keyCode == 27 ) $("#popup_ok").trigger('click'); }); setTimeout(function() { $("#popup_ok").focus(); }, 250); break; case 'confirm': var hideCss = ''; try{ if($.alerts.cancelHide){ // hide Cancel button hideCss = ' style="display: none;"'; } } catch(e){ } $("#popup_message").after(''); // tdchien :: Custom by using extraConfig in jConfirm() if (extraConfig && extraConfig !== null) { // Check hide button OK/Cancel if (extraConfig.hideButtonOK === true) { $("#popup_ok").hide(); } if (extraConfig.hideButtonCancel === true) { $("#popup_cancel").hide(); } } $("#popup_ok").click( function() { var dataObj = $("#popup_container").find('#popup_message :input'); var data = {}; for(var i=0; i').after(''); $("#popup_prompt").width( $("#popup_message").width() ); $("#popup_ok").click( function() { var val = $("#popup_prompt").val(); $.alerts._hide(); if( callback ) callback( val ); }); $("#popup_cancel").click( function() { $.alerts._hide(); if( callback ) callback( null ); }); $("#popup_prompt, #popup_ok, #popup_cancel").keypress( function(e) { if( e.keyCode == 13 ) $("#popup_ok").trigger('click'); if( e.keyCode == 27 ) $("#popup_cancel").trigger('click'); }); if( value ) $("#popup_prompt").val(value); $("#popup_prompt").focus().select(); setTimeout(function() { $("#popup_ok").focus(); }, 250); break; case 'load': $("#popup_message").after(''); $("#popup_ok").click( function() { $.alerts._hide(); callback(true); }); $("#popup_ok").focus().keypress( function(e) { if( e.keyCode == 13 || e.keyCode == 27 ) $("#popup_ok").trigger('click'); }); break; } /* // Make draggable requires jQuery UI draggables if( $.alerts.draggable ) { try { $("#popup_container").draggable({ handle: $("#popup_title") }); $("#popup_title").css({ cursor: 'move' }); } catch(e) { } } */ }, // tdchien :: Custom when showing dialog by extraConfig _show_3_button : function(msg, title, callback, extraConfig) { $.alerts._hide(); $.alerts._overlay('show'); $("BODY").append( ''); if( $.alerts.dialogClass ) $("#popup_container").addClass($.alerts.dialogClass); // IE6 Fix //var pos = ($.browser.msie && parseInt($.browser.version) <= 6 ) ? 'absolute' : 'fixed'; var pos = 'fixed'; $("#popup_container").css({ position: pos, zIndex: 99999, padding: 0, margin: 0 }); //$("#popup_title").text(title); $("#popup_message").text(msg); $("#popup_message").html( $("#popup_message").text().replace(/\n/g, '
') ); $("#popup_container").css({ minWidth: $("#popup_container").outerWidth(), maxWidth: $("#popup_container").outerWidth() }); $.alerts._reposition(); $.alerts._maintainPosition(true); // Fix bug 0024217: Please replace "OK" to "Ok" on all popup dialog contain this character // Auto replace value of $.alerts.okButton if needed var lowerOKValue = $.alerts.okButton.toLowerCase(); if (lowerOKValue === CUSTOM_OK_BUTTON_LABEL.toLowerCase() || lowerOKValue === 'ok') { $.alerts.okButton = CUSTOM_OK_BUTTON_LABEL; } var okLabel = $.alerts.okButton; var cancelLabel = $.alerts.cancelButton; var customLabel = 'Custom'; var customLabel2 = 'Custom2'; var okClass = ''; var cancelClass = ''; var customClass = ''; var customClass2 = ''; var isShowCancel = true; var isShowOK = true; // 'Edit instance' var isShowCustom = true; // 'Edit series' var isShowCustom2 = false; // 'View' if (extraConfig) { if (extraConfig.okLabel && extraConfig.okLabel !== '') { okLabel = extraConfig.okLabel; } if (extraConfig.okClass && extraConfig.okClass !== '') { okClass = extraConfig.okClass; } if (extraConfig.cancelLabel && extraConfig.cancelLabel !== '') { cancelLabel = extraConfig.cancelLabel; } if (extraConfig.cancelClass && extraConfig.cancelClass !== '') { cancelClass = extraConfig.cancelClass; } if (extraConfig.customLabel && extraConfig.customLabel !== '') { customLabel = extraConfig.customLabel; } if (extraConfig.customClass && extraConfig.customClass !== '') { customClass = extraConfig.customClass; } // The custom 2 label if (extraConfig.customLabel2 && extraConfig.customLabel2 !== '') { customLabel2 = extraConfig.customLabel2; } if (extraConfig.customClass2 && extraConfig.customClass2 !== '') { customClass2 = extraConfig.customClass2; } // Check if we need show custom if (extraConfig.isShowCustom2) { isShowCustom2 = true; } if (false == extraConfig.isShowCustom) { isShowCustom = false; } if (false == extraConfig.isShowOk) { isShowOK = false; } } var cntPanel = []; cntPanel.push(''); $("#popup_message").after(cntPanel.join('')); $('#popup_panel').find('#popup_ok, #popup_custom, #popup_custom_2, #popup_cancel').click( function() { var buttonClick = CUSTOM_ALERT_BUTTON_CANCEL_CLICK; switch (this.id) { case 'popup_ok' : buttonClick = CUSTOM_ALERT_BUTTON_OK_CLICK; break; case 'popup_custom' : buttonClick = CUSTOM_ALERT_BUTTON_CUSTOM_CLICK; break; case 'popup_custom_2' : buttonClick = CUSTOM_ALERT_BUTTON_CUSTOM_2_CLICK; break; case 'popup_cancel' : buttonClick = CUSTOM_ALERT_BUTTON_CANCEL_CLICK; break; default : buttonClick = CUSTOM_ALERT_BUTTON_CANCEL_CLICK; } var dataObj = $("#popup_container").find('#popup_message :input'); var data = {}; for(var i=0; i'); $("#popup_overlay").css({ position: 'absolute', zIndex: 99998, top: '0px', left: '0px', width: '100%', height: $(document).height(), background: $.alerts.overlayColor, opacity: $.alerts.overlayOpacity }); break; case 'hide': $("#popup_overlay").remove(); break; } }, _reposition: function() { var top = (($(window).height() / 2) - ($("#popup_container").outerHeight() / 2)) + $.alerts.verticalOffset; var left = (($(window).width() / 2) - ($("#popup_container").outerWidth() / 2)) + $.alerts.horizontalOffset; if( top < 0 ) top = 0; if( left < 0 ) left = 0; // IE6 fix //if( $.browser.msie && parseInt($.browser.version) <= 6 ) top = top + $(window).scrollTop(); // CO-2135 if($.alerts.cssTop != undefined && $.alerts.cssTop != ''){ top = $.alerts.cssTop; } $("#popup_container").css({ top: top + 'px', left: left + 'px' }); $("#popup_overlay").height( $(document).height() ); }, _maintainPosition: function(status) { if( $.alerts.repositionOnResize ) { switch(status) { case true: $(window).bind('resize', $.alerts._reposition); break; case false: $(window).unbind('resize', $.alerts._reposition); break; } } } } // Shortuct functions jAlert = function(message, title, callback) { $.alerts.alert(message, title, callback); } // tdchien :: Custom a parameter for apply more config for jAlert jConfirm = function(message, title, callback, extraConfig) { $.alerts.confirm(message, title, callback, extraConfig); }; jPrompt = function(message, value, title, callback) { $.alerts.prompt(message, value, title, callback); }; jLoad = function(message, title, callback) { $.alerts.load(message, title, callback); } // tdchien :: Custom a new dialog for jConfirm jConfirm3Btn = function(message, callback, extraConfig) { $.alerts._show_3_button(message, '', callback, extraConfig); }; })(jQuery); var HideAlert = function() { $.alerts._hide(); } /* Optional: Overwrites javascript's built-in alert function */ function alert(msg, calback){ jAlert(msg, '', calback); }