Skip to content
Permalink
edaca2a698
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
211 lines (195 sloc) 5.92 KB
var dev1 = {
id: 1,
name:"George",
hardware:"iphone",
checkout: new Date("11/11/2016"),
};
var dev2 = {
id: 2,
name:"Greyson",
hardware:"iphone",
checkout: new Date("8/10/2016"),
};
var dev3 = {
id: 3,
name:"Linkin Park",
hardware:"ipad",
checkout: new Date("8/20/2016"),
};
var dev4 = {
id: 4,
name:"Abercrombie",
hardware:"ipad",
checkout: new Date("11/3/2016"),
};
var dev5 = {
id: 5,
name:"Hulk",
hardware:"computerStick",
checkout: new Date("4/28/2016"),
};
var dev6 = {
id: 6,
name:"Captain America",
hardware:"computerStick",
checkout: new Date("10/22/2016"),
};
var devices = [dev1, dev2, dev3, dev4, dev5, dev6];
//This array corresponds to the hardcoded inventory in request.js. The description has been replaced with a
//checkout date, because I imagine that is more the kind of info to pull from the database for this page.
//IDs, names and hardware are the same.
var options = document.getElementsByClassName('option');
for (var i = options.length - 1; i >= 0; i--) {
options[i].addEventListener('click',filterDeviceList);
}
populateDeviceList(null);
function filterDeviceList()
{
var id = this.getAttribute('id');
month=30*24*60*60*1000;
var upperbound;
var lowerbound;
var filter = new Array();
switch(id)
{
case "all":
populateDeviceList(null);
break;
case "1month":
upperbound=new Date().getTime();
lowerbound=upperbound-month;
filter=[lowerbound, upperbound];
populateDeviceList(filter);
break;
case "1-3months":
upperbound=new Date().getTime()-month;
lowerbound=upperbound-2*month;
filter=[lowerbound, upperbound];
populateDeviceList(filter);
break;
case "3-5months":
upperbound=new Date().getTime()-3*month;
lowerbound=upperbound-2*month;
filter=[lowerbound, upperbound];
populateDeviceList(filter);
break;
case "5+months":
upperbound=new Date().getTime()-5*month;
lowerbound=0;
filter=[lowerbound, upperbound];
populateDeviceList(filter);
break;
}
}
function populateDeviceList(filter)
//generates html and writes to 'devContainer' div in returnPage.html
{
var devicesToList = getCheckedOutDevices(null,filter);
var htmlString="";
var i;
for (i = 0; i < devicesToList.length; i++) {
var id = devicesToList[i].id;
var name = devicesToList[i].name;
var hardware = devicesToList[i].hardware;
var checkout = devicesToList[i].checkout;
htmlString+="<div class=\"deviceContainer\"><div><div class=\"imgContainer\"><img src=\"../imgs/";
htmlString+=hardware;
htmlString+=".png\" class=\"device\">";
htmlString+=name;
htmlString+="</div><div class=\"deviceInfo\"><p>Checked out: "
htmlString+=checkout.toISOString().substring(0,10);
htmlString+="</p><p>You've had this device for ";
var milliseconds=new Date().getTime()-checkout.getTime();
var seconds=Math.floor(milliseconds/1000);
var minutes=Math.floor(seconds/60);
var hours=Math.floor(minutes/60);
var days=Math.floor(hours/24);
var weeks=Math.floor(days/7);
var months=Math.floor(weeks/4);
if(months>=2) htmlString+=months+" months";
else
{
htmlString+= weeks+ " week";
if(weeks!=1) htmlString+="s";
}
htmlString+="!";
htmlString+="</div><div class=\"returnButtonContainer\"><button class=\"returnbutton\" id=\"button ";
htmlString+=(id);
htmlString+="\" type=\"button\">Return ";
htmlString+=name;
htmlString+="</button></div></p></div></div><br><br>";
}
if(i==0) htmlString+="Couldn't find any devices to return. Why not go order some?";
document.getElementById("devContainer").innerHTML = htmlString;
var returnbuttons = document.getElementsByClassName('returnbutton');
for (var i = 0; i < returnbuttons.length; i++) {
returnbuttons[i].addEventListener('click',returnDevice);
}
}
function returnDevice()
{
var id = this.getAttribute('id');
id = parseInt(id.replace(/[^0-9\.]/g,''), 10);
if(isUnavailable(id))
{
var unavailable = getUnavailableIDs();
unavailable.splice(unavailable.indexOf(id),1);
localStorage.setItem('unavailable',JSON.stringify(unavailable));
$('#return').fadeIn(1000);
$('#return').fadeIn(1000);
$('#return').fadeOut(1000);
}
else
alert("That's already marked available. Something may have gone wrong.");
populateDeviceList();
}
function getCheckedOutDevices(user, filter)
//Eventually this will return information about all devices checked out by *user*
//Right now there is only one user, and the function just returns IDs of all checked out devicess.
//'Filter' is passed as a two-element array representing a range of milliseconds.
//If the checkout date for a device falls within that range, then it will be included in the results.
//The filter may be null. If so, all checked out devices will be returned.
{
var unavailable = getUnavailableIDs();
var checkedDevices = new Array();
for (var i = 0; i < unavailable.length; i++) {
for (var j = 0; j < devices.length; j++) {
if(unavailable[i] == devices[j].id)
{
if(filter!=null)
{
var checkedOutMillis = devices[j].checkout.getTime();
if(filter[0]<checkedOutMillis && filter[1]>checkedOutMillis)
checkedDevices.push(devices[j]);
}
else
checkedDevices.push(devices[j]);
break;
}
}
}
return checkedDevices;
}
function isUnavailable(id){
var unavailable = getUnavailableIDs();
if(unavailable.length == 0)
return 0;
else{
for(var i = 0; i < unavailable.length; i++){
if(unavailable[i] == (id))
return 1;
}
}
return 0;
}
function getUnavailableIDs()
//Identical to the function in request.js: just reads the 'unavailable' array in local storage.
//Will need to be changed (or may be obsolete) when we get a database
{
var unavailable = new Array;
var unavailable_str = localStorage.getItem('unavailable');
if(unavailable_str !== "" && unavailable_str !== null){
unavailable = JSON.parse(unavailable_str);
}
return unavailable;
}