├── .gitattributes
├── .gitignore
├── CONTRIBUTING.md
├── Gruntfile.js
├── LICENSE
├── README.md
├── dist
└── Notifier.min.js
├── index.html
├── package.json
└── src
└── Notifier.js
/.gitattributes:
--------------------------------------------------------------------------------
1 | *.html linguist-vendored
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | node_modules
2 | package-lock.json
3 | yarn.lock
--------------------------------------------------------------------------------
/CONTRIBUTING.md:
--------------------------------------------------------------------------------
1 | # Contributing
2 |
3 | When contributing to this repository, please first discuss the change you wish to make via issue,
4 | email, or any other method with the owners of this repository before making a change.
5 |
6 | ## Pull Request Process
7 |
8 | 1. Ensure any install or build dependencies are removed before the end of the layer when doing a
9 | build.
10 | 2. Update the documentation with the details of changes to the usage and behavior.
11 | 3. Increase the version numbers in every file where the version number is present to the new version that this
12 | Pull Request would represent. The versioning scheme we use is [SemVer](http://semver.org/).
13 | 4. You may merge the Pull Request in once you have the sign-off of two other developers, or if you
14 | do not have permission to do that, you may request the second reviewer to merge it for you.
15 |
--------------------------------------------------------------------------------
/Gruntfile.js:
--------------------------------------------------------------------------------
1 | module.exports = function(grunt) {
2 | grunt.initConfig({
3 | babel: {
4 | options: {
5 | sourceMap: false,
6 | comments: false,
7 | sourceType: "unambiguous",
8 | presets: ['@babel/preset-env', 'minify']
9 | },
10 | dist: {
11 | files: {
12 | 'dist/Notifier.min.js': 'src/Notifier.js'
13 | }
14 | }
15 | }
16 | });
17 |
18 | grunt.loadNpmTasks('grunt-babel');
19 | grunt.registerTask('build', ['babel']);
20 | };
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2018 Javier Sanahuja
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Notifier.js
2 | Vanilla Javascript html notifications manager
3 |
4 | [Full documentation and examples here](https://www.sowecms.com/demos/Notifier.js)
--------------------------------------------------------------------------------
/dist/Notifier.min.js:
--------------------------------------------------------------------------------
1 | function _typeof(a){"@babel/helpers - typeof";return _typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(a){return typeof a}:function(a){return a&&"function"==typeof Symbol&&a.constructor===Symbol&&a!==Symbol.prototype?"symbol":typeof a},_typeof(a)}(function(a,b){"function"==typeof define&&define.amd?define([],b):"object"===("undefined"==typeof exports?"undefined":_typeof(exports))?module.exports=b():a.Notifier=b()})(this,function(){var a={autopush:!0,zindex:9999,default_time:4500,vanish_time:300,fps:30,position:"bottom-right",direction:"bottom",success:{classes:"notifyjs-success",textColor:"#155724",borderColor:"#c3e6cb",backgroundColor:"#d4edda",progressColor:"#155724",iconColor:"#155724",icon:""},error:{classes:"notifyjs-danger",textColor:"#721c24",borderColor:"#f5c6cb",backgroundColor:"#f8d7da",progressColor:"#721c24",iconColor:"#721c24",icon:""},warning:{classes:"notifyjs-warning",textColor:"#856404",borderColor:"#fff3cd",backgroundColor:"#ffeeba",progressColor:"#856404",iconColor:"#856404",icon:""},info:{classes:"notifyjs-info",textColor:"#0c5460",borderColor:"#d1ecf1",backgroundColor:"#bee5eb",progressColor:"#0c5460",iconColor:"#0c5460",icon:""}},b=function(a,b,c,d,e,f,g){switch(this.pushed=!1,this.element=document.createElement("div"),this.element.className=c.classes||"",this.element.style.display="none",this.element.style.position="relative",this.element.style.padding="1em 2em 1em 2.5em",a.options.direction){case"top":this.element.style.marginTop="0.5em";break;case"bottom":default:this.element.style.marginBottom="0.5em";}if(this.element.style.width="100%",this.element.style.borderWidth="1px",this.element.style.borderStyle="solid",this.element.style.borderColor=c.borderColor,this.element.style.boxSizing="border-box",this.element.style.backgroundColor=c.backgroundColor,"undefined"!=typeof c.icon){var h=document.createElement("div");h.style.position="absolute",h.style.top="50%",h.style.left="10px",h.style.transform="translateY(-50%)",h.innerHTML=c.icon,-1==c.icon.indexOf("=c&&("function"==typeof g&&g(),k.clear())},b)}},this.clear=function(){if(k.pushed){var a=1e3/f,b=1;"undefined"!=typeof k.interval&&clearInterval(k.interval),k.interval=setInterval(function(){b-=1/(e/a),k.element.style.opacity=b,0>=b&&(clearInterval(k.interval),k.destroy())},a)}},this.destroy=function(){k.pushed&&(k.pushed=!1,"undefined"!=typeof k.interval&&clearInterval(k.interval),a.container.removeChild(k.element))}};return function Notifier(c){if(this.options=Object.assign({},a),this.options=Object.assign(this.options,c),this.container=document.getElementById("notifyjs-container-"+this.options.position),null===this.container){switch(this.container=document.createElement("div"),this.container.id="notifyjs-container-"+this.options.position,this.container.style.zIndex=this.options.zindex,this.container.style.position="fixed",this.container.style.maxWidth="304px",this.container.style.width="100%",this.options.position){case"top-left":this.container.style.top=0,this.container.style.left="0.5em";break;case"top-right":this.container.style.top=0,this.container.style.right="0.5em";break;case"bottom-left":this.container.style.bottom=0,this.container.style.left="0.5em";break;case"bottom-right":default:this.container.style.bottom=0,this.container.style.right="0.5em";}document.getElementsByTagName("body")[0].appendChild(this.container)}this.notify=function(a,c,d,e){if("undefined"==typeof this.options[a])return void console.error("Notify.js: Error, undefined '"+a+"' notification type");"undefined"==typeof d&&(d=this.options.default_time);var f=new b(this,c,this.options[a],d,this.options.vanish_time,this.options.fps,e);return this.options.autopush&&f.push(),f}}});
2 |
--------------------------------------------------------------------------------
/index.html:
--------------------------------------------------------------------------------
1 |
3 |
4 |
5 |
Vanilla Javascript html notifications manager 132 |
133 | 139 |Method | 186 |Type | 187 |Description | 188 |
---|---|---|
193 | constructor(options)
195 | |
196 | 197 | | Instances the notifications manager with the given options. | 198 |
201 | notify(type, msg, time, callback)
202 | |
203 | Notification | 204 |
205 | Creates a new Notification object of the type type with the
206 | message msg . The notification will be shown (on push)
208 | for time miliseconds or
209 | default_time if not specified. The callback will
210 | be called right before the gracefully vanish (on clear).
212 | |
213 |
Attribute | 222 |Type | 223 |Default | 224 |Description | 225 |
---|---|---|---|
230 | autopush
231 | Optional
232 | |
233 | Boolean | 234 |true |
235 | Whether the notifier has to automatically push notifications (on creation) or not. | 236 |
239 | position
240 | Optional
241 | |
242 | string | 243 |bottom-right |
244 | Screen position of the notification. Allows top-left , top-right , bottom-left and bottom-right . |
245 |
248 | direction
249 | Optional
250 | |
251 | string | 252 |bottom |
253 | The direction the notifications will be stacked:
254 |
|
257 |
260 | zindex
261 | Optional
262 | |
263 | int | 264 |9999 |
265 | The CSS z-index the notifications container should have | 266 |
269 | default_time
270 | Optional
271 | |
272 | number [Int] | 273 |4500 |
274 | Number of miliseconds the notification will be visible if not specified. | 275 |
278 | vanish_time
279 | Optional
280 | |
281 | number [Int] | 282 |300 |
283 | Miliseconds the notification vanish animation will take. | 284 |
287 | fps
288 | Optional
289 | |
290 | number [Int] | 291 |30 |
292 | Frames per second of progress and vanish animations. | 293 |
296 | <type>
297 | Optional
298 | |
299 | Notification Type | 300 |301 | success, 302 | error, 303 | warning, 304 | info 305 | | 306 |307 | You can define your own notification types or override existing ones. See Notification Type or the Example 4. 308 | | 309 |
Method | 318 |Type | 319 |Description | 320 ||
---|---|---|---|
325 | push()
326 | |
327 | void | 328 |Displays the notification. | 329 ||
332 | clear()
333 | |
334 | void | 335 |Vanishes the notification gracefully. | 336 ||
339 | destroy()
340 | |
341 | void | 342 |Destroys the notification. Note it will not trigger the
343 | callback . |
344 |
Attribute | 353 |Type | 354 |Description | 355 ||
---|---|---|---|
360 | classes
361 | |
362 | string | 363 |CSS classes that will be added to the notification. | 364 ||
367 | icon
368 | |
369 | string | 370 |
371 | HTML containing the icon. Something like: 372 |
|
376 | |
379 | textColor
380 | |
381 | string | 382 |Text color of the notification. | 383 ||
386 | borderColor
387 | |
388 | string | 389 |Border color of the notification. | 390 ||
393 | backgroundColor
394 | |
395 | string | 396 |Background color of the notification. | 397 ||
400 | progressColor
401 | |
402 | string | 403 |Progress bar color. | 404 ||
407 | iconColor
408 | |
409 | string | 410 |Icon color. This only applies for inline <svg> tags in icon . |
411 | |
414 | iconClasses
415 | |
416 | string | 417 |CSS classes that will be added to the icon. | 418 |
push
your notification using the type
according to
457 | the message.
<script src="dist/Notifier.min.js"></script>
467 | <script>
468 | (function () {
469 | var notifier = new Notifier();
470 |
471 | // Success
472 | document.getElementById("ex1notify1").onclick = function () {
473 | notifier.notify("success", "Successfully logged in!");
474 | };
475 |
476 | // Info
477 | document.getElementById("ex1notify2").onclick = function () {
478 | notifier.notify("info", "New message from <a href=\"https://github.com/jsanahuja/\" target=\"_new\">jsanahuja</a> received!");
479 | };
480 |
481 | // Warning
482 | document.getElementById("ex1notify3").onclick = function () {
483 | notifier.notify("warning", "Flood is not permitted, your chat will be temporary disabled");
484 | };
485 |
486 | // Error
487 | document.getElementById("ex1notify4").onclick = function () {
488 | notification = notifier.notify("error", "Invalid username or password");
489 | };
490 |
491 | })();
492 | </script>
493 |
494 | Set the autopush
to false and push
your notification and clear
or
500 | destroy
them prematurely.
<script src="dist/Notifier.min.js"></script>
509 | <script>
510 | (function(){
511 | var notifier = new Notifier({
512 | autopush: false
513 | });
514 |
515 | var notifications = [];
516 |
517 | // Push 10 notifications
518 | document.getElementById("ex2notify1").onclick = function () {
519 | for(var i = 0; i < 10; i++){
520 | var notification = notifier.notify("info", "Notification "+ i);
521 | notification.push();
522 | notifications.push(notification);
523 | }
524 | };
525 |
526 | // Clear all notifications
527 | document.getElementById("ex2notify2").onclick = function () {
528 | for(var i = 0; i < notifications.length; i++){
529 | notifications[i].clear();
530 | }
531 | notifications = [];
532 | };
533 |
534 | // Destroy all notifications
535 | document.getElementById("ex2notify3").onclick = function () {
536 | for(var i = 0; i < notifications.length; i++){
537 | notifications[i].destroy();
538 | }
539 | notifications = [];
540 | };
541 | })();
542 | </script>
543 |
544 | Set a callback
to trigger actions after the notification.
<script src="dist/Notifier.min.js"></script>
557 | <script>
558 | (function(){
559 | var notifier = new Notifier();
560 |
561 | var notification = null,
562 | counter = 0;
563 |
564 | // Start loop
565 | document.getElementById("ex3notify1").onclick = function () {
566 | if (notification !== null) {
567 | return;
568 | }
569 | var callback = function () {
570 | counter++;
571 | notification = notifier.notify("info", "This is the notification #" + counter, 3500, callback);
572 | }
573 | callback();
574 | };
575 |
576 | // Destroy notification. Clearing it would call the callback!
577 | document.getElementById("ex3notify2").onclick = function () {
578 | if (notification === null) {
579 | return;
580 | }
581 | notification.destroy();
582 | notification = null;
583 | };
584 | })();
585 | </script>
586 |
587 | Use the <type>
option to define your own styles
All the icons, including the ones of this example, used by Notifier.js were taken 616 | from open-iconic
617 | 618 |<script src="dist/Notifier.min.js"></script>
621 | <script>
622 | (function(){
623 | var notifier = new Notifier({
624 | watch: {
625 | textColor: "#24292e",
626 | borderColor: "#c4c8cc",
627 | backgroundColor: "#fafbfc",
628 | progressColor: "#24292e",
629 | iconColor: "#24292e",
630 | icon: "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"8\" height=\"8\" viewBox=\"0 0 8 8\"><path d=\"M4.03 0c-2.53 0-4.03 3-4.03 3s1.5 3 4.03 3c2.47 0 3.97-3 3.97-3s-1.5-3-3.97-3zm-.03 1c1.11 0 2 .9 2 2 0 1.11-.89 2-2 2-1.1 0-2-.89-2-2 0-1.1.9-2 2-2zm0 1c-.55 0-1 .45-1 1s.45 1 1 1 1-.45 1-1c0-.1-.04-.19-.06-.28-.08.16-.24.28-.44.28-.28 0-.5-.22-.5-.5 0-.2.12-.36.28-.44-.09-.03-.18-.06-.28-.06z\" transform=\"translate(0 1)\" /></svg>"
631 | },
632 | star: {
633 | textColor: "#24292e",
634 | borderColor: "#c4c8cc",
635 | backgroundColor: "#fafbfc",
636 | progressColor: "#24292e",
637 | iconColor: "#24292e",
638 | icon: "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"8\" height=\"8\" viewBox=\"0 0 8 8\"><path d=\"M4 0l-1 3h-3l2.5 2-1 3 2.5-2 2.5 2-1-3 2.5-2h-3l-1-3z\" /></svg>"
639 | },
640 | fork: {
641 | textColor: "#24292e",
642 | borderColor: "#c4c8cc",
643 | backgroundColor: "#fafbfc",
644 | progressColor: "#24292e",
645 | iconColor: "#24292e",
646 | icon: "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"8\" height=\"8\" viewBox=\"0 0 8 8\"><path d=\"M1.5 0c-.83 0-1.5.67-1.5 1.5 0 .66.41 1.2 1 1.41v2.19c-.59.2-1 .75-1 1.41 0 .83.67 1.5 1.5 1.5s1.5-.67 1.5-1.5c0-.6-.34-1.1-.84-1.34.09-.09.21-.16.34-.16h2c.82 0 1.5-.68 1.5-1.5v-.59c.59-.2 1-.75 1-1.41 0-.83-.67-1.5-1.5-1.5s-1.5.67-1.5 1.5c0 .66.41 1.2 1 1.41v.59c0 .28-.22.5-.5.5h-2c-.17 0-.35.04-.5.09v-1.19c.59-.2 1-.75 1-1.41 0-.83-.67-1.5-1.5-1.5z\" /></svg>"
647 | }
648 | });
649 |
650 |
651 | document.getElementById("ex4notify1").onclick = function () {
652 | notifier.notify("watch", "Watched!");
653 | };
654 | document.getElementById("ex4notify2").onclick = function () {
655 | notifier.notify("star", "Starred!");
656 | };
657 | document.getElementById("ex4notify3").onclick = function () {
658 | notifier.notify("fork", "Forked!");
659 | };
660 | })();
661 | </script>
662 |
663 | Use the position
and direction
options to change it.
<script src="dist/Notifier.min.js"></script>
678 | <script>
679 | (function(){
680 | var notifierTR = new Notifier({
681 | position: 'top-right',
682 | direction: 'top'
683 | });
684 | var notifierTL = new Notifier({
685 | position: 'top-left',
686 | direction: 'top'
687 | });
688 | var notifierBL = new Notifier({
689 | position: 'bottom-left',
690 | direction: 'bottom'
691 | });
692 | var notifierBR = new Notifier({
693 | position: 'bottom-right',
694 | direction: 'bottom'
695 | });
696 |
697 | document.getElementById("ex5notify1").onclick = function () {
698 | notifierTL.notify("info", "I am on TOP LEFT!");
699 | };
700 | document.getElementById("ex5notify2").onclick = function () {
701 | notifierTR.notify("info", "I am on TOP RIGHT!");
702 | };
703 | document.getElementById("ex5notify3").onclick = function () {
704 | notifier.notify("info", "I am on BOTTOM LEFT!");
705 | };
706 | document.getElementById("ex5notify4").onclick = function () {
707 | notifier.notify("info", "I am on BOTTOM RIGHT!");
708 | };
709 | })();
710 | </script>
711 |
712 |