forked from needim/noty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.noty.new.js
189 lines (152 loc) · 5.25 KB
/
jquery.noty.new.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
/**
* jQuery Noty Plugin v0.1
*
* Copyright (c) 2011 LMS Co Inc.
* Authors: Nedim Arabacı (http://ned.im), Muhittin Özer (http://muhittinozer.com)
*
* Created: 31/10/2011
*
* Licensed under the MIT licenses:
* http://www.opensource.org/licenses/mit-license.php
*
**/
(function($) {
$.noty = function(options) {
// To avoid scope issues, use 'base' instead of 'this'
// to reference this class from internal events and functions.
var base = this;
base.init = function() {
base.options = $.extend({}, $.noty.defaultOptions, options);
// Push notification to que
if (base.options.force || base.options.layout == 'topLeft' || base.options.layout == 'topRight') {
$.noty.que.unshift({options: base.options});
$('#noty_que_list').prepend($('<li/>').addClass(base.options.type).html(base.options.type));
} else {
$.noty.que.push({options: base.options});
$('#noty_que_list').append($('<li/>').addClass(base.options.type).html(base.options.type));
}
base.render();
};
// Render the que
base.render = function() {
if ($.noty.available) {
// Get noty from que
var notification = $.noty.que.shift();
if (jQuery.type(notification) === 'object') {
// Layout spesific container settings
if(notification.options.layout == "topLeft" || notification.options.layout == "topRight")
{
if($("ul.noty_container."+notification.options.layout).length > 0)
{
base.$noty_container = $("ul.noty_container."+notification.options.layout);
}
else
{
base.$noty_container = $('<ul/>').addClass('noty_container').addClass(notification.options.layout);
$("body").prepend(base.$noty_container);
}
base.$notyContainer = $('<li/>');
base.$noty_container.prepend(base.$notyContainer);
$.noty.available = true;
}
else
{
$.noty.available = false;
base.$notyContainer = $("body");
}
base.$bar = $('<div/>').addClass('noty_bar');
base.$message = $('<div/>').addClass('noty_message');
base.$close = $('<div/>').addClass('noty_close');
base.$bar.append(base.$message).append(base.$close);
var $noty = base.$bar;
$noty.data('noty_options', notification.options);
// Basic layout settings
$noty.addClass(notification.options.layout).addClass(notification.options.type);
// Message and style settings
$noty.find('.noty_message').html(notification.options.text).css({textAlign: notification.options.textAlign});
// Closable option
(notification.options.closable) ? $noty.find('.noty_close').show() : $noty.find('.noty_close').remove();
// Prepend noty to container
base.$notyContainer.prepend($noty);
// Bind close event
$noty.bind('noty.close', function(event, callback) {
var options = $noty.data('noty_options');
$noty.stop().animate(
$noty.data('noty_options').animateClose,
$noty.data('noty_options').speed,
$noty.data('noty_options').easing,
$noty.data('noty_options').onClose)
.promise().done(function() {
// Layout spesific cleaning
if (options.layout == 'topLeft' || options.layout == 'topRight') {
$('#noty_que_list li:last').slideUp().remove();
$noty.parent().remove();
} else {
$('#noty_que_list li:first').slideUp().remove();
$noty.remove();
}
$.noty.available = true;
// Are we have a callback function?
if ($.isFunction(callback)) {
callback.apply();
}
// Que render
base.render();
});
});
// Bind close event to button
$noty.find('.noty_close').one('click', function() { $noty.trigger('noty.close'); });
// Set buttons if available
if (notification.options.buttons)
{
$buttons = $('<div/>').addClass('noty_buttons');
$noty.find('.noty_message').append($buttons);
$.each(notification.options.buttons, function(i, v) {
var button = v,
bclass = (button.type) ? button.type : 'gray';
$('<button/>').addClass(bclass).html(button.text).appendTo($noty.find('.noty_buttons')).one("click", function() { $noty.trigger('noty.close', button.click); });
});
}
// Start the show
$noty.animate(
notification.options.animateOpen,
notification.options.speed,
notification.options.easing,
notification.options.onShow);
// If noty is have a timeout option
if (notification.options.timeout) {
$noty.delay(notification.options.timeout).promise().done(function() {$noty.trigger('noty.close');});
}
}
}
};
// Run initializer
base.init();
};
$.noty.que = [];
$.noty.available = true;
$.noty.defaultOptions = {
layout : "top",
animateOpen : {height: 'toggle'},
animateClose : {height: 'toggle'},
easing : 'swing',
text : "null",
textAlign : "center",
type : "alert",
speed : 500,
timeout : 5000,
closable : true,
force : false,
onShow : false,
onClose : false,
buttons : false
};
$.fn.noty = function(options) {
return new $.noty(options);
};
})(jQuery);
// Helper
function noty(options)
{
$.fn.noty(options);
}