public static IBindingsBuilder <T> Value <T, F>( this IBindingsBuilder <T> bindingsBuilder, Expression <Func <T, F> > expression) { if (expression == null) { throw (new ArgumentNullException("expression")); } string clientFormat; string prefix; string postfix; string nullString; int clientType = CoreHTMLHelpers. GetClientInfo(expression, out clientFormat, out prefix, out postfix, out nullString); bindingsBuilder.Add( bindingsBuilder.standardString <T, F>("value", expression, null, null) ); bindingsBuilder.Add("valueType: " + clientType); if (clientFormat.Length > 0) { bindingsBuilder.Add("formatString: "" + clientFormat + """); } return(bindingsBuilder); }
public static IBindingsBuilder <T> Text <T, F>( this IBindingsBuilder <T> bindingsBuilder, Expression <Func <T, F> > expression, string format = null, params LambdaExpression[] otherExpressions) where T : class { if (expression == null) { throw (new ArgumentNullException("expression")); } if (format == null) { string clientFormat; string prefix; string postfix; string nullString; int clientType = CoreHTMLHelpers. GetClientInfo(expression, out clientFormat, out prefix, out postfix, out nullString); bindingsBuilder.Add( string.Format(textScript, bindingsBuilder.GetFullBindingName(expression), clientFormat, clientType, prefix, postfix, nullString) ); return(bindingsBuilder); } bindingsBuilder.Add( bindingsBuilder.standardString <T, F>("text", expression, format, otherExpressions) ); return(bindingsBuilder); }
public BindingsContext(IBindingsBuilder <T> bindings, string prefix, HtmlHelper <VM> htmlHelper, TextWriter writer) { this.bindings = bindings; this.prefix = prefix; this.htmlHelper = htmlHelper; this.writer = writer; }
internal static string standardString <T, F>( this IBindingsBuilder <T> bindingsBuilder, string bindingName, Expression <Func <T, F> > expression, string format, params LambdaExpression[] otherExpressions) { if (format == null) { return(bindingName + ": " + bindingsBuilder.GetFullBindingName(expression)); } else if (otherExpressions == null || otherExpressions.Length == 0) { return(bindingName + ": " + string.Format(format, bindingsBuilder.GetFullBindingName(expression) + "()")); } else { string[] args = new string[otherExpressions.Length + 1]; args[0] = bindingsBuilder.GetFullBindingName(expression) + "()"; int index = 1; foreach (LambdaExpression currExpression in otherExpressions) { args[index] = bindingsBuilder.GetFullBindingName(currExpression) + "()"; index++; } return(bindingName + ": " + string.Format(format, args)); } }
protected string RenderPageField() { if (pageFieldRendered) { return(string.Empty); } pageFieldRendered = true; IBindingsBuilder <VM> bindings = htmlHelper.ClientBindings(); string modelName = bindings == null ? "" : bindings.ModelName; return (string.Format(mainScript, currPage, fieldId, fieldName, totPages.HasValue ? totPages.Value.ToString(CultureInfo.InvariantCulture) : "", pagePrefix == null ? "" : pagePrefix, pagePostfix == null ? "" : pagePostfix, modelName, totPagesFieldName == null ? "" : totPagesFieldName, causeSubmit ? "data-cause-submit='true'" : "", validationType )); }
public KoAutomaticBinder(string input, IBindingsBuilder <T> bindings, string namePrefix = null) { this.input = input; this.bindings = bindings; scriptCount = 0; this.namePrefix = namePrefix; }
public KoAutomaticBinder(string input, IBindingsBuilder <T> bindings, string namePrefix, string addToBody) { this.input = input; this.bindings = bindings; scriptCount = 0; this.namePrefix = namePrefix; this.addToBody = addToBody; }
public static IBindingsBuilder <T> UniqueName <T>(this IBindingsBuilder <T> bindingsBuilder) where T : class { bindingsBuilder.Add( "uniqueName: true" ); return(bindingsBuilder); }
private static MvcHtmlString _cond <T, F>( this HtmlHelper <T> htmlHelper, Expression <Func <T, F> > expression, ExternalContainerType itemsContainer = ExternalContainerType.koComment, object htmlAttributes = null, string afterRender = null, bool negateCondition = false, string format = null, params LambdaExpression[] otherExpressions) { if (expression == null) { throw (new ArgumentNullException("expression")); } IDictionary <string, object> attributes = null; if (htmlAttributes == null) { attributes = new RouteValueDictionary(); } else if (htmlAttributes is IDictionary <string, object> ) { attributes = htmlAttributes as IDictionary <string, object>; } else { attributes = new RouteValueDictionary(htmlAttributes); } IBindingsBuilder <T> bindings = htmlHelper.ClientBindings(); if (bindings == null) { throw (new ArgumentNullException("bindings")); } string bindingsValue = string.Format( ifScript, bindings.standardString(negateCondition ? "ifnot" : "if", expression, format, otherExpressions), bindings.ValidationType == "UnobtrusiveClient" ? "true" : "false", afterRender == null ? "null" : afterRender); if (attributes.ContainsKey("data-bind")) { attributes["data-bind"] = (attributes["data-bind"] as string) + ", " + bindingsValue; } else { attributes["data-bind"] = bindingsValue; } attributes["data-nobinding"] = "true"; string openTag; string closeTag; BasicHtmlHelper.GetContainerTags(itemsContainer, attributes, out openTag, out closeTag); htmlPush("_ClientControlsFlowStack_", closeTag); return(MvcHtmlString.Create( openTag )); }
public static MvcHtmlString ThemedClientPagerFor <VM>( this HtmlHelper <VM> htmlHelper, Expression <Func <VM, int> > page, Expression <Func <VM, int> > totPages = null, string pagePrefix = null, string pagePostfix = null, bool causeSubmit = false, string name = "ClientPager") { if (page == null) { throw (new ArgumentNullException("page")); } string pagerName = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName( ExpressionHelper.GetExpressionText(page)); int initialPage = 1; string totalPagesName = null; int? totalPages = null; try { initialPage = page.Compile().Invoke(htmlHelper.ViewData.Model); } catch { } if (totPages != null) { IBindingsBuilder <VM> bindings = htmlHelper.ClientBindings(); if (bindings == null) { totalPagesName = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName( ExpressionHelper.GetExpressionText(totPages)); } else { totalPagesName = bindings.GetFullBindingName(totPages); } try { totalPages = totPages.Compile().Invoke(htmlHelper.ViewData.Model); } catch { } } return(ThemedClientPager <VM>(htmlHelper, pagerName, initialPage, totalPagesName, totalPages, pagePrefix, pagePostfix, causeSubmit, name)); }
public static IBindingsBuilder <T> SelectedOptions <T, F>( this IBindingsBuilder <T> bindingsBuilder, Expression <Func <T, F> > expression, string format = null, params LambdaExpression[] otherExpressions) { if (expression == null) { throw (new ArgumentNullException("expression")); } bindingsBuilder.Add( bindingsBuilder.standardString <T, F>("selectedOptions", expression, format, otherExpressions) ); bindingsBuilder.Add("valueType: " + CoreHTMLHelpers.GetClientType(typeof(F))); return(bindingsBuilder); }
public Func <string, string> PrepareForCompile <T>(ViewContext vc) where T : class, new() { HtmlHelper <T> htmlHelper = new HtmlHelper <T>(vc, this); IBindingsBuilder <T> bindings = htmlHelper.ClientViewModel(modelJsName, m => m, blockId, false); string viewInitialization = (vc.Writer as StringWriter).ToString(); ViewData["ClientBindings"] = bindings; ViewData["_TruePrefix_"] = string.Empty; ViewData.TemplateInfo.HtmlFieldPrefix = ClientTemplateHelper.templateSymbol + ".A"; Func <string, string> res = (input) => { return(new KoAutomaticBinder <T>(input, bindings, string.Empty, viewInitialization).ToString()); }; return(res); }
public static IBindingsBuilder <T> Submit <T, F>( this IBindingsBuilder <T> bindingsBuilder, Expression <Func <T, F> > expression, string format = null, params LambdaExpression[] otherExpressions) where T : class { if (expression == null) { throw (new ArgumentNullException("expression")); } bindingsBuilder.Add( bindingsBuilder.standardString <T, F>("submit", expression, format, otherExpressions) ); return(bindingsBuilder); }
public static IBindingsBuilder <T> Options <T, F>( this IBindingsBuilder <T> bindingsBuilder, Expression <Func <T, F> > expression, string caption = null) { if (expression == null) { throw (new ArgumentNullException("expression")); } bindingsBuilder.Add( bindingsBuilder.standardString <T, F>("options", expression, null, null) ); int clientType = CoreHTMLHelpers.GetClientType(typeof(F)); bindingsBuilder.Add("valueType: " + clientType); if (!string.IsNullOrWhiteSpace(caption)) { bindingsBuilder.Add("optionsCaption: "" + caption + """); } return(bindingsBuilder); }
public static IBindingsBuilder <T> Options <T, F, IF, IFT>( this IBindingsBuilder <T> bindingsBuilder, Expression <Func <T, IEnumerable <F> > > expression, Expression <Func <F, IF> > valueField, Expression <Func <F, IFT> > textField, string caption = null, string valueFieldFormat = null, string textFieldFormat = null) { if (expression == null) { throw (new ArgumentNullException("expression")); } bindingsBuilder.Add( bindingsBuilder.standardString <T, IEnumerable <F> >("options", expression, null, null) ); if (valueFieldFormat == null) { bindingsBuilder.Add("optionsValue: "" + ExpressionHelper.GetExpressionText(valueField) + """); } else { bindingsBuilder.Add( baseStandardString <F, IF>("optionsValue", valueField, valueFieldFormat, null) ); } string clientFormat; string prefix; string postfix; string nullString; int clientType = CoreHTMLHelpers. GetClientInfo(valueField, out clientFormat, out prefix, out postfix, out nullString); bindingsBuilder.Add("valueType: " + clientType); if (clientFormat.Length > 0) { bindingsBuilder.Add("formatString: "" + clientFormat + """); } if (textFieldFormat == null) { bindingsBuilder.Add("optionsText: "" + ExpressionHelper.GetExpressionText(textField) + """); } else { bindingsBuilder.Add( baseStandardString <F, IFT>("optionsText", textField, textFieldFormat, null) ); } clientType = CoreHTMLHelpers. GetClientInfo(textField, out clientFormat, out prefix, out postfix, out nullString); bindingsBuilder.Add("textType: " + clientType); if (clientFormat.Length > 0) { bindingsBuilder.Add("textFormatString: "" + clientFormat + """); } bindingsBuilder.Add("textPrefix: "" + prefix + """); bindingsBuilder.Add("textPostfix: "" + postfix + """); bindingsBuilder.Add("textNullString: "" + nullString + """); if (!string.IsNullOrWhiteSpace(caption)) { bindingsBuilder.Add("optionsCaption: "" + caption + """); } return(bindingsBuilder); }
public static MvcHtmlString ClientBlockRepeater <T, F>( this HtmlHelper <T> htmlHelper, string templateName, Expression <Func <T, F> > expression, ExternalContainerType itemsContainer = ExternalContainerType.div, object htmlAttributes = null, string afterAdd = null, string beforeRemove = null, string afterRender = null, string afterAllRender = null, object templateOptions = null, bool applyClientValidation = true, bool fastNoJavaScript = false, string templateEngine = null ) where T : class { if (expression == null) { throw (new ArgumentNullException("expression")); } if (string.IsNullOrWhiteSpace(templateName)) { throw (new ArgumentNullException("templateName")); } IDictionary <string, object> attributes = null; if (htmlAttributes == null) { attributes = new RouteValueDictionary(); } else if (htmlAttributes is IDictionary <string, object> ) { attributes = htmlAttributes as IDictionary <string, object>; } else { attributes = new RouteValueDictionary(htmlAttributes); } bool writeTemplates = !TemplatesStack.CheckTemplates(); IBindingsBuilder <T> bindings = htmlHelper.ClientBindings(); if (bindings == null) { throw (new ArgumentNullException("bindings")); } string templates = string.Empty; try { string bindingsValue = bindings.Template( templateName, expression, afterAdd, beforeRemove, afterRender, templateOptions, null, applyClientValidation, fastNoJavaScript, afterAllRender, templateEngine).Get().ToString(); if (attributes.ContainsKey("data-bind")) { attributes["data-bind"] = (attributes["data-bind"] as string) + ", " + bindingsValue; } else { attributes["data-bind"] = bindingsValue; } attributes["data-nobinding"] = "true"; } finally { if (writeTemplates) { templates = TemplatesStack.GetTemplates(); } } string openTag; string closeTag; BasicHtmlHelper.GetContainerTags(itemsContainer, attributes, out openTag, out closeTag); return(MvcHtmlString.Create(openTag + closeTag + templates)); }
public static HtmlHelper <F> _foreach <T, F>( this HtmlHelper <T> htmlHelper, Expression <Func <T, IEnumerable <F> > > expression, ExternalContainerType itemsContainer = ExternalContainerType.koComment, object htmlAttributes = null, string afterAdd = null, string beforeRemove = null, string afterRender = null, string afterAllRender = null ) where F : class { if (expression == null) { throw (new ArgumentNullException("expression")); } IDictionary <string, object> attributes = null; if (htmlAttributes == null) { attributes = new RouteValueDictionary(); } else if (htmlAttributes is IDictionary <string, object> ) { attributes = htmlAttributes as IDictionary <string, object>; } else { attributes = new RouteValueDictionary(htmlAttributes); } IBindingsBuilder <T> bindings = htmlHelper.ClientBindings(); if (bindings == null) { throw (new ArgumentNullException("bindings")); } F model = default(F); Type basicType = typeof(F); if (basicType.IsClass) { var constructor = basicType.GetConstructor(new Type[0]); if (constructor != null) { model = (F)constructor.Invoke(new object[0]); } } string bindingFieldName = bindings.GetFullBindingName(expression); string fieldName = htmlHelper.ViewData.TemplateInfo.GetFullHtmlFieldName(ExpressionHelper.GetExpressionText(expression)); bool nested; int hl = helperLevel(htmlHelper, out nested); string templateSymbol = ClientTemplateHelper.templateSymbol + hl.ToString(); IBindingsBuilder <F> contentBindings = new BindingsBuilder <F>(htmlHelper.ViewContext.Writer, string.Empty, templateSymbol + ".A", bindings.ValidationType, null, htmlHelper); ViewDataDictionary <F> dic = new ViewDataDictionary <F>(model); dic["ClientBindings"] = contentBindings; dic["_TemplateLevel_"] = hl; HttpContext.Current.Items["_TemplateLevel_" + hl.ToString()] = contentBindings; dic.TemplateInfo.HtmlFieldPrefix = templateSymbol + ".A"; HtmlHelper <F> newHelper = new TemplateInvoker <F>().BuildHelper(htmlHelper, dic); string bindingsValue = string.Format( forScript, bindingFieldName, fieldName, BasicHtmlHelper.IdFromName(fieldName), templateSymbol, bindings.ValidationType == "UnobtrusiveClient" ? "true" : "false", afterAdd == null ? "null" : afterAdd, beforeRemove == null ? "null" : beforeRemove, afterRender == null ? "null" : afterRender, afterAllRender == null ? "null" : afterAllRender ); if (attributes.ContainsKey("data-bind")) { attributes["data-bind"] = (attributes["data-bind"] as string) + ", " + bindingsValue; } else { attributes["data-bind"] = bindingsValue; } attributes["data-nobinding"] = "true"; if (!nested && bindings.ValidationType == "UnobtrusiveClient") { bindings.AddServerErrors(fieldName); } string openTag; string closeTag; BasicHtmlHelper.GetContainerTags(itemsContainer, attributes, out openTag, out closeTag); htmlPush("_ClientControlsFlowStartStack_", openTag); htmlPush("_ClientControlsFlowStack_", closeTag); string innerHtml = string.Empty; return(newHelper); }
public AncestorBindingsBuilder(IBindingsBuilder <A> baseBuilder, System.IO.TextWriter writer, string modelName, string modelPrefix, string validationType, string hiddenId, HtmlHelper htmlHelper, string bindingPrefix) : base(writer, modelName, modelPrefix, validationType, hiddenId, htmlHelper, bindingPrefix) { this.baseBuilder = baseBuilder; }
public static IBindingsBuilder <T> Template <T, F>( this IBindingsBuilder <T> bindingsBuilder, string templateName, Expression <Func <T, F> > expression, string afterAdd = null, string beforeRemove = null, string afterRender = null, object templateOptions = null, string prefix = null, bool applyClientValidation = true, bool fastNoJavaScript = false, string afterAllRender = null, string templateEngine = null) where T : class { if (expression == null) { throw (new ArgumentNullException("expression")); } if (string.IsNullOrWhiteSpace(templateName)) { throw (new ArgumentNullException("templateName")); } string format = null; string actualPrefix = BasicHtmlHelper.AddField( bindingsBuilder.ModelPrefix, ExpressionHelper.GetExpressionText(expression)); if (prefix == null) { prefix = actualPrefix; } StringBuilder sb = new StringBuilder(); if (templateName[0] != '@') { sb.Append("template: { name: ""); sb.Append(templateName); if (typeof(IEnumerable).IsAssignableFrom(typeof(F))) { sb.Append("", foreach: "); } else { sb.Append("", data: "); } } else { templateName = templateName.Substring(1); sb.Append("template: { name: "); sb.Append(templateName); if (typeof(IEnumerable).IsAssignableFrom(typeof(F))) { sb.Append(", foreach: "); } else { sb.Append(", data: "); } } sb.Append(bindingsBuilder.GetFullBindingName(expression)); if (afterRender != null) { sb.Append(", afterRender: "); sb.Append(afterRender); } if (afterAdd != null) { sb.Append(", afterAdd: "); sb.Append(afterAdd); } if (beforeRemove != null) { sb.Append(", beforeRemove: "); sb.Append(beforeRemove); } if (afterAllRender != null) { sb.Append(", afterAllRender: "); sb.Append(afterAllRender); } if (templateEngine != null) { sb.Append(", templateEngine: ko."); sb.Append(templateEngine); sb.Append(".instance"); } var additionalOptions = new { ModelPrefix = """ + prefix + """, ModelId = """ + BasicHtmlHelper.IdFromName(prefix) + """, ItemPrefix = """", templateSymbol = """ + ClientTemplateHelper.templateSymbol + "0"" }; sb.Append(", templateOptions: {"); if (templateOptions != null) { sb.Append(BasicHtmlHelper.TranslateAnonymous(templateOptions)); sb.Append(", "); } sb.Append(BasicHtmlHelper.TranslateAnonymous(additionalOptions)); sb.Append(" }"); sb.Append(", processingOptions: {"); if (bindingsBuilder.ValidationType == "UnobtrusiveClient") { sb.Append("unobtrusiveClient: true"); if (bindingsBuilder.GetHelper().ViewData["_TemplateLevel_"] == null) { bindingsBuilder.AddServerErrors(actualPrefix); } } else { sb.Append("unobtrusiveClient: false"); } sb.Append(fastNoJavaScript ? ", fastNoJavaScript: true" : ", fastNoJavaScript: false"); sb.Append(applyClientValidation ? ", applyClientValidation: true" : ", applyClientValidation: false"); sb.Append(" }"); sb.Append(" }"); format = sb.ToString(); bindingsBuilder.Add( format ); return(bindingsBuilder); }