-
Notifications
You must be signed in to change notification settings - Fork 0
/
greet.js
99 lines (89 loc) · 3.23 KB
/
greet.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
(function () {
var nameKey = 'name',
greetText = '<h4>Welcome $name$ </h4><small><span class="label label-primary">$tot$</span> visits total / <span class="label label-info">$ses$</span> this session / <button type="button" id="clrInfo" class="btn btn-info btn-sm">clear</button></small>',
$greet = $('#greet'),
$form = $('#nameForm'),
$button = $form.find('button'),
$field = $form.find('input#name');
$greet.hide();
$form.hide();
function init () {
if (supports_html5_storage()) {
if (localStorage.greetName) {
showGreeting();
} else if (supports_html5_storage()) {
$button.on('click',function () {
if ($field.val()) {
localStorage.greetName = htmlEntities($field.val());
$field.val('');
showSuccess('Hello ' + localStorage.greetName + '!');
$form.hide();
showGreeting();
} else {
showError('Name cannot be enpty');
}
});
$form.show();
}
} else {
showError('Your Browser doesn\'t support Local Storage. Some funtionality will be ommited.');
}
}
function supports_html5_storage () {
try {
return 'localStorage' in window && window['localStorage'] !== null;
} catch (e) {
return false;
}
}
function incrementSessionCount () {
if (sessionStorage.sgvcount) {
sessionStorage.sgvcount ++;
} else {
sessionStorage.sgvcount = 1
}
}
function incrementTotalCount () {
if (localStorage.tgvcount) {
localStorage.tgvcount ++;
} else {
localStorage.tgvcount = 1
}
incrementSessionCount();
}
function showGreeting () {
incrementTotalCount();
greetText = greetText.replace(/\$name\$/g, localStorage.greetName);
greetText = greetText.replace(/\$tot\$/g, localStorage.tgvcount);
greetText = greetText.replace(/\$ses\$/g, sessionStorage.sgvcount);
$greet.append(greetText);
$greet.show();
$('#clrInfo').on('click', function(){
localStorage.removeItem('greetName');
localStorage.removeItem('tgvcount');
sessionStorage.removeItem('sgvcount');
$greet.hide();
init();
})
}
function showError (message) {
$('body').append('<div class="alert alert-danger alert-dismissible fade in" role="alert"><button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>' + message + '</div>');
setTimeout(function () {
$('body').find('.alert').fadeOut('fast', function() {
$(this).remove();
});
}, 2000);
}
function showSuccess (message) {
$('body').append('<div class="alert alert-success alert-dismissible fade in" role="alert"><button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>' + message + '</div>');
setTimeout(function () {
$('body').find('.alert').fadeOut('fast', function() {
$(this).remove();
});
}, 2000);
}
function htmlEntities(str) {
return String(str).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"');
}
init();
}());