jgrowl.js 16 KB

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