Skip to content

Commit dcda47f

Browse files
committed
Readability with comments
Adding comments for better readability Add callback support on toast click
1 parent ac3f68c commit dcda47f

File tree

5 files changed

+104
-29
lines changed

5 files changed

+104
-29
lines changed

README.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,13 @@ Add script at the bottom of the page
2525

2626
## Documentation
2727

28-
*(coming soon)*
28+
```javascript
29+
Toastify({
30+
text: "This is a toast",
31+
duration: 3000,
32+
33+
}).showToast();
34+
```
2935

3036
## License
3137

index.html

+8-3
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,20 @@ <h1>Toastify</h1>
2424
<h2>Usage</h2>
2525
<code>
2626
<p>Toastify({</p>
27-
<p class="pad-left">text: "Happy toasting!",</p>
28-
<p class="pad-left">duration: 500</p>
27+
<p class="pad-left">text: "This is a toast",</p>
28+
<p class="pad-left">duration: 3000</p>
2929
<p>}).showToast();</p>
3030
</code>
3131
</div>
32+
<div class="repo">
33+
<a class="github-button" href="https://github.com/apvarun/toastify-js" data-icon="octicon-star" data-size="large" aria-label="Star apvarun/toastify-js on GitHub">Star</a>
34+
</div>
3235
</div>
3336
</body>
3437

3538
<script type="text/javascript" src="src/toastify.js"></script>
3639
<script type="text/javascript" src="script.js"></script>
40+
<!-- GitHub Star button -->
41+
<script async defer src="https://buttons.github.io/buttons.js"></script>
3742

38-
</html>
43+
</html>

script.css

+3
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,6 @@ code p{
5454
color: #ffffff;
5555
background-color: #424242;
5656
}
57+
.repo{
58+
margin:10px;
59+
}

script.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
1+
// Options for the toast
12
var options = {
23
text: "Happy toasting!",
3-
duration: 500
4+
duration: 1500,
5+
callback: function(){
6+
this.remove();
7+
Toastify.reposition();
8+
}
49
};
510

11+
// Initializing the toast
612
var myToast = Toastify(options);
713

14+
// Toast after delay
815
setTimeout(function () {
916
myToast.showToast();
1017
}, 2000);
@@ -14,9 +21,10 @@ Toastify({
1421
duration: 5000
1522
}).showToast();
1623

24+
// Displaying toast on manual action `Try`
1725
document.getElementById('new-toast').addEventListener('click', function () {
1826
Toastify({
1927
text: "This is a toast",
20-
duration: 5000
28+
duration: 3000
2129
}).showToast();
2230
});

src/toastify.js

+76-23
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,144 @@
1-
; (function (global, root) {
1+
; (function (global) {
22

3+
// Object initialization
34
var Toastify = function (options) {
45

6+
// Returning a new init object
57
return new Toastify.lib.init(options);
68
},
9+
// Library version
710
version = "0.0.1";
811

12+
// Defining the prototype of the object
913
Toastify.lib = Toastify.prototype = {
1014

1115
toastify: version,
1216

1317
constructor: Toastify,
1418

19+
// Initializing the object with required parameters
1520
init: function (options) {
1621

22+
// Verifying and validating the input object
1723
if (!options) {
1824
options = {};
1925
}
2026

27+
// Creating the options object
2128
this.options = {};
2229

30+
// Validating the options
2331
this.options.text = options.text || 'Hi there!';
2432
this.options.duration = options.duration || 3000;
25-
this.options.root = options.root || root;
26-
27-
this.buildToast();
33+
this.options.selector = options.selector;
34+
this.options.callback = options.callback || function () { };
2835

36+
// Returning the current object for chaining functions
2937
return this;
3038
},
3139

40+
// Building the DOM element
3241
buildToast: function () {
3342

43+
// Validating if the options are defined
3444
if (!this.options) {
3545
throw "Toastify is not initialized";
3646
}
3747

48+
// Creating the DOM object
3849
var divElement = document.createElement("div");
3950
divElement.className = 'toastify on';
51+
52+
// Adding the toast message
4053
divElement.innerHTML = this.options.text;
4154

42-
this.toast = divElement;
55+
// Callback for on-click
56+
divElement.addEventListener('click', this.options.callback);
57+
58+
// Returning the generated element
59+
return divElement;
4360
},
4461

62+
// Displaying the toast
4563
showToast: function () {
4664

47-
var toastElement = this.toast;
65+
// Creating the DOM object for the toast
66+
var toastElement = this.buildToast();
4867

49-
var rootElement = document.getElementById(this.options.root);
68+
// Getting the root element to with the toast needs to be added
69+
var rootElement;
70+
if (typeof this.options.selector == "undefined") {
71+
rootElement = document.body;
72+
} else {
73+
rootElement = document.getElementById(this.options.selector);
74+
}
5075

5176
if (!rootElement) {
5277
throw "Root element is not defined";
5378
}
5479

80+
// Adding the DOM element
5581
rootElement.insertBefore(toastElement, rootElement.firstChild);
5682

57-
this.reposition();
83+
// Repositioning the toasts in case multiple toasts are present
84+
Toastify.reposition();
5885

5986
window.setTimeout(function () {
87+
88+
// Hiding the element
6089
toastElement.classList.remove("on");
90+
91+
// Removing the element from DOM after transition end
6192
window.setTimeout(function () {
93+
94+
// Remove the elemenf from the DOM
6295
toastElement.remove();
63-
this.reposition();
64-
}.bind(this), 400);
65-
}.bind(this), this.options.duration);
6696

67-
},
97+
// Repositioning the toasts again
98+
Toastify.reposition();
99+
100+
}.bind(this), 400); // Binding `this` for function invocation
101+
102+
}.bind(this), this.options.duration); // Binding `this` for function invocation
68103

69-
reposition: function () {
104+
// Supporting function chaining
105+
return this;
70106

71-
var topOffset = 15;
72-
73-
var allToasts = document.getElementsByClassName('toastify');
107+
}
74108

75-
for (var i = 0; i < allToasts.length; i++) {
109+
}
76110

77-
var height = allToasts[i].offsetHeight;
78-
var offset = 15;
79-
allToasts[i].style.top = topOffset + 'px';
111+
// Positioning the toasts on the DOM
112+
Toastify.reposition = function () {
80113

81-
topOffset += height + offset;
82-
}
114+
// Top margin
115+
var topOffsetSize = 15;
116+
117+
// Get all toast messages on the DOM
118+
var allToasts = document.getElementsByClassName('toastify');
83119

120+
// Modifying the position of each toast element
121+
for (var i = 0; i < allToasts.length; i++) {
122+
123+
var height = allToasts[i].offsetHeight;
124+
125+
// Spacing between toasts
126+
var offset = 15;
127+
128+
// Setting the position
129+
allToasts[i].style.top = topOffsetSize + 'px';
130+
131+
topOffsetSize += height + offset;
84132
}
133+
134+
// Supporting function chaining
135+
return this;
85136
}
86137

138+
// Setting up the prototype for the init object
87139
Toastify.lib.init.prototype = Toastify.lib;
88140

141+
// Adding the Toastify function to the window object
89142
global.Toastify = Toastify;
90143

91-
}(window, 'root'));
144+
}(window));

0 commit comments

Comments
 (0)