food-summary.js

angular.module('homepage').
directive('foodSummary', function () {
    return {
        restrict: 'E',
        replace: true,
        scope: {
        	food: '=data'
        },
        transclude: true,
        templateUrl: 'components/feature-components/food/food-summary.html'
    };
}).
filter('caloriesCount', function(FoodNutritionSrvc) {
	function getCaloriesCount(foodtitle, params) {
        var value    = "";
        var foodtype = params.foodtype;
        var quantity = params.quantity;
        var qtytype  = params.qtytype;
        var calories = params.calories;
        var nutritionInfo = FoodNutritionSrvc.getNutritionInfo(foodtitle, foodtype, quantity, qtytype);

        if (calories > 0) {
            value = calories;
        }
        else {
            value = nutritionInfo.calories > 0 ? nutritionInfo.calories : 'X';
        }

        return value;
	}

    return getCaloriesCount;
}).
filter('totalCaloriesCount', function (FoodNutritionSrvc) {
    function calculateTotalCalories(items) {
        var nutritionInfo = null;
        var totalCalories = 0;
        var hasUnknown    = false; // If any food calories is unknown

        for(var index in items) {
            var item = items[index];
            var calories = item.count;

            if (item.foodtext === undefined || item.foodtext === '') continue;

            nutritionInfo = FoodNutritionSrvc.getNutritionInfo(item.foodtext, item.mealtype, item.quantity, item.qtytype);

            if (calories > 0) {
                totalCalories += parseInt(calories);
            }
            else if(nutritionInfo.calories > 0) {
                totalCalories += parseInt(nutritionInfo.calories);
            }
            else {
                hasUnknown = true;
            }
        }

        if(totalCalories === 0) {
            return 'X';
        }
        else {
            if(hasUnknown === true) {
                return totalCalories + " + X";
            }
            else {
                return totalCalories;
            }
        }
    }

    return calculateTotalCalories;
});