forked from ThrivingKings/Sticky
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sticky.js
122 lines (102 loc) · 3.87 KB
/
sticky.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
/*
* jquery notification
*
* @development started:13-7-17
* @last update:
* @author:Gongle
*/
;
(function ($, window, document, undefined) {
// Using it without an object
$.sticky = function (note, options, callback) {
return $.fn.sticky(note, options, callback);
};
$.fn.sticky = function (note, options, callback) {
var settings =
{
'speed': 'fast', // animations: fast, slow, or integer
'duplicates': false, // true or false
'autoclose': 5000, // integer or false
'position': 'top-right', // top-center, top-left, top-right, bottom-left, or bottom-right
'type': '' // st-success, st-info, st-error
};
if (options) {
$.extend(settings, options);
}
// Passing in the object instead of specifying a note
if (!note) {
note = this.html();
}
// Variables
var display = true, duplicate = 'no';
// Somewhat of a unique ID
var uniqID = Math.floor(Math.random() * 99999);
// Handling duplicate notes and IDs
$('.sticky-note').each(function () {
if ($(this).html() == note && $(this).is(':visible')) {
duplicate = 'yes';
if (!settings['duplicates']) {
display = false;
}
}
if ($(this).attr('id') == uniqID) {
uniqID = Math.floor(Math.random() * 9999999);
}
});
// Make sure the sticky queue exists
if (!$('body').find('.sticky-queue.' + settings.position).html()) {
$('body').append('<div class="sticky-queue ' + settings.position + '"></div>');
}
// Can it be displayed?
if (display) {
// Building and inserting sticky note
$('.sticky-queue .' + settings.position).prepend('<div class="sticky border-' + settings.position + ' ' + settings.type + '" id="' + uniqID + '"></div>');
$('#' + uniqID).append('<span class="close st-close" rel="' + uniqID + '" title="Close">×</span>');
$('#' + uniqID).append('<div class="sticky-note" rel="' + uniqID + '">' + note + '</div>');
// Smoother animation
var height = $('#' + uniqID).height();
$('#' + uniqID).css('height', height);
$('#' + uniqID).slideDown(settings['speed']);
display = true;
}
// Listeners
$('.sticky').ready(function () {
// If 'autoclose' is enabled, set a timer to close the sticky
if (settings['autoclose']) {
$('#' + uniqID).delay(settings['autoclose']).slideUp(settings['speed'], function () {
var closest = $(this).closest('.sticky-queue');
var elem = closest.find('.sticky');
$(this).remove();
if (elem.length == '1') {
closest.remove()
}
});
}
});
// Closing a sticky
$('.st-close').click(function () {
$('#' + $(this).attr('rel')).dequeue().slideUp(settings['speed'], function () {
var closest = $(this).closest('.sticky-queue');
var elem = closest.find('.sticky');
$(this).remove();
if (elem.length == '1') {
closest.remove()
}
});
});
// Callback data
var response = {
'id': uniqID,
'duplicate': duplicate,
'displayed': display,
'position': settings.position,
'type': settings.type
}
// Callback function?
if (callback) {
callback(response);
} else {
return (response);
}
}
})(jQuery, window, document);