Esempio n. 1
0
        /// <summary>
        /// 对 HTML 中的 script 元素进行绑定
        /// </summary>
        /// <param name="context">当前绑定上下文</param>
        /// <param name="element">当前绑定的元素(仅会对 script 元素起作用)</param>
        /// <returns>永远返回 false,表示其他绑定器可以继续执行</returns>
        public void BindElement(HtmlBindingContext context, IHtmlElement element)
        {
            if (!element.Name.EqualsIgnoreCase("script"))
            {
                return;
            }


            var script = element.InnerHtml();

            script = scriptBindingExpression.Replace(script, match =>
            {
                var expression = BindingExpression.ParseExpression(match.Groups["expression"].Value);
                if (expression == null)
                {
                    return(match.Value);
                }

                object dataObject   = context.GetValue(expression);
                var valueExpression = serializer.Serialize(dataObject);

                return(match.Groups["declare"].Value + valueExpression + ";");
            });


            element.InnerHtml(script);
        }
Esempio n. 2
0
        /// <summary>
        /// 创建一个绑定上下文
        /// </summary>
        /// <param name="bindingContext">目前的绑定上下文</param>
        /// <param name="element">目前正在进行绑定的元素</param>
        /// <param name="dataModel">元素上绑定的数据对象</param>
        /// <returns></returns>
        protected HtmlBindingContext CreateBindingContext(HtmlBindingContext bindingContext, IHtmlElement element, object dataModel)
        {
            var listData = dataModel as ListDataModel;

            if (listData != null)
            {
                return(new HtmlListBindingContext(bindingContext, element, listData));
            }

            return(new HtmlBindingContext(bindingContext, element, dataModel));
        }
Esempio n. 3
0
        /// <summary>
        /// 创建绑定上下文
        /// </summary>
        /// <param name="scope">要进行数据绑定的范围</param>
        /// <param name="dataModel">数据模型</param>
        /// <param name="binders">在这一次数据绑定中要用到的绑定器列表</param>
        /// <returns>绑定上下文</returns>
        public static HtmlBindingContext Create(IHtmlContainer scope, object dataModel, params object[] binders)
        {
            var elementBinders    = new List <IHtmlBinder>();
            var expressionBinders = new List <IExpressionBinder>();


            foreach (var item in binders)
            {
                {
                    var binder = item as IHtmlBinder;
                    if (binder != null)
                    {
                        elementBinders.Add(binder);
                        continue;
                    }
                }

                {
                    var binder = item as IExpressionBinder;
                    if (binder != null)
                    {
                        expressionBinders.Add(binder);
                        continue;
                    }
                }

                {
                    var list = item as IEnumerable <IHtmlBinder>;
                    if (list != null)
                    {
                        elementBinders.AddRange(list);
                        continue;
                    }
                }

                {
                    var list = item as IEnumerable <IExpressionBinder>;
                    if (list != null)
                    {
                        expressionBinders.AddRange(list);
                        continue;
                    }
                }


                throw new ArgumentException(string.Format("不支持 \"{0}\" 类型的绑定器", item.GetType().ToString()), "binders");
            }


            var context = HtmlBindingContext.Create(elementBinders.ToArray(), expressionBinders.ToArray(), scope, dataModel);

            return(context);
        }
Esempio n. 4
0
        /// <summary>
        /// 根据绑定参数获取数据对象
        /// </summary>
        /// <param name="context">绑定上下文</param>
        /// <param name="arguments">绑定参数</param>
        /// <returns>数据对象</returns>
        public static object GetDataObject(HtmlBindingContext context, BindingExpression expression)
        {
            object dataObject = context.DataModel;



            if (dataObject != null)
            {
                string path;

                if (expression.TryGetValue(context, "path", out path))
                {
                    if (dataObject == null)
                    {
                        return(null);
                    }

                    dataObject = DataBinder.Eval(dataObject, path);
                }
            }

            object value;

            if (expression.TryGetValue(context, "value", out value))
            {
                if (dataObject != null && Convert.ToBoolean(dataObject) == true)
                {
                    return(value);
                }

                else if (expression.TryGetValue(context, "alternativeValue", out value))
                {
                    return(value);
                }

                else
                {
                    return(null);
                }
            }

            return(dataObject);
        }
Esempio n. 5
0
        /// <summary>
        /// 绑定元素
        /// </summary>
        /// <param name="context">当前绑定上下文</param>
        /// <param name="element">要绑定的元素</param>
        /// <returns>永远返回 false,表示继续进行后面的绑定</returns>
        public void BindElement(HtmlBindingContext context, IHtmlElement element)
        {
            if (!element.Document.HtmlSpecification.IsCDataElement(element.Name))
            {
                return;
            }

            if (element.Attribute("literal-binder") == null)
            {
                return;
            }

            lock (element.SyncRoot)
            {
                var text = element.InnerHtml();

                text = LiteralBind(text);
            }
        }
Esempio n. 6
0
        /// <summary>
        /// 创建 HtmlBindingContext 实例
        /// </summary>
        /// <param name="scope">要进行数据绑定的范畴</param>
        /// <param name="bindingContext">父级数据绑定上下文</param>
        /// <param name="dataModel">数据模型,若不提供则使用当前的数据模型</param>
        protected HtmlBindingContext(HtmlBindingContext bindingContext, IHtmlContainer scope, object dataModel = null)
        {
            if (bindingContext == null)
            {
                throw new ArgumentNullException("bindingContext");
            }

            if (scope == null)
            {
                throw new ArgumentNullException("scope");
            }

            ParentContext = bindingContext;
            BindingScope  = scope;
            DataModel     = dataModel ?? bindingContext.DataModel;

            Binders            = bindingContext.Binders;
            _expressionBinders = new ExpressionBinderCollection(bindingContext._expressionBinders);
        }
Esempio n. 7
0
        /// <summary>
        /// 对元素进行数据绑定
        /// </summary>
        /// <param name="element">需要绑定数据的元素</param>
        /// <param name="context">绑定上下文</param>
        /// <returns>返回是否对元素进行了不可逆转的操作(例如移除),故而禁止后续的绑定操作</returns>
        public void BindElement(HtmlBindingContext context, IHtmlElement element)
        {
            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();

                    context.CancelChildsBinding = context.BindCompleted = true;
                }
            }



            {
                //处理样式类
                var classAttribute = element.Attribute(classAttributePrefix);
                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(classAttributePrefix);
                }
            }

            {
                var classAttributes = element.Attributes().Where(a => a.Name.StartsWith(classAttributePrefix)).ToArray();
                BindElementClasses(element, classAttributes);
            }

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

            BindElementStyles(element, styleAttributes);
        }
Esempio n. 8
0
        /// <summary>
        /// 获取绑定值
        /// </summary>
        /// <param name="context">绑定上下文</param>
        /// <param name="arguments">绑定参数</param>
        /// <returns>绑定值</returns>
        public static string GetValue(HtmlBindingContext context, BindingExpression expression)
        {
            var dataObject = GetDataObject(context, expression);

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

            {
                string format;
                if (expression.TryGetValue(context, "format", out format))
                {
                    if (format.Contains("{0"))
                    {
                        return(string.Format(CultureInfo.InvariantCulture, format, dataObject));
                    }

                    else
                    {
                        var formattable = dataObject as IFormattable;

                        if (formattable != null)
                        {
                            return(formattable.ToString(format, CultureInfo.InvariantCulture));
                        }

                        else
                        {
                            return(dataObject.ToString());
                        }
                    }
                }
            }



            return(dataObject.ToString());
        }
Esempio n. 9
0
        object IExpressionBinder.GetValue(HtmlBindingContext context, BindingExpression expression)
        {
            var dataModel = GetDataObject(context, expression);

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


            CssElementSelector elementSelector = null;
            string             selector;

            if (expression.TryGetValue(context, "selector", out selector))
            {
                elementSelector = CssParser.ParseElementSelector(selector);
            }

            ListBindingMode mode;

            string modeSetting;

            if (expression.TryGetValue(context, "mode", out modeSetting) && modeSetting.EqualsIgnoreCase("static"))
            {
                mode = ListBindingMode.StaticContent;
            }

            else
            {
                mode = ListBindingMode.DynamicContent;
            }



            return(new ListDataModel((IEnumerable)dataModel, elementSelector, mode));
        }
Esempio n. 10
0
 /// <summary>
 /// 使用默认的绑定器设置创建 HtmlBindingContext 实例
 /// </summary>
 /// <param name="scope">要进行数据绑定的范畴</param>
 /// <param name="dataModel">数据模型</param>
 public static HtmlBindingContext Create(IHtmlContainer scope, object dataModel)
 {
     return(HtmlBindingContext.Create(ElementBinders.ToArray(), ExpressionBinders.ToArray(), scope, dataModel));
 }
Esempio n. 11
0
 object IExpressionBinder.GetValue(HtmlBindingContext context, BindingExpression expression)
 {
     return(GetValue(context, expression));
 }
Esempio n. 12
0
 internal HtmlListBindingContext(HtmlBindingContext context, IHtmlElement element, ListDataModel dataModel)
     : base(context, element, dataModel)
 {
     DataModel      = dataModel;
     BindingElement = element;
 }
 public HtmlBindingContext CreateBindingContext(HtmlBindingContext bindingContext, IHtmlContainer scope, object datamodel)
 {
     return(new HtmlListBindingContext(bindingContext, (IHtmlElement)scope, (ListDataModel)datamodel));
 }