<commit_before>var tagbox = angular.module('tagBox', []);

tagbox.directive('tagbox', function() {
	return {
        restrict: 'E',
		scope: {
			tags: '=ngModel',
			autocomplete: '=',
			placeholder: "@"
		},
		template: '<span ng-repeat="tag in tags"><span class="radius label" ng-class="{selected: $index == selectedtag}" ng-click="selectTagByIndex($index)">{{tag}} <a ng-click="removeTagByIndex($index)">&#x2716;</a></span></span><input type="text" placeholder="{{placeholder}}" ng-model="data" typeahead="item for item in autocomplete | filter:$viewValue | limitTo:8" typeahead-on-select="addTag(data); data=\'\'">',
		link: function(scope, tagbox, attrs, controller) {
			var input = angular.element(tagbox.find('input')[0]);

			tagbox.addClass('tagbox');

			if (!(scope.autocomplete)) {
				scope.autocomplete = []
			}
			scope.data = "";
			scope.selectedtag = -1;

			scope.removeTagByIndex = function(index, back) {
				scope.tags.splice(index, 1);
				if (index < scope.selectedtag) {
					scope.selectedtag = scope.selectedtag - 1;
					if (scope.selectedtag < 0 && scope.tags.length > 0) {
						scope.selectedtag = 0;
					}
				}
				else if (index == scope.selectedtag) {
					if (back == true) {
						scope.selectedtag = scope.selectedtag - 1;
						if (scope.selectedtag < 0 && scope.tags.length > 0) {
							scope.selectedtag = 0;
						}
					}
					if (scope.selectedtag > scope.tags.length - 1) {
						scope.selectedtag = -1;
					}
				}
			}

			scope.selectTagByIndex = function(index) {
				scope.selectedtag = index;
				input[0].selectionStart = 0;
				input[0].focus();
			}

			scope.addTag = function(tag) {
				tag = tag.trim();
				scope.selectedtag = -1;

				if ((scope.tags.indexOf(tag) > -1) || (tag == ''))
					return;

				scope.tags.push(tag);
			};

			input.on('keypress', function(event) {
				scope.$apply(function() {
					if (event.key == "Enter" || event.key == " ")
					{
						scope.addTag(scope.data);
						scope.data = "";
						event.preventDefault();
					}
					else if ((event.key == "Backspace" || event.key == "Delete") && input[0].selectionStart == 0)
					{
						if (scope.selectedtag >= 0) {
							scope.removeTagByIndex(scope.selectedtag, event.key == "Backspace");
						} else {
							scope.removeTagByIndex(scope.tags.length - 1);
						}
						event.preventDefault();
					}
					else if (event.key == "ArrowLeft" && input[0].selectionStart == 0)
					{
						if (scope.selectedtag == -1) {
							scope.selectedtag = scope.tags.length -1;
						}
					else if (scope.selectedtag == 0) {
							scope.selectedtag = 0;
						}
						else {
							scope.selectedtag = scope.selectedtag - 1;
						}
						event.preventDefault();
					}
					else if (event.key == "ArrowRight" && scope.selectedtag < scope.tags.length && scope.selectedtag >= 0)
					{
						if (scope.selectedtag == scope.tags.length - 1) {
							scope.selectedtag = -1;
						}
						else {
							scope.selectedtag = scope.selectedtag + 1;
						}<commit_msg>Fix: backspace/del not working as expected in tagbox<commit_after>var tagbox = angular.module('tagBox', []);

tagbox.directive('tagbox', function() {
	return {
        restrict: 'E',
		scope: {
			tags: '=ngModel',
			autocomplete: '=',
			placeholder: "@"
		},
		template: '<span ng-repeat="tag in tags"><span class="radius label" ng-class="{selected: $index == selectedtag}" ng-click="selectTagByIndex($index)">{{tag}} <a ng-click="removeTagByIndex($index)">&#x2716;</a></span></span><input type="text" placeholder="{{placeholder}}" ng-model="data" typeahead="item for item in autocomplete | filter:$viewValue | limitTo:8" typeahead-on-select="addTag(data); data=\'\'">',
		link: function(scope, tagbox, attrs, controller) {
			var input = angular.element(tagbox.find('input')[0]);

			tagbox.addClass('tagbox');

			if (!(scope.autocomplete)) {
				scope.autocomplete = []
			}
			scope.data = "";
			scope.selectedtag = -1;

			scope.removeTagByIndex = function(index, back) {
				scope.tags.splice(index, 1);
				if (index < scope.selectedtag) {
					scope.selectedtag = scope.selectedtag - 1;
					if (scope.selectedtag < 0 && scope.tags.length > 0) {
						scope.selectedtag = 0;
					}
				}
				else if (index == scope.selectedtag) {
					if (back == true) {
						scope.selectedtag = scope.selectedtag - 1;
						if (scope.selectedtag < 0 && scope.tags.length > 0) {
							scope.selectedtag = 0;
						}
					}
					if (scope.selectedtag > scope.tags.length - 1) {
						scope.selectedtag = -1;
					}
				}
			}

			scope.selectTagByIndex = function(index) {
				scope.selectedtag = index;
				input[0].selectionStart = 0;
				input[0].focus();
			}

			scope.addTag = function(tag) {
				tag = tag.trim();
				scope.selectedtag = -1;

				if ((scope.tags.indexOf(tag) > -1) || (tag == ''))
					return;

				scope.tags.push(tag);
			};

			input.on('keypress', function(event) {
				scope.$apply(function() {
					if (event.key == "Enter" || event.key == " ")
					{
						scope.addTag(scope.data);
						scope.data = "";
						event.preventDefault();
					}
					else if (event.key == "Backspace" && input[0].selectionStart == 0 && input[0].selectionEnd == input[0].selectionStart)
					{
						if (scope.selectedtag >= 0) {
							scope.removeTagByIndex(scope.selectedtag, true);
						} else {
							scope.removeTagByIndex(scope.tags.length - 1);
						}
						event.preventDefault();
					}
					else if (event.key == "Delete" && scope.selectedtag >= 0)
					{
						scope.removeTagByIndex(scope.selectedtag);
						event.preventDefault();
					}
					else if (event.key == "ArrowLeft" && input[0].selectionStart == 0)
					{
						if (scope.selectedtag == -1) {
							scope.selectedtag = scope.tags.length -1;
						}
					else if (scope.selectedtag == 0) {
							scope.selectedtag = 0;
						}
						else {
							scope.selectedtag = scope.selectedtag - 1;
						}
						event.preventDefault();
					}
					else if (event.key == "ArrowRight" && scope.selectedtag < scope.tags.length && scope.selectedtag >= 0)
					{
						if (scope.selectedtag == scope.tags.length - 1) {
							scope.selectedtag = -1;
						}
						else {
							scope.selectedtag = scope.selectedtag + 1;
						}
{'fraction_non_alphanumeric': 0.17026417525773196, 'fraction_numerical': 0.012725515463917526, 'mean_word_length': 2.626752336448598, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 30, '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': '2913757', 'n_tokens_mistral': 2732, 'n_tokens_neox': 2008, 'n_words': 378}