In: Computer Science
javascript
9. “User Info”, submenu of “Setting”.
9.1 _____ (10 pts) A submenu is under “setting”. When the user
clicks on “User Info”, a pop up window
should display the user information (name, gender).
index.html
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="navbar">
<div class="subnav">
<button class="subnavbtn">File <i class="fa fa-caret-down"></i></button>
<div class="subnav-content">
<input id="csvfile" type="file" style="display:none" />
<a href="#" id="loadCSV">Load CSV file</a>
<a href="#" id='navLoginDB' onclick="return loginDB('show');">Login to DB</a>
<a href="#" id='navLogoutDB' onclick="return logoutDB();">Logout DB</a>
<a href="#">Exit</a>
</div>
</div>
<div class="subnav">
<button class="subnavbtn">View <i class="fa fa-caret-down"></i></button>
<div class="subnav-content">
<a href="#">Line</a>
<a href="#">Pie</a>
<a href="#">Bar</a>
<a href="#">Map</a>
</div>
</div>
<div class="subnav">
<button class="subnavbtn">Setting <i class="fa fa-caret-down"></i></button>
<div class="subnav-content">
<a href="#link1">User Info</a>
</div>
</div>
<div class="subnav">
<button class="subnavbtn">Help <i class="fa fa-caret-down"></i></button>
<div class="subnav-content">
<a href="#">Info</a>
<a href="#">Client</a>
</div>
</div>
</div>
<div id="dvCSV">
</div>
<script>
$(document).ready(function () {
var browserSupportFileUpload = function () {
var isCompatible = false;
if (window.File && window.FileReader && window.FileList && window.Blob) {
isCompatible = true;
}
return isCompatible;
};
if (browserSupportFileUpload()) {
document.getElementById('csvfile').addEventListener('change', uploadFile, false);
} else {
$("#message").html('The File APIs is not fully supported in thie browser. please use another browser.');
}
$(function () {
$("#loadCSV").click(function(e){
//$("#loadCSV").on('click', function (e) {
e.preventDefault();
$("#csvfile:hidden").trigger('click');
});
});
var uploadFile = function (event) {
var file = event.target.files[0];
var fileformat;
if (file.name.indexOf("csv") != -1) {
fileformat = true;
}
else {
fileformat = false;
}
var reader = new FileReader();
reader.readAsText(file);
reader.onload = function (event) {
if ((file.size / Math.pow(1024, 3)) > navigator.deviceMemory) {
document.getElementById("message").innerHTML = "The data requires more memory than the client can offer";
}
else if (!fileformat) {
document.getElementById("message").innerHTML = "The data is in wrong format. Only CSV file can be loaded!";
}
else {
createArray($.csv.toArrays(event.target.result));
}
};
};
function createArray(data) {
if (data !== null && data !== "" && data.length > 1) {
this.CSVdata = data;
document.getElementById("message").innerHTML = " File upload successful with" + ((CSVdata.length) - 1) + " records! lower threshold is:" + highlight('OutlierL') + " upper threshold is: " + highlight('OutlierU');
var wage = findAvg('Avg');
var pop = findAvg('Pop');
if (getCookie("p1UserName") != "") {
var uname = getCookie("p1UserName");
$.ajax({
url: "",
type: 'POST',
async: false,
data: {
uname: uname
},
success: function (response) {
var dd = JSON.parse(response);
console.log(dd);
wage = parseFloat(dd[0][0]);
pop = parseFloat(dd[0][1]);
}
});
}
}
}
}
);
</script>
</body>
</html>
style.css
body {
font-family: Arial, Helvetica, sans-serif;
margin: 0;
}
.navbar {
overflow: hidden;
background-color: #333;
}
.navbar a {
float: left;
font-size: 16px;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
.subnav {
float: left;
overflow: hidden;
}
.subnav .subnavbtn {
font-size: 16px;
border: none;
outline: none;
color: white;
padding: 14px 16px;
background-color: inherit;
font-family: inherit;
margin: 0;
}
.navbar a:hover, .subnav:hover .subnavbtn {
background-color: red;
}
.subnav-content {
display: none;
position: absolute;
left: 0;
background-color: red;
width: 100%;
z-index: 1;
}
.subnav-content a {
float: left;
color: white;
text-decoration: none;
}
.subnav-content a:hover {
background-color: #eee;
color: black;
}
.subnav:hover .subnav-content {
display: block;
}
#upload {
display: block;
visibility: hidden;
width: 0;
height: 0;
}
table {
border-collapse: collapse;
}
table, th, td {
border: 1px solid black;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="navbar">
<div class="subnav">
<button class="subnavbtn">File <i class="fa fa-caret-down"></i></button>
<div class="subnav-content">
<input id="csvfile" type="file" style="display:none" />
<a href="#" id="loadCSV">Load CSV file</a>
<a href="#" id='navLoginDB' onclick="return loginDB('show');">Login to DB</a>
<a href="#" id='navLogoutDB' onclick="return logoutDB();">Logout DB</a>
<a href="#">Exit</a>
</div>
</div>
<div class="subnav">
<button class="subnavbtn">View <i class="fa fa-caret-down"></i></button>
<div class="subnav-content">
<a href="#">Line</a>
<a href="#">Pie</a>
<a href="#">Bar</a>
<a href="#">Map</a>
</div>
</div>
<div class="subnav">
<button class="subnavbtn">Setting <i class="fa fa-caret-down"></i></button>
<div class="subnav-content">
<a href="#link1" id="myBtn">User Info</a>
</div>
</div>
<div class="subnav">
<button class="subnavbtn">Help <i class="fa fa-caret-down"></i></button>
<div class="subnav-content">
<a href="#">Info</a>
<a href="#">Client</a>
</div>
</div>
</div>
<div id="dvCSV">
</div>
<div id="myModal" class="modal">
<div class="modal-content">
<span class="close">×</span>
<p>Username : Sobi</p>
<p>Gender : Female</p>
</div>
</div>
<script>
$(document).ready(function () {
var browserSupportFileUpload = function () {
var isCompatible = false;
if (window.File && window.FileReader && window.FileList && window.Blob) {
isCompatible = true;
}
return isCompatible;
};
if (browserSupportFileUpload()) {
document.getElementById('csvfile').addEventListener('change', uploadFile, false);
} else {
$("#message").html('The File APIs is not fully supported in thie browser. please use another browser.');
}
$(function () {
$("#loadCSV").click(function(e){
//$("#loadCSV").on('click', function (e) {
e.preventDefault();
$("#csvfile:hidden").trigger('click');
});
});
var uploadFile = function (event) {
var file = event.target.files[0];
var fileformat;
if (file.name.indexOf("csv") != -1) {
fileformat = true;
}
else {
fileformat = false;
}
var reader = new FileReader();
reader.readAsText(file);
reader.onload = function (event) {
if ((file.size / Math.pow(1024, 3)) > navigator.deviceMemory) {
document.getElementById("message").innerHTML = "The data requires more memory than the client can offer";
}
else if (!fileformat) {
document.getElementById("message").innerHTML = "The data is in wrong format. Only CSV file can be loaded!";
}
else {
createArray($.csv.toArrays(event.target.result));
}
};
};
function createArray(data) {
if (data !== null && data !== "" && data.length > 1) {
this.CSVdata = data;
document.getElementById("message").innerHTML = " File upload successful with" + ((CSVdata.length) - 1) + " records! lower threshold is:" + highlight('OutlierL') + " upper threshold is: " + highlight('OutlierU');
var wage = findAvg('Avg');
var pop = findAvg('Pop');
if (getCookie("p1UserName") != "") {
var uname = getCookie("p1UserName");
$.ajax({
url: "",
type: 'POST',
async: false,
data: {
uname: uname
},
success: function (response) {
var dd = JSON.parse(response);
console.log(dd);
wage = parseFloat(dd[0][0]);
pop = parseFloat(dd[0][1]);
}
});
}
}
}
}
);
</script>
<script>
// Get the modal
var modal = document.getElementById("myModal");
// Get the button that opens the modal
var btn = document.getElementById("myBtn");
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];
// When the user clicks on the button, open the modal
btn.onclick = function() {
modal.style.display = "block";
}
// When the user clicks on <span> (x), close the modal
span.onclick = function() {
modal.style.display = "none";
}
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
</script>
<style type="text/css">
body {
font-family: Arial, Helvetica, sans-serif;
margin: 0;
}
.navbar {
overflow: hidden;
background-color: #333;
}
.navbar a {
float: left;
font-size: 16px;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
.subnav {
float: left;
overflow: hidden;
}
.subnav .subnavbtn {
font-size: 16px;
border: none;
outline: none;
color: white;
padding: 14px 16px;
background-color: inherit;
font-family: inherit;
margin: 0;
}
.navbar a:hover, .subnav:hover .subnavbtn {
background-color: red;
}
.subnav-content {
display: none;
position: absolute;
left: 0;
background-color: red;
width: 100%;
z-index: 1;
}
.subnav-content a {
float: left;
color: white;
text-decoration: none;
}
.subnav-content a:hover {
background-color: #eee;
color: black;
}
.subnav:hover .subnav-content {
display: block;
}
#upload {
display: block;
visibility: hidden;
width: 0;
height: 0;
}
table {
border-collapse: collapse;
}
table, th, td {
border: 1px solid black;
}
/* pop div style */
/* The Modal (background) */
.modal {
display: none; /* Hidden by default */
position: fixed; /* Stay in place */
z-index: 1; /* Sit on top */
left: 0;
top: 0;
width: 100%; /* Full width */
height: 100%; /* Full height */
overflow: auto; /* Enable scroll if needed */
background-color: rgb(0,0,0); /* Fallback color */
background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
}
/* Modal Content/Box */
.modal-content {
background-color: #fefefe;
margin: 15% auto; /* 15% from the top and centered */
padding: 20px;
border: 1px solid #888;
width: 25%; /* Could be more or less, depending on screen size */
}
/* The Close Button */
.close {
color: #aaa;
float: right;
font-size: 28px;
font-weight: bold;
}
.close:hover,
.close:focus {
color: black;
text-decoration: none;
cursor: pointer;
}
/* ends */
</style>
</body>
</html>