forked from pynklynn/notific8
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.notific8.js
199 lines (179 loc) · 7.01 KB
/
jquery.notific8.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
190
191
192
193
194
195
196
197
198
199
/**
* @author Will Steinmetz
*
* jQuery notification plug-in inspired by the notification style of Windows 8
*
* Copyright (c)2013, Will Steinmetz
* Licensed under the BSD license.
* http://opensource.org/licenses/BSD-3-Clause
*/
; (function ($) {
var settings = {
life: 10000,
theme: 'teal',
sticky: false,
verticalEdge: 'right',
horizontalEdge: 'top',
zindex: 1100
};
var methods = {
init: function(message, options) {
return this.each(function() {
var $this = $(this),
data = $this.data('notific8');
if (!data) {
$this.data('notific8', {
target: $this,
settings: {},
message: ""
});
data = $this.data('notific8');
}
data.message = message;
// apply the options
$.extend(data.settings, settings, options);
// add the notification to the stack
methods._buildNotification($this);
});
},
/**
* Destroy the notification
*/
destroy: function($this) {
var data = $this.data('notific8');
$(window).unbind('.notific8');
$this.removeData('notific8');
},
/**
* Build the notification and add it to the screen's stack
*/
_buildNotification: function($this) {
var data = $this.data('notific8'),
notification = $('<div />'),
num = Number($('body').attr('data-notific8s'));
num++;
notification.addClass('jquery-notific8-notification').addClass(data.settings.theme);
notification.attr('id', 'jquery-notific8-notification-' + num);
$('body').attr('data-notific8s', num);
// check for a heading
if (data.settings.hasOwnProperty('heading') && (typeof data.settings.heading == "string")) {
notification.append($('<div />').addClass('jquery-notific8-heading').html(data.settings.heading));
}
// check if the notification is supposed to be sticky
if (data.settings.sticky) {
var close = $('<div />').addClass('jquery-notific8-close-sticky').append(
$('<span />').html('close x')
);
close.click(function(event) {
notification.animate({width: 'hide'}, {
duration: 'fast',
complete: function() {
notification.remove();
}
});
});
notification.append(close);
notification.addClass('sticky');
}
// otherwise, put the normal close button up that is only display
// when the notification is hovered over
else {
var close = $('<div />').addClass('jquery-notific8-close').append(
$('<span />').html('X')
);
close.click(function(event) {
notification.animate({width: 'hide'}, {
duration: 'fast',
complete: function() {
notification.remove();
}
});
});
notification.append(close);
notification.mouseenter(function(event) {
close.show();
}).mouseleave(function(event) {
close.hide();
});;
}
// add the message
notification.append($('<div />').addClass('jquery-notific8-message').html(data.message));
// add the notification to the stack
$('.jquery-notific8-container.' + data.settings.verticalEdge + '.' + data.settings.horizontalEdge).append(notification);
// slide the message onto the screen
notification.animate({width: 'show'}, {
duration: 'fast',
complete: function() {
if (!data.settings.sticky) {
setTimeout(function() {
notification.animate({width: 'hide'}, {
duration: 'fast',
complete: function() {
notification.remove();
}
});
}, data.settings.life);
}
data.settings = {};
}
});
},
/**
* Set up the configuration settings
*/
configure: function(options) {
$.extend(settings, options);
},
/**
* Set up the z-index
*/
zindex: function(zindex) {
settings.zindex = zindex;
}
};
// wrapper since this plug-in is called without selecting an item first
$.notific8 = function(message, options) {
switch (message) {
case 'configure':
case 'config':
return methods.configure.apply(this, [options]);
break;
case 'zindex':
return methods.zindex.apply(this, [options]);
break;
default:
if (typeof options == undefined) {
options = {};
}
// make sure that the stack containers exist
if ($('.jquery-notific8-container').size() === 0) {
$('body').attr('data-notific8s', 0);
$('body').append($('<div />').addClass('jquery-notific8-container').addClass('top').addClass('right'));
$('body').append($('<div />').addClass('jquery-notific8-container').addClass('top').addClass('left'));
$('body').append($('<div />').addClass('jquery-notific8-container').addClass('bottom').addClass('right'));
$('body').append($('<div />').addClass('jquery-notific8-container').addClass('bottom').addClass('left'));
$('.jquery-notific8-container').css('z-index', settings.zindex);
}
// make sure the edge settings exist
if ((!options.hasOwnProperty('verticalEdge')) || ((options.verticalEdge.toLowerCase() != 'right') && (options.verticalEdge.toLowerCase() != 'left'))) {
options.verticalEdge = 'right';
}
if ((!options.hasOwnProperty('horizontalEdge')) || ((options.horizontalEdge.toLowerCase() != 'top') && (options.horizontalEdge.toLowerCase() != 'bottom'))) {
options.horizontalEdge = 'top';
}
options.verticalEdge = options.verticalEdge.toLowerCase();
options.horizontalEdge = options.horizontalEdge.toLowerCase();
//display the notification in the right corner
$('.jquery-notific8-container.' + options.verticalEdge + '.' + options.horizontalEdge).notific8(message, options);
break;
}
};
// plugin setup
$.fn.notific8 = function(message, options) {
if (typeof message == "string") {
return methods.init.apply(this, arguments);
} else {
$.error('jQuery.notific8 takes a string message as the first parameter');
}
};
})(jQuery);