/feincms/static/feincms/jquery.alerts.js

http://github.com/feincms/feincms · JavaScript · 235 lines · 175 code · 30 blank · 30 comment · 34 complexity · 7590d8fab0886cea46b6bfc57d63dbf5 MD5 · raw file

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