notificationFx.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /**
  2. * notificationFx.js v1.0.0
  3. * http://www.codrops.com
  4. *
  5. * Licensed under the MIT license.
  6. * http://www.opensource.org/licenses/mit-license.php
  7. *
  8. * Copyright 2014, Codrops
  9. * http://www.codrops.com
  10. */
  11. ;( function( window ) {
  12. 'use strict';
  13. var docElem = window.document.documentElement,
  14. support = { animations : Modernizr.cssanimations },
  15. animEndEventNames = {
  16. 'WebkitAnimation' : 'webkitAnimationEnd',
  17. 'OAnimation' : 'oAnimationEnd',
  18. 'msAnimation' : 'MSAnimationEnd',
  19. 'animation' : 'animationend'
  20. },
  21. // animation end event name
  22. animEndEventName = animEndEventNames[ Modernizr.prefixed( 'animation' ) ];
  23. /**
  24. * extend obj function
  25. */
  26. function extend( a, b ) {
  27. for( var key in b ) {
  28. if( b.hasOwnProperty( key ) ) {
  29. a[key] = b[key];
  30. }
  31. }
  32. return a;
  33. }
  34. /**
  35. * NotificationFx function
  36. */
  37. function NotificationFx( options ) {
  38. this.options = extend( {}, this.options );
  39. extend( this.options, options );
  40. this._init();
  41. }
  42. /**
  43. * NotificationFx options
  44. */
  45. NotificationFx.prototype.options = {
  46. // element to which the notification will be appended
  47. // defaults to the document.body
  48. wrapper : document.body,
  49. // the message
  50. message : 'yo!',
  51. // layout type: growl|attached|bar|other
  52. layout : 'growl',
  53. // effects for the specified layout:
  54. // for growl layout: scale|slide|genie|jelly
  55. // for attached layout: flip|bouncyflip
  56. // for other layout: boxspinner|cornerexpand|loadingcircle|thumbslider
  57. // ...
  58. effect : 'slide',
  59. // notice, warning, error, success
  60. // will add class ns-type-warning, ns-type-error or ns-type-success
  61. type : 'error',
  62. // if the user doesn´t close the notification then we remove it
  63. // after the following time
  64. ttl : 6000,
  65. // callbacks
  66. onClose : function() { return false; },
  67. onOpen : function() { return false; }
  68. }
  69. /**
  70. * init function
  71. * initialize and cache some vars
  72. */
  73. NotificationFx.prototype._init = function() {
  74. // create HTML structure
  75. this.ntf = document.createElement( 'div' );
  76. this.ntf.className = 'ns-box ns-' + this.options.layout + ' ns-effect-' + this.options.effect + ' ns-type-' + this.options.type;
  77. var strinner = '<div class="ns-box-inner">';
  78. strinner += this.options.message;
  79. strinner += '</div>';
  80. strinner += '<span class="ns-close"></span></div>';
  81. this.ntf.innerHTML = strinner;
  82. // append to body or the element specified in options.wrapper
  83. this.options.wrapper.insertBefore( this.ntf, this.options.wrapper.firstChild );
  84. // dismiss after [options.ttl]ms
  85. var self = this;
  86. this.dismissttl = setTimeout( function() {
  87. //if( self.active ) {
  88. self.dismiss();
  89. //}
  90. }, this.options.ttl );
  91. // init events
  92. this._initEvents();
  93. }
  94. /**
  95. * init events
  96. */
  97. NotificationFx.prototype._initEvents = function() {
  98. var self = this;
  99. // dismiss notification
  100. this.ntf.querySelector( '.ns-close' ).addEventListener( 'click', function() { self.dismiss(); } );
  101. }
  102. /**
  103. * show the notification
  104. */
  105. NotificationFx.prototype.show = function() {
  106. this.active = true;
  107. classie.remove( this.ntf, 'ns-hide' );
  108. classie.add( this.ntf, 'ns-show' );
  109. this.options.onOpen();
  110. }
  111. /**
  112. * dismiss the notification
  113. */
  114. NotificationFx.prototype.dismiss = function() {
  115. var self = this;
  116. this.active = false;
  117. clearTimeout( this.dismissttl );
  118. classie.remove( this.ntf, 'ns-show' );
  119. setTimeout( function() {
  120. classie.add( self.ntf, 'ns-hide' );
  121. // callback
  122. self.options.onClose();
  123. }, 25 );
  124. // after animation ends remove ntf from the DOM
  125. var onEndAnimationFn = function( ev ) {
  126. if( support.animations ) {
  127. if( ev.target !== self.ntf ) return false;
  128. this.removeEventListener( animEndEventName, onEndAnimationFn );
  129. }
  130. self.options.wrapper.removeChild( this );
  131. };
  132. if( support.animations ) {
  133. this.ntf.addEventListener( animEndEventName, onEndAnimationFn );
  134. }
  135. else {
  136. onEndAnimationFn();
  137. }
  138. }
  139. /**
  140. * add to global namespace
  141. */
  142. window.NotificationFx = NotificationFx;
  143. } )( window );