public ChangeVisuallyCondition(ChangeVisuallyAttribute.ChangeTo to, string whenOtherPropertyName, ChangeVisuallyAttribute.DisplayChangeIf ifOperator, object value, bool conditionPassesIfNull)
 {
     To = to;
     WhenOtherPropertyName = whenOtherPropertyName;
     If = ifOperator;
     Value = value;
     ConditionPassesIfNull = conditionPassesIfNull;
 }
Exemplo n.º 2
0
        private static bool ConditionMetForChangeVisually <T>(PropertyInfo prop, T model, PropertyInfo[] modelProps)
        {
            var engine = new ScriptEngine();
            var src    = GetJavaScript();

            engine.Evaluate(src);

            List <string> toValues;
            List <string> whenOtherPropertyNameValues;
            List <string> ifValues;
            List <string> valueValues;
            List <string> conditionPassesIfNullValues;
            List <string> valueTypeToCompareValues;
            List <string> valueFormatValues;

            ChangeVisuallyAttribute.GetValuesForClient(prop, out toValues, out whenOtherPropertyNameValues, out ifValues, out valueValues, out conditionPassesIfNullValues, out valueTypeToCompareValues, out valueFormatValues);

            //check if we have any conditions.. if we do loop through them and return true as soon as one is found to pass
            if (toValues.Any())
            {
                for (var i = 0; i < toValues.Count; i++)
                {
                    //get the "other property" value
                    string otherPropValue = null;
                    var    otherProp      =
                        modelProps.FirstOrDefault(_ => string.Equals(_.Name, whenOtherPropertyNameValues[i]));
                    if (otherProp != null)
                    {
                        var val = otherProp.GetValue(model, null);
                        if (val != null)
                        {
                            otherPropValue = Json.Encode(val);
                        }
                    }

                    var ifOperator            = ifValues[i];
                    var expectedValue         = valueValues[i];
                    var actualValue           = otherPropValue;
                    var conditionPassesIfNull = conditionPassesIfNullValues[i];
                    var valueTypeToCompare    = valueTypeToCompareValues[i];
                    var valueFormat           = valueFormatValues[i];

                    //Call the JavaScript function to evaluate
                    var res = engine.CallGlobalFunction <bool>("ConditionMetForChangeVisually", ifOperator, expectedValue,
                                                               actualValue, conditionPassesIfNull, valueTypeToCompare,
                                                               valueFormat);
                    if (res)
                    {
                        //we found one where the condition was met
                        return(true);
                    }
                }
            }

            return(false);
        }