Beispiel #1
0
        /// <summary>
        /// 对元素属性进行绑定操作
        /// </summary>
        /// <param name="attribute">要绑定的元素属性</param>
        /// <param name="context">绑定上下文</param>
        /// <returns>是否成功绑定</returns>
        public bool BindAttribute(IHtmlAttribute attribute, HtmlBindingContext context)
        {
            if (attribute.Name == "datacontext")
            {
                return(false);
            }

            var expression = AttributeExpression.ParseExpression(attribute);

            if (expression == null || !expression.Name.EqualsIgnoreCase("Binding"))
            {
                return(false);
            }

            var dataObject = GetDataObject(expression, context);

            if (dataObject == null)
            {
                attribute.Remove();
                return(true);
            }


            string value = GetBindingValue(expression, dataObject);

            attribute.SetValue(value);

            return(true);
        }
Beispiel #2
0
        private static object GetDataObject(AttributeExpression expression, HtmlBindingContext context)
        {
            //获取绑定数据源

            string key;
            object dataObject;

            if (expression.Arguments.TryGetValue("key", out key) || expression.Arguments.TryGetValue("name", out key))
            {
                context.Data.TryGetValue(key, out dataObject);
            }
            else
            {
                dataObject = context.DataContext;
            }

            if (dataObject == null)
            {
                return(null);
            }


            string path;

            if (expression.Arguments.TryGetValue("path", out path))
            {
                dataObject = DataBinder.Eval(dataObject, path);
            }

            return(dataObject);
        }
Beispiel #3
0
        /// <summary>
        /// 实现 BindElement 方法对元素的 URL 属性进行处理
        /// </summary>
        /// <param name="context">当前绑定上下文</param>
        /// <param name="element">当前元素上下文</param>
        public void BindElement(HtmlBindingContext context, IHtmlElement element)
        {
            if (ProcessActionLink(element))//对元素进行 Action URL 处理,若处理成功,则跳过元素。
            {
                return;
            }

            element.Attributes().ToArray()
            .Where(attribute => Specification.IsUriValue(attribute))
            .ForAll(attribute => UrlHelper.ResolveUri(attribute, DocumentPath));
        }
Beispiel #4
0
        /// <summary>
        /// 对元素属性进行绑定操作
        /// </summary>
        /// <param name="attribute">要绑定的元素属性</param>
        /// <param name="context">绑定上下文</param>
        /// <returns>是否成功绑定</returns>
        public bool BindAttribute( IHtmlAttribute attribute, HtmlBindingContext context )
        {
            if ( attribute.Name == "datacontext" )
            return false;

              var expression = AttributeExpression.ParseExpression( attribute );
              if ( expression == null || !expression.Name.EqualsIgnoreCase( "Binding" ) )
            return false;

              var dataObject = GetDataObject( expression, context );

              if ( dataObject == null )
              {
            attribute.Remove();
            return true;
              }

              string value = GetBindingValue( expression, dataObject );

              attribute.SetValue( value );

              return true;
        }
Beispiel #5
0
    /// <summary>
    /// 实现 BindElement 方法对元素的 URL 属性进行处理
    /// </summary>
    /// <param name="context">当前绑定上下文</param>
    /// <param name="element">当前元素上下文</param>
    public void BindElement( HtmlBindingContext context, IHtmlElement element )
    {
      if ( ProcessActionLink( element ) )//对元素进行 Action URL 处理,若处理成功,则跳过元素。
        return;

      element.Attributes().ToArray()
        .Where( attribute => Specification.IsUriValue( attribute ) )
        .ForAll( attribute => UrlHelper.ResolveUri( attribute, DocumentPath ) );
    }
Beispiel #6
0
        /// <summary>
        /// 对元素进行数据绑定
        /// </summary>
        /// <param name="element">需要绑定数据的元素</param>
        /// <param name="context">绑定上下文</param>
        /// <param name="dataContext">数据上下文</param>
        /// <returns>是否进行了绑定</returns>
        public bool BindElement( IHtmlElement element, HtmlBindingContext context, out object dataContext )
        {
            dataContext = null;

              if ( element.Attribute( "binding-visible" ) != null )
              {
            var visible = element.Attribute( "binding-visible" ).Value();
            if ( visible.EqualsIgnoreCase( "false" ) || visible.EqualsIgnoreCase( "hidden" ) || visible.EqualsIgnoreCase( "invisible" ) )
              element.Remove();
            return true;
              }

              //处理样式类
              {
            var classAttribute = element.Attribute( classAttributeName );
            if ( classAttribute != null )
            {
              if ( !string.IsNullOrWhiteSpace( classAttribute.AttributeValue ) )
              {

            var classes = Regulars.whiteSpaceSeparatorRegex.Split( classAttribute.AttributeValue ).Where( c => c != "" ).ToArray();
            if ( classes.Any() )
              element.Class( classes );
              }

              element.RemoveAttribute( classAttributeName );
            }
              }

              //处理CSS样式
              var styleAttributes = element.Attributes().Where( a => a.Name.StartsWith( styleAttributePrefix ) ).ToArray();
              if ( styleAttributes.Any() )
            BindElementStyles( element, styleAttributes );

              if ( !element.Name.EqualsIgnoreCase( "view" ) && !element.Name.EqualsIgnoreCase( "binding" ) )
              {
            var dataContextExpression = AttributeExpression.ParseExpression( element.Attribute( "datacontext" ) );
            if ( dataContextExpression != null )
              dataContext = GetDataObject( dataContextExpression, context );

            return false;
              }

              var expression = new AttributeExpression( element );

              object dataObject = GetDataObject( expression, context );

              if ( dataObject == null )
            return false;

              var format = element.Attribute( "format" ).Value();

              //绑定到客户端脚本对象
              var variableName = element.Attribute( "var" ).Value() ?? element.Attribute( "variable" ).Value();
              if ( variableName != null )
              {

            if ( format != null )
              dataObject = string.Format( format, dataObject );

            var hostName = element.Attribute( "host" ).Value();

            var script = (string) null;

            if ( hostName == null )
              script = string.Format( "(function(){{ this['{0}'] = {1} }})();", variableName, ToJson( dataObject ) );

            else
              script = string.Format( "(function(){{ this['{0}'] = {1} }})();", variableName, ToJson( dataObject ) );

            element.ReplaceWith( string.Format( "<script type=\"text/javascript\">{0}</script>", script ) );
            return true;
              }

              //绑定为 HTML 文本
              var bindValue = string.Format( format ?? "{0}", dataObject );

              var attributeName = element.Attribute( "attribute" ).Value() ?? element.Attribute( "attr" ).Value();
              if ( attributeName != null )
              {
            var nextElement = element.NextElement();
            if ( nextElement == null )
              return false;

            nextElement.SetAttribute( attributeName, bindValue );
            return true;
              }

              element.ReplaceWith( bindValue );
              return true;
        }
Beispiel #7
0
        private static object GetDataObject( AttributeExpression expression, HtmlBindingContext context )
        {
            //获取绑定数据源

              string key;
              object dataObject;

              if ( expression.Arguments.TryGetValue( "key", out key ) || expression.Arguments.TryGetValue( "name", out key ) )
            context.Data.TryGetValue( key, out dataObject );
              else
            dataObject = context.DataContext;

              if ( dataObject == null )
            return null;

              string path;

              if ( expression.Arguments.TryGetValue( "path", out path ) )
            dataObject = DataBinder.Eval( dataObject, path );

              return dataObject;
        }
Beispiel #8
0
        /// <summary>
        /// 对元素进行数据绑定
        /// </summary>
        /// <param name="element">需要绑定数据的元素</param>
        /// <param name="context">绑定上下文</param>
        /// <param name="dataContext">数据上下文</param>
        /// <returns>是否进行了绑定</returns>
        public bool BindElement(IHtmlElement element, HtmlBindingContext context, out object dataContext)
        {
            dataContext = null;

            if (element.Attribute("binding-visible") != null)
            {
                var visible = element.Attribute("binding-visible").Value();
                if (visible.EqualsIgnoreCase("false") || visible.EqualsIgnoreCase("hidden") || visible.EqualsIgnoreCase("invisible"))
                {
                    element.Remove();
                }
                return(true);
            }



            //处理样式类
            {
                var classAttribute = element.Attribute(classAttributeName);
                if (classAttribute != null)
                {
                    if (!string.IsNullOrWhiteSpace(classAttribute.AttributeValue))
                    {
                        var classes = Regulars.whiteSpaceSeparatorRegex.Split(classAttribute.AttributeValue).Where(c => c != "").ToArray();
                        if (classes.Any())
                        {
                            element.Class(classes);
                        }
                    }

                    element.RemoveAttribute(classAttributeName);
                }
            }


            //处理CSS样式
            var styleAttributes = element.Attributes().Where(a => a.Name.StartsWith(styleAttributePrefix)).ToArray();

            if (styleAttributes.Any())
            {
                BindElementStyles(element, styleAttributes);
            }



            if (!element.Name.EqualsIgnoreCase("view") && !element.Name.EqualsIgnoreCase("binding"))
            {
                var dataContextExpression = AttributeExpression.ParseExpression(element.Attribute("datacontext"));
                if (dataContextExpression != null)
                {
                    dataContext = GetDataObject(dataContextExpression, context);
                }

                return(false);
            }

            var expression = new AttributeExpression(element);

            object dataObject = GetDataObject(expression, context);

            if (dataObject == null)
            {
                return(false);
            }



            var format = element.Attribute("format").Value();


            //绑定到客户端脚本对象
            var variableName = element.Attribute("var").Value() ?? element.Attribute("variable").Value();

            if (variableName != null)
            {
                if (format != null)
                {
                    dataObject = string.Format(format, dataObject);
                }


                var hostName = element.Attribute("host").Value();

                var script = (string)null;

                if (hostName == null)
                {
                    script = string.Format("(function(){{ this['{0}'] = {1} }})();", variableName, ToJson(dataObject));
                }

                else
                {
                    script = string.Format("(function(){{ this['{0}'] = {1} }})();", variableName, ToJson(dataObject));
                }


                element.ReplaceWith(string.Format("<script type=\"text/javascript\">{0}</script>", script));
                return(true);
            }



            //绑定为 HTML 文本
            var bindValue = string.Format(format ?? "{0}", dataObject);

            var attributeName = element.Attribute("attribute").Value() ?? element.Attribute("attr").Value();

            if (attributeName != null)
            {
                var nextElement = element.NextElement();
                if (nextElement == null)
                {
                    return(false);
                }

                nextElement.SetAttribute(attributeName, bindValue);
                return(true);
            }

            element.ReplaceWith(bindValue);
            return(true);
        }