Final Hinh
by Alex Cowan
HTML
<!-- The core Firebase JS SDK is always required and must be listed first -->
<script src="https://www.gstatic.com/firebasejs/8.2.5/firebase-app.js"></script>
<!-- TODO: Add SDKs for Firebase products that you want to use
https://firebase.google.com/docs/web/setup#available-libraries -->
<script src="https://www.gstatic.com/firebasejs/8.2.1/firebase-auth.js"></script>
<script src="https://www.gstatic.com/firebasejs/8.2.5/firebase-firestore.js"></script>
<script src="https://www.gstatic.com/firebasejs/8.2.5/firebase-analytics.js"></script>
<script>
// Your web app's Firebase configuration
// For Firebase JS SDK v7.20.0 and later, measurementId is optional
var firebaseConfig = {
apiKey: "AIzaSyCOPgDQ4PAwNtOxZQtkkRpy0LV37VVEzJg",
authDomain: "hinh-78995.firebaseapp.com",
projectId: "hinh-78995",
storageBucket: "hinh-78995.appspot.com",
messagingSenderId: "389014950808",
appId: "1:389014950808:web:5599635662f42697b091ed",
measurementId: "G-XL6BSRZLQE"
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
firebase.analytics();
</script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<div class="top-bar">
<ul class="nav-list">
<li><a href="#">Home</a></li>
<li><a href="#">Help</a></li>
</ul>
<div id="search-box">
<input type="text" name="parts-search" placeholder="Mfg., Model, or Part Number" size=30>
</div>
<div id="auth-control">
<img src="https://www.alexandercowan.com/wp-content/uploads/2018/04/login-icon.png" alt="auth" style="width:30px;float: left;">
<ul class="nav-list">
<li id="login-trigger"><a href="#" id="auth-text">Log In</a></li>
</ul>
<div id="login-content">
<!-- On Forms: -->
<!-- onsubmit="return false" will stop the form submission but allow us to pick up the submission in JQuery with $('#login-form').submit(), otherwise JSFiddle can break. -->
<!-- allows...
CSS
.top-bar, #search-box, .nav-list li, .nav-list a {
background-color: #002D45;
font-family: Proxima Nova,helvetica neue,helvetica,sans-serif;
font-weight: bold;
font-size: 110%;
color: #ffffff;
}
.top-bar {
color: #FFFFFF;
height: 40px;
margin: 0px;
padding: 10px;
display: flex;
align-items: center;
}
#Header {
font-family: Proxima Nova,helvetica neue,helvetica,sans-serif;
font-weight: bold;
font-size: 200%;
color: rgb(0, 0, 0);
}
.nav-list {
list-style-type: none;
padding: 0;
display: inline;
}
.nav-list li {
float: left;
}
.nav-list a {
display: inline;
padding: 8px;
text-decoration: none;
}
#search-box {
margin-left: auto;
margin-right: 0;
padding: 10px;
}
#login-content {
display: none;
position: absolute;
margin-right: 10px;
top: 60px;
right: 0;
left: 1000;
z-index: 999;
background: #ffffff;
padding: 15px;
border: 1px solid #CCC;
width: 200px;
}
/* Keeps this in sync w/ the login-content div */
#login-form {
display: none;
}
#login-content input {
margin-bottom: 3px;
padding: 10px;
width: 90%;
border: 1px solid #CCC;
}
#login-content label {
cursor: pointer;
font-family: Proxima Nova,helvetica neue,helvetica,sans-serif;
color: #002D45;
}
#login-content #register-form {
display: none;
}
#login-content #forgotten-form {
display: none;
}
#login-content, #after-reset, #if-error, #if-reg-error {
display: none;
}
#login-content .auth-link {
cursor: pointer;
font-family: Proxima Nova,helvetica neue,helvetica, sans-serif;
color: #002D45;
}
#forgotten-link, #reset-response, #after-reset, #login-link-modal, #if-error, #if-reg-error {
cursor: pointer;
font-family: Proxima Nova,helvetica neue,helvetica,sans-serif;
color: #002D45;
font-size: 80%;
}
#login-link-modal {
font-size: 120%;
}
#toggle-search {
position: relative;
padding: 5px 0px;
height: 100%;
width: 100%;
}
#toggle-search img {
margin-left: 94%;
}
#search-controls...
JavaScript
//create a database object by connecting to the firestore using credentials defined in HTML
var db = firebase.firestore();
//query the parts database and take a snapshot
db.collection("parts")
.get().then((querySnapshot) => {
// loop through all part documents in the parts collection
querySnapshot.forEach((doc) => {
//call the drawDiv function to add the parts to each page
drawDiv(doc.data(), doc.id, '#parts-content');
})
})
//drawDiv function modified from the JavaScript case to pull data from firestore
function drawDiv(divData, divID, parent) {
if (divData == null) return null;
//divData is the data from the document that is pulled in the query above
// to access the part attributes simply use the dot notation like you would to navigate a JSON.
partNum = divID;
imgURL = divData.imageURL;
mfr = divData.Manufacturer;
model = divData.Model;
type = divData.Type;
// round the price to two decimal places
price = divData.Price.toFixed(2);
avail = divData.Availability;
orders = divData.orders;
//Write parts-content div
var $partsDiv = $("<div/>");
$partsDiv.addClass('catalog-part');
$partsDiv.attr({
'mfr': mfr,
'model': model,
'id': partNum
});
var img = $('<img />').attr({
'src': ('https://' + imgURL)
})
$partsDiv.prepend(img);
//Write overlay div
var $overlayDiv = $("<div/>");
$overlayDiv.addClass('overlay');
var overlaycontents = $("<p></p>").html('<br>' + orders + " Orders <br>" + '$' + price + "<br> " + avail + '<br>');
var $link = $('<a/>')
$link.addClass("part-ref")
$link.attr({
'href': '#',
//'onClick': 'partLink(this)'
})
var $starSpan
//Loop through 5 stars add them to the overlay and check them if needed based on average rating.
total_score = divData.total_score;
numRating = divData.total_ratings;
//round down -- design choice
avgRating = Math.round(total_score /...