{"size": 1769, "ext": "js", "max_stars_repo_path": "Help/Exercise11/ingredient/editor.controller.js", "max_stars_repo_name": "ryanmcdonough/AngularWorkbook", "max_stars_repo_stars_event_min_datetime": "2015-01-12T18:37:03.000Z", "max_stars_repo_stars_event_max_datetime": "2021-07-29T13:20:54.000Z", "max_issues_repo_path": "Help/Exercise11/ingredient/editor.controller.js", "max_issues_repo_name": "ryanmcdonough/AngularWorkbook", "max_issues_repo_issues_event_min_datetime": "2015-12-27T14:50:17.000Z", "max_issues_repo_issues_event_max_datetime": "2015-12-27T15:02:06.000Z", "max_forks_repo_path": "Help/Exercise11/ingredient/editor.controller.js", "max_forks_repo_name": "ryanmcdonough/AngularWorkbook", "max_forks_repo_forks_event_min_datetime": "2015-01-25T13:59:50.000Z", "max_forks_repo_forks_event_max_datetime": "2021-05-07T08:40:44.000Z"}
{"max_stars_count": 90, "max_issues_count": 1, "max_forks_count": 35, "avg_line_length": 24.9154929577, "max_line_length": 92, "alphanum_fraction": 0.5517241379}
angular.module("umbraco")
	.controller("My.IngredientController", function($scope, ingredientResource, assetsService){
		
		assetsService.loadJs("/app_plugins/ingredient/highcharts.min.js")
		.then(function(){
				
			$scope.find = function(event, term){
				if (term !== "") {
	                $scope.ingredients = ingredientResource.search(term);
	            } else {
	                $scope.ingredients = undefined;
	            }
			};



			$scope.renderChart = function (){

				//collect an array from model.value
				var data = [];

				_.forEach($scope.model.value, function (ing) {
					//only add if ingredient has a .percentage
				       if (ing.percentage) {
			                 	data.push([ing.name, ing.percentage]);
				       }
				});
				
				//we find an element with ID 'container" and add the chart data to.
				$('#container').highcharts({
					series: [{
				                    type: 'pie',
				                    data: data
				            }]
				});
			};


			$scope.$watch("model.value", 
				function(newIngredients, oldIngredients) {
				       		$scope.renderChart();  
				}, true);



			if(!angular.isArray($scope.model.value)){
				$scope.model.value =[];
			}
			$scope.select = function(ingredient){
				$scope.model.value.push(ingredient);
			};

			$scope.remove = function(index){
				$scope.model.value.splice(index, 1);
			}

			
			//notice how throlled find is a parameter-less function
			//but we can still pass in $scope.searchTerm
			$scope.throttledFind = _.throttle(function(){
					if ($scope.searchTerm !== "") {
		                $scope.ingredients = ingredientResource.search($scope.searchTerm);
		            } else {
		                $scope.ingredients = undefined;
		            }
			},500);

		});
		
	});
