<commit_before>/**
 * Javascript required to power the user defined forms.
 * 
 * Rewritten from the prototype FieldEditor and constantly
 * being refactored to be be less specific on the UDF dom.
 */
(function($) {
	$(document).ready(function() {
		/**
		 * Namespace
		 */
		var userforms = userforms || {};
		
		/**
		 * Messages from UserForms are translatable using i18n.
		 */
		userforms.messages = {
			CONFIRM_DELETE_ALL_SUBMISSIONS: 'All submissions will be permanently removed. Continue?',
			ERROR_CREATING_FIELD: 'Error creating field',
			ADDING_FIELD: 'Adding new field',
			ADDED_FIELD: 'Added new field',
			HIDE_OPTIONS: 'Hide options',
			SHOW_OPTIONS: 'Show options',
			ADDING_OPTION: 'Adding option',
			ADDED_OPTION: 'Added option',
			ERROR_CREATING_OPTION: 'Error creating option',
			REMOVED_OPTION: 'Removed option',
			ADDING_RULE: 'Adding rule'
		};
		
		/**
		 * Returns a given translatable string from a passed key. Keys
		 * should be all caps without any spaces.
		 */
		userforms.message = function() {
			en = arguments[1] || userforms.messages[arguments[0]];
			
			return ss.i18n._t("UserForms."+ arguments[0], en);
		};
		
		/**
		 * Update the sortable properties of the form as a function
		 * since the application will need to refresh the UI dynamically based
		 * on a number of factors including when the user adds a page or
		 * swaps between pages
		 *
		 */
		userforms.update = function() {
			$("#Fields_fields").sortable({
				handle: '.fieldHandler',
				cursor: 'pointer',
				items: 'li.EditableFormField',
				placeholder: 'removed-form-field',
				opacity: 0.6,
				revert: 'true',
				change : function (event, ui) {
					$("#Fields_fields").sortable('refreshPositions');
				},
				update : function (event, ui) {
					var sort = 1;

					$("li.EditableFormField").each(function() {
						$(this).find(".sortHidden").val(sort++);
					});
				}
			});

			$(".editableOptions").sortable({
				handle: '.handle',
				cursor:'pointer',
				items: 'li',
				placeholder: 'removed-form-field',
					opacity: 0.6,
				revert: true,
				change : function (event, ui) {
					$(this).sortable('refreshPositions');
				},
				update : function (event, ui) {
					var sort = 1;
					$(".editableOptions li").each(function() {
						$(this).find(".sortOptionHidden").val(sort++);
					});
				}
			});
		};
		
		/**
		 * Workaround for not refreshing the sort.
		 * 
		 * TODO: better solution would to not fire this on every hover but needs to
		 *		ensure it doesn't have edge cases. The sledge hammer approach.
		 */
		$(".fieldHandler, .handle").live('hover', function() {
			userforms.update();
		});
		
		/**
		 * Kick off the UserForms UI
		 */
		userforms.update();
		
		
		$.entwine('udf', function($){
			
			/*--------------------- SUBMISSIONS ------------------------ */
			
			/**
			 * Delete a given Submission from the form
			 */
			$("#userforms-submissions .deleteSubmission").live('click', function(event) {
				event.preventDefault();
				var deletedSubmission = $(this);
				$.post($(this).attr('href'), function(data) {
					deletedSubmission.parents('div.userform-submission').slideUp(function(){$(this).remove()});
				});
			});

			/**
			 * Delete all submissions and fade them out if successful
			 */
			$("#userforms-submissions .deleteAllSubmissions").live('click', function(event) {
				event.preventDefault();
				if(!confirm(userforms.message('CONFIRM_DELETE_ALL_SUBMISSIONS'))) {
					return;
				}
				var self = this;
				$.post($(this).attr('href'), function(data) {
					$(self).parents('#userforms-submissions').children().slideUp(function(){$(this).remove()})
				});

			});
			
			/*-------------------- FIELD EDITOR ----------------------- */
			
			/**
			 * Create a new instance of a field in the current form 
			 * area. the type information should all be on this object
			 */
			$('div.FieldEditor .MenuHolder .action').entwine({
				onclick: function(e) {
					e.preventDefault();
					if($("#Fields").hasClass('readonly')) {
						return;
					}
					
					// variables
					var $form = $("#Form_EditForm");
					var length = $(".FieldInfo").length + 1;
					var fieldType = $(this).siblings("select").val();
					var formData = $form.serialize()+'NewID='+ length +"&Type="+ fieldType;
					var $fieldEditor = $(this.closest('.FieldEditor'));
					// Due to some very weird behaviout of jquery.metadata, the url have to be double quoted
					var addURL = $fieldEditor.attr('data-add-url').substr(1,$fieldEditor.attr('data-add-url').length-2);
					$.ajax({
						headers: {"X-Pjax" : 'Partial'},
						type: "POST",
						url: addURL,
						data: formData, 
						success: function(data) {
							$('#Fields_fields').append(data);
							statusMessage(userforms.message('ADDED_FIELD'));
							var name = $("#Fields_fields li.EditableFormField:last").attr("id").split(' ');
							$("#Fields_fields select.fieldOption").append("<option value='"+ name[2] +"'>New "+ name[2] + "</option>");
						},
						error: function(e) {
							alert(ss.i18n._t('GRIDFIELD.ERRORINTRANSACTION', 'An error occured while fetching data from the server\n Please try again later.'));
						}
					});
					$("#Fields_fields").sortable('refresh');
				}
			});
			
			/**
			 * Delete a field from the user defined form
			 */
			$(".EditableFormField .delete").entwine({
				onclick: function(e) {
					e.preventDefault();
					
					var text = $(this).parents("li").find(".fieldInfo .text").val();
					<commit_msg>FIX: refresh sortable *after* loading new content<commit_after>/**
 * Javascript required to power the user defined forms.
 * 
 * Rewritten from the prototype FieldEditor and constantly
 * being refactored to be be less specific on the UDF dom.
 */
(function($) {
	$(document).ready(function() {
		/**
		 * Namespace
		 */
		var userforms = userforms || {};
		
		/**
		 * Messages from UserForms are translatable using i18n.
		 */
		userforms.messages = {
			CONFIRM_DELETE_ALL_SUBMISSIONS: 'All submissions will be permanently removed. Continue?',
			ERROR_CREATING_FIELD: 'Error creating field',
			ADDING_FIELD: 'Adding new field',
			ADDED_FIELD: 'Added new field',
			HIDE_OPTIONS: 'Hide options',
			SHOW_OPTIONS: 'Show options',
			ADDING_OPTION: 'Adding option',
			ADDED_OPTION: 'Added option',
			ERROR_CREATING_OPTION: 'Error creating option',
			REMOVED_OPTION: 'Removed option',
			ADDING_RULE: 'Adding rule'
		};
		
		/**
		 * Returns a given translatable string from a passed key. Keys
		 * should be all caps without any spaces.
		 */
		userforms.message = function() {
			en = arguments[1] || userforms.messages[arguments[0]];
			
			return ss.i18n._t("UserForms."+ arguments[0], en);
		};
		
		/**
		 * Update the sortable properties of the form as a function
		 * since the application will need to refresh the UI dynamically based
		 * on a number of factors including when the user adds a page or
		 * swaps between pages
		 *
		 */
		userforms.update = function() {
			$("#Fields_fields").sortable({
				handle: '.fieldHandler',
				cursor: 'pointer',
				items: 'li.EditableFormField',
				placeholder: 'removed-form-field',
				opacity: 0.6,
				revert: 'true',
				change : function (event, ui) {
					$("#Fields_fields").sortable('refreshPositions');
				},
				update : function (event, ui) {
					var sort = 1;

					$("li.EditableFormField").each(function() {
						$(this).find(".sortHidden").val(sort++);
					});
				}
			});

			$(".editableOptions").sortable({
				handle: '.handle',
				cursor:'pointer',
				items: 'li',
				placeholder: 'removed-form-field',
					opacity: 0.6,
				revert: true,
				change : function (event, ui) {
					$(this).sortable('refreshPositions');
				},
				update : function (event, ui) {
					var sort = 1;
					$(".editableOptions li").each(function() {
						$(this).find(".sortOptionHidden").val(sort++);
					});
				}
			});
		};
		
		/**
		 * Workaround for not refreshing the sort.
		 * 
		 * TODO: better solution would to not fire this on every hover but needs to
		 *		ensure it doesn't have edge cases. The sledge hammer approach.
		 */
		$(".fieldHandler, .handle").live('hover', function() {
			userforms.update();
		});
		
		/**
		 * Kick off the UserForms UI
		 */
		userforms.update();
		
		
		$.entwine('udf', function($){
			
			/*--------------------- SUBMISSIONS ------------------------ */
			
			/**
			 * Delete a given Submission from the form
			 */
			$("#userforms-submissions .deleteSubmission").live('click', function(event) {
				event.preventDefault();
				var deletedSubmission = $(this);
				$.post($(this).attr('href'), function(data) {
					deletedSubmission.parents('div.userform-submission').slideUp(function(){$(this).remove()});
				});
			});

			/**
			 * Delete all submissions and fade them out if successful
			 */
			$("#userforms-submissions .deleteAllSubmissions").live('click', function(event) {
				event.preventDefault();
				if(!confirm(userforms.message('CONFIRM_DELETE_ALL_SUBMISSIONS'))) {
					return;
				}
				var self = this;
				$.post($(this).attr('href'), function(data) {
					$(self).parents('#userforms-submissions').children().slideUp(function(){$(this).remove()})
				});

			});
			
			/*-------------------- FIELD EDITOR ----------------------- */
			
			/**
			 * Create a new instance of a field in the current form 
			 * area. the type information should all be on this object
			 */
			$('div.FieldEditor .MenuHolder .action').entwine({
				onclick: function(e) {
					e.preventDefault();
					if($("#Fields").hasClass('readonly')) {
						return;
					}
					
					// variables
					var form = $("#Form_EditForm");
					var length = $(".FieldInfo").length + 1;
					var fieldType = $(this).siblings("select").val();
					var formData = form.serialize()+'NewID='+ length +"&Type="+ fieldType;
					var fieldEditor = $(this).closest('.FieldEditor');

					// Due to some very weird behaviout of jquery.metadata, the url have to be double quoted
					var addURL = fieldEditor.attr('data-add-url').substr(1, fieldEditor.attr('data-add-url').length-2);

					$.ajax({
						headers: {"X-Pjax" : 'Partial'},
						type: "POST",
						url: addURL,
						data: formData, 
						success: function(data) {
							$('#Fields_fields').append(data);
							statusMessage(userforms.message('ADDED_FIELD'));
							var name = $("#Fields_fields li.EditableFormField:last").attr("id").split(' ');

							$("#Fields_fields select.fieldOption").append("<option value='"+ name[2] +"'>New "+ name[2] + "</option>");
							$("#Fields_fields").sortable('refresh');
						},
						error: function(e) {
							alert(ss.i18n._t('GRIDFIELD.ERRORINTRANSACTION', 'An error occured while fetching data from the server\n Please try again later.'));
						}
					});
				}
			});
			
			/**
			 * Delete a field from the user defined form
			 */
			$(".EditableFormField .delete").entwine({
				onclick: function(e) {
					e.preventDefault();
					
					var text = $(this).parents("li").find(".fieldInfo .text").val();
					
{'fraction_non_alphanumeric': 0.1749297561859875, 'fraction_numerical': 0.003625487174839119, 'mean_word_length': 2.8061400482925145, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '2812086', 'n_tokens_mistral': 4270, 'n_tokens_neox': 3242, 'n_words': 829}