|
1 | 1 | <!DOCTYPE html> |
2 | | -<html lang="en"> |
| 2 | +<html> |
3 | 3 |
|
4 | 4 | <head> |
5 | 5 | <meta charset="utf-8"> |
|
10 | 10 | <!-- Bootstrap --> |
11 | 11 | <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"> |
12 | 12 | <link href="styles.css" rel="stylesheet"> |
| 13 | + <!-- jQuery (necessary for Bootstrap's JavaScript plugins) --> |
| 14 | + <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> |
| 15 | + <!-- Include all compiled plugins (below), or include individual files as needed --> |
| 16 | + <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> |
| 17 | + <script src="js/lib/jquery.i18n/jquery.i18n.js"></script> |
| 18 | + <script src="js/lib/jquery.i18n/jquery.i18n.messagestore.js"></script> |
| 19 | + <script src="js/lib/jquery.i18n/jquery.i18n.fallbacks.js"></script> |
| 20 | + <script src="js/lib/jquery.i18n/jquery.i18n.language.js"></script> |
| 21 | + <script src="js/lib/jquery.i18n/jquery.i18n.parser.js"></script> |
| 22 | + <script src="js/lib/jquery.i18n/jquery.i18n.emitter.js"></script> |
| 23 | + <script src="js/lib/jquery.i18n/jquery.i18n.emitter.bidi.js"></script> |
| 24 | + <script src="antixss.js" type="text/javascript"></script> |
| 25 | + |
| 26 | + <script> |
| 27 | + $( document ).ready(function() { |
| 28 | + $.i18n().load( { |
| 29 | + en: { |
| 30 | + "welcome": "Welcome.", |
| 31 | + "name": "name", |
| 32 | + "what_is_your_name": "What is your name?", |
| 33 | + "hello": "Hello $1", |
| 34 | + "added_to_database": "Hello $1, I've added you to the database!", |
| 35 | + "database_contents": "Database contents: " |
| 36 | + }, |
| 37 | + ja: { |
| 38 | + "welcome": "ようこそ。", |
| 39 | + "name": "名前", |
| 40 | + "what_is_your_name": "お名前を教えてください。", |
| 41 | + "hello": "こんにちは $1", |
| 42 | + "added_to_database": "こんにちは $1 さん、あなたをデータベースに追加しました。", |
| 43 | + "database_contents": "データベースの内容: " |
| 44 | + } |
| 45 | + } ); |
| 46 | + $('body').i18n(); |
| 47 | + $('#user_name').attr("placeholder", $.i18n('name') ); |
| 48 | + }); |
| 49 | + </script> |
| 50 | + |
13 | 51 | </head> |
14 | 52 |
|
15 | 53 | <body> |
16 | | - <div class="container"> |
17 | | - <h1>Welcome.</h1> |
| 54 | + <div class="container" id="container"> |
| 55 | + <h1 data-i18n="welcome"></h1> <!- Welcome -> |
18 | 56 | <div id="nameInput" class="input-group-lg center-block helloInput"> |
19 | | - <p class="lead">What is your name?</p> |
20 | | - <input id="user_name" type="text" class="form-control" placeholder="name" aria-describedby="sizing-addon1" value="" /> |
| 57 | + <p class="lead" data-i18n="what_is_your_name"></p> |
| 58 | + <input id="user_name" type="text" class="form-control" aria-describedby="sizing-addon1" value="" /> |
21 | 59 | </div> |
22 | 60 | <p id="response" class="lead text-center"></p> |
23 | 61 |
|
24 | 62 | <p id="databaseNames" class="lead text-center"></p> |
25 | 63 | </div> |
26 | | - <!-- jQuery (necessary for Bootstrap's JavaScript plugins) --> |
27 | | - <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> |
28 | | - <!-- Include all compiled plugins (below), or include individual files as needed --> |
29 | | - <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> |
30 | | - <script src="antixss.js" type="text/javascript"></script> |
31 | 64 |
|
32 | | - <script> |
| 65 | +</body> |
| 66 | + |
| 67 | + |
| 68 | +</html> |
| 69 | + |
| 70 | + |
| 71 | +<script> |
33 | 72 | //Submit data when enter key is pressed |
34 | 73 | $('#user_name').keydown(function(e) { |
35 | 74 | var name = $('#user_name').val(); |
36 | 75 | if (e.which == 13 && name.length > 0) { //catch Enter key |
37 | | - $('#nameInput').hide(); |
38 | | - $('#response').html("loading..."); |
39 | 76 | //POST request to API to create a new visitor entry in the database |
40 | 77 | $.ajax({ |
41 | 78 | method: "POST", |
42 | 79 | url: "./api/visitors", |
43 | 80 | contentType: "application/json", |
44 | 81 | data: JSON.stringify({name: name }) |
45 | 82 | }) |
46 | | - .done(function(data) { |
47 | | - $('#response').html(AntiXSS.sanitizeInput(data)); |
| 83 | + .done(function(data) { |
| 84 | + if(data && data.name){ |
| 85 | + if(data.database) |
| 86 | + $('#response').html($.i18n('added_to_database', AntiXSS.sanitizeInput(data.name))); |
| 87 | + else |
| 88 | + $('#response').html($.i18n('hello', AntiXSS.sanitizeInput(data.name))); |
| 89 | + } |
| 90 | + else { |
| 91 | + $('#response').html(AntiXSS.sanitizeInput(data)); |
| 92 | + } |
| 93 | + $('#nameInput').hide(); |
48 | 94 | getNames(); |
49 | 95 | }); |
50 | 96 | } |
51 | 97 | }); |
52 | | - |
53 | | - //Retreive all the visitors from the database |
| 98 | + |
| 99 | + //Retrieve all the visitors from the database |
54 | 100 | function getNames(){ |
55 | 101 | $.get("./api/visitors") |
56 | 102 | .done(function(data) { |
57 | 103 | if(data.length > 0) { |
58 | 104 | data.forEach(function(element, index) { |
59 | 105 | data[index] = AntiXSS.sanitizeInput(element) |
60 | 106 | }); |
61 | | - $('#databaseNames').html("Database contents: " + JSON.stringify(data)); |
| 107 | + $('#databaseNames').html($.i18n('database_contents') + JSON.stringify(data)); |
62 | 108 | } |
63 | 109 | }); |
64 | 110 | } |
65 | | - |
| 111 | + |
66 | 112 | //Call getNames on page load. |
67 | 113 | getNames(); |
68 | | - |
69 | | - |
70 | | - </script> |
71 | | -</body> |
72 | 114 |
|
73 | | -</html> |
| 115 | + |
| 116 | + </script> |
0 commit comments