<commit_before>// Copyright (c) 2010 - 2012  <NAME>
// Copyright (c) 2012 - 2017  MantisBT Team - <EMAIL>
// Licensed under the MIT license

jQuery(document).ready(function($) {
	"use strict";

	/**
	 * Return MantisBT XMLHttpRequest URL for given endpoint
	 * @param {string} entrypoint
	 * @returns {string} XMLHttpRequest URL
	 */
	function xhrurl(entrypoint) {
		return "xmlhttprequest.php?entrypoint=plugin_snippets_" + entrypoint;
	}

	/**
	 * Primary Snippets functionality.
	 * Use an AJAX request to retrieve the user's available snippets, and
	 * then insert select boxes into the DOM for each supported textarea.
	 */
	function SnippetsInit() {
		/**
		 * Initialize Snippets user interface.
		 * Adds a selection list before each textarea.
		 * @param {object} data - JSON object returned by XHR (see PHPDoc for
		 *                        xmlhttprequest_plugin_snippets_data() for details)
		 * @param {string} data.selector
		 * @param {string} data.label
		 * @param {string} data.default
		 * @param {object} data.snippets - Snippets list
		 */
		function SnippetsUI(data) {
			$(data.selector).each(function() {
				var textarea = $(this);

				// Only display snippets selector if there are any
				if (data.snippets) {
					try {
						// Create Snippets select
						var select = $("<select></select>");
						select.append("<option title='' value=''>" + data.default + "</option>");

						$.each(data.snippets, function(key, snippet) {
							// Escape single quotes
							var value = snippet.value.replace(/'/g, "&#39;");

							select.append(
								"<option value='" + value + "' title='" + value + "'>" + snippet.name + "</option>"
							);
						});

						select.change(function() {
							var text = $(this).val();
							textarea.textrange('replace', text);
							$(this).val("");
						});

						var label = $("<label>" + data.label + " </label>");
						label.append(select);

						textarea.before(label);
						textarea.before('<div class="space-4"></div>');
					} catch(e) {
						console.error('Error occured while generating Snippets UI', e);
					}
				}
			});
		}

		// If we have any textareas (excluding those in the plugin's own
		// edit pages) then fetch Snippets
		if ($("textarea").not(".snippetspatternhelp textarea").length > 0) {
			var bug_id = 0;

			$("form[name='bugnoteadd'] input[name='bug_id']").each(function() {
				bug_id = $(this).val();
			});
			$("form[action='bug_update.php'] input[name='bug_id']").each(function() {
				bug_id = $(this).val();
			});

			var url = xhrurl('data');
			if (bug_id > 0) {
				url += "&bug_id=" + bug_id;
			}

			$.getJSON(url)
				.done(SnippetsUI)
				.fail(function() {
					console.error('Error occured while retrieving Snippets');
				});
		}
	}

	/**
	 * Initialize Placeholder help tooltip for given object
	 * @param {object} domObject
	 * @param {object} data - JSON object returned by XHR
	 */
	function AddTooltip(domObject, data) {
		domObject.qtip({
			content: {
				text: data.text,
				title: data.title,
				button: true
			},
			position: {
				target: domObject.children('textarea'),
				my: 'bottom right',
				at: 'top right'
			},
			hide: {
				fixed: true
			}
		});
	}


	try {
		SnippetsInit();
	} catch(e) {
		alert(e);
	}

	// Snippet list behaviors
	$("input.snippets_select_all").change(function() {
		$("input[name='snippet_list[]']").prop("checked", $(this).prop("checked"));
	});

	// Snippet pattern help
	var selector = $(".snippetspatternhelp");
	if (selector.length > 0 ) {
		$.get(xhrurl('pattern_help'))
			.done(function(data) {
				selector.each(function() {
					AddTooltip($(this), data);
				});
			})
			.fail(function () {
				console.error('Error occured while retrieving Snippets pattern help');
			});
	}

});
<commit_msg>Fix Placeholder Help tooltip displayed off-screen<commit_after>// Copyright (c) 2010 - 2012  <NAME>
// Copyright (c) 2012 - 2017  MantisBT Team - <EMAIL>
// Licensed under the MIT license

jQuery(document).ready(function($) {
	"use strict";

	/**
	 * Return MantisBT XMLHttpRequest URL for given endpoint
	 * @param {string} entrypoint
	 * @returns {string} XMLHttpRequest URL
	 */
	function xhrurl(entrypoint) {
		return "xmlhttprequest.php?entrypoint=plugin_snippets_" + entrypoint;
	}

	/**
	 * Primary Snippets functionality.
	 * Use an AJAX request to retrieve the user's available snippets, and
	 * then insert select boxes into the DOM for each supported textarea.
	 */
	function SnippetsInit() {
		/**
		 * Initialize Snippets user interface.
		 * Adds a selection list before each textarea.
		 * @param {object} data - JSON object returned by XHR (see PHPDoc for
		 *                        xmlhttprequest_plugin_snippets_data() for details)
		 * @param {string} data.selector
		 * @param {string} data.label
		 * @param {string} data.default
		 * @param {object} data.snippets - Snippets list
		 */
		function SnippetsUI(data) {
			$(data.selector).each(function() {
				var textarea = $(this);

				// Only display snippets selector if there are any
				if (data.snippets) {
					try {
						// Create Snippets select
						var select = $("<select></select>");
						select.append("<option title='' value=''>" + data.default + "</option>");

						$.each(data.snippets, function(key, snippet) {
							// Escape single quotes
							var value = snippet.value.replace(/'/g, "&#39;");

							select.append(
								"<option value='" + value + "' title='" + value + "'>" + snippet.name + "</option>"
							);
						});

						select.change(function() {
							var text = $(this).val();
							textarea.textrange('replace', text);
							$(this).val("");
						});

						var label = $("<label>" + data.label + " </label>");
						label.append(select);

						textarea.before(label);
						textarea.before('<div class="space-4"></div>');
					} catch(e) {
						console.error('Error occured while generating Snippets UI', e);
					}
				}
			});
		}

		// If we have any textareas (excluding those in the plugin's own
		// edit pages) then fetch Snippets
		if ($("textarea").not(".snippetspatternhelp textarea").length > 0) {
			var bug_id = 0;

			$("form[name='bugnoteadd'] input[name='bug_id']").each(function() {
				bug_id = $(this).val();
			});
			$("form[action='bug_update.php'] input[name='bug_id']").each(function() {
				bug_id = $(this).val();
			});

			var url = xhrurl('data');
			if (bug_id > 0) {
				url += "&bug_id=" + bug_id;
			}

			$.getJSON(url)
				.done(SnippetsUI)
				.fail(function() {
					console.error('Error occured while retrieving Snippets');
				});
		}
	}

	/**
	 * Initialize Placeholder help tooltip for given object
	 * @param {object} domObject
	 * @param {object} data - JSON object returned by XHR
	 */
	function AddTooltip(domObject, data) {
		domObject.qtip({
			content: {
				text: data.text,
				title: data.title,
				button: true
			},
			position: {
				target: domObject.children('textarea'),
				my: 'bottom right',
				at: 'top right',
				viewport: $(window),
				adjust: {
					method: 'flip'
				}
			},
			hide: {
				fixed: true
			}
		});
	}


	try {
		SnippetsInit();
	} catch(e) {
		alert(e);
	}

	// Snippet list behaviors
	$("input.snippets_select_all").change(function() {
		$("input[name='snippet_list[]']").prop("checked", $(this).prop("checked"));
	});

	// Snippet pattern help
	var selector = $(".snippetspatternhelp");
	if (selector.length > 0 ) {
		$.get(xhrurl('pattern_help'))
			.done(function(data) {
				selector.each(function() {
					AddTooltip($(this), data);
				});
			})
			.fail(function () {
				console.error('Error occured while retrieving Snippets pattern help');
			});
	}

});

{'fraction_non_alphanumeric': 0.16531625718766335, 'fraction_numerical': 0.0060115002613695765, 'mean_word_length': 2.9185867895545314, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, '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': '760212', 'n_tokens_mistral': 3107, 'n_tokens_neox': 2406, 'n_words': 604}