close

  
<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.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" ).autocomplete({  
    source: foodList(),
    autoFocus: true 
  });
  $('#food').focus();
}  

NUTS.updateAddButton = function() {
  if($('#food').val() &amp;&amp; $('#foodGrams').val()
      &amp;&amp; $('#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] &amp;&amp; domObjects[i]
  //~ }
//~ }

//~ DOM2JS.toDOM = function() {

//~ }

//~ NUTS.populate = function(inputs, values) {
  //~ for(i in inputs) {
    //~ inputs[i].value = values[i];
  //~ }
//~ }

NUTS.populateInputs = function(values) {
  for(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(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();
  }
  cell = row.insertCell(-1);
  cell.appendChild(btn);
}

NUTS.deleteAllRows = function(table) {
  var i = table.rows ? table.rows.length : 0;
  for(; i &gt; 0; i -= 1) {
    table.deleteRow(i -1);
  }  
}

NUTS.addRowsFromArray =function(table, array) {
  for(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 &amp;&amp; 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>

Comments

Hide