<commit_before>/**
 * Provides a link menu for the Redactor editor, which integrates
 * with Elefant's list of internal pages so a user doesn't have
 * to copy and paste or manually type a link to a page within
 * their own site.
 */

if (typeof RedactorPlugins === 'undefined') var RedactorPlugins = {};

RedactorPlugins.links = {
	// Initialize the plugin
	init: function () {
		$.getJSON (
			'/admin/wysiwyg/links',
			$.proxy (
				function (res) {
					this._links = res;
				},
				this
			)
		);
	},
	
	// Initialize the plugin when the button is clicked
	insert: function (self, evt, button) {
		this.insert_link_node = false;
		this.has_selected_text = false;
		var sel = this.getCurrent (),
			url = '', text = '', target = '',
			page_matched = false,
			base = window.location.origin
				? window.location.origin
				: window.location.protocol + '//' + window.location.host;

		if (sel && sel.anchorNode && sel.anchorNode.parentNode.tagName === 'A') {
			this.insert_link_node = sel.anchorNode.parentNode;
			url = sel.anchorNode.parentNode.href;
			text = sel.anchorNode.parentNode.text;
			target = sel.anchorNode.parentNode.target;
		} else if (sel.tagName && sel.tagName === 'A') {
			this.insert_link_node = sel;
			url = sel.href;
			text = sel.text;
			target = sel.target;
		} else {
			var parent = this.getParent ();
			if (parent.nodeName === 'A') {
				this.insert_link_node = $(parent);
				text = this.insert_link_node.text ();
				url = this.insert_link_node.attr ('href');
				target = this.insert_link_node.attr ('target');
			} else {
				text = this.getSelectionText ();
			}
		}
		
		if (text.length !== 0) {
			this.has_selected_text = true;
		}
		
		if (url.search (base) === 0) {
			url = url.replace (base, '');
		}

		this.selectionSave ();

		$.open_dialog (
			$.i18n ('Link'),
			'<div class="links-content">' +
				'<p>' +
					'<span class="links-btn" id="links-page-btn">' + $.i18n ('Page') + '</span>' +
					'<span class="links-btn" id="links-url-btn">' + $.i18n ('URL') + '</span>' +
					'<span class="links-btn" id="links-email-btn">' + $.i18n ('Email') + '</span>' +
					'<br />' +
					'<select id="links-page">' +
					'</select>' +
					'<input type="text" id="links-url" size="65" placeholder="http://" />' +
					'<input type="email" id="links-email" size="65" placeholder="<EMAIL>" />' +
				'</p>' +
				'<p>' +
					$.i18n ('Text') + '<br />' +
					'<input type="text" id="links-text" size="65" />' +
				'</p>' +
				'<p>' +
					'<input type="checkbox" id="links-tab"> ' +
					'<label for="links-tab">' +
						$.i18n ('Open link in new tab') +
					'</label>' +
				'</p>' +
				'<p>' +
					'<input type="submit" id="links-submit" value="' + $.i18n ('Insert') + '" />' +
					' &nbsp; ' +
					'<a href="#" id="links-cancel">' + $.i18n ('Cancel') + '</a>' +
				'</p>' +
			'</div>'
		);

		if (this._links) {
			var sel = $('#links-page');
			sel.append (
				$('<option>')
					.attr ('value', '')
					.text ($.i18n ('- select -'))
			);
			for (var i in this._links) {
				if (url === this._links[i].url) {
					sel.append (
						$('<option>')
							.attr ('value', this._links[i].url)
							.text (this._links[i].title)
							.attr ('selected', 'selected')
					);
					page_matched = true;
				} else {
					sel.append (
						$('<option>')
							.attr ('value', this._links[i].url)
							.text (this._links[i].title)
					);
				}
			}
		}

		if (page_matched) {
			this.show_pages ();
		} else if (url.search ('mailto:') === 0) {
			$('#links-email').val (url.replace ('mailto:', ''));
			this.show_email ();
		} else if (url.length !== 0) {
			$('#links-url').val (url);
			this.show_url ();
		} else {
			this.show_pages ();
		}

		$('#links-text').val (text);<commit_msg>Updated links to show URL first for easier external linking<commit_after>/**
 * Provides a link menu for the Redactor editor, which integrates
 * with Elefant's list of internal pages so a user doesn't have
 * to copy and paste or manually type a link to a page within
 * their own site.
 */

if (typeof RedactorPlugins === 'undefined') var RedactorPlugins = {};

RedactorPlugins.links = {
	// Initialize the plugin
	init: function () {
		$.getJSON (
			'/admin/wysiwyg/links',
			$.proxy (
				function (res) {
					this._links = res;
				},
				this
			)
		);
	},
	
	// Initialize the plugin when the button is clicked
	insert: function (self, evt, button) {
		this.insert_link_node = false;
		this.has_selected_text = false;
		var sel = this.getCurrent (),
			url = '', text = '', target = '',
			page_matched = false,
			base = window.location.origin
				? window.location.origin
				: window.location.protocol + '//' + window.location.host;

		if (sel && sel.anchorNode && sel.anchorNode.parentNode.tagName === 'A') {
			this.insert_link_node = sel.anchorNode.parentNode;
			url = sel.anchorNode.parentNode.href;
			text = sel.anchorNode.parentNode.text;
			target = sel.anchorNode.parentNode.target;
		} else if (sel.tagName && sel.tagName === 'A') {
			this.insert_link_node = sel;
			url = sel.href;
			text = sel.text;
			target = sel.target;
		} else {
			var parent = this.getParent ();
			if (parent.nodeName === 'A') {
				this.insert_link_node = $(parent);
				text = this.insert_link_node.text ();
				url = this.insert_link_node.attr ('href');
				target = this.insert_link_node.attr ('target');
			} else {
				text = this.getSelectionText ();
			}
		}
		
		if (text.length !== 0) {
			this.has_selected_text = true;
		}
		
		if (url.search (base) === 0) {
			url = url.replace (base, '');
		}

		this.selectionSave ();

		$.open_dialog (
			$.i18n ('Link'),
			'<div class="links-content">' +
				'<p>' +
					'<span class="links-btn" id="links-url-btn">' + $.i18n ('URL') + '</span>' +
					'<span class="links-btn" id="links-page-btn">' + $.i18n ('Page') + '</span>' +
					'<span class="links-btn" id="links-email-btn">' + $.i18n ('Email') + '</span>' +
					'<br />' +
					'<input type="text" id="links-url" size="65" placeholder="http://" />' +
					'<select id="links-page">' +
					'</select>' +
					'<input type="email" id="links-email" size="65" placeholder="<EMAIL>" />' +
				'</p>' +
				'<p>' +
					$.i18n ('Text') + '<br />' +
					'<input type="text" id="links-text" size="65" />' +
				'</p>' +
				'<p>' +
					'<input type="checkbox" id="links-tab"> ' +
					'<label for="links-tab">' +
						$.i18n ('Open link in new tab') +
					'</label>' +
				'</p>' +
				'<p>' +
					'<input type="submit" id="links-submit" value="' + $.i18n ('Insert') + '" />' +
					' &nbsp; ' +
					'<a href="#" id="links-cancel">' + $.i18n ('Cancel') + '</a>' +
				'</p>' +
			'</div>'
		);

		if (this._links) {
			var sel = $('#links-page');
			sel.append (
				$('<option>')
					.attr ('value', '')
					.text ($.i18n ('- select -'))
			);
			for (var i in this._links) {
				if (url === this._links[i].url) {
					sel.append (
						$('<option>')
							.attr ('value', this._links[i].url)
							.text (this._links[i].title)
							.attr ('selected', 'selected')
					);
					page_matched = true;
				} else {
					sel.append (
						$('<option>')
							.attr ('value', this._links[i].url)
							.text (this._links[i].title)
					);
				}
			}
		}

		if (page_matched) {
			this.show_pages ();
		} else if (url.search ('mailto:') === 0) {
			$('#links-email').val (url.replace ('mailto:', ''));
			this.show_email ();
		} else if (url.length !== 0) {
			$('#links-url').val (url);
			this.show_url ();
		} else {
			this.show_url ();
		}

		$('#links-text').val (text);
{'fraction_non_alphanumeric': 0.2137748344370861, 'fraction_numerical': 0.007417218543046358, 'mean_word_length': 2.6129186602870815, 'pattern_counts': {'":': 0, '<': 69, '<?xml version=': 0, '>': 69, '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': '3733277', 'n_tokens_mistral': 3349, 'n_tokens_neox': 2695, 'n_words': 584}