<p> //TODO not sure if this is the best way to store the foods? Is SessionStorage better?
var NUTS = {};
NUTS.eaten = [];
NUTS.foods = {};
NUTS.FBS = {};
NUTS.defaultNutrient = function() {
return "protein";
};
NUTS.foodsKey = function() {
return "foods";
};
NUTS.eatenInputs = function() {
return $('#date, #food, #foodGrams, #nutrientGrams'); //aka "div.entryPane input:text"
};
NUTS.foodList = function () {
var result = [];
for(var prop in NUTS.foods) {
if(NUTS.foods[prop].name) {
result.push(prop);
}
}
return result;
};
NUTS.FBS.qtyQuery = function(foodName) {
return [{ "type": "/food/nutrition_fact",
"food": foodName,
"nutrient": "protein",
"quantity": null
}];
$.getJSON(NUTS.FBS.url(query), function(response) {
$('body').append(JSON.stringify(response));
});
};
NUTS.FBS.url = function(query) {
var query_envelope = {'query' : query};
var service_url = 'http://api.freebase.com/api/service/mqlread';
return service_url + '?callback=?&query=' + encodeURIComponent(JSON.stringify(query_envelope));
};
NUTS.FBS.execQtyQuery = function(food) {
var query = NUTS.FBS.qtyQuery(food.name);
var url = NUTS.FBS.url(query);
$.getJSON(url, function(response) {
//NUTS.foods
$('body').append(JSON.stringify(response));
});
};
NUTS.init = function() {
$('#addButton').click(NUTS.addButtonClick);
$('#foodGrams').blur(NUTS.foodInputOnBlur);
$(NUTS.eatenInputs()).blur(NUTS.updateAddButton);
$(NUTS.eatenInputs()).keyup(NUTS.addOnKeyUp);
var foodJson = localStorage.getItem(NUTS.foodsKey());
NUTS.foods = foodJson ? JSON.parse(foodJson) : {};
$("#food").suggest({type:'/food/food'});
//.bind("fb-select", function(e, data) { NUTS.FBS.execQtyQuery(data) });
//$( "#food" ).autocomplete({ source: NUTS.foodList(), autoFocus: true });
$('#food').focus();
};
NUTS.updateAddButton = function() {
if($('#food').val() && $('#foodGrams').val()
&& $('#nutrientGrams').val()) {
$('#addButton').removeAttr('disabled');
} else {
$('#addButton').attr('disabled', 'disabled');
}
};
NUTS.addOnKeyUp = function(event) {
var keycode = (event.keyCode ? event.keyCode : event.which); //event.keyCode || event.which?
if(keycode === '13'){
$('#addButton').click();
}
NUTS.updateAddButton();
};
NUTS.inputsToObject = function(inputs, inputAttribute) {
var prop = inputAttribute || "id";
var result = {};
for(var i in inputs) {
if(inputs[i][prop]) {
result[inputs[i][prop]] = inputs[i].value;
}
}
return result;
};
//TODO maybe we could take the inputstoobject and populateinputs and make a generic js object
//~ var DOM2JS = {};
//~ function DOM2JS() {
//~ this.domObjects = domObjects;
//~ this.jsObject = jsObject;
//~ this.domObjectAttribute = domObjectAttribute || "id";
//~ }
//~ DOM2JS.toJS = function(domObjects, jsObject) {
//~ for(i in domObjects) {
//~ if(domObjects[i] && domObjects[i]
//~ }
//~ }
//~ DOM2JS.toDOM = function() {
//~ }
//~ NUTS.populate = function(inputs, values) {
//~ for(i in inputs) {
//~ inputs[i].value = values[i];
//~ }
//~ }
NUTS.populateInputs = function(values) {
for(var value in values) {
if(document.getElementById(value)) {
document.getElementById(value).value = values[value];
}
}
};
NUTS.addRowFromObject = function(table, obj) {
var rowIdx = table.rows.length;
var row = table.insertRow(-1);
for(var prop in obj) {
var cell=row.insertCell(-1);
cell.innerHTML = obj[prop];
}
var btn = document.createElement('button');
btn.innerHTML = "Edit";
btn.onclick = function() {
NUTS.populateInputs(obj);
NUTS.eaten.splice(rowIdx,1); //bad idea
NUTS.deleteAllRows(table);
NUTS.addRowsFromArray(table, NUTS.eaten);
//table.deleteRow(rowIdx);
var t = NUTS.sumOfProperty(NUTS.eaten,"nutrientGrams");
document.getElementById("totalAmt").innerHTML = t;
NUTS.updateAddButton();
$('#food').focus();
};
var insertedCell = row.insertCell(-1);
insertedCell.appendChild(btn);
};
NUTS.deleteAllRows = function(table) {
var i = table.rows ? table.rows.length : 0;
for(; i > 0; i -= 1) {
table.deleteRow(i -1);
}
};
NUTS.addRowsFromArray =function(table, array) {
for(var el in array) {
NUTS.addRowFromObject(table, array[el]);
}
};
//~ function getKey(){
//~ return this.userName + "-" + this.APP;
//~ }
//~ function storageKey(userName) {
//~ this.userName = userName;
//~ this.APP = "NUTS";
//~ this.get = getKey;
//~ }
//TODO write a test
//~ for(var i in inputs) {
//~ if(inputs[i].value) {
//~ inputs[i].value = "";
//~ }
//~ }
//~ }
function Food(name, nutrient, ratio) {
this.name = name;
this.nutrientRatios = {};
if(nutrient && ratio) {
this.nutrientRatios[nutrient] = ratio;
}
}
//~ not used
//~ Food.prototype.setRatio = function(nutrient, foodGrams, nutrientGrams) {
//~ var ratio = nutrientGrams / foodGrams;
//~ this.nutrientRatios[nutrient] = ratio;
//~ }
//TODO need to make sure that the input is a number
NUTS.sumOfProperty = function(array, property) {
var result = 0;
for(var i = 0; i </p>