Represents the result of evaluating a binding expression.
Example #1
0
        internal Adapter(BindingResult binding, IDictionary<string, string> parameters)
        {
            this.binding = binding;
            this.properties = new Dictionary<string,string>();

            if (parameters.ContainsKey("optionsTransform"))
                throw new ApplicationException(string.Format("Option \"optionsTransform\" is obsolete, use \"allowedValuesTransform\" instead. Path = \"{0}\".", binding.Property));

            // Compile the allowed values transform if one is specified
            if (parameters.ContainsKey("allowedValuesTransform"))
            {
                if (parameters["allowedValuesTransform"].Contains("groupBy("))
                    throw new ApplicationException("The allowedValuesTransform property does not support grouping");

                this.allowedValuesTransform = Transform.Compile(parameters["allowedValuesTransform"]);
            }

            // Copy custom properties that are allowed
            foreach (string key in parameters.Keys)
            {
                var disallowed = disallowedProperties.Contains(key);

                // Allow overriding nullOption property for booleans.
                if (disallowed && key == "nullOption" && IsBoolean(Property))
                    disallowed = false;

                if (!disallowed)
                    this.properties.Add(key, parameters[key]);
            }
        }
Example #2
0
        internal Adapter(BindingResult binding, IDictionary <string, string> parameters)
        {
            this.binding    = binding;
            this.properties = new Dictionary <string, string>();

            if (parameters.ContainsKey("optionsTransform"))
            {
                throw new ApplicationException(string.Format("Option \"optionsTransform\" is obsolete, use \"allowedValuesTransform\" instead. Path = \"{0}\".", binding.Property));
            }

            // Compile the allowed values transform if one is specified
            if (parameters.ContainsKey("allowedValuesTransform"))
            {
                if (parameters["allowedValuesTransform"].Contains("groupBy("))
                {
                    throw new ApplicationException("The allowedValuesTransform property does not support grouping");
                }

                this.allowedValuesTransform = Transform.Compile(parameters["allowedValuesTransform"]);
            }

            // Copy custom properties that are allowed
            foreach (string key in parameters.Keys)
            {
                var disallowed = disallowedProperties.Contains(key);

                // Allow overriding nullOption property for booleans.
                if (disallowed && key == "nullOption" && IsBoolean(Property))
                {
                    disallowed = false;
                }

                if (!disallowed)
                {
                    this.properties.Add(key, parameters[key]);
                }
            }
        }
Example #3
0
File: Page.cs Project: vc3/ExoWeb
        /// <summary>
        /// Gets the result of evaluating the specified path in the current data context.
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public BindingResult EvaluatePath(string path)
        {
            var result = new BindingResult();

            // Return the current context if a path was not specified
            if (String.IsNullOrEmpty(path))
            {
                result.Value = Context.DataItem;
                result.IsValid = true;
                if (Context.DataItem is Adapter)
                {
                    var adapter = (Adapter)Context.DataItem;
                    result.Source = adapter.Source;
                    result.Property = adapter.Property;
                }
                return result;
            }

            // Default the context to the current template context data item
            var context = Context.DataItem;

            // First see if the binding expression represents a model level source
            if (path.StartsWith("window.context.model."))
                path = path.Substring(7);

            if (path.StartsWith("context.model."))
            {
                path = path.Substring(14);
                var index = path.IndexOf('.');
                var model = index > 0 ? path.Substring(0, index) : path;

                // Attempt to fetch the specified model value
                if (Model.TryGetValue(model, out context) && context != null)
                {
                    // Immediately return if this is the end of the path
                    if (index < 0)
                    {
                        result.IsValid = true;
                        result.Value = context;
                        return result;
                    }

                    // Trim the path down to the unevaluated portion
                    path = path.Substring(index + 1);
                }
                else
                    return result;
            }

            // Transform grouping
            if (context is Transform.Grouping)
            {
                object groupResult;
                if (((Transform.Grouping)context).TryGetValue(path, out path, out groupResult))
                {
                    context = result.Value = groupResult;

                    if (string.IsNullOrEmpty(path))
                    {
                        result.IsValid = true;
                        return result;
                    }
                }
            }

            // ModelInstance
            if (context is ModelInstance)
            {
                result.Source = context as ModelInstance;

                // Exit immediately if the path is not valid
                ModelPath modelPath;
                if (result.Source.Type.TryGetPath(path, out modelPath))
                {
                    bool pathWalked = false;

                    // Walk the path, honoring only first steps
                    for (var step = FirstApplicableStep(result.Source, modelPath.FirstSteps); step != null; step = FirstApplicableStep(result.Source, step.NextSteps))
                    {
                        pathWalked = true;

                        if (step.NextSteps.Any())
                        {
                            // Exit immediately if an intermediary step is a list
                            if (step.Property.IsList)
                                return result;

                            // Attempt to walk the instance path if the current step is valid
                            if (result.Source != null)
                                result.Source = result.Source.Type.Properties.Contains(step.Property) ? result.Source.GetReference((ModelReferenceProperty)step.Property) : null;
                        }
                        else
                            result.Property = step.Property;
                    }

                    if (!pathWalked)
                        return result;

                    // Indicate that the result is now considered valid since the path could be walked
                    result.IsValid = true;

                    // Attempt to evaluate the last step along the path
                    if (result.Source != null)
                    {
                        // Evaluate the last step along the path
                        if (result.Property is ModelValueProperty)
                            result.Value = result.Source.GetValue((ModelValueProperty)result.Property);
                        else if (result.Property.IsList)
                            result.Value = result.Source.GetList((ModelReferenceProperty)result.Property);
                        else
                            result.Value = result.Source.GetReference((ModelReferenceProperty)result.Property);
                    }

                    return result;
                }
            }

            // Condition
            if (context is Condition)
                return ConditionWrapper.Evaluate((Condition)context, path);

            // IBindable
            if (context is IBindable)
                result = ((IBindable)context).Evaluate(path);

            // As a last resort, see if this is a static path
            if (!result.IsValid)
            {
                ModelSource source;
                // Return immediately if the path does not represent a valid static property
                if (!ModelSource.TryGetSource(null, path, out source))
                    return result;

                result.IsValid = true;
                result.Property = ModelContext.Current.GetModelType(source.SourceType).Properties[source.SourceProperty];
                result.Value =
                    result.Property is ModelValueProperty ? source.GetValue(null) :
                    result.Property.IsList ? (object)source.GetList(null) :
                    source.GetReference(null);
                return result;
            }

            // Invalid
            return result;
        }
Example #4
0
        /// <summary>
        /// Gets the result of evaluating the specified path in the current data context.
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public BindingResult EvaluatePath(string path)
        {
            var result = new BindingResult();

            // Return the current context if a path was not specified
            if (String.IsNullOrEmpty(path))
            {
                result.Value   = Context.DataItem;
                result.IsValid = true;
                if (Context.DataItem is Adapter)
                {
                    var adapter = (Adapter)Context.DataItem;
                    result.Source   = adapter.Source;
                    result.Property = adapter.Property;
                }
                return(result);
            }

            // Default the context to the current template context data item
            var context = Context.DataItem;

            // First see if the binding expression represents a model level source
            if (path.StartsWith("window.context.model."))
            {
                path = path.Substring(7);
            }

            if (path.StartsWith("context.model."))
            {
                path = path.Substring(14);
                var index = path.IndexOf('.');
                var model = index > 0 ? path.Substring(0, index) : path;

                // Attempt to fetch the specified model value
                if (Model.TryGetValue(model, out context) && context != null)
                {
                    // Immediately return if this is the end of the path
                    if (index < 0)
                    {
                        result.IsValid = true;
                        result.Value   = context;
                        return(result);
                    }

                    // Trim the path down to the unevaluated portion
                    path = path.Substring(index + 1);
                }
                else
                {
                    return(result);
                }
            }

            // Transform grouping
            if (context is Transform.Grouping)
            {
                object groupResult;
                if (((Transform.Grouping)context).TryGetValue(path, out path, out groupResult))
                {
                    context = result.Value = groupResult;

                    if (string.IsNullOrEmpty(path))
                    {
                        result.IsValid = true;
                        return(result);
                    }
                }
            }

            // ModelInstance
            if (context is ModelInstance)
            {
                result.Source = context as ModelInstance;

                // Exit immediately if the path is not valid
                ModelPath modelPath;
                if (result.Source.Type.TryGetPath(path, out modelPath))
                {
                    bool pathWalked = false;

                    // Walk the path, honoring only first steps
                    for (var step = FirstApplicableStep(result.Source, modelPath.FirstSteps); step != null; step = FirstApplicableStep(result.Source, step.NextSteps))
                    {
                        pathWalked = true;

                        if (step.NextSteps.Any())
                        {
                            // Exit immediately if an intermediary step is a list
                            if (step.Property.IsList)
                            {
                                return(result);
                            }

                            // Attempt to walk the instance path if the current step is valid
                            if (result.Source != null)
                            {
                                result.Source = result.Source.Type.Properties.Contains(step.Property) ? result.Source.GetReference((ModelReferenceProperty)step.Property) : null;
                            }
                        }
                        else
                        {
                            result.Property = step.Property;
                        }
                    }

                    if (!pathWalked)
                    {
                        return(result);
                    }

                    // Indicate that the result is now considered valid since the path could be walked
                    result.IsValid = true;

                    // Attempt to evaluate the last step along the path
                    if (result.Source != null)
                    {
                        // Evaluate the last step along the path
                        if (result.Property is ModelValueProperty)
                        {
                            result.Value = result.Source.GetValue((ModelValueProperty)result.Property);
                        }
                        else if (result.Property.IsList)
                        {
                            result.Value = result.Source.GetList((ModelReferenceProperty)result.Property);
                        }
                        else
                        {
                            result.Value = result.Source.GetReference((ModelReferenceProperty)result.Property);
                        }
                    }

                    return(result);
                }
            }

            // Condition
            if (context is Condition)
            {
                return(ConditionWrapper.Evaluate((Condition)context, path));
            }

            // IBindable
            if (context is IBindable)
            {
                result = ((IBindable)context).Evaluate(path);
            }

            // As a last resort, see if this is a static path
            if (!result.IsValid)
            {
                ModelSource source;
                // Return immediately if the path does not represent a valid static property
                if (!ModelSource.TryGetSource(null, path, out source))
                {
                    return(result);
                }

                result.IsValid  = true;
                result.Property = ModelContext.Current.GetModelType(source.SourceType).Properties[source.SourceProperty];
                result.Value    =
                    result.Property is ModelValueProperty?source.GetValue(null) :
                        result.Property.IsList ? (object)source.GetList(null) :
                        source.GetReference(null);

                return(result);
            }

            // Invalid
            return(result);
        }