Skip to content

Commit 5e59e5e

Browse files
agent725Mateusz Zalega
authored and
Mateusz Zalega
committed
client-side dynamic notifications
1 parent 83ceb78 commit 5e59e5e

File tree

4 files changed

+112
-1
lines changed

4 files changed

+112
-1
lines changed

nak/webapp/views/admin.phtml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
<?php include ('notify/js/notify.pjs'); ?>
12
<?php include ('admin/js/admin.pjs'); ?>
23

34
<?php include ('admin/menu.phtml'); ?>
5+
<?php include ('notify/notify.phtml'); ?>
46

5-
<div class="row">
7+
<div class="row" style="margin-top: 30px;">
68
<div class="col-xs-12 col-md-6">
79
<img src="/img/logo/logo-nak-square.png">
810
<?php echo _('<h3>Welcome!</h3>'); ?>

nak/webapp/views/notify/js/notify.pjs

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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>

nak/webapp/views/notify/notify.phtml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<div id="notify" class="alert notify-hidden" role="alert" style="position: relative;">
2+
<div class="alert-content">
3+
</div>
4+
</div>

www/dist/css/style.css

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,29 @@ body {
3636
#wifi-list .radio {
3737
padding-right: 8px;
3838
}
39+
40+
#notify {
41+
transition: all 2s ease;
42+
overflow: hidden;
43+
max-height: 128px;
44+
opacity: 1;
45+
}
46+
47+
.notify-hidden {
48+
transition: all 2s ease;
49+
max-height: 0 !important;
50+
opacity: 0 !important;
51+
padding: 0;
52+
}
53+
54+
.notify-hidden .notify-buttons {
55+
bottom: 0;
56+
}
57+
58+
/* Notifications now converted and handled by client-side Javascript */
59+
.tile.info {
60+
display: none;
61+
}
62+
.tile.error {
63+
display: none;
64+
}

0 commit comments

Comments
 (0)