Skip to content
Permalink
f3c7478b5b
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
459 lines (368 sloc) 11.5 KB
var app = angular.module('app', ['ngRoute', 'ngResource', 'ngCookies']);
// Root controllers in /root/ folder
// Page controllers in /pages/[your-page] folder
// Factories
app.factory('Foods', function($resource) {
return $resource('api/food/all');
// return $resource('assets/json/foods.json');
});
app.factory('FoodDetail', function($resource) {
// http://foodbank.develop.digitalmediauconn.org/api/getProductInfo/049000031249
// return $resource('https://example.com/api/food/:barcode', {barcode: '@barcode'});
return $resource('assets/json/foodDetail.json');
});
app.factory('Categories', function($resource) {
return $resource('http://foodbank.develop.digitalmediauconn.org/api/index.php/category/all');
});
app.factory('Nutrients', function($resource) {
return $resource('http://foodbank.develop.digitalmediauconn.org/api/index.php/nutrient/all');
});
// Just does a PUT request when u call Food.update
// app.factory('Food', function ($resource) {
// // {food: '@id'} means replace :food with $scope.foodCRUD.id
// var data = $resource('http://foodbank.develop.digitalmediauconn.org/api/food/:food', {food: '@id'}, {
// update:{ method:'PUT' }
// });
// return data;
// });
// app.factory('Nutrient', function ($resource) {
// var data = $resource('http://foodbank.develop.digitalmediauconn.org/api/nutrients/:nutrient', {nutrient: '@id'}, {
// update:{ method:'PUT' }
// });
// return data;
// });
app.config( function ($routeProvider, $locationProvider) {
$routeProvider
.when('/', {
templateUrl: 'app/pages/categories/view.html',
controller: 'CategoryCtrl'
})
.when('/login', {
templateUrl: 'app/pages/login/view.html',
controller: 'LoginCtrl'
})
.when('/categories', {
templateUrl: 'app/pages/categories/view.html',
controller: 'CategoryCtrl'
})
.when('/food', {
templateUrl: 'app/pages/food/view.html',
controller: 'FoodCtrl'
})
.when('/nutrients', {
templateUrl: 'app/pages/nutrients/view.html',
controller: 'NutrientsCtrl'
})
// Sandbox Demo
.when('/rest', {
templateUrl: 'app/sandbox/rest.html',
controller: 'RestCtrl'
})
// 404 Not Found
.otherwise({
templateUrl: 'app/pages/404/view.html',
controller: '404Ctrl'
});
// $locationProvider.html5Mode(true);
});
app.controller('root', ['$scope', '$location', function ($scope, $location) {
$scope.$watch('page', function () {
$scope.title = $scope.page.title + ' | F2E';
}, true);
$scope.page = {
'title' : 'Root',
'id' : 'root',
'blackNav' : false
};
$scope.loop = ['Apple Pie','Bananas','Peas','Newman\'s Own Oil and Vinegar Dressing','Cucumber Salad','Kraft Macaroni and Cheese','Some food','Some more food','Some junk food','Some healthy food'];
$scope.go = function ( path ) {
$location.path( path );
};
}]);
app.controller('RestCtrl', ['$scope', 'Foods', function ($scope, Foods) {
$scope.page.title = 'REST';
$scope.page.id = 'rest';
$scope.page.blackNav = false;
Foods.get({}, function (data) {
$scope.foods = data.data;
});
}]);
app.controller('NewsCtrl', ['$scope', function ($scope) {
$scope.page.title = 'News';
$scope.page.id = 'news';
$scope.page.blackNav = false;
}]);
app.controller('CompetitionCtrl', ['$scope', function ($scope) {
$scope.page.title = 'Competitions';
$scope.page.id = 'comps';
$scope.page.blackNav = true;
}]);
app.controller('TodoCtrl', ['$scope', function ($scope) {
$scope.todos = [
{ name: 'Master HTML/CSS/Javascript', completed: true },
{ name: 'Learn AngularJS', completed: false },
{ name: 'Build NodeJS backend', completed: false },
{ name: 'Get started with ExpressJS', completed: false },
{ name: 'Setup MongoDB database', completed: false },
{ name: 'Be awesome!', completed: false },
];
}]);
app.controller('EmailController', ['$scope', function ($scope) {
$scope.name = "Gabe";
$scope.email = "rogangabe@gmail.com";
}]);
app.controller('404Ctrl', ['$scope', function ($scope) {
$scope.page.title = '404';
$scope.page.id = '404';
$scope.page.yourVar = "globalStuff";
$scope.var2 = "localStuff";
}]);
function GetRanks() {
return [{name: 'green', id: 1}, {name: 'yellow', id: 2}];
}
function GetNutrients() {
return ['sodium', 'sugar', 'satfat'];
}
function GetOperations() {
return [{name: 'lt', symbol: 'Less Than'},
{name: 'lte', symbol: 'Less Than or Equal'},
{name: 'gt', symbol: 'Greater Than'},
{name: 'gte', symbol: 'Greater Than or Equal'},
{name: 'e', symbol: 'Equal To'}];
}
app.controller('CategoryCtrl', ['$scope', '$http', 'Categories', 'Nutrients', function ($scope, $http, Categories, Nutrients) {
$scope.page.title = 'Categories';
$scope.page.id = 'cat';
// GET list of categories
Categories.get().$promise.then(function(response){
$scope.categories = response.data;
;
});
$scope.ranks = GetRanks();
$scope.units = ['mg', 'g'];
Nutrients.get().$promise.then(function(response){
$scope.nutrients = response.data;
});
$scope.operations = GetOperations();
$scope.getCategoryIndexFromName = function(categoryName) {
for(var i = 0; i < $scope.categories.length; i++) {
if($scope.categories[i].name == categoryName) {
return i;
}
}
return -1;
};
$scope.addRule = function(categoryName) {
var categoryIndex = $scope.getCategoryIndexFromName(categoryName);
if(categoryIndex != -1) {
$scope.categories[categoryIndex].rules[0].push({
ruleId: 0,
nutrientId: $scope.nutrients[0].id,
operator: "lte",
threshold: 10,
units: "mg",
rank: 1,
categoryId: categoryIndex
});
}
};
$scope.removeRule = function(category, rule) {
for(var i = 0; i < category.rules[0].length; i++) {
if(category.rules[0][i] == rule) {
category.rules[0].splice(i, 1);
}
}
};
$scope.addCategory = function() {
//$scope.categories.push({name: 'New Category', rules: []})
};
$scope.removeCategory = function(category) {
for(var i = 0; i < $scope.categories.length; i++) {
if($scope.categories[i] == category) {
$scope.categories.splice(i, 1);
}
}
};
$scope.saveData = function(category) {
$http.post('http://foodbank.develop.digitalmediauconn.org/api/index.php/rules/saveBatch/' + category.id, category.rules[0]);
};
}]);
app.controller('FoodCtrl', ['$scope', '$http', 'Foods', 'FoodDetail', 'Categories', '$cookies', function ($scope, $http, Foods, FoodDetail, Categories, $cookies) {
$scope.page.title = 'Food';
$scope.page.id = 'food';
// Cookie for Table/Card view prefence
$scope.cardView = $cookies.get('showTable') === 'true';
$scope.$watch('cardView', function () {
$cookies.put('showTable', $scope.cardView ? 'true' : 'false');
});
// Initialize Filters
$scope.foods = [];
$scope.categories = [];
$scope.rankSelection = {
"1": true,
"2": true,
"3": true,
"4": true
};
$scope.categorySelection = { "all": true };
$scope.searchTerm = "";
// Initialize CRUD form
$scope.foodCRUD = {};
$scope.nutrientDetail = [];
// GET foods
Foods.get({}, function (data) {
$scope.foods = data.data;
});
// GET list of categories
Categories.get({}, function (data) {
var categories = data.data;
categories.forEach(function(cat){
$scope.categories[cat.id] = cat;
});
$scope.categories.forEach(function(c){
$scope.categorySelection[c.id] = false;
});
// Preset category filters
$scope.resetCategorySelection();
});
// Set category filters to false
$scope.resetCategorySelection = function () {
$scope.categories.forEach(function(c){
$scope.categorySelection[c.id] = false;
});
}
// Category filter function
$scope.categoryFilter = function (food) {
return ( $scope.categorySelection[food.category.id] | $scope.categorySelection["all"] );
}
// Rank filter function
$scope.rankFilter = function (food) {
return ( $scope.rankSelection[food.rank.id] );
}
// Modal : View Food
$scope.view = function (foodToView) {
$scope.showModal = true;
$scope.foodCRUD = foodToView;
FoodDetail.get({barcode: foodToView.barcode}, function (data) {
$scope.nutrientDetail = data.data.nutrients;
});
};
// Modal : Cancel
$scope.cancel = function () {
$scope.showModal = false;
};
// NOTE: End of code for now
// Initialize CRUD modes
// $scope.editMode = false;
// $scope.createMode = false;
// CRUD: edit food
// $scope.edit = function (foodToEdit) {
// $scope.editMode = true;
// $scope.foodCRUD = foodToEdit;
// $scope.foodCRUD.action = "update";
// console.log($scope.foodCRUD);
// };
//
// // CRUD: create food
// $scope.create = function () {
// $scope.createMode = true;
// $scope.foodCRUD = {};
// $scope.foodCRUD.rank_mode = "auto";
// $scope.foodCRUD.action = "create";
// // console.log($scope.foodCRUD);
// };
//
// // CRUD: cancel
// $scope.cancel = function () {
// $scope.editMode = false;
// $scope.createMode = false;
// };
//
// // CRUD: POST
// $scope.submit = function () {
// $scope.cancel(); // hide modal
// Food.update($scope.foodCRUD);
// };
}]);
app.controller('LoginCtrl', ['$scope', 'loginService', function ($scope, loginService) {
$scope.page.title = 'Login';
$scope.page.id = 'login';
$scope.page.yourVar = "globalStuff";
$scope.selected = {
username: "",
password: ""
};
$scope.loginAttempt = {
username: "",
password: "",
attempt: false,
login: false
};
$scope.loginService = loginService;
$scope.login = function( username, password ) {
$scope.loginAttempt.username = username;
$scope.loginAttempt.password = password;
$scope.loginAttempt.attempt = true;
// Sanitize? Pre-check?
console.log($scope.loginService);
$scope.loginService.login( username, password ).then(function(response) {
console.log(response);
// Mock the fact that this takes longer
setTimeout(function() {
$scope.$apply(function() {
$scope.loginAttempt.login = response.login;
$scope.loginAttempt.attempt = false;
});
}, 1000);
}, function(error) {
console.error(error);
});
};
}]);
app.factory('loginService', ['$http', function($http) {
return {
login: function( username, password ) {
return $http.get('/app/pages/login/example.json').then(function(response) {
return response.data;
}, function(error) {
return error;
});
}
};
}]);
app.controller('NutrientsCtrl', ['$scope', 'Nutrients', function ($scope, Nutrients) {
$scope.page.title = 'Nutrients';
$scope.page.id = 'nutrients';
// Initialize CRUD modes
$scope.editMode = false;
$scope.createMode = false;
// Initialize CRUD form
$scope.nutrientCRUD = {};
// GET nutrients
Nutrients.get({}, function (data) {
$scope.nutrients = data.data;
});
// CRUD: edit food
$scope.edit = function (nutrientToEdit) {
$scope.editMode = true;
$scope.nutrientCRUD = nutrientToEdit;
$scope.nutrientCRUD.action = "update";
};
// CRUD: create food
$scope.create = function () {
$scope.createMode = true;
$scope.nutrientCRUD = {};
$scope.nutrientCRUD.action = "create";
};
// CRUD: cancel
$scope.cancel = function () {
$scope.editMode = false;
$scope.createMode = false;
};
// CRUD: POST
$scope.submit = function () {
if ($scope.editMode) {Nutrient.update($scope.nutrientCRUD);}
else if ($scope.createMode) {Nutrient.save($scope.nutrientCRUD);}
$scope.cancel(); // hide modal
};
}]);