This repository has been archived by the owner on May 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
/
jquery.notifyBar.js
185 lines (170 loc) · 6.38 KB
/
jquery.notifyBar.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/**
* Notify Bar - jQuery plugin
*
* Copyright (c) 2009-2016 Dmitri Smirnov
*
* Licensed under the MIT license:
* http://www.opensource.org/licenses/mit-license.php
*
* Project home:
* http://www.whoop.ee/posts/2013/04/05/the-resurrection-of-jquery-notify-bar.html
*
* Uses CommonJS, AMD or browser globals to create a jQuery plugin.
* Ref: https://github.com/umdjs/umd/blob/master/templates/jqueryPlugin.js
*/
(function (factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['jquery'], factory);
} else if (typeof module === 'object' && module.exports) {
// Node/CommonJS
module.exports = function( root, jQuery ) {
if ( jQuery === undefined ) {
// require('jQuery') returns a factory that requires window to
// build a jQuery instance, we normalize how we use modules
// that require this pattern but the window provided is a noop
// if it's defined (how jquery works)
if ( typeof window !== 'undefined' ) {
jQuery = require('jquery');
}
else {
jQuery = require('jquery')(root);
}
}
factory(jQuery);
return jQuery;
};
} else {
// Browser globals
factory(jQuery);
}
}(function ($) {
'use strict';
$.notifyBar = function (options) {
var rand = parseInt(Math.random() * 100000000, 0),
text_wrapper, asTime,
$bar = {},
settings = {};
settings = $.extend({
html : 'Your message here', // Notify bar's text or HTML
delay : 3000, // How many microseconds notifybar will be shown
animationSpeed : 200, // Animation time
cssClass : '', // CSS class
jqObject : '', // Custom jQuery object
close : false, // Do we show close button?
closeText : '×', // Text for close button
closeOnClick : true,
waitingForClose : true,
closeOnOver : false,
onBeforeShow : null,
onShow : null,
onBeforeHide : null,
onHide : null,
position : 'top'
}, options);
// Use these methods as private.
this.fn.showNB = function () {
if (typeof settings.onBeforeShow === 'function') {
settings.onBeforeShow.call();
}
$(this).stop().slideDown(asTime, function () {
if (typeof settings.onShow === 'function') {
settings.onShow.call();
}
});
};
this.fn.hideNB = function () {
if (typeof settings.onBeforeHide === 'function') {
settings.onBeforeHide.call();
}
$(this).stop().slideUp(asTime, function () {
if ($bar.attr("id") === '__notifyBar' + rand) {
$(this).slideUp(asTime, function () {
$(this).remove();
if (typeof settings.onHide === 'function') {
settings.onHide.call();
}
});
} else {
$(this).slideUp(asTime, function () {
if (typeof settings.onHide === 'function') {
settings.onHide.call();
}
});
}
});
};
if (settings.jqObject) {
$bar = settings.jqObject;
settings.html = $bar.html();
} else {
$bar = $("<div></div>")
.addClass("jquery-notify-bar")
.addClass(settings.cssClass)
.attr("id", "__notifyBar" + rand);
}
text_wrapper = $("<span></span>")
.addClass("notify-bar-text-wrapper")
.html(settings.html);
$bar.html(text_wrapper).hide();
var id = $bar.attr("id");
switch (settings.animationSpeed) {
case "slow":
asTime = 600;
break;
case "default":
case "normal":
asTime = 400;
break;
case "fast":
asTime = 200;
break;
default:
asTime = settings.animationSpeed;
}
$("body").prepend($bar);
// Close button style in CSS file
if (settings.close) {
// If close settings is true. Set delay to one billion seconds.
// It'a about 31 years - more than enough for cases when notify bar is used :-)
if (settings.waitingForClose) {
settings.delay = Math.pow(10, 9);
}
$bar.append($("<a href='#' class='notify-bar-close'>" + settings.closeText + "</a>"));
$(".notify-bar-close").on('click', function (event) {
event.preventDefault();
$bar.hideNB();
});
}
// Check if we've got any visible bars and if we have,
// slide them up before showing the new one
if ($('.jquery-notify-bar:visible').length > 0) {
$('.jquery-notify-bar:visible').stop().slideUp(asTime, function () {
$bar.showNB();
});
} else {
$bar.showNB();
}
// Allow the user to click on the bar to close it
if (settings.closeOnClick) {
$bar.on('click', function (event) {
$bar.hideNB();
});
}
// Allow the user to move mouse on the bar to close it
if (settings.closeOnOver) {
$bar.on('mouseover', function (event) {
$bar.hideNB();
});
}
setTimeout(function () {
$bar.hideNB(settings.delay);
}, settings.delay + asTime);
if (settings.position === 'bottom') {
$bar.addClass('bottom');
} else if (settings.position === 'top') {
$bar.addClass('top');
}
return $bar;
};
}));