<commit_before>/*!
 NAAM VAN ONS PROJECT, v1.0
 Created by <NAME> & <NAME>, commissioned by UZ Ghent
 https://github.com/kianilannoye/Chronicals

 This file contains the controller to add and modify headaches.
 */

angular.module('Chronic').controller('headacheController', function($scope, dataService){

    ons.ready(function() {
        $('.hidden').removeClass("hidden");
    });

  $scope.headache = dataService.getCurrentHeadache();

  if($scope.headache == null){
  	$scope.headache = { intensityValues: [], end: null, location: null, triggers: dataService.getTriggers(), symptoms: dataService.getSymptoms()};
  }

  $scope.setEnd = function(endDate, endTime){
  	if(endDate != null && endTime != null){
  		$scope.headache.end = new Date(endDate.getFullYear(), endDate.getMonth(), endDate.getDate(), endTime.getHours(), endTime.getMinutes());
  	}
  };

  if($scope.headache != null){
	  if($scope.end != null){
	  	$scope.end = new Date($scope.headache.end);
	  	$scope.endDate = $scope.end;
	  	$scope.endTime = $scope.end;
	  }
  } else $scope.end = null;

  $scope.setEndDate = function(endDate){
  	if(endDate != null){
  		if($scope.end == null) $scope.end = new Date();
	  	$scope.end.setFullYear(endDate.getFullYear());
	  	$scope.end.setMonth(endDate.getMonth());
	  	$scope.end.setDate(endDate.getDate());
  	}
  };

  $scope.setEndTime = function(endTime){
  	if(endTime != null){
  		if($scope.end == null) $scope.end = new Date();
	  	$scope.end.setHours(endTime.getHours());
	  	$scope.end.setMinutes(endTime.getMinutes());
	}
  };

  $scope.getIndexOfHeadache = function(){
  	headaches = dataService.getHeadacheList();
  	if(headaches == null || headaches.length == 0) return -1;
  	for(headache in headaches){
  		equalIntensityValues = true;
  		for(value in headache.intensityValues){
  			if(headache.intensityValues[value] != $scope.headache.intensityValues[value]) equalIntensityValues = false;
  		}
  		equalEnd = $scope.headache.end == headaches[headache].end;
  		equalLocation = $scope.headache.location == headaches[headache].location;
  		if(equalIntensityValues && equalEnd && equalLocation) return headache;
  		return -1;
  	}
  };

  $scope.headacheIndex = $scope.getIndexOfHeadache();

  /* Create a nice short time string from the start date and time */

  $scope.updateStartTimeString = function(){
  	if($scope.headache.intensityValues[0] == null) {
  		$scope.startTimeString = "";
  		return;
  	}
  	var months = ["jan.", "feb.", "mrt.", "apr.", "mei", "jun.", "jul.", "aug.", "sept.", "okt.", "nov.", "dec."];
  	var month = months[(new Date($scope.headache.intensityValues[0].key).getMonth())];
  	var day = (new Date($scope.headache.intensityValues[0].key).getDate().toString());
  	var hour =(new Date($scope.headache.intensityValues[0].key).getHours().toString());
  	if(hour < 10) hour = "0"+hour;
  	var minute = new Date($scope.headache.intensityValues[0].key).getMinutes().toString();
  	if(minute < 10) minute = "0"+minute;
  	$scope.startTimeString = day + " " + month + " " + hour + ":" + minute;
  };

  $scope.$watch('headache.intensityValues[0]', $scope.updateStartTimeString);

  /* closeAndSave is called when the user pressed the "Sla op" button */

  $scope.closeAndSave = function(){
  	console.log($scope.headacheIndex);

  	if($scope.headacheIndex != -1){
  	list = dataService.getHeadacheList();
  	list[$scope.headacheIndex] = $scope.headache;
  	dataService.setHeadacheList(list);
  	} else dataService.addHeadache($scope.headache);

  	dataService.setCurrentHeadache(null);
  	location.href="dashboard.html";
  };

  $scope.cancel = function(){
  	dataService.setCurrentHeadache(null);
  	location.href="dashboard.html";
  };

  /* Some ugly hack with jQuery to link a popover the the corresponding help buttons */
  $scope.message = "";

  var searchIndexById = function(list, id){
  	// Search the index of an id in a list of objects with ids
  	for(object in list){
  		if(list[object].id == id) return object;
  	}
  	return -1;
  };

  for(trigger in $scope.headache.triggers){ // Close your eyes and pretend this is not here ;)
  	// Initialize function on each helpButton for each trigger
  	$(document).on("click", '#helpButtonTrigger'+$scope.headache.triggers[trigger].id, function(){
  		var id = ($(this)[0].id).split('helpButtonTrigger');
  		var index = searchIndexById($scope.headache.triggers, id[1]);
  		$scope.message = $scope.headache.triggers[index].description;
  		$scope.popover.show("#"+$(this)[0].id);
  	});
  };

  for(symptom in $scope.headache.symptoms){ // Close your eyes and pretend this is not here ;)
  	// Initialize function on each helpButton for each trigger
  	$(document).on("click", '#helpButtonSymptom'+$scope.headache.symptoms[symptom].id, function(){
  		var id = ($(this)[0].id).split('helpButtonSymptom');
  		var index = searchIndexById($scope.headache.symptoms, id[1]);
  		$scope.message = $scope.headache.symptoms[index].description;
  		$scope.popover.show("#"+$(this)[0].id);
  	});
  };

  $(document).on("click", '.popover', function(){
  	$scope.popover.hide();
  });
  
  document.addEventListener("backbutton", onBackKeyDown, false);
	function onBackKeyDown(e) {
	  e.preventDefault();
	}

  ons.createPopover('popover.html').then(function(popover) {
  	// Create a popover for the help buttons
    $scope.popover = popover;
  });

  /* All variables and functions used to add intensity values and delete them */

  $scope.newHeadacheValue;
  $scope.newHeadacheDate;
  $scope.newHeadacheTime;

  $scope.deleteEntry = function(item){
  	$scope.headache.intensityValues.splice($scope.headache.intensityValues.indexOf(item), 1);
  	if($scope.headache.intensityValues.length == 0) $("#endDateForm").hide();
  };
<commit_msg>does the back button work?<commit_after>/*!
 NAAM VAN ONS PROJECT, v1.0
 Created by <NAME> & <NAME>, commissioned by UZ Ghent
 https://github.com/kianilannoye/Chronicals

 This file contains the controller to add and modify headaches.
 */

angular.module('Chronic').controller('headacheController', function($scope, dataService){

    ons.ready(function() {
        $('.hidden').removeClass("hidden");
    });

  $scope.headache = dataService.getCurrentHeadache();

  if($scope.headache == null){
  	$scope.headache = { intensityValues: [], end: null, location: null, triggers: dataService.getTriggers(), symptoms: dataService.getSymptoms()};
  }

  $scope.setEnd = function(endDate, endTime){
  	if(endDate != null && endTime != null){
  		$scope.headache.end = new Date(endDate.getFullYear(), endDate.getMonth(), endDate.getDate(), endTime.getHours(), endTime.getMinutes());
  	}
  };

  if($scope.headache != null){
	  if($scope.end != null){
	  	$scope.end = new Date($scope.headache.end);
	  	$scope.endDate = $scope.end;
	  	$scope.endTime = $scope.end;
	  }
  } else $scope.end = null;

  $scope.setEndDate = function(endDate){
  	if(endDate != null){
  		if($scope.end == null) $scope.end = new Date();
	  	$scope.end.setFullYear(endDate.getFullYear());
	  	$scope.end.setMonth(endDate.getMonth());
	  	$scope.end.setDate(endDate.getDate());
  	}
  };

  $scope.setEndTime = function(endTime){
  	if(endTime != null){
  		if($scope.end == null) $scope.end = new Date();
	  	$scope.end.setHours(endTime.getHours());
	  	$scope.end.setMinutes(endTime.getMinutes());
	}
  };

  $scope.getIndexOfHeadache = function(){
  	headaches = dataService.getHeadacheList();
  	if(headaches == null || headaches.length == 0) return -1;
  	for(headache in headaches){
  		equalIntensityValues = true;
  		for(value in headache.intensityValues){
  			if(headache.intensityValues[value] != $scope.headache.intensityValues[value]) equalIntensityValues = false;
  		}
  		equalEnd = $scope.headache.end == headaches[headache].end;
  		equalLocation = $scope.headache.location == headaches[headache].location;
  		if(equalIntensityValues && equalEnd && equalLocation) return headache;
  		return -1;
  	}
  };

  $scope.headacheIndex = $scope.getIndexOfHeadache();

  /* Create a nice short time string from the start date and time */

  $scope.updateStartTimeString = function(){
  	if($scope.headache.intensityValues[0] == null) {
  		$scope.startTimeString = "";
  		return;
  	}
  	var months = ["jan.", "feb.", "mrt.", "apr.", "mei", "jun.", "jul.", "aug.", "sept.", "okt.", "nov.", "dec."];
  	var month = months[(new Date($scope.headache.intensityValues[0].key).getMonth())];
  	var day = (new Date($scope.headache.intensityValues[0].key).getDate().toString());
  	var hour =(new Date($scope.headache.intensityValues[0].key).getHours().toString());
  	if(hour < 10) hour = "0"+hour;
  	var minute = new Date($scope.headache.intensityValues[0].key).getMinutes().toString();
  	if(minute < 10) minute = "0"+minute;
  	$scope.startTimeString = day + " " + month + " " + hour + ":" + minute;
  };

  $scope.$watch('headache.intensityValues[0]', $scope.updateStartTimeString);

  /* closeAndSave is called when the user pressed the "Sla op" button */

  $scope.closeAndSave = function(){
  	console.log($scope.headacheIndex);

  	if($scope.headacheIndex != -1){
  	list = dataService.getHeadacheList();
  	list[$scope.headacheIndex] = $scope.headache;
  	dataService.setHeadacheList(list);
  	} else dataService.addHeadache($scope.headache);

  	dataService.setCurrentHeadache(null);
  	location.href="dashboard.html";
  };

  $scope.cancel = function(){
  	dataService.setCurrentHeadache(null);
  	location.href="dashboard.html";
  };

  /* Some ugly hack with jQuery to link a popover the the corresponding help buttons */
  $scope.message = "";

  var searchIndexById = function(list, id){
  	// Search the index of an id in a list of objects with ids
  	for(object in list){
  		if(list[object].id == id) return object;
  	}
  	return -1;
  };

  for(trigger in $scope.headache.triggers){ // Close your eyes and pretend this is not here ;)
  	// Initialize function on each helpButton for each trigger
  	$(document).on("click", '#helpButtonTrigger'+$scope.headache.triggers[trigger].id, function(){
  		var id = ($(this)[0].id).split('helpButtonTrigger');
  		var index = searchIndexById($scope.headache.triggers, id[1]);
  		$scope.message = $scope.headache.triggers[index].description;
  		$scope.popover.show("#"+$(this)[0].id);
  	});
  };

  for(symptom in $scope.headache.symptoms){ // Close your eyes and pretend this is not here ;)
  	// Initialize function on each helpButton for each trigger
  	$(document).on("click", '#helpButtonSymptom'+$scope.headache.symptoms[symptom].id, function(){
  		var id = ($(this)[0].id).split('helpButtonSymptom');
  		var index = searchIndexById($scope.headache.symptoms, id[1]);
  		$scope.message = $scope.headache.symptoms[index].description;
  		$scope.popover.show("#"+$(this)[0].id);
  	});
  };

  $(document).on("click", '.popover', function(){
  	$scope.popover.hide();
  });
  
  document.addEventListener("backbutton", onBackKeyDown, false);
	function onBackKeyDown(e) {
		alert("dit is een test");
	  e.preventDefault();
	}

  ons.createPopover('popover.html').then(function(popover) {
  	// Create a popover for the help buttons
    $scope.popover = popover;
  });

  /* All variables and functions used to add intensity values and delete them */

  $scope.newHeadacheValue;
  $scope.newHeadacheDate;
  $scope.newHeadacheTime;

  $scope.deleteEntry = function(item){
  	$scope.headache.intensityValues.splice($scope.headache.intensityValues.indexOf(item), 1);
  	if($scope.headache.intensityValues.length == 0) $("#endDateForm").hide();
  };

{'fraction_non_alphanumeric': 0.16803811173668254, 'fraction_numerical': 0.0046773495019488956, 'mean_word_length': 4.120177383592018, 'pattern_counts': {'":': 2, '<': 11, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2474877', 'n_tokens_mistral': 3994, 'n_tokens_neox': 3858, 'n_words': 754}