<commit_before>/**
 * inputfields.js - JS specific to behavior of ProcessWire inputfields.
 * 
 * For other admin theme developers: you do not need to include this file in your admin theme
 * unless you want to (and it's better not to). Instead you should direct your admin theme to
 * load this exact file: $config->urls->root . 'wire/templates-admin/scripts/inputfields.js';
 *
 */

var InputfieldDebugMode = false; 

function consoleLog(note) {
	// uncomment the line below to enable debugging console
	if(InputfieldDebugMode) console.log(note);
}


/**
 * Inputfield Depedendencies
 *
 */
var InputfieldDependenciesProcessing = false;

function InputfieldDependencies() {

	if(InputfieldDependenciesProcessing) return;
	InputfieldDependenciesProcessing = true; 

	$(".InputfieldStateShowIf, .InputfieldStateRequiredIf").each(function() {

		 // Wrapper of field that we are operating on (i.e. #wrap_Inputfield_[name])
		var $fieldToShow = $(this);
		
		//Name of the field contained by $fieldToShow
		var fieldNameToShow = $fieldToShow.attr('id').replace(/wrap_Inputfield_/, '');

		// Array of conditions required to show a field
		var conditions = [];

		/**
		 * Remove quotes from value (if present)
		 *
		 * @param value
		 * @return string
		 *
		 */
		function trimParseValue(value) {
			// determine if we need to trim off quotes
			value = jQuery.trim(value);
			var first = value.substring(0,1);
			var last = value.substring(value.length-1, value.length);
			if((first == '"' || first == "'") && first == last) value = value.substring(1, value.length-1);
			return parseValue(value);
		}

		/**
		 * Convert string value to integer or float when appropriate
		 * 
		 * @param str string
		 * @param str2 string Optional second value for context
		 * @return string|int|float
		 * 
		 */
		function parseValue(str, str2) {
			
			str = jQuery.trim(str);
			if(str.length > 0 && !jQuery.isNumeric(str)) {
				return str; 
			}
			
			if(str.length == 0) {
				// empty value: should it be a blank or a 0?
				var t = typeof str2;
				if(t != "undefined") {
					// str2 is present for context
					if(t == "integer") return 0;
					if(t == "float") return 0.0;
					return str;
				} else {
					// no context, assume blank
					return str; 	
				}
			}
			
			var dot1 = str.indexOf('.');
			var dot2 = str.lastIndexOf('.');
			
			if(dot1 == -1 && /^-?\d+$/.test(str)) {
				// no dot present, and all numbers so must be integer
				return parseInt(str);
			}
			
			if(dot2 > -1 && dot1 != dot2) {
				// more than one dot, can't be a float
				return str; 
			}
			
			if(/^-?[\d.]+$/.test(str)) {
				// looks to be a float
				return parseFloat(str);
			}
			
			return str;
		}

		/**
		 * Returns whether or not value matched
		 *
		 * @param field Name of field
		 * @param operator
		 * @param value value to match for
		 * @param conditionValue
		 * @return int 0=value didn't match, 1=value matched
		 *
		 */
		function matchValue(field, operator, value, conditionValue) {
			var matched = 0;

			switch(operator) {
				case '=': if(value == conditionValue) matched++; break;
				case '!=': if(value != conditionValue) matched++; break;
				case '>': if(value > conditionValue) matched++; break;
				case '<': if(value < conditionValue) matched++; break;
				case '>=': if(value >= conditionValue) matched++; break;
				case '<=': if(value <= conditionValue) matched++; break;
				case '*=':
				case '%=': if(value.indexOf(conditionValue) > -1) matched++; break;
			}

			consoleLog('Field ' + field + ' - Current value: ' + value);
			consoleLog('Field ' + field + ' - Matched? ' + (matched > 0 ? 'YES' : 'NO'));

			return matched;
		}

		/**
		 * Called when a targeted Inputfield has changed
		 *
		 */
		function inputfieldChange() {
			InputfieldDependenciesProcessing = true; 

			if(InputfieldDebugMode) {
				consoleLog('-------------------------------------------------------------------');
				consoleLog('Field "' + $fieldToShow.attr('id') + '" detected a change to a dependency field! Beginning dependency checks...');
			}

			// number of changes that were actually made to field visibility
			var numVisibilityChanges = 0;
			var show = true; 
			var requiredMatches = 0; 
			var notRequiredMatches = 0;

			for(var c = 0; c < conditions.length; c++) {

				// current condition we are checking in this iteration 
				var condition = conditions[c];

				if(InputfieldDebugMode) {
					consoleLog('----');
					consoleLog('Start Dependency ' + c);
					consoleLog('Condition type: ' + condition.type);
					consoleLog('Field: ' + condition.field);
					if (condition.subfield.length > 0) consoleLog('Subfield: ' + condition.subfield);
					consoleLog('Operator: ' + condition.operator);
					consoleLog('Required value: ' + condition.value);
				}

				// matched contains positive value when condition matches
				var matched = 0;
		
				// iterate through all OR fields (this will most often just be 1 field)
				for(var fn = 0; fn < condition.fields.length; fn++) {

					var value = null;
					var $field = $("#Inputfield_" + condition.fields[fn]);
					
					if($field.length == 0) {
						// in case they manually specified id property
						$field = $("#" + condition.fields[fn]);
					}

					if ($field.length == 0) {
						// if field isn't present by #id it may be present by #id+value as a checkbox/radio field is
						consoleLog('Detected possible checkbox or radio: ' + condition.field + condition.operator + condition.value);
						if (condition.subfield == 'count' || condition.subfield == 'count-checkbox') {
							// count number of matching checked inputs
							$field = $("#wrap_Inputfield_" + condition.fields[fn] + " :input");
							value = $("#wrap_Inputfield_" + condition.fields[fn] + " :checked").length;
							consoleLog('Using count checkbox condition');
							condition.subfield = 'count-checkbox';
						} else {
							// @todo OR support here?
							var conditionValue = new String(condition.value);
							conditionValue = conditionValue.replace(/\s/g, '_');<commit_msg>Minor tweak to inputfields.js to correct checkbox dependency issue<commit_after>/**
 * inputfields.js - JS specific to behavior of ProcessWire inputfields.
 * 
 * For other admin theme developers: you do not need to include this file in your admin theme
 * unless you want to (and it's better not to). Instead you should direct your admin theme to
 * load this exact file: $config->urls->root . 'wire/templates-admin/scripts/inputfields.js';
 *
 */

var InputfieldDebugMode = true; 

function consoleLog(note) {
	// uncomment the line below to enable debugging console
	if(InputfieldDebugMode) console.log(note);
}


/**
 * Inputfield Depedendencies
 *
 */
var InputfieldDependenciesProcessing = false;

function InputfieldDependencies() {

	if(InputfieldDependenciesProcessing) return;
	InputfieldDependenciesProcessing = true; 

	$(".InputfieldStateShowIf, .InputfieldStateRequiredIf").each(function() {

		 // Wrapper of field that we are operating on (i.e. #wrap_Inputfield_[name])
		var $fieldToShow = $(this);
		
		//Name of the field contained by $fieldToShow
		var fieldNameToShow = $fieldToShow.attr('id').replace(/wrap_Inputfield_/, '');

		// Array of conditions required to show a field
		var conditions = [];
		
		function trimValue(value) {
			value = jQuery.trim(value);
			var first = value.substring(0,1);
			var last = value.substring(value.length-1, value.length);
			if((first == '"' || first == "'") && first == last) value = value.substring(1, value.length-1);
			return value;
		}

		/**
		 * Remove quotes from value (if present)
		 *
		 * @param value
		 * @return string
		 *
		 */
		function trimParseValue(value) {
			// determine if we need to trim off quotes
			return parseValue(trimValue(value));
		}

		/**
		 * Convert string value to integer or float when appropriate
		 * 
		 * @param str string
		 * @param str2 string Optional second value for context
		 * @return string|int|float
		 * 
		 */
		function parseValue(str, str2) {
			
			str = jQuery.trim(str);
			if(str.length > 0 && !jQuery.isNumeric(str)) {
				return str; 
			}
			
			if(str.length == 0) {
				// empty value: should it be a blank or a 0?
				var t = typeof str2;
				if(t != "undefined") {
					// str2 is present for context
					if(t == "integer") return 0;
					if(t == "float") return 0.0;
					return str;
				} else {
					// no context, assume blank
					return str; 	
				}
			}
			
			var dot1 = str.indexOf('.');
			var dot2 = str.lastIndexOf('.');
			
			if(dot1 == -1 && /^-?\d+$/.test(str)) {
				// no dot present, and all numbers so must be integer
				return parseInt(str);
			}
			
			if(dot2 > -1 && dot1 != dot2) {
				// more than one dot, can't be a float
				return str; 
			}
			
			if(/^-?[\d.]+$/.test(str)) {
				// looks to be a float
				return parseFloat(str);
			}
			
			return str;
		}

		/**
		 * Returns whether or not value matched
		 *
		 * @param field Name of field
		 * @param operator
		 * @param value value to match for
		 * @param conditionValue
		 * @return int 0=value didn't match, 1=value matched
		 *
		 */
		function matchValue(field, operator, value, conditionValue) {
			var matched = 0;

			switch(operator) {
				case '=': if(value == conditionValue) matched++; break;
				case '!=': if(value != conditionValue) matched++; break;
				case '>': if(value > conditionValue) matched++; break;
				case '<': if(value < conditionValue) matched++; break;
				case '>=': if(value >= conditionValue) matched++; break;
				case '<=': if(value <= conditionValue) matched++; break;
				case '*=':
				case '%=': if(value.indexOf(conditionValue) > -1) matched++; break;
			}

			consoleLog('Field ' + field + ' - Current value: ' + value);
			consoleLog('Field ' + field + ' - Matched? ' + (matched > 0 ? 'YES' : 'NO'));

			return matched;
		}

		/**
		 * Called when a targeted Inputfield has changed
		 *
		 */
		function inputfieldChange() {
			InputfieldDependenciesProcessing = true; 

			if(InputfieldDebugMode) {
				consoleLog('-------------------------------------------------------------------');
				consoleLog('Field "' + $fieldToShow.attr('id') + '" detected a change to a dependency field! Beginning dependency checks...');
			}

			// number of changes that were actually made to field visibility
			var numVisibilityChanges = 0;
			var show = true; 
			var requiredMatches = 0; 
			var notRequiredMatches = 0;

			for(var c = 0; c < conditions.length; c++) {

				// current condition we are checking in this iteration 
				var condition = conditions[c];

				if(InputfieldDebugMode) {
					consoleLog('----');
					consoleLog('Start Dependency ' + c);
					consoleLog('Condition type: ' + condition.type);
					consoleLog('Field: ' + condition.field);
					if (condition.subfield.length > 0) consoleLog('Subfield: ' + condition.subfield);
					consoleLog('Operator: ' + condition.operator);
					consoleLog('Required value: ' + condition.value);
				}

				// matched contains positive value when condition matches
				var matched = 0;
		
				// iterate through all OR fields (this will most often just be 1 field)
				for(var fn = 0; fn < condition.fields.length; fn++) {

					var value = null;
					var $field = $("#Inputfield_" + condition.fields[fn]);
					
					if($field.length == 0) {
						// in case they manually specified id property
						$field = $("#" + condition.fields[fn]);
					}

					if ($field.length == 0) {
						// if field isn't present by #id it may be present by #id+value as a checkbox/radio field is
						consoleLog('Detected possible checkbox or radio: ' + condition.field + condition.operator + condition.value);
						if (condition.subfield == 'count' || condition.subfield == 'count-checkbox') {
							// count number of matching checked inputs
							$field = $("#wrap_Inputfield_" + condition.fields[fn] + " :input");
							value = $("#wrap_Inputfield_" + condition.fields[fn] + " :checked").length;
							consoleLog('Using count checkbox condition');
							condition.subfield = 'count-checkbox';
						} else {
							// @todo OR support here?
							var conditionValue = new String(condition.value);
							conditionValue = trimValue(conditionValue.replace(/\s/g, '_'));
{'fraction_non_alphanumeric': 0.1458231159064901, 'fraction_numerical': 0.006212195520680072, 'mean_word_length': 2.8186641697877652, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 25, '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': '2224852', 'n_tokens_mistral': 4437, 'n_tokens_neox': 3479, 'n_words': 1106}