<commit_before>/**
 * @package assets
 */

(function($) {

	/**
	 * This plugin inserts tags from a list into an input field. It offers three modes: 
	 * singular - allowing only one tag at a time
	 * multiple - allowing multiple tags, comma separated
	 * inline - which adds tags at the current cursor position
	 *
	 * @param {Object} custom_settings
	 *  An object specifying the tag list items
	 */
	$.fn.symphonyTags = function(custom_settings) {
		var objects = this;
		var settings = {
			items:				'li'
		};

		$.extend(settings, custom_settings);

	/*-----------------------------------------------------------------------*/

		objects = objects.map(function() {
			var object = $(this);
			
			object.find(settings.items).bind('click', function() {

				var input = $(this).parent().prevAll('label').find('input')[0];
				var tag = this.className || $(this).text();

				if(input === undefined) {
					input = $(this).parent().prevAll('#error').find('label input')[0]
				}

				input.focus();

				// Singular
				if (object.hasClass('singular')) {
					input.value = tag;
				}

				// Inline
				else if (object.hasClass('inline')) {
					var start = input.selectionStart;
					var end = input.selectionEnd;

					if (start >= 0) {
						input.value = input.value.substring(0, start) + tag + input.value.substring(end, input.value.length);
					}

					else {
						input.value += tag;
					}

					input.selectionStart = start + tag.length;
					input.selectionEnd = start + tag.length;
				}

				// Multiple
				else {
					var exp = new RegExp('^' + tag + '$', 'i');
					var tags = input.value.split(/,\s*/);
					var removed = false;

					for (var index in tags) {
						if (tags[index].match(exp)) {
							tags.splice(index, 1);
							removed = true;
						}

						else if (tags[index] == '') {
							tags.splice(index, 1);
						}
					}

					if (!removed) tags.push(tag);

					input.value = tags.join(', ');
				}
			});

			return object;
		});

		return objects;
	};

})(jQuery.noConflict());<commit_msg>Fix focus bug in Safari (#446) and make use of event delegation to avoid the usage of map which breaks jQuery chaining.<commit_after>/**
 * @package assets
 */

(function($) {

	/**
	 * This plugin inserts tags from a list into an input field. It offers three modes: 
	 * singular - allowing only one tag at a time
	 * multiple - allowing multiple tags, comma separated
	 * inline - which adds tags at the current cursor position
	 *
	 * @param {Object} custom_settings
	 *  An object specifying the tag list items
	 */
	$.fn.symphonyTags = function(custom_settings) {
		var objects = this;
		var settings = {
			items: 'li'
		};

		$.extend(settings, custom_settings);

	/*-----------------------------------------------------------------------*/

		return objects.delegate(settings.items, 'click.tags', function(event) {
			var item = $(this),
				object = item.parent(),
				input = object.parent().find('label input'),
				value = input.val(),
				tag = item.attr('class') || item.text();

			// Singular
			if(object.is('.singular')) {
				input.val(tag);
			}

			// Inline
			else if(object.is('.inline')) {
				var start = input[0].selectionStart,
					end = input[0].selectionEnd,
					position = 0;

				// Insert tag
				if(start > 0) {
					input.val(value.substring(0, start) + tag + value.substring(end, value.length));
					position = start + tag.length;
				}

				// Append tag
				else {
					input.val(value + tag);
					position = value.length + tag.length;
				}

				// Reset cursor position
				input[0].selectionStart = position;
				input[0].selectionEnd = position;
			}

			// Multiple
			else {
				var exp = new RegExp('^' + tag + '$', 'i'),
					tags = value.split(/,\s*/),
					removed = false;

				// Check existing tags
				for(var index in tags) {
				
					// Remove existing tag
					if(tags[index].match(exp)) {
						tags.splice(index, 1);
						removed = true;
					}

					// Remove empty tags
					else if(tags[index] == '') {
						tags.splice(index, 1);
					}
				}

				// Add new tag
				if(!removed) {
					tags.push(tag);
				}

				// Save tags
				input.val(tags.join(', '));
			}
		});
	};

})(jQuery.noConflict());
{'fraction_non_alphanumeric': 0.18424184717924305, 'fraction_numerical': 0.0042846941204475126, 'mean_word_length': 2.1499250374812595, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '2209730', 'n_tokens_mistral': 1681, 'n_tokens_neox': 1251, 'n_words': 339}