Skip to content

Commit 9c54b43

Browse files
jQuery Toaster file sync
1 parent d810dee commit 9c54b43

File tree

3 files changed

+116
-0
lines changed

3 files changed

+116
-0
lines changed

index.html

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<html>
2+
<head>
3+
<title>jQUery Toaster</title>
4+
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
5+
<script src="toaster.js"></script>
6+
</head>
7+
8+
<body>
9+
<input type="text" id="text-content" placeholder="text">
10+
<input type="text" id="color" placeholder="color">
11+
<input type="text" id="background" placeholder="background">
12+
<button onclick="toast()">Toast it</button>
13+
<div id="id"></div>
14+
<script>
15+
var toaster = new Toaster({
16+
'text': 'Hi',
17+
'color': '#FFF',
18+
'background': '#333',
19+
'vertical-position': 'bottom',
20+
'horizontal-position': 'right',
21+
'container' : '#id'
22+
});
23+
function toast() {
24+
var text = $("#text-content").val();
25+
var color = $("#color").val();
26+
var background = $("#background").val();
27+
toaster.data.text = text;
28+
toaster.data.color = color;
29+
toaster.data.background = background;
30+
toaster.publish();
31+
}
32+
</script>
33+
</body>
34+
</html>

toaster.js

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/**
2+
* Global variables
3+
*/
4+
var globalVariales = {
5+
defaultContainer: "#toasterContainer"
6+
};
7+
8+
/**
9+
* Toaster class
10+
* @param {*} data
11+
*/
12+
function Toaster(data) {
13+
this.data = data,
14+
this.color = "#FFF",
15+
this.background = "#333",
16+
this.verticalPosition = "bottom",
17+
this.horizontalPosition = "right",
18+
this.container = globalVariales.defaultContainer,
19+
this.width = "290px",
20+
this.height = "40px",
21+
this.padding = "5px",
22+
this.corderRadius = "5px",
23+
this.text = "",
24+
this.publish = publish
25+
}
26+
27+
// Instantiating Toaster class
28+
var toasterInstance = new Toaster();
29+
30+
/**
31+
* Publishing the toaster
32+
*/
33+
function publish() {
34+
// Setting toaster properties
35+
setData(this);
36+
37+
// Appending default container if not specified
38+
if (toasterInstance.container == globalVariales.defaultContainer) {
39+
$("body").append('<div id="toasterContainer"></div>');
40+
}
41+
$(toasterInstance.container).show();
42+
$(toasterInstance.container).css("margin-bottom", "0px");
43+
$(toasterInstance.container).text(toasterInstance.text).animate({ 'marginBottom': '50px' }, 800).fadeOut(5000).css({
44+
'position': 'absolute',
45+
'bottom': '30px',
46+
'right': '30px',
47+
'width': toasterInstance.width,
48+
'height': toasterInstance.height,
49+
'background': toasterInstance.background,
50+
'color': toasterInstance.color,
51+
'padding': toasterInstance.padding,
52+
'border-radius': toasterInstance.corderRadius
53+
});
54+
}
55+
56+
/**
57+
* Setting Toaster class properties
58+
* @param {*} toaster
59+
*/
60+
function setData(toaster) {
61+
// Checking if toaster exists
62+
if (nullValidator(toaster)) {
63+
// Checking if data in toaster exists
64+
if (nullValidator(toaster.data)) {
65+
toasterInstance.color = nullValidator(toaster.data.color) ? toaster.data.color : toaster.color;
66+
toasterInstance.background = nullValidator(toaster.data.background) ? toaster.data.background : toaster.background;
67+
toasterInstance.verticalPosition = nullValidator(toaster.data.verticalPosition) ? toaster.data.verticalPosition : toaster.verticalPosition;
68+
toasterInstance.horizontalPosition = nullValidator(toaster.data.horizontalPosition) ? toaster.data.horizontalPosition : toaster.horizontalPosition;
69+
toasterInstance.container = nullValidator(toaster.data.container) ? toaster.data.container : toaster.container;
70+
toasterInstance.text = nullValidator(toaster.data.text) ? toaster.data.text : toaster.text;
71+
}
72+
}
73+
}
74+
75+
/**
76+
* Validating null and undefined entries
77+
* @param {*} entity
78+
*/
79+
function nullValidator(entity) {
80+
return (entity != null || entity != undefined) && entity != "" ? true : false;
81+
}

toaster.min.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)