PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/files/jgrowl/1.2.6/jquery.jgrowl.js

https://gitlab.com/Mirros/jsdelivr
JavaScript | 338 lines | 172 code | 37 blank | 129 comment | 56 complexity | 4db1c4f092e14c7b2d5488b3d0055021 MD5 | raw file
  1. /**
  2. * jGrowl 1.2.6
  3. *
  4. * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
  5. * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
  6. *
  7. * Written by Stan Lemon <stosh1985@gmail.com>
  8. * Last updated: 2011.03.27
  9. *
  10. * jGrowl is a jQuery plugin implementing unobtrusive userland notifications. These
  11. * notifications function similarly to the Growl Framework available for
  12. * Mac OS X (http://growl.info).
  13. *
  14. * To Do:
  15. * - Move library settings to containers and allow them to be changed per container
  16. *
  17. * Changes in 1.2.6
  18. * - Fixed js error when a notification is opening and closing at the same time
  19. *
  20. * Changes in 1.2.5
  21. * - Changed wrapper jGrowl's options usage to "o" instead of $.jGrowl.defaults
  22. * - Added themeState option to control 'highlight' or 'error' for jQuery UI
  23. * - Ammended some CSS to provide default positioning for nested usage.
  24. * - Changed some CSS to be prefixed with jGrowl- to prevent namespacing issues
  25. * - Added two new options - openDuration and closeDuration to allow
  26. * better control of notification open and close speeds, respectively
  27. * Patch contributed by Jesse Vincet.
  28. * - Added afterOpen callback. Patch contributed by Russel Branca.
  29. *
  30. * Changes in 1.2.4
  31. * - Fixed IE bug with the close-all button
  32. * - Fixed IE bug with the filter CSS attribute (special thanks to gotwic)
  33. * - Update IE opacity CSS
  34. * - Changed font sizes to use "em", and only set the base style
  35. *
  36. * Changes in 1.2.3
  37. * - The callbacks no longer use the container as context, instead they use the actual notification
  38. * - The callbacks now receive the container as a parameter after the options parameter
  39. * - beforeOpen and beforeClose now check the return value, if it's false - the notification does
  40. * not continue. The open callback will also halt execution if it returns false.
  41. * - Fixed bug where containers would get confused
  42. * - Expanded the pause functionality to pause an entire container.
  43. *
  44. * Changes in 1.2.2
  45. * - Notification can now be theme rolled for jQuery UI, special thanks to Jeff Chan!
  46. *
  47. * Changes in 1.2.1
  48. * - Fixed instance where the interval would fire the close method multiple times.
  49. * - Added CSS to hide from print media
  50. * - Fixed issue with closer button when div { position: relative } is set
  51. * - Fixed leaking issue with multiple containers. Special thanks to Matthew Hanlon!
  52. *
  53. * Changes in 1.2.0
  54. * - Added message pooling to limit the number of messages appearing at a given time.
  55. * - Closing a notification is now bound to the notification object and triggered by the close button.
  56. *
  57. * Changes in 1.1.2
  58. * - Added iPhone styled example
  59. * - Fixed possible IE7 bug when determining if the ie6 class shoudl be applied.
  60. * - Added template for the close button, so that it's content could be customized.
  61. *
  62. * Changes in 1.1.1
  63. * - Fixed CSS styling bug for ie6 caused by a mispelling
  64. * - Changes height restriction on default notifications to min-height
  65. * - Added skinned examples using a variety of images
  66. * - Added the ability to customize the content of the [close all] box
  67. * - Added jTweet, an example of using jGrowl + Twitter
  68. *
  69. * Changes in 1.1.0
  70. * - Multiple container and instances.
  71. * - Standard $.jGrowl() now wraps $.fn.jGrowl() by first establishing a generic jGrowl container.
  72. * - Instance methods of a jGrowl container can be called by $.fn.jGrowl(methodName)
  73. * - Added glue preferenced, which allows notifications to be inserted before or after nodes in the container
  74. * - Added new log callback which is called before anything is done for the notification
  75. * - Corner's attribute are now applied on an individual notification basis.
  76. *
  77. * Changes in 1.0.4
  78. * - Various CSS fixes so that jGrowl renders correctly in IE6.
  79. *
  80. * Changes in 1.0.3
  81. * - Fixed bug with options persisting across notifications
  82. * - Fixed theme application bug
  83. * - Simplified some selectors and manipulations.
  84. * - Added beforeOpen and beforeClose callbacks
  85. * - Reorganized some lines of code to be more readable
  86. * - Removed unnecessary this.defaults context
  87. * - If corners plugin is present, it's now customizable.
  88. * - Customizable open animation.
  89. * - Customizable close animation.
  90. * - Customizable animation easing.
  91. * - Added customizable positioning (top-left, top-right, bottom-left, bottom-right, center)
  92. *
  93. * Changes in 1.0.2
  94. * - All CSS styling is now external.
  95. * - Added a theme parameter which specifies a secondary class for styling, such
  96. * that notifications can be customized in appearance on a per message basis.
  97. * - Notification life span is now customizable on a per message basis.
  98. * - Added the ability to disable the global closer, enabled by default.
  99. * - Added callbacks for when a notification is opened or closed.
  100. * - Added callback for the global closer.
  101. * - Customizable animation speed.
  102. * - jGrowl now set itself up and tears itself down.
  103. *
  104. * Changes in 1.0.1:
  105. * - Removed dependency on metadata plugin in favor of .data()
  106. * - Namespaced all events
  107. */
  108. (function($) {
  109. /** jGrowl Wrapper - Establish a base jGrowl Container for compatibility with older releases. **/
  110. $.jGrowl = function( m , o ) {
  111. // To maintain compatibility with older version that only supported one instance we'll create the base container.
  112. if ( $('#jGrowl').size() == 0 )
  113. $('<div id="jGrowl"></div>').addClass( (o && o.position) ? o.position : $.jGrowl.defaults.position ).appendTo('body');
  114. // Create a notification on the container.
  115. $('#jGrowl').jGrowl(m,o);
  116. };
  117. /** Raise jGrowl Notification on a jGrowl Container **/
  118. $.fn.jGrowl = function( m , o ) {
  119. if ( $.isFunction(this.each) ) {
  120. var args = arguments;
  121. return this.each(function() {
  122. var self = this;
  123. /** Create a jGrowl Instance on the Container if it does not exist **/
  124. if ( $(this).data('jGrowl.instance') == undefined ) {
  125. $(this).data('jGrowl.instance', $.extend( new $.fn.jGrowl(), { notifications: [], element: null, interval: null } ));
  126. $(this).data('jGrowl.instance').startup( this );
  127. }
  128. /** Optionally call jGrowl instance methods, or just raise a normal notification **/
  129. if ( $.isFunction($(this).data('jGrowl.instance')[m]) ) {
  130. $(this).data('jGrowl.instance')[m].apply( $(this).data('jGrowl.instance') , $.makeArray(args).slice(1) );
  131. } else {
  132. $(this).data('jGrowl.instance').create( m , o );
  133. }
  134. });
  135. };
  136. };
  137. $.extend( $.fn.jGrowl.prototype , {
  138. /** Default JGrowl Settings **/
  139. defaults: {
  140. pool: 0,
  141. header: '',
  142. group: '',
  143. sticky: false,
  144. position: 'top-right',
  145. glue: 'after',
  146. theme: 'default',
  147. themeState: 'highlight',
  148. corners: '10px',
  149. check: 250,
  150. life: 3000,
  151. closeDuration: 'normal',
  152. openDuration: 'normal',
  153. easing: 'swing',
  154. closer: true,
  155. closeTemplate: '&times;',
  156. closerTemplate: '<div>[ close all ]</div>',
  157. log: function(e,m,o) {},
  158. beforeOpen: function(e,m,o) {},
  159. afterOpen: function(e,m,o) {},
  160. open: function(e,m,o) {},
  161. beforeClose: function(e,m,o) {},
  162. close: function(e,m,o) {},
  163. animateOpen: {
  164. opacity: 'show'
  165. },
  166. animateClose: {
  167. opacity: 'hide'
  168. }
  169. },
  170. notifications: [],
  171. /** jGrowl Container Node **/
  172. element: null,
  173. /** Interval Function **/
  174. interval: null,
  175. /** Create a Notification **/
  176. create: function( message , o ) {
  177. var o = $.extend({}, this.defaults, o);
  178. /* To keep backward compatibility with 1.24 and earlier, honor 'speed' if the user has set it */
  179. if (typeof o.speed !== 'undefined') {
  180. o.openDuration = o.speed;
  181. o.closeDuration = o.speed;
  182. }
  183. this.notifications.push({ message: message , options: o });
  184. o.log.apply( this.element , [this.element,message,o] );
  185. },
  186. render: function( notification ) {
  187. var self = this;
  188. var message = notification.message;
  189. var o = notification.options;
  190. // Support for jQuery theme-states, if this is not used it displays a widget header
  191. o.themeState = (o.themeState == '') ? '' : 'ui-state-' + o.themeState;
  192. var notification = $(
  193. '<div class="jGrowl-notification ' + o.themeState + ' ui-corner-all' +
  194. ((o.group != undefined && o.group != '') ? ' ' + o.group : '') + '">' +
  195. '<div class="jGrowl-close">' + o.closeTemplate + '</div>' +
  196. '<div class="jGrowl-header">' + o.header + '</div>' +
  197. '<div class="jGrowl-message">' + message + '</div></div>'
  198. ).data("jGrowl", o).addClass(o.theme).children('div.jGrowl-close').bind("click.jGrowl", function() {
  199. $(this).parent().trigger('jGrowl.close');
  200. }).parent();
  201. /** Notification Actions **/
  202. $(notification).bind("mouseover.jGrowl", function() {
  203. $('div.jGrowl-notification', self.element).data("jGrowl.pause", true);
  204. }).bind("mouseout.jGrowl", function() {
  205. $('div.jGrowl-notification', self.element).data("jGrowl.pause", false);
  206. }).bind('jGrowl.beforeOpen', function() {
  207. if ( o.beforeOpen.apply( notification , [notification,message,o,self.element] ) != false ) {
  208. $(this).trigger('jGrowl.open');
  209. }
  210. }).bind('jGrowl.open', function() {
  211. if ( o.open.apply( notification , [notification,message,o,self.element] ) != false ) {
  212. if ( o.glue == 'after' ) {
  213. $('div.jGrowl-notification:last', self.element).after(notification);
  214. } else {
  215. $('div.jGrowl-notification:first', self.element).before(notification);
  216. }
  217. $(this).animate(o.animateOpen, o.openDuration, o.easing, function() {
  218. // Fixes some anti-aliasing issues with IE filters.
  219. if ($.browser.msie && (parseInt($(this).css('opacity'), 10) === 1 || parseInt($(this).css('opacity'), 10) === 0))
  220. this.style.removeAttribute('filter');
  221. if ( $(this).data("jGrowl") != null ) // Happens when a notification is closing before it's open.
  222. $(this).data("jGrowl").created = new Date();
  223. $(this).trigger('jGrowl.afterOpen');
  224. });
  225. }
  226. }).bind('jGrowl.afterOpen', function() {
  227. o.afterOpen.apply( notification , [notification,message,o,self.element] );
  228. }).bind('jGrowl.beforeClose', function() {
  229. if ( o.beforeClose.apply( notification , [notification,message,o,self.element] ) != false )
  230. $(this).trigger('jGrowl.close');
  231. }).bind('jGrowl.close', function() {
  232. // Pause the notification, lest during the course of animation another close event gets called.
  233. $(this).data('jGrowl.pause', true);
  234. $(this).animate(o.animateClose, o.closeDuration, o.easing, function() {
  235. if ( $.isFunction(o.close) ) {
  236. if ( o.close.apply( notification , [notification,message,o,self.element] ) !== false )
  237. $(this).remove();
  238. } else {
  239. $(this).remove();
  240. }
  241. });
  242. }).trigger('jGrowl.beforeOpen');
  243. /** Optional Corners Plugin **/
  244. if ( o.corners != '' && $.fn.corner != undefined ) $(notification).corner( o.corners );
  245. /** Add a Global Closer if more than one notification exists **/
  246. if ( $('div.jGrowl-notification:parent', self.element).size() > 1 &&
  247. $('div.jGrowl-closer', self.element).size() == 0 && this.defaults.closer != false ) {
  248. $(this.defaults.closerTemplate).addClass('jGrowl-closer ' + this.defaults.themeState + ' ui-corner-all').addClass(this.defaults.theme)
  249. .appendTo(self.element).animate(this.defaults.animateOpen, this.defaults.speed, this.defaults.easing)
  250. .bind("click.jGrowl", function() {
  251. $(this).siblings().trigger("jGrowl.beforeClose");
  252. if ( $.isFunction( self.defaults.closer ) ) {
  253. self.defaults.closer.apply( $(this).parent()[0] , [$(this).parent()[0]] );
  254. }
  255. });
  256. };
  257. },
  258. /** Update the jGrowl Container, removing old jGrowl notifications **/
  259. update: function() {
  260. $(this.element).find('div.jGrowl-notification:parent').each( function() {
  261. if ( $(this).data("jGrowl") != undefined && $(this).data("jGrowl").created != undefined &&
  262. ($(this).data("jGrowl").created.getTime() + parseInt($(this).data("jGrowl").life)) < (new Date()).getTime() &&
  263. $(this).data("jGrowl").sticky != true &&
  264. ($(this).data("jGrowl.pause") == undefined || $(this).data("jGrowl.pause") != true) ) {
  265. // Pause the notification, lest during the course of animation another close event gets called.
  266. $(this).trigger('jGrowl.beforeClose');
  267. }
  268. });
  269. if ( this.notifications.length > 0 &&
  270. (this.defaults.pool == 0 || $(this.element).find('div.jGrowl-notification:parent').size() < this.defaults.pool) )
  271. this.render( this.notifications.shift() );
  272. if ( $(this.element).find('div.jGrowl-notification:parent').size() < 2 ) {
  273. $(this.element).find('div.jGrowl-closer').animate(this.defaults.animateClose, this.defaults.speed, this.defaults.easing, function() {
  274. $(this).remove();
  275. });
  276. }
  277. },
  278. /** Setup the jGrowl Notification Container **/
  279. startup: function(e) {
  280. this.element = $(e).addClass('jGrowl').append('<div class="jGrowl-notification"></div>');
  281. this.interval = setInterval( function() {
  282. $(e).data('jGrowl.instance').update();
  283. }, parseInt(this.defaults.check));
  284. if ($.browser.msie && parseInt($.browser.version) < 7 && !window["XMLHttpRequest"]) {
  285. $(this.element).addClass('ie6');
  286. }
  287. },
  288. /** Shutdown jGrowl, removing it and clearing the interval **/
  289. shutdown: function() {
  290. $(this.element).removeClass('jGrowl').find('div.jGrowl-notification').remove();
  291. clearInterval( this.interval );
  292. },
  293. close: function() {
  294. $(this.element).find('div.jGrowl-notification').each(function(){
  295. $(this).trigger('jGrowl.beforeClose');
  296. });
  297. }
  298. });
  299. /** Reference the Defaults Object for compatibility with older versions of jGrowl **/
  300. $.jGrowl.defaults = $.fn.jGrowl.prototype.defaults;
  301. })(jQuery);