forked from goxiaoy/flutter_survey_js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Made it so that visibleIf is respected. Does not yet handle multiple …
…conditions chained with AND or OR - DC
- Loading branch information
1 parent
ba2bcc4
commit a079b82
Showing
7 changed files
with
761 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
import 'package:flutter_survey_js/survey.dart'; | ||
|
||
class VisibilityHelper { | ||
bool isElementVisible( | ||
{required Question element, | ||
required Map<String, dynamic> surveyResponse}) { | ||
final VisibilityHelper visibilityHelper = VisibilityHelper(); | ||
bool isVisible = true; | ||
String? visibleIf = element.visibleIf?.replaceAll(' ', ''); | ||
if (visibleIf == null) { | ||
return true; | ||
} | ||
void findMatch(Map<String, dynamic> surveyResponse) { | ||
if (!_curlyBraceRegExp.hasMatch(visibleIf!)) { | ||
return; | ||
} | ||
final match = _curlyBraceRegExp.firstMatch(visibleIf!); | ||
final matchedString = | ||
match?.group(0)!.replaceAll('{', '').replaceAll('}', ''); | ||
String comparisonOperator = | ||
visibilityHelper.determineComparisonOperatorString( | ||
matchedElementName: match!, visibleIf: visibleIf!); | ||
|
||
dynamic rightOperand = visibilityHelper.determineRightOperand( | ||
visibleIf: visibleIf!, | ||
matchedElementName: match, | ||
comparisonOperatorLength: comparisonOperator.length); | ||
|
||
visibleIf = visibleIf!.replaceFirst(_curlyBraceRegExp, ''); | ||
|
||
if (rightOperand is num) { | ||
if (surveyResponse[matchedString] == null) { | ||
isVisible = false; | ||
return; | ||
} | ||
switch (comparisonOperator) { | ||
case '=': | ||
if (surveyResponse[matchedString] != rightOperand) { | ||
isVisible = false; | ||
return; | ||
} | ||
break; | ||
case '<': | ||
if (surveyResponse[matchedString] >= rightOperand) { | ||
isVisible = false; | ||
return; | ||
} | ||
break; | ||
case '<=': | ||
if (surveyResponse[matchedString] > rightOperand) { | ||
isVisible = false; | ||
return; | ||
} | ||
break; | ||
case '>': | ||
if (surveyResponse[matchedString] <= rightOperand) { | ||
isVisible = false; | ||
return; | ||
} | ||
break; | ||
case '>=': | ||
if (surveyResponse[matchedString] < rightOperand) { | ||
isVisible = false; | ||
return; | ||
} | ||
break; | ||
} | ||
} else { | ||
rightOperand = rightOperand.substring(1, rightOperand.length - 1); | ||
if (surveyResponse[matchedString] != '$rightOperand') { | ||
isVisible = false; | ||
return; | ||
} | ||
} | ||
findMatch(surveyResponse); | ||
} | ||
|
||
findMatch(surveyResponse); | ||
return isVisible; | ||
} | ||
|
||
final RegExp _curlyBraceRegExp = RegExp(r'{(.*?)}'); | ||
final List<String> _comparisonOperatorSymbols = ['=', '<', '>']; | ||
|
||
String determineComparisonOperatorString( | ||
{required String visibleIf, required RegExpMatch matchedElementName}) { | ||
String comparisonOperator = ''; | ||
void evaluateChar() { | ||
final String charToCheck = visibleIf.substring( | ||
matchedElementName.end + comparisonOperator.length, | ||
matchedElementName.end + comparisonOperator.length + 1); | ||
if (_comparisonOperatorSymbols.contains(charToCheck)) { | ||
comparisonOperator += charToCheck; | ||
evaluateChar(); | ||
} | ||
} | ||
|
||
evaluateChar(); | ||
return comparisonOperator; | ||
} | ||
|
||
dynamic determineRightOperand( | ||
{required String visibleIf, | ||
required RegExpMatch matchedElementName, | ||
required int comparisonOperatorLength}) { | ||
bool rightOperandIsNum = false; | ||
String rightOperand = ''; | ||
|
||
void evaluateChar() { | ||
if ((matchedElementName.end + | ||
comparisonOperatorLength + | ||
rightOperand.length + | ||
1) > | ||
visibleIf.length) { | ||
return; | ||
} | ||
final String charToCheck = visibleIf.substring( | ||
matchedElementName.end + | ||
comparisonOperatorLength + | ||
rightOperand.length, | ||
matchedElementName.end + | ||
comparisonOperatorLength + | ||
rightOperand.length + | ||
1); | ||
if (rightOperand.isEmpty) { | ||
if (charToCheck != '\'') { | ||
rightOperandIsNum = true; | ||
} | ||
} else { | ||
if ((rightOperandIsNum && int.tryParse(charToCheck) != null)) { | ||
return; | ||
} | ||
if (charToCheck == '\'') { | ||
rightOperand += charToCheck; | ||
return; | ||
} | ||
} | ||
rightOperand += charToCheck; | ||
evaluateChar(); | ||
} | ||
|
||
evaluateChar(); | ||
return rightOperandIsNum ? int.parse(rightOperand) : rightOperand; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.