Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented simple OR Selector for field dependencies #991

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 72 additions & 20 deletions wire/templates-admin/scripts/inputfields.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,22 @@ function InputfieldDependencies() {
// Array of conditions required to show a field
var conditions = [];

/**
* prettify value parsed from attribute
*
* @param value
* @return string|int|float
*
*/
function prettifyValue(value) {
// determine if we need to trim off quotes
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
*
Expand Down Expand Up @@ -86,6 +102,34 @@ function InputfieldDependencies() {
return str;
}

/**
* Convert string value to integer or float when appropriate
*
* @param n Number of the iteration
* @param values values to match for
* @return int
*
*/
function matchValue(n, 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('Value #' + n + ' - Current value: ' + value);
consoleLog('Value #' + n + ' - Matched? ' + (matched > 0 ? 'YES' : 'NO'));

return matched;
}

/**
* Called when a targeted Inputfield has changed
*
Expand Down Expand Up @@ -174,32 +218,30 @@ function InputfieldDependencies() {

// also allow for matching a "0" as an unchecked value
if((attrType == 'checkbox' || attrType == 'radio') && !$field.is(":checked")) values[1] = '0';

// force value to be array
if(!jQuery.isArray(condition.value)) conditionValues = [condition.value];
else conditionValues = condition.value;

//
if(condition.operator == '!=') numMatchesRequired = (values.length * conditionValues.length);
else numMatchesRequired = 1;
consoleLog([values, conditionValues, numMatchesRequired]);

// cycle through the values (most of the time, just 1 value).
// increment variable 'show' each time a condition matches
for(var n = 0; n < values.length; n++) {
value = parseValue(values[n], condition.value);

switch(condition.operator) {
case '=': if(value == condition.value) matched++; break;
case '!=': if(value != condition.value) matched++; break;
case '>': if(value > condition.value) matched++; break;
case '<': if(value < condition.value) matched++; break;
case '>=': if(value >= condition.value) matched++; break;
case '<=': if(value <= condition.value) matched++; break;
case '*=':
case '%=': if(value.indexOf(condition.value) > -1) matched++; break;
for (var i = conditionValues.length - 1; i >= 0; i--) {
value = parseValue(values[n], conditionValues[i]);
matched += matchValue(n, condition.operator, value, conditionValues[i]);
}

consoleLog('Value #' + n + ' - Current value: ' + value);
consoleLog('Value #' + n + ' - Matched? ' + (matched > 0 ? 'YES' : 'NO'));
}

consoleLog('----');

// determine whether to show or hide the field
if(condition.type == 'show') {
if(matched > 0) {
if(matched >= numMatchesRequired) {
// show it, which is the default behavior
} else {
show = false;
Expand Down Expand Up @@ -315,18 +357,28 @@ function InputfieldDependencies() {
consoleLog("Operator: " + operator);
consoleLog("value: " + value);

// determine if we need to trim off quotes
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);
// detect OR selector |
if(value.indexOf("|") > -1){
var value = value.split("|");
consoleLog("OR dependency: " + value);
}

// prettify value(s): remove quotes, parse numbers
if(jQuery.isArray(value)){
for (var i = value.length - 1; i >= 0; i--) {
value[i] = prettifyValue(value[i]);
}
}else{
value = prettifyValue(value);
}

// build the condition
var condition = {
'type': conditionType,
'field': field,
'subfield': subfield,
'operator': operator,
'value': parseValue(value)
'value': value
};

// append to conditions array
Expand Down