angular.module('homepage').
directive('foodPanel', function () {
return {
controller: 'foodPanelController',
restrict: 'E',
replace: true,
scope: {
food: '='
},
templateUrl: 'components/feature-components/food/food-panel.html'
};
}).
controller('foodPanelController', function ($scope) {
function setItem() {
return {
mealtype: '',
quantity: 1,
qtytype: ''
};
}
$scope.isVisible = {};
$scope.food.list = [];
$scope.food.list[0] = setItem();
/**
* @param {type}
* possible values: FOOD, DRNK
*/
$scope.setType = function (type) {
$scope.food.list[0].mealtype = type;
};
/**
* @param {type}
* possible values:
* FOOD - PCE, TSP, BWL, ONC, GRM
* DRNK - CUP, GLS, BTL, PTR, TWR, LTR;
*/
$scope.setQtyType = function (type) {
$scope.food.list[0].qtytype = type;
};
/**
* Remove the item from zeroth index and add it to list end and empty zeroth index
*/
$scope.addNewItem = function () {
var length = $scope.food.list.length;
$scope.food.list[length] = $scope.food.list[0];
$scope.food.list[0] = setItem();
// Reset view
$scope.isVisible = {};
// Remove class 'chosen'
var elements = document.querySelectorAll('.input-box-wrapper .food-panel .chosen');
for(var i=0;i<elements.length;i++) {
elements[i].classList.remove('chosen');
}
};
}).
filter('filterMealType', function () {
/**
* Returns items by type and excludes the first item in the list
*/
return function (items, type) {
var filtered = [];
for (var index in items) {
if(items[index].mealtype === type && index != 0) {
filtered.push(items[index]);
}
}
return filtered;
};
}).
filter('quantityType', function () {
/**
* Returns items by type and excludes the first item in the list
*/
return function (type) {
var text = '';
switch(type) {
// DRINKs
case 'CUP': text = 'Cup'; break;
case 'GLS': text = 'Glass'; break;
case 'BTL': text = 'Bottle'; break;
case 'PTR': text = 'Pitcher'; break;
case 'TWR': text = 'Tower'; break;
case 'LTR': text = 'Liter'; break;
//FOODs
case 'PCE': text = 'Pieces'; break;
case 'TSP': text = 'Table Spoon'; break;
case 'BWL': text = 'Bowl'; break;
case 'ONC': text = 'Ounce'; break;
case 'GRM': text = 'Gram'; break;
}
return text;
};
});