|
| 1 | + |
| 2 | +// flash.js |
| 3 | +// Customable jQuery library for web applications flash messages |
| 4 | +// Author: Theodoros Georgopoulos |
| 5 | +// Date: March 20, 2018 |
| 6 | +// Version: 1.0.0 |
| 7 | +// Copyright: MIT |
| 8 | + |
| 9 | + |
| 10 | +// define the message container div |
| 11 | +var $message_container = $("<div></div>"); |
| 12 | +$('body').append($message_container); |
| 13 | + |
| 14 | +// define the default options |
| 15 | +var $options = { |
| 16 | + 'bgColor' : '#5cb85c', |
| 17 | + 'duration' : 4000, |
| 18 | + 'ftColor' : 'white', |
| 19 | + 'vPosition' : 'bottom', |
| 20 | + 'hPosition' : 'right', |
| 21 | + 'fadeIn' : 400, |
| 22 | + 'fadeOut' : 400, |
| 23 | + 'clickable' : true, |
| 24 | + 'autohide' : true |
| 25 | +}; |
| 26 | + |
| 27 | + |
| 28 | +function flash(message, options = null){ |
| 29 | + var type = typeof options; |
| 30 | + if (options !== null && type === 'object') { |
| 31 | + $.extend($options, options) |
| 32 | + } |
| 33 | +//Message container div css |
| 34 | + msg_container_css = { |
| 35 | + "position": "fixed", |
| 36 | + "margin-left" : '7px', |
| 37 | + "z-index" : '50', |
| 38 | + }; |
| 39 | + msg_container_css[$options.vPosition] = "3px"; |
| 40 | + msg_container_css[$options.hPosition] = "5px"; |
| 41 | + $message_container.css(msg_container_css); |
| 42 | + |
| 43 | + |
| 44 | +// define the message div |
| 45 | +var $message = $("<div></div>"); |
| 46 | + |
| 47 | +//Message div css |
| 48 | + msg_css = { |
| 49 | + 'text-align' : 'right', |
| 50 | + 'margin-top' : '10px', |
| 51 | + 'padding' : '15px', |
| 52 | + 'border' : '1px solid #dcdcdc', |
| 53 | + 'border-radius': '5px', |
| 54 | + 'float': 'right', |
| 55 | + 'clear': 'right', |
| 56 | + 'background-color': $options.bgColor, |
| 57 | + 'color' : $options.ftColor, |
| 58 | + 'font-family': "Arial, Helvetica, sans-serif", |
| 59 | + }; |
| 60 | + $message.css(msg_css); |
| 61 | +//Adding text to message |
| 62 | + $message.text(message); |
| 63 | + |
| 64 | +//Appeding message div to message container div |
| 65 | + $message_container.append($message).children(':last').hide().fadeIn($options.fadeIn); |
| 66 | +//Check if message is clickable to enable message click hide action |
| 67 | + if ($options.clickable) { |
| 68 | + $message.on('click', function(){ |
| 69 | + $(this).fadeOut($options.fadeOut); |
| 70 | + }); |
| 71 | + } |
| 72 | + |
| 73 | +//Check if message is enabled to autohide |
| 74 | + if ($options.autohide) { |
| 75 | + setTimeout(function(){ |
| 76 | + $message.fadeOut($options.fadeOut); |
| 77 | + },$options.duration); |
| 78 | + } |
| 79 | + |
| 80 | + |
| 81 | +}; |
0 commit comments