forked from dknight/jQuery-Notify-bar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.notifyBar.js
153 lines (140 loc) · 4.85 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
/*
* Notify Bar - $ plugin
*
* Copyright (c) 2009-2013 Dmitri Smirnov
*
* Licensed under the MIT license:
* http://www.opensource.org/licenses/mit-license.php
*
* Version: 1.4
*
* Project home:
* http://www.whoop.ee/posts/2013-04-05-the-resurrection-of-jquery-notify-bar/
*/
(function ($) {
$.notifyBar = function (options) {
var rand = parseInt(Math.random() * 100000000, 0),
text_wrapper,
bar = {},
settings = {};
settings = $.extend({
html : 'Your message here',
delay : 2000,
animationSpeed : 200,
cssClass : '',
jqObject : '',
close : false,
closeText : 'Close [X]',
closeOnClick : 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 (delayed) {
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;
}
if (bar !== 'object') {
$("body").prepend(bar);
}
// Style close button in CSS file
if (settings.close) {
// If close settings is true. Set delay to one billion seconds.
// It'a about 31 years - mre than enough for cases when notify bar is used.
settings.delay = Math.pow(10, 9);
bar.append($("<a href='#' class='notify-bar-close'>" + settings.closeText + "</a>"));
$(".notify-bar-close").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.click(function () {
bar.hideNB();
});
}
// Allow the user to move mouse on the bar to close it
if (settings.closeOnOver) {
bar.mouseover(function () {
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');
}
};
})(jQuery);