/
blurb.js
233 lines (203 loc) · 7.44 KB
/
blurb.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
function loadjscssfile(filename, filetype) {
if (filetype == "js") { //if filename is a external JavaScript file
var fileref = document.createElement('script')
fileref.setAttribute("type", "text/javascript")
fileref.setAttribute("src", filename)
}
}
// Load google font
loadjscssfile("https://fonts.googleapis.com/css?family=Roboto+Condensed:300,400", "css");
// Slides an element out from hidden position
function slideOut(el, animateFrom) {
el.style.opacity = 1;
var posOut = 20;
var animationSpeed = 0;
var id = setInterval(animateIn, animationSpeed);
function animateIn() {
if (posOut == "-80") {
clearInterval(id);
} else {
posOut--;
if(animateFrom == "top") {
el.style.top = posOut + "px";
} else if (animateFrom == "bottom") {
el.style.bottom = posOut + "px";
}
}
}
}
// Slides an element in from hidden position
function slideIn(el, animateFrom) {
el.style.display = "block";
var pos = -80;
var animationSpeed = 5;
var id = setInterval(animateIn, animationSpeed);
function animateIn() {
if (pos == "20") {
clearInterval(id);
} else {
pos++;
if(animateFrom == "top") {
el.style.top = pos + "px";
} else if (animateFrom == "bottom") {
el.style.bottom = pos + "px";
}
}
}
}
function Blurb(config) {
// Store reference to this blurb object
var blurb = this;
// Get the body
var body = document.body;
// Create empty config object if not set
if(config == null) { config = {}; }
// Create the blurb box
var box = document.createElement("div");
box.className = "blurb";
box.style.width = config.width ? config.width : "240px";
box.style.height = config.height ? config.height : "70px";
box.style.position = config.position ? config.position : "fixed";
// Set position of Blurb based on corner selection
position = config.corner ? config.corner : "bottomright";
switch (position) {
case "topleft":
box.style.top = "-500px";
box.style.top = "-" + box.style.height.replace("px", "");
box.style.left = "20px";
config.animateFrom = "top";
break;
case "topright":
box.style.top = "-500px";
box.style.top = "-" + box.style.height.replace("px", "");
box.style.right = "20px";
config.animateFrom = "top";
break;
case "bottomleft":
box.style.bottom = "-500px";
box.style.bottom = "-" + box.style.height.replace("px", "");
box.style.left = "20px";
config.animateFrom = "bottom";
break;
case "bottomright":
box.style.bottom = "-500px";
box.style.bottom = "-" + box.style.height.replace("px", "");
box.style.right = "20px";
config.animateFrom = "bottom";
break;
}
// Create blurb box styles
box.style.color = config.color ? config.color : "#FFF";
box.style.backgroundColor = config.bgcolor ? config.bgcolor : "rgba(0, 0, 0, 0.8)";
box.style.borderRadius = config.borderRadius ? config.borderRadius : "3px 3px 3px 3px";
box.style.boxShadow = config.shadow ? config.shadow : "0 2px 10px rgba(0, 0, 0, 0.8)";
box.style.display = "none";
body.appendChild(box);
// Create icon img
var icon = document.createElement('div');
icon.style.position = "relative";
icon.style.float = "left";
icon.style.width = "40px";
icon.style.height = "40px";
icon.style.margin = "0 10px";
icon.style.bottom = "0px";
icon.style.top = "50%";
icon.style.transform = "translate(0, -50%)";
icon.style.borderRadius = "50%";
icon.style.backgroundImage = config.iconImage ? "url("+ config.iconImage+")" : "";
icon.style.backgroundSize = "cover";
box.appendChild(icon);
// Create the message box
var blurbtext = document.createElement("div");
blurbtext.style.width = box.style.width.replace("px", "") - 60 + "px";
blurbtext.style.float = "right";
blurbtext.style.height = "100%";
blurbtext.style.fontFamily = config.fontFamily ? config.fontFamily : "'Roboto Condensed', sans-serif";
blurbtext.style.fontWeight = "300";
blurbtext.style.overflow = "hidden";
blurbtext.style.display = "flex";
blurbtext.style.alignItems = "center";
blurbtext.textContent = config.message;
// Create optional message link
var link = document.createElement('a');
if (config.link != null) {
link.exists = true;
link.href = config.link ? config.link : "";
link.target = "_blank";
link.style.textDecoration = "none";
link.style.color = config.color ? config.color : "#FFF";
link.appendChild(blurbtext);
box.appendChild(link);
} else {
box.appendChild(blurbtext);
}
// Get mouseOver state to keep visible when mouse is on the blurb
var mouseOver = false;
box.onmouseover = function() { mouseOver = true; }
box.onmouseout = function() { mouseOver = false; }
// Set message
this.setMessage = function(message) {
blurbtext.textContent = message;
};
// Set link
this.setLink = function(path) {
console.log(path);
console.log(blurb);
if(link.exists) {
link.href = path;
} else {
link.exists = true;
link.href = path;
link.target = "_blank";
link.style.textDecoration = "none";
link.style.color = "#FFF";
link.appendChild(blurbtext);
box.appendChild(link);
}
}
// Set position
this.setPosition = function(position) {
switch (position) {
case "topleft":
box.style.top = "-500px";
box.style.top = "-" + box.style.height.replace("px", "");
box.style.left = "20px";
config.animateFrom = "top";
break;
case "topright":
box.style.top = "-500px";
box.style.top = "-" + box.style.height.replace("px", "");
box.style.right = "20px";
config.animateFrom = "top";
break;
case "bottomleft":
box.style.bottom = "-500px";
box.style.bottom = "-" + box.style.height.replace("px", "");
box.style.left = "20px";
config.animateFrom = "bottom";
break;
case "bottomright":
box.style.bottom = "-500px";
box.style.bottom = "-" + box.style.height.replace("px", "");
box.style.right = "20px";
config.animateFrom = "bottom";
break;
}
};
// Show the Blurb
this.show = function() {
slideIn(box, config.animateFrom);
var wait = config.wait ? config.wait : "3";
setTimeout(blurb.hide, wait * 1000);
};
// Hide the Blurb
this.hide = function() {
// If mouse is ! over the blurb then fadeout
if (mouseOver) {
var wait = config.wait ? config.wait : "3";
setTimeout(blurb.hide, wait * 1000);
} else {
slideOut(box, config.animateFrom);
}
};
}