diff --git a/perfectSteak.js b/perfectSteak.js index b64b631..6b68d44 100644 --- a/perfectSteak.js +++ b/perfectSteak.js @@ -3,89 +3,98 @@ var perfectSteak = function (div) { function Model(div) { - var currentInfo={'meatTemp':23, 'thickness':3, 'data':[], 'numRows':2, 'time':0, 'OKToGraph':true, 'recipe':{}, totalTime:0}; - + var currentInfo = { + 'meatTemp': 23, + 'thickness': 3, + 'data': [], + 'numRows': 2, + 'time': 0, + 'OKToGraph': true, + 'recipe': {}, + totalTime: 0 + }; + var timeStep = 15; - + var changeThickness = function (newVal) { currentInfo["thickness"] = newVal; } - var browserInfo=function(M){ - currentInfo["browser"]=M; - } - - var updateTotalTime=function(secs){ - currentInfo["totalTime"]=secs; - } - - var checkDiv=function(){ - currentInfo["OKToGraph"]=true; - $(".alert").remove(); - for (var h=0; hToo low!"); - $("#row"+h+"side1").append(side1Alert); - currentInfo["OKToGraph"]=false; - } - if(parseFloat($("#inp2_"+h).val())<0){ - var side2Alert=$("
Too low!
"); - $("#row"+h+"side2").append(side2Alert); - currentInfo["OKToGraph"]=false; - } - if(parseFloat($("#row"+h+"time").val())<0){ - var timeAlert=$("
Negative time
"); - $("#duration"+h).append(timeAlert); - currentInfo["OKToGraph"]=false; - } - } - } - - var addTime=function(value){ - currentInfo['time']+=value; - } - - var changeTime=function(value){ - currentInfo['time']=value; - } - - var addRecipe=function(name, recipe){ - currentInfo['recipe'][name]=recipe; - } - - - - //CHANGES X SECONDS INTO Y:X WHERE Y IS MINUTES X IS SECONDS - var convertTime=function(secs){ - console.log("calling convertTime") - var minutes=Math.floor(parseInt(secs)/60); - var seconds=parseInt(secs)%60; - console.log("minutes "+minutes+" seconds "+seconds); - if (minutes==0 && seconds<10){ - console.log("1") - console.log(String(0)+":0"+String(seconds)) - return String(0)+":0"+String(seconds); - }else if (seconds==0){ - console.log(2); - console.log(String(minutes)+':'+String(seconds)+'0') - return String(minutes)+':'+String(seconds)+'0'; - }else{ - console.log(3); - console.log(String(minutes)+':'+String(seconds)) - return String(minutes)+':'+String(seconds); - } - } - - var numRowsPlus=function(){ - currentInfo["numRows"]++; - } - - var numRowsMinus=function(){ - currentInfo["numRows"]--; - } + var browserInfo = function (M) { + currentInfo["browser"] = M; + } + + var updateTotalTime = function (secs) { + currentInfo["totalTime"] = secs; + } + + var checkDiv = function () { + currentInfo["OKToGraph"] = true; + $(".alert").remove(); + for (var h = 0; h < currentInfo["numRows"]; h++) { + + if (parseFloat($("#inp1_" + h).val()) < 0) { + var side1Alert = $("
Too low!
"); + $("#row" + h + "side1").append(side1Alert); + currentInfo["OKToGraph"] = false; + } + if (parseFloat($("#inp2_" + h).val()) < 0) { + var side2Alert = $("
Too low!
"); + $("#row" + h + "side2").append(side2Alert); + currentInfo["OKToGraph"] = false; + } + if (parseFloat($("#row" + h + "time").val()) < 0) { + var timeAlert = $("
Negative time
"); + $("#duration" + h).append(timeAlert); + currentInfo["OKToGraph"] = false; + } + } + } + + var addTime = function (value) { + currentInfo['time'] += value; + } + + var changeTime = function (value) { + currentInfo['time'] = value; + } + + var addRecipe = function (name, recipe) { + currentInfo['recipe'][name] = recipe; + } + + + + //CHANGES X SECONDS INTO Y:X WHERE Y IS MINUTES X IS SECONDS + var convertTime = function (secs) { + console.log("calling convertTime") + var minutes = Math.floor(parseInt(secs) / 60); + var seconds = parseInt(secs) % 60; + console.log("minutes " + minutes + " seconds " + seconds); + if (minutes == 0 && seconds < 10) { + console.log("1") + console.log(String(0) + ":0" + String(seconds)) + return String(0) + ":0" + String(seconds); + } else if (seconds == 0) { + console.log(2); + console.log(String(minutes) + ':' + String(seconds) + '0') + return String(minutes) + ':' + String(seconds) + '0'; + } else { + console.log(3); + console.log(String(minutes) + ':' + String(seconds)) + return String(minutes) + ':' + String(seconds); + } + } + + var numRowsPlus = function () { + currentInfo["numRows"]++; + } + + var numRowsMinus = function () { + currentInfo["numRows"]--; + } var changeMeatTemp = function (newVal) { currentInfo["meatTemp"] = newVal; @@ -93,7 +102,7 @@ var perfectSteak = function (div) { //OK THIS WORKS NOW var dataClear = function () { - currentInfo["data"]=[]; + currentInfo["data"] = []; } //THIS ADDS AN ELEMENT TO THE DATA ARRAY @@ -104,89 +113,88 @@ var perfectSteak = function (div) { //THIS IS FOR CHANGING THE ENTIRE DATA ARRAY var dataChange = function (array) { - currentInfo["data"]=array; + currentInfo["data"] = array; } - - var saveRecipe=function(name){ - + + var saveRecipe = function (name) { + var steak = [currentInfo["data"][0][1]]; for (var m = 0; m < parseFloat($("#thicknessInp").val()) * 10; m++) { - steak.push(parseFloat($("#steakTemp").val())) + steak.push(parseFloat($("#steakTemp").val())) } steak.push(currentInfo["data"][0][2]); var myheatsolver = HeatSolver(steak); - var Thedata=myheatsolver.sixty_graph_arrays_duration(currentInfo["data"]); - var maxTemps=Thedata.maxTemps; + var Thedata = myheatsolver.sixty_graph_arrays_duration(currentInfo["data"]); + var maxTemps = Thedata.maxTemps; var meatType = $("input[type='radio'][name='meat']:checked").attr('id'); - var recipe=[meatType,maxTemps,currentInfo["data"],currentInfo["meatTemp"]]; - addRecipe(name,recipe); - + var recipe = [meatType, maxTemps, currentInfo["data"], currentInfo["meatTemp"]]; + addRecipe(name, recipe); + } - - - var buildData=function(){ - var newData=[]; - - for (var g=0; g2){ - var timeMin=function(time){ - - var timeString=''; - for (var x=0; x 2) { + var timeMin = function (time) { + + var timeString = ''; + for (var x = 0; x < time.length; x++) { + if (time.charAt(x) == ':') { + break; + } else { + timeString += time.charAt(x); + } + } + return parseInt(timeString) } - } - else{ - var timeMin=function(time){ + } else { + var timeMin = function (time) { return 0; + } } - } - var timeSec = parseInt(timedata.charAt(timedata.length-2) + timedata.charAt(timedata.length-1)); - var timeForGraph=60*timeMin(timedata)+timeSec; + var timeSec = parseInt(timedata.charAt(timedata.length - 2) + timedata.charAt(timedata.length - 1)); + var timeForGraph = 60 * timeMin(timedata) + timeSec; newData.push([timeForGraph, side1data, side2data]); - + } - + dataChange(newData); - + } - - var parseRecipe = function(recipeStr){ + + var parseRecipe = function (recipeStr) { var recipeStr = "3 cm Steak starts at 23°C 15 seconds at 180°C and 23°C 15 seconds at 180°C and 23°C" var pattTemp = /\d+/g; var pattCelsius = /\°(.+)$/g; - + var numArray = recipeStr.match(pattTemp); - for(var i=0;i"); var tableTabs = $(''); var tabContent = $("
Duration (m:s)Side 1 (°C)Side 2 (°C)
"); - displayDiv.append(tableTabs,tabContent); - + displayDiv.append(tableTabs, tabContent); + var addButton; var flipButton; var cookButton; - var cookButt=$(""); - cookButt.css("width",'100%'); - + var cookButt = $(""); + cookButt.css("width", '100%'); + displayDiv.append(saveModal) - + /* updateTime goes through the table and updates the total cooking time entered. It also checks for any entries that are in seconds and calls to change them into the format mm:ss */ - var updateTime=function(){ - var time=0; - for(var i=0;i
Saved Methods
"); - var dropdown1=$(''); - var dropdown2=$(''); - - for(var key in model.currentInfo['recipe']){ - dropdown1.append($('')); - dropdown2.append($('')); + + var dropdownDiv = $(""); + var dropdown1 = $(''); + var dropdown2 = $(''); + + for (var key in model.currentInfo['recipe']) { + dropdown1.append($('')); + dropdown2.append($('')); } - - dropdownDiv.change(function(){ + + dropdownDiv.change(function () { var e1 = document.getElementById("d1"); var name1 = e1.options[e1.selectedIndex].text; var e2 = document.getElementById("d2"); var name2 = e2.options[e2.selectedIndex].text; - var info=model.currentInfo['recipe'][name1]; + var info = model.currentInfo['recipe'][name1]; d3.selectAll('.finalsteak').remove(); - drawFinished(info[0],info[1],info[2],info[3],0); - var inf=model.currentInfo['recipe'][name2]; - drawFinished(inf[0],inf[1],inf[2],inf[3],0); + drawFinished(info[0], info[1], info[2], info[3], 0); + var inf = model.currentInfo['recipe'][name2]; + drawFinished(inf[0], inf[1], inf[2], inf[3], 0); }); - - dropdownDiv.append(dropdown1,dropdown2); + + dropdownDiv.append(dropdown1, dropdown2); $(".span9").prepend(dropdownDiv); } - + /* buildDisplay places the necessary items on the screen for the user to interact with. */ var buildDisplay = function () { - if (model.currentInfo["OKToGraph"]){ + if (model.currentInfo["OKToGraph"]) { div.append("
"); $(".span3").append(displayDiv); - var switches=$('
'); - div.append(switches); - - switches.change(function(){ - graph(false,$('.mytog:checked').attr('id')); + var switches = $('
'); + div.append(switches); + + switches.change(function () { + graph(false, $('.mytog:checked').attr('id')); }); - + $("#startModal").modal("show"); - + cookButton = $(".cookButton"); buildTable(); - } - - else{ + } else { (".") } addDropdown(); } - + var buildTable = function () { - + var timeStep = model.timeStep; var len = model.currentInfo["data"].length; var newData = [] - var sumtime=0; + var sumtime = 0; for (var i = 0; i < model.currentInfo["numRows"]; i++) { var iminus = i - 1; addButton = $(""); - + flipButton = $(""); - - var row = $(""); - - if(i>0){ - var vals=parseFloat($("#row" + (i-1) + "time").val()); + + var row = $(""); + + if (i > 0) { + var vals = parseFloat($("#row" + (i - 1) + "time").val()); } - - var duration = $(""); + + var duration = $(""); var inp1 = $(""); - var inp2 = $(""); + var inp2 = $(""); var step1Col = $(""); step1Col.append(inp1); var step2Col = $(""); step2Col.append(inp2); step1Col.append(flipButton); - + row.append(duration, step1Col, step2Col); $(".inputTable").append(row); if (i == model.currentInfo["numRows"] - 1) { - saveBut=$('Save'); - var saveModal=$(''); - var nameInp=$(''); - var okModal=$(''); - okModal.on("click", function(){ - - - model.saveRecipe($("#recipeName").val()); - var name=$("#recipeName").val(); - var dropdown1=$("#d1"); - var dropdown2=$("#d2"); - dropdown1.append($('')); - dropdown2.append($('')); - var e1 = document.getElementById("d1"); - var name1 = e1.options[e1.selectedIndex].text; - var e2 = document.getElementById("d2"); - var name2 = e2.options[e2.selectedIndex].text; - var info=model.currentInfo['recipe'][name1]; - d3.selectAll('.finalsteak').remove(); - drawFinished(info[0],info[1],info[2],info[3],0); - var inf=model.currentInfo['recipe'][name2]; - drawFinished(inf[0],inf[1],inf[2],inf[3],0); - }) - saveModal.append(nameInp,okModal); - - cookButt.on("click",function(){ - model.checkDiv() - model.buildData(); - updateTime(); - model.buildData(); - - if(clicked&&model.currentInfo["OKToGraph"]){ - graph(false,$('.mytog:checked').attr('id')) - } - - else{ - d3.selectAll(".containers").remove(); - d3.selectAll(".mysteak").remove(); - model.dataClear(); - } - }) - - $(".inputTable").append(addButton);//, saveBut,saveModal); - $(".span3").append(cookButt); - addDropdown(); + saveBut = $('Save'); + var saveModal = $(''); + var nameInp = $(''); + var okModal = $(''); + okModal.on("click", function () { + + + model.saveRecipe($("#recipeName").val()); + var name = $("#recipeName").val(); + var dropdown1 = $("#d1"); + var dropdown2 = $("#d2"); + dropdown1.append($('')); + dropdown2.append($('')); + var e1 = document.getElementById("d1"); + var name1 = e1.options[e1.selectedIndex].text; + var e2 = document.getElementById("d2"); + var name2 = e2.options[e2.selectedIndex].text; + var info = model.currentInfo['recipe'][name1]; + d3.selectAll('.finalsteak').remove(); + drawFinished(info[0], info[1], info[2], info[3], 0); + var inf = model.currentInfo['recipe'][name2]; + drawFinished(inf[0], inf[1], inf[2], inf[3], 0); + }) + saveModal.append(nameInp, okModal); + + cookButt.on("click", function () { + model.checkDiv() + model.buildData(); + updateTime(); + model.buildData(); + + if (clicked && model.currentInfo["OKToGraph"]) { + graph(false, $('.mytog:checked').attr('id')) + } else { + d3.selectAll(".containers").remove(); + d3.selectAll(".mysteak").remove(); + model.dataClear(); + } + }) + + $(".inputTable").append(addButton); //, saveBut,saveModal); + $(".span3").append(cookButt); + addDropdown(); } - var sumtime=0; - var time=$("#row" + i + "time").val().replace(':','.').split('.'); - for (var k=0;k"); - var i = model.currentInfo["numRows"] - 1; - var row = $(""); - - - - if(i>0){ - var vals=parseFloat($("#row" + (i-1) + "time").val()); - - + + model.dataClear(); + addButtonFun(); + CookButtonFun(); + closeRowFun(); + }; + + + var addRow = function (table) { + flipButton = $(""); + var i = model.currentInfo["numRows"] - 1; + var row = $(""); + + + + if (i > 0) { + var vals = parseFloat($("#row" + (i - 1) + "time").val()); + + var duration = $(""); var inp1 = $(""); var inp2 = $(""); var step1Col = $(""); step1Col.append(inp1); - var step2Col = $(""); + var step2Col = $(""); step2Col.append(inp2); step1Col.append(flipButton); row.append(duration, step1Col, step2Col); - $('.displayDiv').stop().animate({ - scrollTop: $(".displayDiv")[0].scrollHeight - }, 800); - + $('.displayDiv').stop().animate({ + scrollTop: $(".displayDiv")[0].scrollHeight + }, 800); + timeFun(i); flipButtonFun(i); table.append(row); - inp1.val($("#inp1_" + (i-1)).val()); - inp2.val($("#inp2_" + (i-1)).val()); - var sumtime=0; - var time=$("#row" + i + "time").val().replace(':','.').split('.'); - for (var k=0;k1){ - var sumtime=parseFloat(time[1]); - - sumtime+=parseFloat(60*time[0]); - } - else{var sumtime=parseFloat(time[0]);} - - - model.dataAdd([sumtime, cur1, cur2]); - } - var OKtoCook=true; //IF WE HAVE INVALID INPUTS, IT WILL BE CHANGED TO FALSE - - //THIS BIT IS CHECKING WHETHER THE THICKNESS AND INITIAL TEMP INPUTS ARE VALID - $("#tempAlert").remove(); - $("#thickAlert").remove(); - var tempAlert=$("
Temperature is not a valid number
"); - var thicknessAlert=$("
Thickness is not a valid number
"); - if (String(parseInt($("#steakTemp").val()))=='NaN'){ - $("#tempInpDiv").append(tempAlert); - OKtoCook=false; - }else if(parseInt($("#steakTemp").val())<-273||parseInt($("#steakTemp").val())>300){ - $("#tempInpDiv").append(tempAlert); - OKtoCook=false; - }else{ - model.changeMeatTemp(parseFloat($("#steakTemp").val())) - }; - - if (String(parseInt($("#thicknessInp").val()))=='NaN'){ - $("#thickInpDiv").append(thicknessAlert); - OKtoCook=false; - }else if(parseFloat($("#thicknessInp").val())<0.5||parseFloat($("thicknessInp").val())>35){ - $("#thickInpDiv").append(thicknessAlert); - OKtoCook=false; - }else{ - model.changeThickness(parseFloat($("#thicknessInp").val())) - }; - - //add to on click and calculate(blah,blah,blah, meatType) - var meatType = $("input[type='radio'][name='meat']:checked").attr('id'); - if(falseColor=='T') - { - meatType='False'; - } - //THIS WILL COOK THE STEAK IF WE HAVE VALID INPUTS - if (OKtoCook==true){ - var steak = [model.currentInfo["data"][0][1]]; - for (var m = 0; m < parseFloat($("#thicknessInp").val()) * 10; m++) { - steak.push(parseFloat($("#steakTemp").val())) - } - steak.push(model.currentInfo["data"][0][2]); - calculate(model.currentInfo["data"], steak,meatType,isFirst,model.currentInfo["totalTime"]) - } - } - var CookButtonFun = function () { - $(".cookButton").on("click", function () { - clicked=true; - model.checkDiv(); - d3.selectAll(".mysteak").remove(); - d3.selectAll(".containers").remove(); - if (model.currentInfo["OKToGraph"]){ - d3.selectAll("svg").remove(); - model.dataClear(); - graph(true,$('.mytog:checked').attr('id')); - }; - }); + + + var graph = function (isFirst, falseColor) { + d3.selectAll(".mysteak").remove(); + d3.selectAll(".containers").remove(); + model.dataClear(); + + for (var e = 0; e < model.currentInfo["numRows"]; e++) { + var curTime = $("#row" + e + "time").val(); + var cur1 = parseFloat($("#inp1_" + e).val()); + var cur2 = parseFloat($("#inp2_" + e).val()); + + var time = curTime.replace(':', '.').split('.'); + if (time.length > 1) { + var sumtime = parseFloat(time[1]); + + sumtime += parseFloat(60 * time[0]); + } else { + var sumtime = parseFloat(time[0]); + } + + + model.dataAdd([sumtime, cur1, cur2]); } - - var timeFun = function (j) { - $("#row" + j + "time").change(function () { - - if (j == 0) { - timeStep = parseInt($("#row" + j + "time").value); - } - - - if (j == 0) { - timeStep = parseInt($("#row" + j + "time").value); - } - - }) + var OKtoCook = true; //IF WE HAVE INVALID INPUTS, IT WILL BE CHANGED TO FALSE + + //THIS BIT IS CHECKING WHETHER THE THICKNESS AND INITIAL TEMP INPUTS ARE VALID + $("#tempAlert").remove(); + $("#thickAlert").remove(); + var tempAlert = $("
Temperature is not a valid number
"); + var thicknessAlert = $("
Thickness is not a valid number
"); + if (String(parseInt($("#steakTemp").val())) == 'NaN') { + $("#tempInpDiv").append(tempAlert); + OKtoCook = false; + } else if (parseInt($("#steakTemp").val()) < -273 || parseInt($("#steakTemp").val()) > 300) { + $("#tempInpDiv").append(tempAlert); + OKtoCook = false; + } else { + model.changeMeatTemp(parseFloat($("#steakTemp").val())) }; - - var flipButtonFun = function (k) { - flipButton.on("click", function () { - side1data = 0 - side1data += parseInt(parseFloat($('#inp1_' + k).val())) || 0; - side2data = parseInt(parseFloat($('#inp2_' + k).val())); - $('#inp1_' + k).val(side2data); - $('#inp2_' + k).val(side1data); - }) + + if (String(parseInt($("#thicknessInp").val())) == 'NaN') { + $("#thickInpDiv").append(thicknessAlert); + OKtoCook = false; + } else if (parseFloat($("#thicknessInp").val()) < 0.5 || parseFloat($("thicknessInp").val()) > 35) { + $("#thickInpDiv").append(thicknessAlert); + OKtoCook = false; + } else { + model.changeThickness(parseFloat($("#thicknessInp").val())) }; + //add to on click and calculate(blah,blah,blah, meatType) + var meatType = $("input[type='radio'][name='meat']:checked").attr('id'); + if (falseColor == 'T') { + meatType = 'False'; + } + //THIS WILL COOK THE STEAK IF WE HAVE VALID INPUTS + if (OKtoCook == true) { + var steak = [model.currentInfo["data"][0][1]]; + for (var m = 0; m < parseFloat($("#thicknessInp").val()) * 10; m++) { + steak.push(parseFloat($("#steakTemp").val())) + } + steak.push(model.currentInfo["data"][0][2]); + calculate(model.currentInfo["data"], steak, meatType, isFirst, model.currentInfo["totalTime"]) + } + } + var CookButtonFun = function () { + $(".cookButton").on("click", function () { + clicked = true; + model.checkDiv(); + d3.selectAll(".mysteak").remove(); + d3.selectAll(".containers").remove(); + if (model.currentInfo["OKToGraph"]) { + d3.selectAll("svg").remove(); + model.dataClear(); + graph(true, $('.mytog:checked').attr('id')); + }; + }); + } + + var timeFun = function (j) { + $("#row" + j + "time").change(function () { + + if (j == 0) { + timeStep = parseInt($("#row" + j + "time").value); + } + + + if (j == 0) { + timeStep = parseInt($("#row" + j + "time").value); + } + + }) + }; + + var flipButtonFun = function (k) { + flipButton.on("click", function () { + side1data = 0 + side1data += parseInt(parseFloat($('#inp1_' + k).val())) || 0; + side2data = parseInt(parseFloat($('#inp2_' + k).val())); + $('#inp1_' + k).val(side2data); + $('#inp2_' + k).val(side1data); + }) + }; + return { buildDisplay: buildDisplay, buildTable: buildTable, @@ -639,12 +643,14 @@ var perfectSteak = function (div) { var setup = function (div) { var model = Model(); var view = View(div, model); - + model.parseRecipe(); - + view.buildDisplay(); - $('.inputTable').offset({top:0}); - + $('.inputTable').offset({ + top: 0 + }); + };