|
| 1 | +<script type="text/javascript"> |
| 2 | + notifications = []; |
| 3 | + notification_active = false; |
| 4 | + |
| 5 | + // push a notification to the client-side messages stack |
| 6 | + pushNotification = function(notification) { |
| 7 | + if(typeof notification != 'undefined') { |
| 8 | + notifications.push(notification); |
| 9 | + } |
| 10 | + if(notification_active == false) { |
| 11 | + openNotification(notifications.pop()); |
| 12 | + } |
| 13 | + } |
| 14 | + |
| 15 | + // open a notification |
| 16 | + openNotification = function(notification) { |
| 17 | + if(typeof notification != 'undefined') { |
| 18 | + notification_active == true; |
| 19 | + var message = notification[0]; |
| 20 | + var type = notification[1]; |
| 21 | + var closetext = notification[2]; |
| 22 | + var buttontext = notification[3]; |
| 23 | + var buttonfunction = notification[4]; |
| 24 | + if(typeof type == 'undefined' || type == '') { type = 'success'; } |
| 25 | + if(typeof closetext == 'undefined' || closetext == '') { closetext = 'X'; } |
| 26 | + // buttons |
| 27 | + output = '<div class="notify-buttons" style="position: absolute; bottom: 15px; right: 15px;">'; |
| 28 | + if(typeof buttontext != 'undefined') { |
| 29 | + output += '<button onclick="'+buttonfunction+';" class="btn btn-sm btn-primary">'+buttontext+'</button><div style="width: 12px; display: inline-block;"></div>'; |
| 30 | + } |
| 31 | + output += '<button onclick="closeNotification();" class="btn btn-sm btn-danger">'+closetext+'</button></div>'; |
| 32 | + // content |
| 33 | + // <p style="font-weight: bold;">Alert title</p> |
| 34 | + output += '<p>'+message+'</p>'; |
| 35 | + $('#notify').attr('class','alert alert-'+type); |
| 36 | + $('#notify > div').html(output); |
| 37 | + // close after 30 seconds |
| 38 | + notification_active = setTimeout( function() { |
| 39 | + closeNotification(); |
| 40 | + }, 30000); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + // close a notification |
| 45 | + closeNotification = function() { |
| 46 | + clearTimeout(notification_active); |
| 47 | + // close current message |
| 48 | + var notifyclass = $('#notify').attr('class'); |
| 49 | + $('#notify').attr('class',notifyclass+' notify-hidden'); |
| 50 | + setTimeout( function() { |
| 51 | + // any new messages on the stack? open the next one |
| 52 | + notification_active == false; |
| 53 | + openNotification(notifications.pop()); |
| 54 | + }, 1000); |
| 55 | + } |
| 56 | + |
| 57 | + // get all PHP generated info boxes, and convert them to push Notifications |
| 58 | + convertNotifications = function() { |
| 59 | + var infobox = 'div.tile.error'; |
| 60 | + $(infobox).each(function() { |
| 61 | + var message = $(infobox).html(); |
| 62 | + pushNotification([message,'error','Close']); |
| 63 | + }); |
| 64 | + var infobox = 'div.tile.info'; |
| 65 | + $(infobox).each(function() { |
| 66 | + var message = $(infobox).html(); |
| 67 | + pushNotification([message,'success','Close']); |
| 68 | + }); |
| 69 | + } |
| 70 | + |
| 71 | + $(document).ready(function() { |
| 72 | + convertNotifications(); |
| 73 | + }); |
| 74 | + |
| 75 | + // EXAMPLES: |
| 76 | + // pushNotification(['This is a test also...','success','Close','Test','alert("WOOT!")']); |
| 77 | + // pushNotification(['This is a test and more stuff!...','warning','Close']); |
| 78 | + |
| 79 | +</script> |
0 commit comments