forked from ThrivingKings/Sticky
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sticky.js
176 lines (141 loc) · 5.32 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
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
/*
Sticky2 - by makuchaku ([email protected])
Sticky v1.0 by Daniel Raftery
http://thrivingkings.com/sticky
http://twitter.com/ThrivingKings
*/
(function ($) {
// Using it without an object
$.sticky = function (note, options, openCallback, closeCallback) {
return $.fn.sticky(note, options, openCallback, closeCallback);
};
/*
if noteData is an object, we need to construct the note from noteTemplate - else, just use it as it is.
noteData => {
image : "http://foo/path.png",
text : "Note's text",
title : "Note's title",
link : "http://google.com"
}
options => {
speed : "fast", // or any other jquery speed definition
duplicates : true, // boolean
autoclose : 5000, // milisec after the sticky autocloses, or false
position : "top-right" // top-left, top-right, bottom-left or bottom-right
}
openCallback => function, called when sticky is shown. Args => {'id': uniqID, 'duplicate': duplicate, 'displayed': display, 'position': position},
closeCallback => function, called when sticky has been closed (same args as openCallback)
*/
$.fn.sticky = function (noteData, options, openCallback, closeCallback) {
// Default settings
var settings = {
'speed': 'fast', // animations: fast, slow, or integer
'duplicates': true, // true or false
'autoclose': 5000, // integer or false
'position' : "top-right" // top-left, top-right, bottom-left, or bottom-right
};
options = $.extend(settings, options); // inject the defaults into options
var position = options.position;
var closeImageData = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAA4AAAAOCAYAAAAfSC3RAAAA1klEQVQoz6WSOw6CQBCG90gWXsjKxph4HZAEsgUSHlsAAa6ilzDGgopxP5Ix2K7FJH/+x+wMjBERoxXH8d5aey2K4l6W5ZMCw6FtvV+Qpumlrut313UyDIOM47gWGA4Nz08QomkaadtW+r5fA9M0rQWGQ8OjYRNF0c53mxH8aLc8z8/OuYWXKDAcGh68ZAzzMwpdveFEtyzLDt6AScBwaHjwkjF++cem+6zGJEmOlDZCUx8ZU1XVS3eC9K8sGtAGcGi6M5nwYPCowR8n+HcEH8BfJxdy5B8L5i9vzgm5WAAAAABJRU5ErkJggg==";
// Note template
var noteTemplate = '\
<div class="sticky2-message-container">\
<div class="sticky2-message-image-container">\
<img src="__IMAGE__"/>\
</div>\
<div class="sticky2-message-text-container">\
<div class="sticky2-message-title">__TITLE__</div>\
<div class="sticky2-message-text">__TEXT__</div>\
</div>\
</div>\
';
// if noteData is an object, we need to construct the note from noteTemplate
// else, just use it as it is.
if(typeof(noteData) == typeof(""))
note = noteData;
else
{
// construct the note from note template & input data
var note = noteTemplate
.replace("__IMAGE__", noteData.image)
.replace("__TITLE__", noteData.title)
.replace("__TEXT__", noteData.text);
}
// Passing in the object instead of specifying a note
if (!note)
note = this.html();
if (options)
$.extend(settings, options);
// Variables
var display = true;
var 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').html())
$('body').append('<div class="sticky-queue ' + position + '"></div>');
// Can it be displayed?
if (display)
{
// Building and inserting sticky note
$('.sticky-queue').prepend('<div class="sticky border-' + position + '" id="' + uniqID + '"></div>');
$('#' + uniqID)
.append('<img src="' + closeImageData + '" class="sticky-close" rel="' + uniqID + '" title="Close" />')
.append('<div class="sticky-note" rel="' + uniqID + '">' + note + '</div>');
if(noteData && typeof(noteData.link) == typeof(""))
{
$("#" + uniqID + " .sticky-note")
.css({cursor : "pointer"})
.click(function() {
window.open(noteData.link);
});
}
// 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() {
$(this).remove(); // remove the note from dom
});
});
// Callback data
var response = {
'id': uniqID,
'duplicate': duplicate,
'displayed': display,
'position': position
}
// Closing a sticky
$('.sticky-close').click(function () {
$('#' + $(this).attr('rel')).dequeue().slideUp(settings['speed'], function() {
$(this).remove(); // remove the note from dom
if(closeCallback)
closeCallback(response);
});
});
// Callback function?
if (openCallback)
openCallback(response);
else
return (response);
} // $.fn.sticky
})(jQuery);