<commit_before>'use strict';

// Shortcuts controller
angular.module('shortcuts').controller('ShortcutsController', ['$scope', '$stateParams', '$location', 'Authentication', 'Shortcuts', 'Favorites', 'lodash',
	function($scope, $stateParams, $location, Authentication, Shortcuts, Favorites, lodash) {
		var _ = lodash;

		$scope.authentication = Authentication;
		$scope.user = Authentication.user;

		// Create new Shortcut
		$scope.create = function() {
			// Create new Shortcut object
			var shortcut = new Shortcuts({
				keyCombination: this.keyCombination,
				application: this.application,
				description: this.description,
				operatingSystem: this.operatingSystem,
				category: this.category
			});

			// Redirect after save
			shortcut.$save(function(response) {
				$location.path('shortcuts/' + response._id);

				// Clear form fields
				$scope.name = '';
			}, function(errorResponse) {
				$scope.error = errorResponse.data.message;
			});
		};

		// Remove existing Shortcut
		$scope.remove = function(shortcut) {
			if (shortcut) {
				shortcut.$remove();

				for (var i in $scope.shortcuts) {
					if ($scope.shortcuts[i] === shortcut) {
						$scope.shortcuts.splice(i, 1);
					}
				}
			} else {
				$scope.shortcut.$remove(function() {
					$location.path('shortcuts');
				});
			}
		};

		// Update existing Shortcut
		$scope.update = function() {
			var shortcut = $scope.shortcut;

			shortcut.$update(function() {
				$location.path('shortcuts/' + shortcut._id);
			}, function(errorResponse) {
				$scope.error = errorResponse.data.message;
			});
		};

		// Find a list of Shortcuts
		$scope.find = function() {
			$scope.shortcuts = Shortcuts.query();
			$scope.shortcuts.$promise.then(function() {
				$scope.applications = $scope.shortcuts.reduce(function(previousValue, currentValue) {
					if (previousValue.indexOf(currentValue.application) === -1) {
						previousValue.push(currentValue.application);
					}
					return previousValue;
				}, []);

				$scope.operatingSystems = $scope.shortcuts.reduce(function(previousValue, currentValue) {
					if (previousValue.indexOf(currentValue.operatingSystem) === -1) {
						previousValue.push(currentValue.operatingSystem);
					}
					return previousValue;
				}, []);
			});
		};

		// Find existing Shortcut
		$scope.findOne = function() {
			$scope.shortcut = Shortcuts.get({
				shortcutId: $stateParams.shortcutId
			});
		};

		$scope.selectedApplication = $stateParams.application;

		$scope.selectedOS = '';
		if (navigator.appVersion.indexOf('Win') !== -1) {
			$scope.selectedOS = 'Windows';
		} else if (
			navigator.appVersion.indexOf('Mac') !== -1) {
			$scope.selectedOS = 'OS X';
		} else if (
			navigator.appVersion.indexOf('X11') !== -1) {
			$scope.selectedOS = 'UNIX';
		} else if (
			navigator.appVersion.indexOf('Linux') !== -1) {
			$scope.selectedOS = 'Linux';
		}


		$scope.isEditor = function(user) {
			if (user.roles) {
				return user.roles.indexOf('editor') !== -1;
			} else {
				return false;
			}
		};

		$scope.toggleFavorite = function(shortcut) {
			if (!$scope.user) {
				return;
			}
			if (!$scope.isFavorite(shortcut)) {
				Favorites.save(shortcut, function(favorites) {
					$scope.user.favorites = favorites;
				});
			} else {
				Favorites.remove({
					id: shortcut._id
				}, function(favorites) {
					$scope.user.favorites = favorites;
				});
			}
		};

		$scope.isFavorite = function(shortcut) {
			if (!$scope.user) return false;
			return $scope.user.favorites.indexOf(shortcut._id) !== -1;
		};

		$scope.view = function(shortcut) {
			$location.path('/shortcuts/' + shortcut._id);
		};

		$scope.$watch('shortcuts | applicationFilter:selectedApplication | operatingSystemFilter:selectedOS | groupBy', function(categories) {
			$scope.categories = categories;<commit_msg>fixed shortcut grouping into categories<commit_after>'use strict';

// Shortcuts controller
angular.module('shortcuts').controller('ShortcutsController', ['$scope', '$stateParams', '$location', 'Authentication', 'Shortcuts', 'Favorites', 'lodash',
	function($scope, $stateParams, $location, Authentication, Shortcuts, Favorites, lodash) {
		var _ = lodash;

		$scope.authentication = Authentication;
		$scope.user = Authentication.user;

		// Create new Shortcut
		$scope.create = function() {
			// Create new Shortcut object
			var shortcut = new Shortcuts({
				keyCombination: this.keyCombination,
				application: this.application,
				description: this.description,
				operatingSystem: this.operatingSystem,
				category: this.category
			});

			// Redirect after save
			shortcut.$save(function(response) {
				$location.path('shortcuts/' + response._id);

				// Clear form fields
				$scope.name = '';
			}, function(errorResponse) {
				$scope.error = errorResponse.data.message;
			});
		};

		// Remove existing Shortcut
		$scope.remove = function(shortcut) {
			if (shortcut) {
				shortcut.$remove();

				for (var i in $scope.shortcuts) {
					if ($scope.shortcuts[i] === shortcut) {
						$scope.shortcuts.splice(i, 1);
					}
				}
			} else {
				$scope.shortcut.$remove(function() {
					$location.path('shortcuts');
				});
			}
		};

		// Update existing Shortcut
		$scope.update = function() {
			var shortcut = $scope.shortcut;

			shortcut.$update(function() {
				$location.path('shortcuts/' + shortcut._id);
			}, function(errorResponse) {
				$scope.error = errorResponse.data.message;
			});
		};

		// Find a list of Shortcuts
		$scope.find = function() {
			$scope.shortcuts = Shortcuts.query();
			$scope.shortcuts.$promise.then(function() {
				$scope.applications = $scope.shortcuts.reduce(function(previousValue, currentValue) {
					if (previousValue.indexOf(currentValue.application) === -1) {
						previousValue.push(currentValue.application);
					}
					return previousValue;
				}, []);

				$scope.operatingSystems = $scope.shortcuts.reduce(function(previousValue, currentValue) {
					if (previousValue.indexOf(currentValue.operatingSystem) === -1) {
						previousValue.push(currentValue.operatingSystem);
					}
					return previousValue;
				}, []);
			});
		};

		// Find existing Shortcut
		$scope.findOne = function() {
			$scope.shortcut = Shortcuts.get({
				shortcutId: $stateParams.shortcutId
			});
		};

		$scope.selectedApplication = $stateParams.application;

		$scope.selectedOS = '';
		if (navigator.appVersion.indexOf('Win') !== -1) {
			$scope.selectedOS = 'Windows';
		} else if (
			navigator.appVersion.indexOf('Mac') !== -1) {
			$scope.selectedOS = 'OS X';
		} else if (
			navigator.appVersion.indexOf('X11') !== -1) {
			$scope.selectedOS = 'UNIX';
		} else if (
			navigator.appVersion.indexOf('Linux') !== -1) {
			$scope.selectedOS = 'Linux';
		}


		$scope.isEditor = function(user) {
			if (user.roles) {
				return user.roles.indexOf('editor') !== -1;
			} else {
				return false;
			}
		};

		$scope.toggleFavorite = function(shortcut) {
			if (!$scope.user) {
				return;
			}
			if (!$scope.isFavorite(shortcut)) {
				Favorites.save(shortcut, function(favorites) {
					$scope.user.favorites = favorites;
				});
			} else {
				Favorites.remove({
					id: shortcut._id
				}, function(favorites) {
					$scope.user.favorites = favorites;
				});
			}
		};

		$scope.isFavorite = function(shortcut) {
			if (!$scope.user) return false;
			return $scope.user.favorites.indexOf(shortcut._id) !== -1;
		};

		$scope.view = function(shortcut) {
			$location.path('/shortcuts/' + shortcut._id);
		};

		$scope.$watch('shortcuts | applicationFilter:selectedApplication | operatingSystemFilter:selectedOS | groupBy:"category"', function(categories) {
			$scope.categories = categories;
{'fraction_non_alphanumeric': 0.15870329384194767, 'fraction_numerical': 0.00286421038927223, 'mean_word_length': 3.4124066628374496, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, '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': '4044871', 'n_tokens_mistral': 3034, 'n_tokens_neox': 2416, 'n_words': 419}