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