示例#1
0
        public IHtmlContent Generate <TModel, TProperty>(IHtmlHelper html, object viewModel, Expression <Func <TModel, TProperty> > property, object htmlAttributes)
        {
            if (viewModel == null)
            {
                throw new ArgumentNullException(nameof(viewModel));
            }
            if (property == null)
            {
                throw new ArgumentNullException(nameof(property));
            }

            var propertyInfo = property.GetProperty();
            var propertyName = propertyInfo.Name;
            var blob         = propertyInfo.GetValue(viewModel) as BlobViewModel ?? new BlobViewModel();

            var result = new HtmlContentBuilder();

            string getId(string prop) => $"\"{propertyName}_{prop}\"";

            string GetHiddenInput(Expression <Func <BlobViewModel, object> > expression)
            {
                var propName = expression.GetProperty().Name;
                var id       = getId(propName);
                var func     = expression.Compile();

                return($@"<input type=""hidden"" id={id} name={id} class=""{propName}"" value=""{func(blob)}"" />");
            }

            result.AppendHtmlLine($@"
                <div class=""file-upload"">
                    <span class=""current-file"" aria-label=""Preview the file""{" style=\"display:none\"".OnlyWhen(blob.IsEmpty)}>
                        <a target=""_blank"" href=""{blob.Url?.HtmlEncode()}"">{blob.Filename.OrEmpty().HtmlEncode()}</a>
                    </span>
                    <label for={getId("fileInput")} hidden>HiddenLabel</label>
                    {html.TextBox(propertyName, "value".OnlyWhen(blob.HasValue), string.Empty, HiddenFieldSettings).GetString()}
                    <input type=""file"" id={getId("fileInput")} name=""files"" {OliveMvcExtensions.ToHtmlAttributes(htmlAttributes)}/>
                    {GetHiddenInput(x => x.Action)}
                    {GetHiddenInput(x => x.TempFileId)}
                    {GetHiddenInput(x => x.Filename)}
                    {GetHiddenInput(x => x.ItemId)}
                    {GetHiddenInput(x => x.Url)}
                    {GetHiddenInput(x => x.IsEmpty)}
                    <div class=""progress-bar"" role=""progressbar""></div>
                    <span class=""delete-file fa fa-remove btn"" style=""display: none""></span>
                </div>
            ");

            return(result);
        }
示例#2
0
        public IHtmlContent Generate <TModel, TProperty>(IHtmlHelper html, object model, Expression <Func <TModel, TProperty> > property, object htmlAttributes)
        {
            var propertyInfo = property.GetProperty();
            var blob         = propertyInfo.GetValue(model) as Blob ?? Blob.Empty();
            var value        = html.ViewContext.HttpContext.Request.HasFormContentType ?
                               html.ViewContext.HttpContext.Request.Form[propertyInfo.Name] :
                               Microsoft.Extensions.Primitives.StringValues.Empty;

            if (value == "KEEP")
            {
                var itemProperty         = model.GetType().GetProperty("Item");
                var item                 = itemProperty.GetValue(model);
                var originalPropertyInfo = item.GetType().GetProperty(propertyInfo.Name);
                blob = originalPropertyInfo.GetValue(item) as Blob ?? Blob.Empty();
            }

            // Note: If this method is called with an IEnumerable<Blob> property,
            // then the existing data will never be loaded.
            var result = new HtmlContentBuilder();

            result.AppendHtmlLine("<div class=\"file-upload\">");
            result.AppendHtmlLine($"<span class=\"current-file\"{" style=\"display:none\"".OnlyWhen(blob.IsEmpty())}>" +
                                  $"<a target=\"_blank\" href=\"{blob.Url().HtmlEncode()}\">{blob.FileName.OrEmpty().HtmlEncode()}</a></span>");
            result.AppendHtmlLine($"<input type=\"file\" name=\"files\" {OliveMvcExtensions.ToHtmlAttributes(htmlAttributes)}/>");
            // For validation to work, this works instead of Hidden.
            if (value.ToString().IsEmpty() && blob.HasValue())
            {
                value = "KEEP";
            }
            result.AppendHtml(html.TextBox(propertyInfo.Name, value.OrEmpty(), string.Empty,
                                           new { tabindex = "-1", style = "width:1px; height:0; border:0; padding:0; margin:0;", @class = "file-id", autocomplete = "off" }));
            result.AppendHtmlLine("<div class=\"progress-bar\" role=\"progressbar\"></div>");
            result.AppendHtmlLine("<span class=\"delete-file fa fa-remove btn\" style=\"display: none\"></span>");
            result.AppendHtmlLine("</div>");

            return(result);
        }
示例#3
0
        public HtmlString Render()
        {
            if (Paging.PageSize == null || Paging.TotalItemsCount == 0)
            {
                return(null);
            }

            if (ListPagination.DisplayForSinglePage == false && Paging.LastPage == 1)
            {
                return(null);
            }

            FindBoundaries();

            var r = new StringBuilder();

            if (ListPagination.WrapperCssClass.HasValue())
            {
                r.AppendLine($"<div class=\"{ListPagination.WrapperCssClass}\">");
            }

            r.AddFormattedLine("<ul{0}>", OliveMvcExtensions.ToHtmlAttributes(HtmlAttributes));

            var isFirst = Paging.CurrentPage == 1;
            var isLast  = Paging.CurrentPage == Paging.LastPage;

            // add first page control
            if (Paging.ShowFirstLastLinks)
            {
                AddPaginationControl(r, Paging.FirstText, "First page", Paging.CurrentPage == 1, 1);
            }

            // add previous page control
            if (Paging.ShowPreviousNextLinks)
            {
                var previousPage = isFirst ? 1 : Paging.CurrentPage - 1;
                AddPaginationControl(r, Paging.PreviousText, "Previous page", isFirst, previousPage);
            }

            for (var i = Start; i <= End; i++)
            {
                r.AppendFormat("<li {0}>", " class=\"active\"".OnlyWhen(i == Paging.CurrentPage));
                r.AddFormattedLine("<a {0}>{1}</a></li>", GetLinkAttributes(i), i);
            }

            // add next page control
            if (Paging.ShowPreviousNextLinks)
            {
                var nextPage = isLast ? Paging.LastPage : Paging.CurrentPage + 1;
                AddPaginationControl(r, Paging.NextText, "Next page", isLast, nextPage);
            }

            // add last page control
            if (Paging.ShowFirstLastLinks)
            {
                AddPaginationControl(r, Paging.LastText, "Last page", isLast, Paging.LastPage);
            }

            r.AppendLine("</ul>");
            r.AppendLineIf("</div>", ListPagination.WrapperCssClass.HasValue());

            return(new HtmlString(r.ToString()));
        }
示例#4
0
 protected virtual string GetHtmlAttributes(object htmlAttributes) =>
 OliveMvcExtensions.ToHtmlAttributes(htmlAttributes);
示例#5
0
        public IHtmlContent Generate <TModel, TProperty>(IHtmlHelper html, object viewModel, Expression <Func <TModel, TProperty> > property, object htmlAttributes)
        {
            if (viewModel == null)
            {
                throw new ArgumentNullException(nameof(viewModel));
            }
            if (property == null)
            {
                throw new ArgumentNullException(nameof(property));
            }

            var propertyInfo = property.GetProperty();
            var propertyName = propertyInfo.Name;
            var blob         = propertyInfo.GetValue(viewModel) as Blob ?? Blob.Empty();

            var action = html.Request().HasFormContentType ? html.Request().Form[propertyName] : StringValues.Empty;

            if (action == "KEEP")
            {
                blob = GetOldValue(viewModel, propertyName) ?? blob;
            }

            var result = new HtmlContentBuilder();

            // For validation to work, this works instead of Hidden.
            if (action.ToString().IsEmpty() && blob.HasValue())
            {
                action = "KEEP";
            }

            result.AppendHtmlLine($@"
                <div class=""file-upload"">
                    <span class=""current-file"" aria-label=""Preview the file""{" style=\"display:none\"".OnlyWhen(blob.IsEmpty())}>
                        <a target=""_blank"" href=""{blob.Url().HtmlEncode()}"">{blob.FileName.OrEmpty().HtmlEncode()}</a>
                    </span>
                    <label for=""{propertyName}_fileInput"" hidden>HiddenLabel</label>
                    <input type=""file"" id=""{propertyName}_fileInput"" name=""files"" {OliveMvcExtensions.ToHtmlAttributes(htmlAttributes)}/>
                    {html.TextBox(propertyName, action.OrEmpty(), string.Empty, HiddenFieldSettings).GetString()}
                    <div class=""progress-bar"" role=""progressbar""></div>
                    <span class=""delete-file fa fa-remove btn"" style=""display: none""></span>
                </div>
            ");

            return(result);
        }