wire/templates-admin/scripts/inputfields.js
/**
	 * Called when targeted Inputfield has changed
	 * 
	 * @param conditions
	 * @param $fieldToShow
	 * 
	 */
Called when targeted Inputfield has changed

@param conditions
@param $fieldToShow
Called when targeted Inputfield has changed @param conditions @param $fieldToShow
function inputfieldChange(conditions, $fieldToShow) {
		Inputfields.processingIfs = true;
		var fieldNameToShow = $fieldToShow.attr('id').replace(/wrap_Inputfield_/, '');
		if(Inputfields.debug) {
			consoleLog('-------------------------------------------------------------------');
			consoleLog('Field "' + fieldNameToShow + '" detected a change to a dependency field! Beginning dependency checks...');
		}
		var numVisibilityChanges = 0;
		var show = true;
		var requiredMatches = 0;
		var notRequiredMatches = 0;
		for(var c = 0; c < conditions.length; c++) {
			var condition = conditions[c];
			if(Inputfields.debug) {
				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);
			}
			var matched = 0;
			for(var fn = 0; fn < condition.fields.length; fn++) {
				var fieldAndSubfield = extractFieldAndSubfield(condition.fields[fn]); 
				var conditionField = fieldAndSubfield.field;
				var conditionSubfield = fieldAndSubfield.subfield;
				var value = null;
				var $field = $("#Inputfield_" + conditionField);
				var hasCheckboxes = false;
				if($field.length == 0) $field = $("#" + conditionField);
				if($field.length == 0) {
					consoleLog('Detected possible checkbox or radio: ' + condition.field + condition.operator + condition.value);
					var fieldAndValue = getCheckboxFieldAndValue(condition, conditionField, conditionSubfield);
					if(fieldAndValue) {
						$field = fieldAndValue.field;
						value = fieldAndValue.value;
						condition = fieldAndValue.condition;
						hasCheckboxes = true;
					}
				} 
				if($field.length == 0) {
					consoleLog("Unable to locate field: " + conditionField);
					continue;
				}
				var $inputfield = $field.closest('.Inputfield');
				if (value === null) {
					if($field.attr('type') == 'checkbox') {
						value = $field.is(":checked") ? $field.val() : null;
					} else {
						value = $field.val();
					}
				}
				var values = [];
				if (value == null) value = '';
				if (condition.subfield == 'count') value = value.length;
				if (typeof value == 'object') {
					values = jQuery.makeArray(value);
				} else if(typeof value == 'array') {
					values = value;
				} else if(typeof value == "string" && $inputfield.hasClass('InputfieldPage') && value.indexOf(',') > -1 && value.match(/^[,0-9]+$/)) {
					values = value.split(',');
				} else {
					values[0] = value;
				}
				var numMatchesRequired = 1;
				if (condition.operator == '!=') numMatchesRequired = (values.length * condition.values.length);
				if(($field.attr('type') == 'checkbox' || $field.attr('type') == 'radio') && !$field.is(":checked")) {
					if($("#Inputfield_" + conditionField + "_0").length == 0) {
						values[1] = '0';
					}
				}
				for (var n = 0; n < values.length; n++) {
					for (var i = 0; i < condition.values.length; i++) {
						var v = parseValue(values[n], condition.values[i]);
						matched += matchValue(conditionField, condition.operator, v, condition.values[i]);
					}
				}
				if(matched >= numMatchesRequired) break;
			} 
			consoleLog('----');
			if(condition.type == 'show') {
				if(matched >= numMatchesRequired) {
				} else {
					show = false;
				}
			} else if(condition.type == 'required') {
				if(matched > 0) {
					requiredMatches++;
				} else {
					notRequiredMatches++;
				}
			}
		} 
		var required = requiredMatches > 0 && notRequiredMatches == 0;
		if(show) {
			consoleLog('Determined that field "' + fieldNameToShow + '" should be visible.');
			if(Inputfields.hidden($fieldToShow)) {
				Inputfields.show($fieldToShow);
				numVisibilityChanges++;
				consoleLog('Field is now visible.');
			} else {
				consoleLog('Field is already visible.');
			}
		} else {
			consoleLog('Determined that field "' + fieldNameToShow + '" should be hidden.');
			if(Inputfields.hidden($fieldToShow)) {
				consoleLog('Field is already hidden.');
			} else {
				Inputfields.hide($fieldToShow);
				consoleLog('Field is now hidden.');
				numVisibilityChanges++;
			}
			if(required) {
				consoleLog('Field is required but cancelling that since it is not visible.');
				required = false;
			}
		}
		if(required && requiredMatches > 0) {
			consoleLog('Determined that field "' + fieldNameToShow + '" should be required.');
			$fieldToShow.addClass('InputfieldStateRequired').find(":input:visible[type!=hidden]").addClass('required'); 
		} else if(!required && notRequiredMatches > 0) {
			consoleLog('Determined that field "' + fieldNameToShow + '" should not be required.');
			$fieldToShow.removeClass('InputfieldStateRequired').find(":input.required").removeClass('required');
		}
		if(numVisibilityChanges > 0) {
			consoleLog(numVisibilityChanges + ' visibility changes were made.');
		}
		Inputfields.processingIfs = false;
	} 
function inputfieldChange ( conditions , $fieldToShow ) { Inputfields . processingIfs = true ; var fieldNameToShow = $fieldToShow . attr ( 'id' ) . replace ( / wrap_Inputfield_ / , '' ) ; if ( Inputfields . debug ) { consoleLog ( '-------------------------------------------------------------------' ) ; consoleLog ( 'Field "' + fieldNameToShow + '" detected a change to a dependency field! Beginning dependency checks...' ) ; } var numVisibilityChanges = 0 ; var show = true ; var requiredMatches = 0 ; var notRequiredMatches = 0 ; for ( var c = 0 ; c < conditions . length ; c ++ ) { var condition = conditions [ c ] ; if ( Inputfields . debug ) { 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 ) ; } var matched = 0 ; for ( var fn = 0 ; fn < condition . fields . length ; fn ++ ) { var fieldAndSubfield = extractFieldAndSubfield ( condition . fields [ fn ] ) ; var conditionField = fieldAndSubfield . field ; var conditionSubfield = fieldAndSubfield . subfield ; var value = null ; var $field = $ ( "#Inputfield_" + conditionField ) ; var hasCheckboxes = false ; if ( $field . length == 0 ) $field = $ ( "#" + conditionField ) ; if ( $field . length == 0 ) { consoleLog ( 'Detected possible checkbox or radio: ' + condition . field + condition . operator + condition . value ) ; var fieldAndValue = getCheckboxFieldAndValue ( condition , conditionField , conditionSubfield ) ; if ( fieldAndValue ) { $field = fieldAndValue . field ; value = fieldAndValue . value ; condition = fieldAndValue . condition ; hasCheckboxes = true ; } } if ( $field . length == 0 ) { consoleLog ( "Unable to locate field: " + conditionField ) ; continue ; } var $inputfield = $field . closest ( '.Inputfield' ) ; if ( value === null ) { if ( $field . attr ( 'type' ) == 'checkbox' ) { value = $field . is ( ":checked" ) ? $field . val ( ) : null ; } else { value = $field . val ( ) ; } } var values = [ ] ; if ( value == null ) value = '' ; if ( condition . subfield == 'count' ) value = value . length ; if ( typeof value == 'object' ) { values = jQuery . makeArray ( value ) ; } else if ( typeof value == 'array' ) { values = value ; } else if ( typeof value == "string" && $inputfield . hasClass ( 'InputfieldPage' ) && value . indexOf ( ',' ) > - 1 && value . match ( / ^[,0-9]+$ / ) ) { values = value . split ( ',' ) ; } else { values [ 0 ] = value ; } var numMatchesRequired = 1 ; if ( condition . operator == '!=' ) numMatchesRequired = ( values . length * condition . values . length ) ; if ( ( $field . attr ( 'type' ) == 'checkbox' || $field . attr ( 'type' ) == 'radio' ) && ! $field . is ( ":checked" ) ) { if ( $ ( "#Inputfield_" + conditionField + "_0" ) . length == 0 ) { values [ 1 ] = '0' ; } } for ( var n = 0 ; n < values . length ; n ++ ) { for ( var i = 0 ; i < condition . values . length ; i ++ ) { var v = parseValue ( values [ n ] , condition . values [ i ] ) ; matched += matchValue ( conditionField , condition . operator , v , condition . values [ i ] ) ; } } if ( matched >= numMatchesRequired ) break ; } consoleLog ( '----' ) ; if ( condition . type == 'show' ) { if ( matched >= numMatchesRequired ) { } else { show = false ; } } else if ( condition . type == 'required' ) { if ( matched > 0 ) { requiredMatches ++ ; } else { notRequiredMatches ++ ; } } } var required = requiredMatches > 0 && notRequiredMatches == 0 ; if ( show ) { consoleLog ( 'Determined that field "' + fieldNameToShow + '" should be visible.' ) ; if ( Inputfields . hidden ( $fieldToShow ) ) { Inputfields . show ( $fieldToShow ) ; numVisibilityChanges ++ ; consoleLog ( 'Field is now visible.' ) ; } else { consoleLog ( 'Field is already visible.' ) ; } } else { consoleLog ( 'Determined that field "' + fieldNameToShow + '" should be hidden.' ) ; if ( Inputfields . hidden ( $fieldToShow ) ) { consoleLog ( 'Field is already hidden.' ) ; } else { Inputfields . hide ( $fieldToShow ) ; consoleLog ( 'Field is now hidden.' ) ; numVisibilityChanges ++ ; } if ( required ) { consoleLog ( 'Field is required but cancelling that since it is not visible.' ) ; required = false ; } } if ( required && requiredMatches > 0 ) { consoleLog ( 'Determined that field "' + fieldNameToShow + '" should be required.' ) ; $fieldToShow . addClass ( 'InputfieldStateRequired' ) . find ( ":input:visible[type!=hidden]" ) . addClass ( 'required' ) ; } else if ( ! required && notRequiredMatches > 0 ) { consoleLog ( 'Determined that field "' + fieldNameToShow + '" should not be required.' ) ; $fieldToShow . removeClass ( 'InputfieldStateRequired' ) . find ( ":input.required" ) . removeClass ( 'required' ) ; } if ( numVisibilityChanges > 0 ) { consoleLog ( numVisibilityChanges + ' visibility changes were made.' ) ; } Inputfields . processingIfs = false ; }
Called when targeted Inputfield has changed
Called when targeted Inputfield has changed
// Name of the field contained by $fieldToShow // number of changes that were actually made to field visibility // current condition we are checking in this iteration  // matched contains positive value when condition matches // iterate through all OR fields (this will most often just be 1 field) // in case they manually specified id property // field still not found, perhaps this is a checkbox/radio field which have id properties // that contain the values in them as well // identification of checkbox/radio fields // if we haven't matched a field by now, skip over it // value of the dependency field we are checking (if not already populated above) // value will be placed in values so we can handle multiple value checks // prefer blank to null for our checks // special case for 'count' subfield condition,  // where we take the value's length rather than the value // if value is an object, make it in array // in either case, convert value to an array called values // object, convert to array // array, already // CSV string of page IDs // string: single value array // determine how many matches will be required // consoleLog([values, condition.values, numMatchesRequired]); // also allow for matching a "0" as an unchecked value, but only if there's isn't already an input with that value // cycle through the values (most of the time, just 1 value). // increment variable 'show' each time a condition matches // if requirements met exit the loop // foreach fields // determine whether to show or hide the field // show it, which is the default behavior // make it required it // foreach(conditions) // consoleLog('Summary (required/matched): ' + conditions.length + ' / ' + show); // hide it // may need to focus a specific input? // Inputfields.redraw(0); // END inputfieldChange()