forked from sciactive/pnotify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pnotify.history.js
189 lines (179 loc) · 5.73 KB
/
pnotify.history.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
186
187
188
189
// History
// Uses AMD or browser globals for jQuery.
(function (factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as a module.
define('pnotify.history', ['jquery', 'pnotify'], factory);
} else {
// Browser globals
factory(jQuery, PNotify);
}
}(function($, PNotify){
var history_menu,
history_handle_top;
$(function(){
$("body").on("pnotify.history-all", function(){
// Display all notices. (Disregarding non-history notices.)
$.each(PNotify.notices, function(){
if (this.modules.history.inHistory) {
if (this.elem.is(":visible")) {
// The hide variable controls whether the history pull down should
// queue a removal timer.
if (this.options.hide)
this.queueRemove();
} else if (this.open)
this.open();
}
});
}).on("pnotify.history-last", function(){
var pushTop = (PNotify.prototype.options.stack.push === "top");
// Look up the last history notice, and display it.
var i = (pushTop ? 0 : -1);
var notice;
do {
if (i === -1)
notice = PNotify.notices.slice(i);
else
notice = PNotify.notices.slice(i, i+1);
if (!notice[0])
return false;
i = (pushTop ? i + 1 : i - 1);
} while (!notice[0].modules.history.inHistory || notice[0].elem.is(":visible"));
if (notice[0].open)
notice[0].open();
});
});
PNotify.prototype.options.history = {
// Place the notice in the history.
history: true,
// Display a pull down menu to redisplay previous notices.
menu: false,
// Make the pull down menu fixed to the top of the viewport.
fixed: true,
// Maximum number of notifications to have onscreen.
maxonscreen: Infinity,
// The various displayed text, helps facilitating internationalization.
labels: {
redisplay: "Redisplay",
all: "All",
last: "Last"
}
};
PNotify.prototype.modules.history = {
// The history variable controls whether the notice gets redisplayed
// by the history pull down.
inHistory: false,
init: function(notice, options){
// Make sure that no notices get destroyed.
notice.options.destroy = false;
this.inHistory = options.history;
if (options.menu) {
// If there isn't a history pull down, create one.
if (typeof history_menu === "undefined") {
history_menu = $("<div />", {
"class": "ui-pnotify-history-container "+notice.styles.hi_menu,
"mouseleave": function(){
history_menu.animate({top: "-"+history_handle_top+"px"}, {duration: 100, queue: false});
}
})
.append($("<div />", {"class": "ui-pnotify-history-header", "text": options.labels.redisplay}))
.append($("<button />", {
"class": "ui-pnotify-history-all "+notice.styles.hi_btn,
"text": options.labels.all,
"mouseenter": function(){
$(this).addClass(notice.styles.hi_btnhov);
},
"mouseleave": function(){
$(this).removeClass(notice.styles.hi_btnhov);
},
"click": function(){
$(this).trigger("pnotify.history-all");
return false;
}
}))
.append($("<button />", {
"class": "ui-pnotify-history-last "+notice.styles.hi_btn,
"text": options.labels.last,
"mouseenter": function(){
$(this).addClass(notice.styles.hi_btnhov);
},
"mouseleave": function(){
$(this).removeClass(notice.styles.hi_btnhov);
},
"click": function(){
$(this).trigger("pnotify.history-last");
return false;
}
}))
.appendTo("body");
// Make a handle so the user can pull down the history tab.
var handle = $("<span />", {
"class": "ui-pnotify-history-pulldown "+notice.styles.hi_hnd,
"mouseenter": function(){
history_menu.animate({top: "0"}, {duration: 100, queue: false});
}
})
.appendTo(history_menu);
// Get the top of the handle.
console.log(handle.offset());
history_handle_top = handle.offset().top + 2;
// Hide the history pull down up to the top of the handle.
history_menu.css({top: "-"+history_handle_top+"px"});
// Apply the fixed styling.
if (options.fixed) {
history_menu.addClass('ui-pnotify-history-fixed');
}
}
}
},
update: function(notice, options){
// Update values for history menu access.
this.inHistory = options.history;
if (options.fixed && history_menu) {
history_menu.addClass('ui-pnotify-history-fixed');
} else if (history_menu) {
history_menu.removeClass('ui-pnotify-history-fixed');
}
},
beforeOpen: function(notice, options){
// Remove oldest notifications leaving only options.maxonscreen on screen
if (PNotify.notices && (PNotify.notices.length > options.maxonscreen)) {
// Oldest are normally in front of array, or if stack.push=="top" then
// they are at the end of the array! (issue #98)
var el;
if (notice.options.stack.push !== "top")
el = PNotify.notices.slice(0, PNotify.notices.length - options.maxonscreen);
else
el = PNotify.notices.slice(options.maxonscreen, PNotify.notices.length);
$.each(el, function(){
if (this.remove)
this.remove();
});
}
}
};
$.extend(PNotify.styling.jqueryui, {
hi_menu: "ui-state-default ui-corner-bottom",
hi_btn: "ui-state-default ui-corner-all",
hi_btnhov: "ui-state-hover",
hi_hnd: "ui-icon ui-icon-grip-dotted-horizontal"
});
$.extend(PNotify.styling.bootstrap2, {
hi_menu: "well",
hi_btn: "btn",
hi_btnhov: "",
hi_hnd: "icon-chevron-down"
});
$.extend(PNotify.styling.bootstrap3, {
hi_menu: "well",
hi_btn: "btn btn-default",
hi_btnhov: "",
hi_hnd: "glyphicon glyphicon-chevron-down"
});
$.extend(PNotify.styling.fontawesome, {
hi_menu: "well",
hi_btn: "btn btn-default",
hi_btnhov: "",
hi_hnd: "fa fa-chevron-down"
});
}));